Remove Path::getLast.
[oota-llvm.git] / include / llvm / Support / PathV1.h
1 //===- llvm/Support/PathV1.h - Path Operating System Concept ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the llvm::sys::Path class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_PATHV1_H
15 #define LLVM_SUPPORT_PATHV1_H
16
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/TimeValue.h"
20 #include <set>
21 #include <string>
22 #include <vector>
23
24 #define LLVM_PATH_DEPRECATED_MSG(replacement) \
25   "PathV1 has been deprecated and will be removed as soon as all LLVM and" \
26   " Clang clients have been moved over to PathV2. Please use `" #replacement \
27   "` from PathV2 instead."
28
29 namespace llvm {
30 namespace sys {
31
32   /// This structure provides basic file system information about a file. It
33   /// is patterned after the stat(2) Unix operating system call but made
34   /// platform independent and eliminates many of the unix-specific fields.
35   /// However, to support llvm-ar, the mode, user, and group fields are
36   /// retained. These pertain to unix security and may not have a meaningful
37   /// value on non-Unix platforms. However, the other fields should
38   /// always be applicable on all platforms.  The structure is filled in by
39   /// the PathWithStatus class.
40   /// @brief File status structure
41   class FileStatus {
42   public:
43     uint64_t    fileSize;   ///< Size of the file in bytes
44     TimeValue   modTime;    ///< Time of file's modification
45     uint32_t    mode;       ///< Mode of the file, if applicable
46     uint32_t    user;       ///< User ID of owner, if applicable
47     uint32_t    group;      ///< Group ID of owner, if applicable
48     uint64_t    uniqueID;   ///< A number to uniquely ID this file
49     bool        isDir  : 1; ///< True if this is a directory.
50     bool        isFile : 1; ///< True if this is a file.
51
52     FileStatus() : fileSize(0), modTime(0,0), mode(0777), user(999),
53                    group(999), uniqueID(0), isDir(false), isFile(false) { }
54
55     TimeValue getTimestamp() const { return modTime; }
56     uint64_t getSize() const { return fileSize; }
57     uint32_t getMode() const { return mode; }
58     uint32_t getUser() const { return user; }
59     uint32_t getGroup() const { return group; }
60     uint64_t getUniqueID() const { return uniqueID; }
61   };
62
63   /// This class provides an abstraction for the path to a file or directory
64   /// in the operating system's filesystem and provides various basic operations
65   /// on it.  Note that this class only represents the name of a path to a file
66   /// or directory which may or may not be valid for a given machine's file
67   /// system. The class is patterned after the java.io.File class with various
68   /// extensions and several omissions (not relevant to LLVM).  A Path object
69   /// ensures that the path it encapsulates is syntactically valid for the
70   /// operating system it is running on but does not ensure correctness for
71   /// any particular file system. That is, a syntactically valid path might
72   /// specify path components that do not exist in the file system and using
73   /// such a Path to act on the file system could produce errors. There is one
74   /// invalid Path value which is permitted: the empty path.  The class should
75   /// never allow a syntactically invalid non-empty path name to be assigned.
76   /// Empty paths are required in order to indicate an error result in some
77   /// situations. If the path is empty, the isValid operation will return
78   /// false. All operations will fail if isValid is false. Operations that
79   /// change the path will either return false if it would cause a syntactically
80   /// invalid path name (in which case the Path object is left unchanged) or
81   /// throw an std::string exception indicating the error. The methods are
82   /// grouped into four basic categories: Path Accessors (provide information
83   /// about the path without accessing disk), Disk Accessors (provide
84   /// information about the underlying file or directory), Path Mutators
85   /// (change the path information, not the disk), and Disk Mutators (change
86   /// the disk file/directory referenced by the path). The Disk Mutator methods
87   /// all have the word "disk" embedded in their method name to reinforce the
88   /// notion that the operation modifies the file system.
89   /// @since 1.4
90   /// @brief An abstraction for operating system paths.
91   class Path {
92     /// @name Constructors
93     /// @{
94     public:
95       /// Construct a path to a unique temporary directory that is created in
96       /// a "standard" place for the operating system. The directory is
97       /// guaranteed to be created on exit from this function. If the directory
98       /// cannot be created, the function will throw an exception.
99       /// @returns an invalid path (empty) on error
100       /// @param ErrMsg Optional place for an error message if an error occurs
101       /// @brief Construct a path to an new, unique, existing temporary
102       /// directory.
103       static Path GetTemporaryDirectory(std::string* ErrMsg = 0);
104
105       /// Construct a path to the current directory for the current process.
106       /// @returns The current working directory.
107       /// @brief Returns the current working directory.
108       static Path GetCurrentDirectory();
109
110       /// Return the suffix commonly used on file names that contain an
111       /// executable.
112       /// @returns The executable file suffix for the current platform.
113       /// @brief Return the executable file suffix.
114       static StringRef GetEXESuffix();
115
116       /// GetMainExecutable - Return the path to the main executable, given the
117       /// value of argv[0] from program startup and the address of main itself.
118       /// In extremis, this function may fail and return an empty path.
119       static Path GetMainExecutable(const char *argv0, void *MainAddr);
120
121       /// This is one of the very few ways in which a path can be constructed
122       /// with a syntactically invalid name. The only *legal* invalid name is an
123       /// empty one. Other invalid names are not permitted. Empty paths are
124       /// provided so that they can be used to indicate null or error results in
125       /// other lib/System functionality.
126       /// @brief Construct an empty (and invalid) path.
127       Path() : path() {}
128       Path(const Path &that) : path(that.path) {}
129
130       /// This constructor will accept a char* or std::string as a path. No
131       /// checking is done on this path to determine if it is valid. To
132       /// determine validity of the path, use the isValid method.
133       /// @param p The path to assign.
134       /// @brief Construct a Path from a string.
135       explicit Path(StringRef p);
136
137       /// This constructor will accept a character range as a path.  No checking
138       /// is done on this path to determine if it is valid.  To determine
139       /// validity of the path, use the isValid method.
140       /// @param StrStart A pointer to the first character of the path name
141       /// @param StrLen The length of the path name at StrStart
142       /// @brief Construct a Path from a string.
143       Path(const char *StrStart, unsigned StrLen);
144
145     /// @}
146     /// @name Operators
147     /// @{
148     public:
149       /// Makes a copy of \p that to \p this.
150       /// @returns \p this
151       /// @brief Assignment Operator
152       Path &operator=(const Path &that) {
153         path = that.path;
154         return *this;
155       }
156
157       /// Makes a copy of \p that to \p this.
158       /// @param that A StringRef denoting the path
159       /// @returns \p this
160       /// @brief Assignment Operator
161       Path &operator=(StringRef that);
162
163       /// Compares \p this Path with \p that Path for equality.
164       /// @returns true if \p this and \p that refer to the same thing.
165       /// @brief Equality Operator
166       bool operator==(const Path &that) const;
167
168       /// Compares \p this Path with \p that Path for inequality.
169       /// @returns true if \p this and \p that refer to different things.
170       /// @brief Inequality Operator
171       bool operator!=(const Path &that) const { return !(*this == that); }
172
173       /// Determines if \p this Path is less than \p that Path. This is required
174       /// so that Path objects can be placed into ordered collections (e.g.
175       /// std::map). The comparison is done lexicographically as defined by
176       /// the std::string::compare method.
177       /// @returns true if \p this path is lexicographically less than \p that.
178       /// @brief Less Than Operator
179       bool operator<(const Path& that) const;
180
181     /// @}
182     /// @name Path Accessors
183     /// @{
184     public:
185       /// This function will use an operating system specific algorithm to
186       /// determine if the current value of \p this is a syntactically valid
187       /// path name for the operating system. The path name does not need to
188       /// exist, validity is simply syntactical. Empty paths are always invalid.
189       /// @returns true iff the path name is syntactically legal for the
190       /// host operating system.
191       /// @brief Determine if a path is syntactically valid or not.
192       bool isValid() const;
193
194       /// This function determines if the contents of the path name are empty.
195       /// That is, the path name has a zero length. This does NOT determine if
196       /// if the file is empty. To get the length of the file itself, Use the
197       /// PathWithStatus::getFileStatus() method and then the getSize() method
198       /// on the returned FileStatus object.
199       /// @returns true iff the path is empty.
200       /// @brief Determines if the path name is empty (invalid).
201       bool isEmpty() const { return path.empty(); }
202
203       /// This function strips off the path and suffix of the file or directory
204       /// name and returns just the basename. For example /a/foo.bar would cause
205       /// this function to return "foo".
206       /// @returns StringRef containing the basename of the path
207       /// @brief Get the base name of the path
208       LLVM_ATTRIBUTE_DEPRECATED(StringRef getBasename() const,
209         LLVM_PATH_DEPRECATED_MSG(path::stem));
210
211       /// This function strips off the suffix of the path beginning with the
212       /// path separator ('/' on Unix, '\' on Windows) and returns the result.
213       LLVM_ATTRIBUTE_DEPRECATED(StringRef getDirname() const,
214         LLVM_PATH_DEPRECATED_MSG(path::parent_path));
215
216       /// This function strips off the path and basename(up to and
217       /// including the last dot) of the file or directory name and
218       /// returns just the suffix. For example /a/foo.bar would cause
219       /// this function to return "bar".
220       /// @returns StringRef containing the suffix of the path
221       /// @brief Get the suffix of the path
222       LLVM_ATTRIBUTE_DEPRECATED(StringRef getSuffix() const,
223         LLVM_PATH_DEPRECATED_MSG(path::extension));
224
225       /// Obtain a 'C' string for the path name.
226       /// @returns a 'C' string containing the path name.
227       /// @brief Returns the path as a C string.
228       const char *c_str() const { return path.c_str(); }
229       const std::string &str() const { return path; }
230
231
232       /// size - Return the length in bytes of this path name.
233       size_t size() const { return path.size(); }
234
235       /// empty - Returns true if the path is empty.
236       unsigned empty() const { return path.empty(); }
237
238     /// @}
239     /// @name Disk Accessors
240     /// @{
241     public:
242       /// This function determines if the path name is absolute, as opposed to
243       /// relative.
244       /// @brief Determine if the path is absolute.
245       LLVM_ATTRIBUTE_DEPRECATED(
246         bool isAbsolute() const,
247         LLVM_PATH_DEPRECATED_MSG(path::is_absolute));
248
249       /// This function determines if the path name is absolute, as opposed to
250       /// relative.
251       /// @brief Determine if the path is absolute.
252       LLVM_ATTRIBUTE_DEPRECATED(
253         static bool isAbsolute(const char *NameStart, unsigned NameLen),
254         LLVM_PATH_DEPRECATED_MSG(path::is_absolute));
255
256       /// This function opens the file associated with the path name provided by
257       /// the Path object and reads its magic number. If the magic number at the
258       /// start of the file matches \p magic, true is returned. In all other
259       /// cases (file not found, file not accessible, etc.) it returns false.
260       /// @returns true if the magic number of the file matches \p magic.
261       /// @brief Determine if file has a specific magic number
262       LLVM_ATTRIBUTE_DEPRECATED(bool hasMagicNumber(StringRef magic) const,
263         LLVM_PATH_DEPRECATED_MSG(fs::has_magic));
264
265       /// This function retrieves the first \p len bytes of the file associated
266       /// with \p this. These bytes are returned as the "magic number" in the
267       /// \p Magic parameter.
268       /// @returns true if the Path is a file and the magic number is retrieved,
269       /// false otherwise.
270       /// @brief Get the file's magic number.
271       bool getMagicNumber(std::string& Magic, unsigned len) const;
272
273       /// This function determines if the path name in the object references an
274       /// archive file by looking at its magic number.
275       /// @returns true if the file starts with the magic number for an archive
276       /// file.
277       /// @brief Determine if the path references an archive file.
278       bool isArchive() const;
279
280       /// This function determines if the path name in the object references an
281       /// LLVM Bitcode file by looking at its magic number.
282       /// @returns true if the file starts with the magic number for LLVM
283       /// bitcode files.
284       /// @brief Determine if the path references a bitcode file.
285       bool isBitcodeFile() const;
286
287       /// This function determines if the path name in the object references a
288       /// native Dynamic Library (shared library, shared object) by looking at
289       /// the file's magic number. The Path object must reference a file, not a
290       /// directory.
291       /// @returns true if the file starts with the magic number for a native
292       /// shared library.
293       /// @brief Determine if the path references a dynamic library.
294       bool isDynamicLibrary() const;
295
296       /// This function determines if the path name in the object references a
297       /// native object file by looking at it's magic number. The term object
298       /// file is defined as "an organized collection of separate, named
299       /// sequences of binary data." This covers the obvious file formats such
300       /// as COFF and ELF, but it also includes llvm ir bitcode, archives,
301       /// libraries, etc...
302       /// @returns true if the file starts with the magic number for an object
303       /// file.
304       /// @brief Determine if the path references an object file.
305       bool isObjectFile() const;
306
307       /// This function determines if the path name references an existing file
308       /// or directory in the file system.
309       /// @returns true if the pathname references an existing file or
310       /// directory.
311       /// @brief Determines if the path is a file or directory in
312       /// the file system.
313       LLVM_ATTRIBUTE_DEPRECATED(bool exists() const,
314         LLVM_PATH_DEPRECATED_MSG(fs::exists));
315
316       /// This function determines if the path name references an
317       /// existing directory.
318       /// @returns true if the pathname references an existing directory.
319       /// @brief Determines if the path is a directory in the file system.
320       LLVM_ATTRIBUTE_DEPRECATED(bool isDirectory() const,
321         LLVM_PATH_DEPRECATED_MSG(fs::is_directory));
322
323       /// This function determines if the path name references an
324       /// existing symbolic link.
325       /// @returns true if the pathname references an existing symlink.
326       /// @brief Determines if the path is a symlink in the file system.
327       LLVM_ATTRIBUTE_DEPRECATED(bool isSymLink() const,
328         LLVM_PATH_DEPRECATED_MSG(fs::is_symlink));
329
330       /// This function determines if the path name references a readable file
331       /// or directory in the file system. This function checks for
332       /// the existence and readability (by the current program) of the file
333       /// or directory.
334       /// @returns true if the pathname references a readable file.
335       /// @brief Determines if the path is a readable file or directory
336       /// in the file system.
337       bool canRead() const;
338
339       /// This function determines if the path name references a writable file
340       /// or directory in the file system. This function checks for the
341       /// existence and writability (by the current program) of the file or
342       /// directory.
343       /// @returns true if the pathname references a writable file.
344       /// @brief Determines if the path is a writable file or directory
345       /// in the file system.
346       bool canWrite() const;
347
348       /// This function checks that what we're trying to work only on a regular
349       /// file. Check for things like /dev/null, any block special file, or
350       /// other things that aren't "regular" regular files.
351       /// @returns true if the file is S_ISREG.
352       /// @brief Determines if the file is a regular file
353       bool isRegularFile() const;
354
355       /// This function determines if the path name references an executable
356       /// file in the file system. This function checks for the existence and
357       /// executability (by the current program) of the file.
358       /// @returns true if the pathname references an executable file.
359       /// @brief Determines if the path is an executable file in the file
360       /// system.
361       bool canExecute() const;
362
363       /// This function builds a list of paths that are the names of the
364       /// files and directories in a directory.
365       /// @returns true if an error occurs, true otherwise
366       /// @brief Build a list of directory's contents.
367       bool getDirectoryContents(
368         std::set<Path> &paths, ///< The resulting list of file & directory names
369         std::string* ErrMsg    ///< Optional place to return an error message.
370       ) const;
371
372     /// @}
373     /// @name Path Mutators
374     /// @{
375     public:
376       /// The path name is cleared and becomes empty. This is an invalid
377       /// path name but is the *only* invalid path name. This is provided
378       /// so that path objects can be used to indicate the lack of a
379       /// valid path being found.
380       /// @brief Make the path empty.
381       void clear() { path.clear(); }
382
383       /// This method sets the Path object to \p unverified_path. This can fail
384       /// if the \p unverified_path does not pass the syntactic checks of the
385       /// isValid() method. If verification fails, the Path object remains
386       /// unchanged and false is returned. Otherwise true is returned and the
387       /// Path object takes on the path value of \p unverified_path
388       /// @returns true if the path was set, false otherwise.
389       /// @param unverified_path The path to be set in Path object.
390       /// @brief Set a full path from a StringRef
391       bool set(StringRef unverified_path);
392
393       /// One path component is removed from the Path. If only one component is
394       /// present in the path, the Path object becomes empty. If the Path object
395       /// is empty, no change is made.
396       /// @returns false if the path component could not be removed.
397       /// @brief Removes the last directory component of the Path.
398       bool eraseComponent();
399
400       /// The \p component is added to the end of the Path if it is a legal
401       /// name for the operating system. A directory separator will be added if
402       /// needed.
403       /// @returns false if the path component could not be added.
404       /// @brief Appends one path component to the Path.
405       bool appendComponent(StringRef component);
406
407       /// A period and the \p suffix are appended to the end of the pathname.
408       /// When the \p suffix is empty, no action is performed.
409       /// @brief Adds a period and the \p suffix to the end of the pathname.
410       void appendSuffix(StringRef suffix);
411
412       /// The suffix of the filename is erased. The suffix begins with and
413       /// includes the last . character in the filename after the last directory
414       /// separator and extends until the end of the name. If no . character is
415       /// after the last directory separator, then the file name is left
416       /// unchanged (i.e. it was already without a suffix) but the function
417       /// returns false.
418       /// @returns false if there was no suffix to remove, true otherwise.
419       /// @brief Remove the suffix from a path name.
420       bool eraseSuffix();
421
422       /// The current Path name is made unique in the file system. Upon return,
423       /// the Path will have been changed to make a unique file in the file
424       /// system or it will not have been changed if the current path name is
425       /// already unique.
426       /// @throws std::string if an unrecoverable error occurs.
427       /// @brief Make the current path name unique in the file system.
428       bool makeUnique( bool reuse_current /*= true*/, std::string* ErrMsg );
429
430       /// The current Path name is made absolute by prepending the
431       /// current working directory if necessary.
432       LLVM_ATTRIBUTE_DEPRECATED(
433         void makeAbsolute(),
434         LLVM_PATH_DEPRECATED_MSG(fs::make_absolute));
435
436     /// @}
437     /// @name Disk Mutators
438     /// @{
439     public:
440       /// This method attempts to make the file referenced by the Path object
441       /// available for reading so that the canRead() method will return true.
442       /// @brief Make the file readable;
443       bool makeReadableOnDisk(std::string* ErrMsg = 0);
444
445       /// This method attempts to make the file referenced by the Path object
446       /// available for writing so that the canWrite() method will return true.
447       /// @brief Make the file writable;
448       bool makeWriteableOnDisk(std::string* ErrMsg = 0);
449
450       /// This method attempts to make the file referenced by the Path object
451       /// available for execution so that the canExecute() method will return
452       /// true.
453       /// @brief Make the file readable;
454       bool makeExecutableOnDisk(std::string* ErrMsg = 0);
455
456       /// This method allows the last modified time stamp and permission bits
457       /// to be set on the disk object referenced by the Path.
458       /// @throws std::string if an error occurs.
459       /// @returns true on error.
460       /// @brief Set the status information.
461       bool setStatusInfoOnDisk(const FileStatus &SI,
462                                std::string *ErrStr = 0) const;
463
464       /// This method attempts to create a directory in the file system with the
465       /// same name as the Path object. The \p create_parents parameter controls
466       /// whether intermediate directories are created or not. if \p
467       /// create_parents is true, then an attempt will be made to create all
468       /// intermediate directories, as needed. If \p create_parents is false,
469       /// then only the final directory component of the Path name will be
470       /// created. The created directory will have no entries.
471       /// @returns true if the directory could not be created, false otherwise
472       /// @brief Create the directory this Path refers to.
473       bool createDirectoryOnDisk(
474         bool create_parents = false, ///<  Determines whether non-existent
475            ///< directory components other than the last one (the "parents")
476            ///< are created or not.
477         std::string* ErrMsg = 0 ///< Optional place to put error messages.
478       );
479
480       /// This method attempts to create a file in the file system with the same
481       /// name as the Path object. The intermediate directories must all exist
482       /// at the time this method is called. Use createDirectoriesOnDisk to
483       /// accomplish that. The created file will be empty upon return from this
484       /// function.
485       /// @returns true if the file could not be created, false otherwise.
486       /// @brief Create the file this Path refers to.
487       bool createFileOnDisk(
488         std::string* ErrMsg = 0 ///< Optional place to put error messages.
489       );
490
491       /// This is like createFile except that it creates a temporary file. A
492       /// unique temporary file name is generated based on the contents of
493       /// \p this before the call. The new name is assigned to \p this and the
494       /// file is created.  Note that this will both change the Path object
495       /// *and* create the corresponding file. This function will ensure that
496       /// the newly generated temporary file name is unique in the file system.
497       /// @returns true if the file couldn't be created, false otherwise.
498       /// @brief Create a unique temporary file
499       bool createTemporaryFileOnDisk(
500         bool reuse_current = false, ///< When set to true, this parameter
501           ///< indicates that if the current file name does not exist then
502           ///< it will be used without modification.
503         std::string* ErrMsg = 0 ///< Optional place to put error messages
504       );
505
506       /// This method renames the file referenced by \p this as \p newName. The
507       /// file referenced by \p this must exist. The file referenced by
508       /// \p newName does not need to exist.
509       /// @returns true on error, false otherwise
510       /// @brief Rename one file as another.
511       bool renamePathOnDisk(const Path& newName, std::string* ErrMsg);
512
513       /// This method attempts to destroy the file or directory named by the
514       /// last component of the Path. If the Path refers to a directory and the
515       /// \p destroy_contents is false, an attempt will be made to remove just
516       /// the directory (the final Path component). If \p destroy_contents is
517       /// true, an attempt will be made to remove the entire contents of the
518       /// directory, recursively. If the Path refers to a file, the
519       /// \p destroy_contents parameter is ignored.
520       /// @param destroy_contents Indicates whether the contents of a destroyed
521       /// @param Err An optional string to receive an error message.
522       /// directory should also be destroyed (recursively).
523       /// @returns false if the file/directory was destroyed, true on error.
524       /// @brief Removes the file or directory from the filesystem.
525       bool eraseFromDisk(bool destroy_contents = false,
526                          std::string *Err = 0) const;
527
528
529       /// MapInFilePages - This is a low level system API to map in the file
530       /// that is currently opened as FD into the current processes' address
531       /// space for read only access.  This function may return null on failure
532       /// or if the system cannot provide the following constraints:
533       ///  1) The pages must be valid after the FD is closed, until
534       ///     UnMapFilePages is called.
535       ///  2) Any padding after the end of the file must be zero filled, if
536       ///     present.
537       ///  3) The pages must be contiguous.
538       ///
539       /// This API is not intended for general use, clients should use
540       /// MemoryBuffer::getFile instead.
541       static const char *MapInFilePages(int FD, size_t FileSize,
542                                         off_t Offset);
543
544       /// UnMapFilePages - Free pages mapped into the current process by
545       /// MapInFilePages.
546       ///
547       /// This API is not intended for general use, clients should use
548       /// MemoryBuffer::getFile instead.
549       static void UnMapFilePages(const char *Base, size_t FileSize);
550
551     /// @}
552     /// @name Data
553     /// @{
554     protected:
555       // Our win32 implementation relies on this string being mutable.
556       mutable std::string path;   ///< Storage for the path name.
557
558
559     /// @}
560   };
561
562   /// This class is identical to Path class except it allows you to obtain the
563   /// file status of the Path as well. The reason for the distinction is one of
564   /// efficiency. First, the file status requires additional space and the space
565   /// is incorporated directly into PathWithStatus without an additional malloc.
566   /// Second, obtaining status information is an expensive operation on most
567   /// operating systems so we want to be careful and explicit about where we
568   /// allow this operation in LLVM.
569   /// @brief Path with file status class.
570   class PathWithStatus : public Path {
571     /// @name Constructors
572     /// @{
573     public:
574       /// @brief Default constructor
575       PathWithStatus() : Path(), status(), fsIsValid(false) {}
576
577       /// @brief Copy constructor
578       PathWithStatus(const PathWithStatus &that)
579         : Path(static_cast<const Path&>(that)), status(that.status),
580            fsIsValid(that.fsIsValid) {}
581
582       /// This constructor allows construction from a Path object
583       /// @brief Path constructor
584       PathWithStatus(const Path &other)
585         : Path(other), status(), fsIsValid(false) {}
586
587       /// This constructor will accept a char* or std::string as a path. No
588       /// checking is done on this path to determine if it is valid. To
589       /// determine validity of the path, use the isValid method.
590       /// @brief Construct a Path from a string.
591       explicit PathWithStatus(
592         StringRef p ///< The path to assign.
593       ) : Path(p), status(), fsIsValid(false) {}
594
595       /// This constructor will accept a character range as a path.  No checking
596       /// is done on this path to determine if it is valid.  To determine
597       /// validity of the path, use the isValid method.
598       /// @brief Construct a Path from a string.
599       explicit PathWithStatus(
600         const char *StrStart,  ///< Pointer to the first character of the path
601         unsigned StrLen        ///< Length of the path.
602       ) : Path(StrStart, StrLen), status(), fsIsValid(false) {}
603
604       /// Makes a copy of \p that to \p this.
605       /// @returns \p this
606       /// @brief Assignment Operator
607       PathWithStatus &operator=(const PathWithStatus &that) {
608         static_cast<Path&>(*this) = static_cast<const Path&>(that);
609         status = that.status;
610         fsIsValid = that.fsIsValid;
611         return *this;
612       }
613
614       /// Makes a copy of \p that to \p this.
615       /// @returns \p this
616       /// @brief Assignment Operator
617       PathWithStatus &operator=(const Path &that) {
618         static_cast<Path&>(*this) = static_cast<const Path&>(that);
619         fsIsValid = false;
620         return *this;
621       }
622
623     /// @}
624     /// @name Methods
625     /// @{
626     public:
627       /// This function returns status information about the file. The type of
628       /// path (file or directory) is updated to reflect the actual contents
629       /// of the file system.
630       /// @returns 0 on failure, with Error explaining why (if non-zero),
631       /// otherwise returns a pointer to a FileStatus structure on success.
632       /// @brief Get file status.
633       const FileStatus *getFileStatus(
634         bool forceUpdate = false, ///< Force an update from the file system
635         std::string *Error = 0    ///< Optional place to return an error msg.
636       ) const;
637
638     /// @}
639     /// @name Data
640     /// @{
641     private:
642       mutable FileStatus status; ///< Status information.
643       mutable bool fsIsValid;    ///< Whether we've obtained it or not
644
645     /// @}
646   };
647
648   /// This function can be used to copy the file specified by Src to the
649   /// file specified by Dest. If an error occurs, Dest is removed.
650   /// @returns true if an error occurs, false otherwise
651   /// @brief Copy one file to another.
652   bool CopyFile(const Path& Dest, const Path& Src, std::string* ErrMsg);
653
654   /// This is the OS-specific path separator: a colon on Unix or a semicolon
655   /// on Windows.
656   extern const char PathSeparator;
657 }
658
659 }
660
661 #endif