monotone

monotone Mtn Source Tree

Root/paths.hh

1#ifndef __PATHS_HH__
2#define __PATHS_HH__
3
4// Copyright (C) 2005 Nathaniel Smith <njs@pobox.com>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13// safe, portable, fast, simple path handling -- in that order.
14// but they all count.
15//
16// this file defines the vocabulary we speak in when dealing with the
17// filesystem. this is an extremely complex problem by the time one worries
18// about normalization, security issues, character sets, and so on;
19// furthermore, path manipulation has historically been a performance
20// bottleneck in monotone. so the goal here is the efficient implementation
21// of a design that makes it hard or impossible to introduce as many classes
22// of bugs as possible.
23//
24// Our approach is to have three different types of paths:
25// -- system_path
26// this is a path to anywhere in the fs. it is in native format. it is
27// always absolute. when constructed from a string, it interprets the
28// string as being relative to the directory that monotone was run in.
29// (note that this may be different from monotone's current directory, as
30// when run in workspace monotone chdir's to the project root.)
31//
32// one can also construct a system_path from one of the below two types
33// of paths. this is intelligent, in that it knows that these sorts of
34// paths are considered to be relative to the project root. thus
35// system_path(file_path_internal("foo"))
36// is not, in general, the same as
37// system_path("foo")
38//
39// -- file_path
40// this is a path representing a versioned file. it is always
41// a fully normalized relative path, that does not escape the project
42// root. it is always relative to the project root.
43// you cannot construct a file_path directly from a string; you must pick
44// a constructor:
45// file_path_internal: use this for strings that come from
46// "monotone-internal" places, e.g. parsing revisions. this turns on
47// stricter checking -- the string must already be normalized -- and
48// is extremely fast. such strings are interpreted as being relative
49// to the project root.
50// file_path_external: use this for strings that come from the user.
51// these strings are normalized before being checked, and if there is
52// a problem trigger N() invariants rather than I() invariants. if in
53// a workspace, such strings are interpreted as being
54// _relative to the user's original directory_.
55// if not in a workspace, strings are treated as referring to some
56// database object directly.
57// file_path's also provide optimized splitting and joining
58// functionality.
59//
60// -- bookkeeping_path
61// this is a path representing something in the _MTN/ directory of a
62// workspace. it has the same format restrictions as a file_path,
63// except instead of being forbidden to point into the _MTN directory, it
64// is _required_ to point into the _MTN directory. the one constructor is
65// strict, and analogous to file_path_internal. however, the normal way
66// to construct bookkeeping_path's is to use the global constant
67// 'bookkeeping_root', which points to the _MTN directory. Thus to
68// construct a path pointing to _MTN/options, use:
69// bookkeeping_root / "options"
70//
71// All path types should always be constructed from utf8-encoded strings.
72//
73// All path types provide an "operator /" which allows one to construct new
74// paths pointing to things underneath a given path. E.g.,
75// file_path_internal("foo") / "bar" == file_path_internal("foo/bar")
76//
77// All path types subclass 'any_path', which provides:
78// -- emptyness checking with .empty()
79// -- a method .as_internal(), which returns the utf8-encoded string
80// representing this path for internal use. for instance, this is the
81// string that should be embedded into the text of revisions.
82// -- a method .as_external(), which returns a std::string suitable for
83// passing to filesystem interface functions. in practice, this means
84// that it is recoded into an appropriate character set, etc.
85// -- a operator<< for ostreams. this should always be used when writing
86// out paths for display to the user. at the moment it just calls one
87// of the above functions, but this is _not_ correct. there are
88// actually 3 different logical character sets -- internal (utf8),
89// user (locale-specific), and filesystem (locale-specific, except
90// when it's not, i.e., on OS X). so we need three distinct operations,
91// and you should use the correct one.
92//
93// all this means that when you want to print out a path, you usually
94// want to just say:
95// F("my path is %s") % my_path
96// i.e., nothing fancy necessary, for purposes of F() just treat it like
97// it were a string
98//
99//
100// There is also one "not really a path" type, 'split_path'. This is a vector
101// of path_component's, and semantically equivalent to a file_path --
102// file_path's can be split into split_path's, and split_path's can be joined
103// into file_path's.
104
105
106#include <iosfwd>
107#include <string>
108#include <vector>
109#include <set>
110
111#include "vocab.hh"
112
113typedef std::vector<path_component> split_path;
114
115const path_component the_null_component;
116
117inline bool
118null_name(path_component pc)
119{
120 return pc == the_null_component;
121}
122
123bool
124workspace_root(split_path const & sp);
125
126template <> void dump(split_path const & sp, std::string & out);
127
128// It's possible this will become a proper virtual interface in the future,
129// but since the implementation is exactly the same in all cases, there isn't
130// much point ATM...
131class any_path
132{
133public:
134 // converts to native charset and path syntax
135 // this is a path that you can pass to the operating system
136 std::string as_external() const;
137 // leaves as utf8
138 std::string const & as_internal() const
139 { return data(); }
140 bool empty() const
141 { return data().empty(); }
142protected:
143 utf8 data;
144 any_path() {}
145 any_path(any_path const & other)
146 : data(other.data) {}
147 any_path & operator=(any_path const & other)
148 { data = other.data; return *this; }
149};
150
151std::ostream & operator<<(std::ostream & o, any_path const & a);
152std::ostream & operator<<(std::ostream & o, split_path const & s);
153
154class file_path : public any_path
155{
156public:
157 file_path() {}
158 // join a file_path out of pieces
159 file_path(split_path const & sp);
160
161 // this currently doesn't do any normalization or anything.
162 file_path operator /(std::string const & to_append) const;
163
164 void split(split_path & sp) const;
165
166 bool operator ==(const file_path & other) const
167 { return data == other.data; }
168
169 bool operator <(const file_path & other) const
170 { return data < other.data; }
171
172private:
173 typedef enum { internal, external } source_type;
174 // input is always in utf8, because everything in our world is always in
175 // utf8 (except interface code itself).
176 // external paths:
177 // -- are converted to internal syntax (/ rather than \, etc.)
178 // -- normalized
179 // -- assumed to be relative to the user's cwd, and munged
180 // to become relative to root of the workspace instead
181 // both types of paths:
182 // -- are confirmed to be normalized and relative
183 // -- not to be in _MTN/
184 file_path(source_type type, std::string const & path);
185 friend file_path file_path_internal(std::string const & path);
186 friend file_path file_path_external(utf8 const & path);
187};
188
189// these are the public file_path constructors
190inline file_path file_path_internal(std::string const & path)
191{
192 return file_path(file_path::internal, path);
193}
194inline file_path file_path_external(utf8 const & path)
195{
196 return file_path(file_path::external, path());
197}
198
199class bookkeeping_path : public any_path
200{
201public:
202 bookkeeping_path() {};
203 // path _should_ contain the leading _MTN/
204 // and _should_ look like an internal path
205 // usually you should just use the / operator as a constructor!
206 bookkeeping_path(std::string const & path);
207 bookkeeping_path operator /(std::string const & to_append) const;
208 // exposed for the use of walk_tree
209 static bool is_bookkeeping_path(std::string const & path);
210 bool operator ==(const bookkeeping_path & other) const
211 { return data == other.data; }
212
213 bool operator <(const bookkeeping_path & other) const
214 { return data < other.data; }
215};
216
217extern bookkeeping_path const bookkeeping_root;
218extern path_component const bookkeeping_root_component;
219
220// this will always be an absolute path
221class system_path : public any_path
222{
223public:
224 system_path() {};
225 system_path(system_path const & other) : any_path(other) {};
226 // the optional argument takes some explanation. this constructor takes a
227 // path relative to the workspace root. the question is how to interpret
228 // that path -- since it's possible to have multiple workspaces over the
229 // course of a the program's execution (e.g., if someone runs 'checkout'
230 // while already in a workspace). if 'true' is passed (the default),
231 // then monotone will trigger an invariant if the workspace changes after
232 // we have already interpreted the path relative to some other working
233 // copy. if 'false' is passed, then the path is taken to be relative to
234 // whatever the current workspace is, and will continue to reference it
235 // even if the workspace later changes.
236 explicit system_path(any_path const & other,
237 bool in_true_workspace = true);
238 // this path can contain anything, and it will be absolutified and
239 // tilde-expanded. it will considered to be relative to the directory
240 // monotone started in. it should be in utf8.
241 system_path(std::string const & path);
242 system_path(utf8 const & path);
243 system_path operator /(std::string const & to_append) const;
244};
245
246void
247dirname_basename(split_path const & sp,
248 split_path & dirname, path_component & basename);
249
250void
251save_initial_path();
252
253system_path
254current_root_path();
255
256// returns true if workspace found, in which case cwd has been changed
257// returns false if workspace not found
258bool
259find_and_go_to_workspace(system_path const & search_root);
260
261// this is like change_current_working_dir, but also initializes the various
262// root paths that are needed to interpret paths
263void
264go_to_workspace(system_path const & new_workspace);
265
266typedef std::set<split_path> path_set;
267
268// equivalent to file_path_internal(path).split(sp) but more efficient.
269void
270internal_string_to_split_path(std::string const & path, split_path & sp);
271
272// Local Variables:
273// mode: C++
274// fill-column: 76
275// c-file-style: "gnu"
276// indent-tabs-mode: nil
277// End:
278// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
279
280#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status