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