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