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