LLVM API Documentation

Path.h

Go to the documentation of this file.
00001 //===- llvm/System/Path.h - Path Operating System Concept -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the llvm::sys::Path class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_SYSTEM_PATH_H
00015 #define LLVM_SYSTEM_PATH_H
00016 
00017 #include "llvm/System/TimeValue.h"
00018 #include <set>
00019 #include <string>
00020 #include <vector>
00021 #include <iosfwd>
00022 
00023 namespace llvm {
00024 namespace sys {
00025 
00026   /// This structure provides basic file system information about a file. It
00027   /// is patterned after the stat(2) Unix operating system call but made
00028   /// platform independent and eliminates many of the unix-specific fields.
00029   /// However, to support llvm-ar, the mode, user, and group fields are
00030   /// retained. These pertain to unix security and may not have a meaningful
00031   /// value on non-Unix platforms. However, the other fields fields should
00032   /// always be applicable on all platforms.  The structure is filled in by
00033   /// the PathWithStatus class.
00034   /// @brief File status structure
00035   class FileStatus {
00036   public:
00037     uint64_t    fileSize;   ///< Size of the file in bytes
00038     TimeValue   modTime;    ///< Time of file's modification
00039     uint32_t    mode;       ///< Mode of the file, if applicable
00040     uint32_t    user;       ///< User ID of owner, if applicable
00041     uint32_t    group;      ///< Group ID of owner, if applicable
00042     uint64_t    uniqueID;   ///< A number to uniquely ID this file
00043     bool        isDir  : 1; ///< True if this is a directory.
00044     bool        isFile : 1; ///< True if this is a file.
00045 
00046     FileStatus() : fileSize(0), modTime(0,0), mode(0777), user(999),
00047                    group(999), uniqueID(0), isDir(false), isFile(false) { }
00048 
00049     TimeValue getTimestamp() const { return modTime; }
00050     uint64_t getSize() const { return fileSize; }
00051     uint32_t getMode() const { return mode; }
00052     uint32_t getUser() const { return user; }
00053     uint32_t getGroup() const { return group; }
00054     uint64_t getUniqueID() const { return uniqueID; }
00055   };
00056 
00057   /// This class provides an abstraction for the path to a file or directory
00058   /// in the operating system's filesystem and provides various basic operations
00059   /// on it.  Note that this class only represents the name of a path to a file
00060   /// or directory which may or may not be valid for a given machine's file
00061   /// system. The class is patterned after the java.io.File class with various
00062   /// extensions and several omissions (not relevant to LLVM).  A Path object
00063   /// ensures that the path it encapsulates is syntactically valid for the
00064   /// operating system it is running on but does not ensure correctness for
00065   /// any particular file system. That is, a syntactically valid path might
00066   /// specify path components that do not exist in the file system and using
00067   /// such a Path to act on the file system could produce errors. There is one
00068   /// invalid Path value which is permitted: the empty path.  The class should
00069   /// never allow a syntactically invalid non-empty path name to be assigned.
00070   /// Empty paths are required in order to indicate an error result in some
00071   /// situations. If the path is empty, the isValid operation will return
00072   /// false. All operations will fail if isValid is false. Operations that
00073   /// change the path will either return false if it would cause a syntactically
00074   /// invalid path name (in which case the Path object is left unchanged) or
00075   /// throw an std::string exception indicating the error. The methods are
00076   /// grouped into four basic categories: Path Accessors (provide information
00077   /// about the path without accessing disk), Disk Accessors (provide
00078   /// information about the underlying file or directory), Path Mutators
00079   /// (change the path information, not the disk), and Disk Mutators (change
00080   /// the disk file/directory referenced by the path). The Disk Mutator methods
00081   /// all have the word "disk" embedded in their method name to reinforce the
00082   /// notion that the operation modifies the file system.
00083   /// @since 1.4
00084   /// @brief An abstraction for operating system paths.
00085   class Path {
00086     /// @name Constructors
00087     /// @{
00088     public:
00089       /// Construct a path to the root directory of the file system. The root
00090       /// directory is a top level directory above which there are no more
00091       /// directories. For example, on UNIX, the root directory is /. On Windows
00092       /// it is C:\. Other operating systems may have different notions of
00093       /// what the root directory is or none at all. In that case, a consistent
00094       /// default root directory will be used.
00095       static Path GetRootDirectory();
00096 
00097       /// Construct a path to a unique temporary directory that is created in
00098       /// a "standard" place for the operating system. The directory is
00099       /// guaranteed to be created on exit from this function. If the directory
00100       /// cannot be created, the function will throw an exception.
00101       /// @returns an invalid path (empty) on error
00102       /// @param ErrMsg Optional place for an error message if an error occurs
00103       /// @brief Constrct a path to an new, unique, existing temporary
00104       /// directory.
00105       static Path GetTemporaryDirectory(std::string* ErrMsg = 0);
00106 
00107       /// Construct a vector of sys::Path that contains the "standard" system
00108       /// library paths suitable for linking into programs. This function *must*
00109       /// return the value of LLVM_LIB_SEARCH_PATH as the first item in \p Paths
00110       /// if that environment variable is set and it references a directory.
00111       /// @brief Construct a path to the system library directory
00112       static void GetSystemLibraryPaths(std::vector<sys::Path>& Paths);
00113 
00114       /// Construct a vector of sys::Path that contains the "standard" bitcode
00115       /// library paths suitable for linking into an llvm program. This function
00116       /// *must* return the value of LLVM_LIB_SEARCH_PATH as well as the value
00117       /// of LLVM_LIBDIR. It also must provide the System library paths as
00118       /// returned by GetSystemLibraryPaths.
00119       /// @see GetSystemLibraryPaths
00120       /// @brief Construct a list of directories in which bitcode could be
00121       /// found.
00122       static void GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths);
00123 
00124       /// Find the path to a library using its short name. Use the system
00125       /// dependent library paths to locate the library.
00126       /// @brief Find a library.
00127       static Path FindLibrary(std::string& short_name);
00128 
00129       /// Construct a path to the default LLVM configuration directory. The
00130       /// implementation must ensure that this is a well-known (same on many
00131       /// systems) directory in which llvm configuration files exist. For
00132       /// example, on Unix, the /etc/llvm directory has been selected.
00133       /// @brief Construct a path to the default LLVM configuration directory
00134       static Path GetLLVMDefaultConfigDir();
00135 
00136       /// Construct a path to the LLVM installed configuration directory. The
00137       /// implementation must ensure that this refers to the "etc" directory of
00138       /// the LLVM installation. This is the location where configuration files
00139       /// will be located for a particular installation of LLVM on a machine.
00140       /// @brief Construct a path to the LLVM installed configuration directory
00141       static Path GetLLVMConfigDir();
00142 
00143       /// Construct a path to the current user's home directory. The
00144       /// implementation must use an operating system specific mechanism for
00145       /// determining the user's home directory. For example, the environment
00146       /// variable "HOME" could be used on Unix. If a given operating system
00147       /// does not have the concept of a user's home directory, this static
00148       /// constructor must provide the same result as GetRootDirectory.
00149       /// @brief Construct a path to the current user's "home" directory
00150       static Path GetUserHomeDirectory();
00151 
00152       /// Construct a path to the current directory for the current process.
00153       /// @returns The current working directory.
00154       /// @brief Returns the current working directory.
00155       static Path GetCurrentDirectory();
00156 
00157       /// Return the suffix commonly used on file names that contain a shared
00158       /// object, shared archive, or dynamic link library. Such files are
00159       /// linked at runtime into a process and their code images are shared
00160       /// between processes.
00161       /// @returns The dynamic link library suffix for the current platform.
00162       /// @brief Return the dynamic link library suffix.
00163       static std::string GetDLLSuffix();
00164 
00165       /// GetMainExecutable - Return the path to the main executable, given the
00166       /// value of argv[0] from program startup and the address of main itself.
00167       static Path GetMainExecutable(const char *argv0, void *MainAddr);
00168 
00169       /// This is one of the very few ways in which a path can be constructed
00170       /// with a syntactically invalid name. The only *legal* invalid name is an
00171       /// empty one. Other invalid names are not permitted. Empty paths are
00172       /// provided so that they can be used to indicate null or error results in
00173       /// other lib/System functionality.
00174       /// @brief Construct an empty (and invalid) path.
00175       Path() : path() {}
00176       Path(const Path &that) : path(that.path) {}
00177 
00178       /// This constructor will accept a std::string as a path. No checking is
00179       /// done on this path to determine if it is valid. To determine validity
00180       /// of the path, use the isValid method.
00181       /// @param p The path to assign.
00182       /// @brief Construct a Path from a string.
00183       explicit Path(const std::string& p);
00184 
00185       /// This constructor will accept a character range as a path.  No checking
00186       /// is done on this path to determine if it is valid.  To determine
00187       /// validity of the path, use the isValid method.
00188       /// @param StrStart A pointer to the first character of the path name
00189       /// @param StrLen The length of the path name at StrStart
00190       /// @brief Construct a Path from a string.
00191       Path(const char *StrStart, unsigned StrLen);
00192 
00193     /// @}
00194     /// @name Operators
00195     /// @{
00196     public:
00197       /// Makes a copy of \p that to \p this.
00198       /// @returns \p this
00199       /// @brief Assignment Operator
00200       Path &operator=(const Path &that) {
00201         path = that.path;
00202         return *this;
00203       }
00204 
00205       /// Makes a copy of \p that to \p this.
00206       /// @param \p that A std::string denoting the path
00207       /// @returns \p this
00208       /// @brief Assignment Operator
00209       Path &operator=(const std::string &that);
00210 
00211       /// Compares \p this Path with \p that Path for equality.
00212       /// @returns true if \p this and \p that refer to the same thing.
00213       /// @brief Equality Operator
00214       bool operator==(const Path &that) const;
00215 
00216       /// Compares \p this Path with \p that Path for inequality.
00217       /// @returns true if \p this and \p that refer to different things.
00218       /// @brief Inequality Operator
00219       bool operator!=(const Path &that) const;
00220 
00221       /// Determines if \p this Path is less than \p that Path. This is required
00222       /// so that Path objects can be placed into ordered collections (e.g.
00223       /// std::map). The comparison is done lexicographically as defined by
00224       /// the std::string::compare method.
00225       /// @returns true if \p this path is lexicographically less than \p that.
00226       /// @brief Less Than Operator
00227       bool operator<(const Path& that) const;
00228 
00229     /// @}
00230     /// @name Path Accessors
00231     /// @{
00232     public:
00233       /// This function will use an operating system specific algorithm to
00234       /// determine if the current value of \p this is a syntactically valid
00235       /// path name for the operating system. The path name does not need to
00236       /// exist, validity is simply syntactical. Empty paths are always invalid.
00237       /// @returns true iff the path name is syntactically legal for the
00238       /// host operating system.
00239       /// @brief Determine if a path is syntactically valid or not.
00240       bool isValid() const;
00241 
00242       /// This function determines if the contents of the path name are empty.
00243       /// That is, the path name has a zero length. This does NOT determine if
00244       /// if the file is empty. To get the length of the file itself, Use the
00245       /// PathWithStatus::getFileStatus() method and then the getSize() method
00246       /// on the returned FileStatus object.
00247       /// @returns true iff the path is empty.
00248       /// @brief Determines if the path name is empty (invalid).
00249       bool isEmpty() const { return path.empty(); }
00250 
00251       /// This function returns the current contents of the path as a
00252       /// std::string. This allows the underlying path string to be manipulated.
00253       /// @returns std::string containing the path name.
00254       /// @brief Returns the path as a std::string.
00255       const std::string &toString() const { return path; }
00256 
00257       /// This function returns the last component of the path name. The last
00258       /// component is the file or directory name occuring after the last
00259       /// directory separator. If no directory separator is present, the entire
00260       /// path name is returned (i.e. same as toString).
00261       /// @returns std::string containing the last component of the path name.
00262       /// @brief Returns the last component of the path name.
00263       std::string getLast() const;
00264 
00265       /// This function strips off the path and suffix of the file or directory
00266       /// name and returns just the basename. For example /a/foo.bar would cause
00267       /// this function to return "foo".
00268       /// @returns std::string containing the basename of the path
00269       /// @brief Get the base name of the path
00270       std::string getBasename() const;
00271 
00272       /// This function strips off the suffix of the path beginning with the
00273       /// path separator ('/' on Unix, '\' on Windows) and returns the result.
00274       std::string getDirname() const;
00275 
00276       /// This function strips off the path and basename(up to and
00277       /// including the last dot) of the file or directory name and
00278       /// returns just the suffix. For example /a/foo.bar would cause
00279       /// this function to return "bar".
00280       /// @returns std::string containing the suffix of the path
00281       /// @brief Get the suffix of the path
00282       std::string getSuffix() const;
00283 
00284       /// Obtain a 'C' string for the path name.
00285       /// @returns a 'C' string containing the path name.
00286       /// @brief Returns the path as a C string.
00287       const char *c_str() const { return path.c_str(); }
00288 
00289       /// size - Return the length in bytes of this path name.
00290       size_t size() const { return path.size(); }
00291 
00292       /// empty - Returns true if the path is empty.
00293       unsigned empty() const { return path.empty(); }
00294 
00295     /// @}
00296     /// @name Disk Accessors
00297     /// @{
00298     public:
00299       /// This function determines if the path name in this object references
00300       /// the root (top level directory) of the file system. The details of what
00301       /// is considered the "root" may vary from system to system so this method
00302       /// will do the necessary checking.
00303       /// @returns true iff the path name references the root directory.
00304       /// @brief Determines if the path references the root directory.
00305       bool isRootDirectory() const;
00306 
00307       /// This function determines if the path name is absolute, as opposed to
00308       /// relative.
00309       /// @brief Determine if the path is absolute.
00310       bool isAbsolute() const;
00311 
00312       /// This function opens the file associated with the path name provided by
00313       /// the Path object and reads its magic number. If the magic number at the
00314       /// start of the file matches \p magic, true is returned. In all other
00315       /// cases (file not found, file not accessible, etc.) it returns false.
00316       /// @returns true if the magic number of the file matches \p magic.
00317       /// @brief Determine if file has a specific magic number
00318       bool hasMagicNumber(const std::string& magic) const;
00319 
00320       /// This function retrieves the first \p len bytes of the file associated
00321       /// with \p this. These bytes are returned as the "magic number" in the
00322       /// \p Magic parameter.
00323       /// @returns true if the Path is a file and the magic number is retrieved,
00324       /// false otherwise.
00325       /// @brief Get the file's magic number.
00326       bool getMagicNumber(std::string& Magic, unsigned len) const;
00327 
00328       /// This function determines if the path name in the object references an
00329       /// archive file by looking at its magic number.
00330       /// @returns true if the file starts with the magic number for an archive
00331       /// file.
00332       /// @brief Determine if the path references an archive file.
00333       bool isArchive() const;
00334 
00335       /// This function determines if the path name in the object references an
00336       /// LLVM Bitcode file by looking at its magic number.
00337       /// @returns true if the file starts with the magic number for LLVM
00338       /// bitcode files.
00339       /// @brief Determine if the path references a bitcode file.
00340       bool isBitcodeFile() const;
00341 
00342       /// This function determines if the path name in the object references a
00343       /// native Dynamic Library (shared library, shared object) by looking at
00344       /// the file's magic number. The Path object must reference a file, not a
00345       /// directory.
00346       /// @return strue if the file starts with the magid number for a native
00347       /// shared library.
00348       /// @brief Determine if the path reference a dynamic library.
00349       bool isDynamicLibrary() const;
00350 
00351       /// This function determines if the path name references an existing file
00352       /// or directory in the file system.
00353       /// @returns true if the pathname references an existing file or
00354       /// directory.
00355       /// @brief Determines if the path is a file or directory in
00356       /// the file system.
00357       bool exists() const;
00358 
00359       /// This function determines if the path name refences an
00360       /// existing directory.
00361       /// @returns true if the pathname references an existing directory.
00362       /// @brief Determins if the path is a directory in the file system.
00363       bool isDirectory() const;
00364 
00365       /// This function determines if the path name references a readable file
00366       /// or directory in the file system. This function checks for
00367       /// the existence and readability (by the current program) of the file
00368       /// or directory.
00369       /// @returns true if the pathname references a readable file.
00370       /// @brief Determines if the path is a readable file or directory
00371       /// in the file system.
00372       bool canRead() const;
00373 
00374       /// This function determines if the path name references a writable file
00375       /// or directory in the file system. This function checks for the
00376       /// existence and writability (by the current program) of the file or
00377       /// directory.
00378       /// @returns true if the pathname references a writable file.
00379       /// @brief Determines if the path is a writable file or directory
00380       /// in the file system.
00381       bool canWrite() const;
00382 
00383       /// This function determines if the path name references an executable
00384       /// file in the file system. This function checks for the existence and
00385       /// executability (by the current program) of the file.
00386       /// @returns true if the pathname references an executable file.
00387       /// @brief Determines if the path is an executable file in the file
00388       /// system.
00389       bool canExecute() const;
00390 
00391       /// This function builds a list of paths that are the names of the
00392       /// files and directories in a directory.
00393       /// @returns true if an error occurs, true otherwise
00394       /// @brief Build a list of directory's contents.
00395       bool getDirectoryContents(
00396         std::set<Path> &paths, ///< The resulting list of file & directory names
00397         std::string* ErrMsg    ///< Optional place to return an error message.
00398       ) const;
00399 
00400     /// @}
00401     /// @name Path Mutators
00402     /// @{
00403     public:
00404       /// The path name is cleared and becomes empty. This is an invalid
00405       /// path name but is the *only* invalid path name. This is provided
00406       /// so that path objects can be used to indicate the lack of a
00407       /// valid path being found.
00408       /// @brief Make the path empty.
00409       void clear() { path.clear(); }
00410 
00411       /// This method sets the Path object to \p unverified_path. This can fail
00412       /// if the \p unverified_path does not pass the syntactic checks of the
00413       /// isValid() method. If verification fails, the Path object remains
00414       /// unchanged and false is returned. Otherwise true is returned and the
00415       /// Path object takes on the path value of \p unverified_path
00416       /// @returns true if the path was set, false otherwise.
00417       /// @param unverified_path The path to be set in Path object.
00418       /// @brief Set a full path from a std::string
00419       bool set(const std::string& unverified_path);
00420 
00421       /// One path component is removed from the Path. If only one component is
00422       /// present in the path, the Path object becomes empty. If the Path object
00423       /// is empty, no change is made.
00424       /// @returns false if the path component could not be removed.
00425       /// @brief Removes the last directory component of the Path.
00426       bool eraseComponent();
00427 
00428       /// The \p component is added to the end of the Path if it is a legal
00429       /// name for the operating system. A directory separator will be added if
00430       /// needed.
00431       /// @returns false if the path component could not be added.
00432       /// @brief Appends one path component to the Path.
00433       bool appendComponent( const std::string& component );
00434 
00435       /// A period and the \p suffix are appended to the end of the pathname.
00436       /// The precondition for this function is that the Path reference a file
00437       /// name (i.e. isFile() returns true). If the Path is not a file, no
00438       /// action is taken and the function returns false. If the path would
00439       /// become invalid for the host operating system, false is returned.
00440       /// @returns false if the suffix could not be added, true if it was.
00441       /// @brief Adds a period and the \p suffix to the end of the pathname.
00442       bool appendSuffix(const std::string& suffix);
00443 
00444       /// The suffix of the filename is erased. The suffix begins with and
00445       /// includes the last . character in the filename after the last directory
00446       /// separator and extends until the end of the name. If no . character is
00447       /// after the last directory separator, then the file name is left
00448       /// unchanged (i.e. it was already without a suffix) but the function
00449       /// returns false.
00450       /// @returns false if there was no suffix to remove, true otherwise.
00451       /// @brief Remove the suffix from a path name.
00452       bool eraseSuffix();
00453 
00454       /// The current Path name is made unique in the file system. Upon return,
00455       /// the Path will have been changed to make a unique file in the file
00456       /// system or it will not have been changed if the current path name is
00457       /// already unique.
00458       /// @throws std::string if an unrecoverable error occurs.
00459       /// @brief Make the current path name unique in the file system.
00460       bool makeUnique( bool reuse_current /*= true*/, std::string* ErrMsg );
00461 
00462     /// @}
00463     /// @name Disk Mutators
00464     /// @{
00465     public:
00466       /// This method attempts to make the file referenced by the Path object
00467       /// available for reading so that the canRead() method will return true.
00468       /// @brief Make the file readable;
00469       bool makeReadableOnDisk(std::string* ErrMsg = 0);
00470 
00471       /// This method attempts to make the file referenced by the Path object
00472       /// available for writing so that the canWrite() method will return true.
00473       /// @brief Make the file writable;
00474       bool makeWriteableOnDisk(std::string* ErrMsg = 0);
00475 
00476       /// This method attempts to make the file referenced by the Path object
00477       /// available for execution so that the canExecute() method will return
00478       /// true.
00479       /// @brief Make the file readable;
00480       bool makeExecutableOnDisk(std::string* ErrMsg = 0);
00481 
00482       /// This method allows the last modified time stamp and permission bits
00483       /// to be set on the disk object referenced by the Path.
00484       /// @throws std::string if an error occurs.
00485       /// @returns true on error.
00486       /// @brief Set the status information.
00487       bool setStatusInfoOnDisk(const FileStatus &SI,
00488                                std::string *ErrStr = 0) const;
00489 
00490       /// This method attempts to create a directory in the file system with the
00491       /// same name as the Path object. The \p create_parents parameter controls
00492       /// whether intermediate directories are created or not. if \p
00493       /// create_parents is true, then an attempt will be made to create all
00494       /// intermediate directories, as needed. If \p create_parents is false,
00495       /// then only the final directory component of the Path name will be
00496       /// created. The created directory will have no entries.
00497       /// @returns true if the directory could not be created, false otherwise
00498       /// @brief Create the directory this Path refers to.
00499       bool createDirectoryOnDisk(
00500         bool create_parents = false, ///<  Determines whether non-existent
00501            ///< directory components other than the last one (the "parents")
00502            ///< are created or not.
00503         std::string* ErrMsg = 0 ///< Optional place to put error messages.
00504       );
00505 
00506       /// This method attempts to create a file in the file system with the same
00507       /// name as the Path object. The intermediate directories must all exist
00508       /// at the time this method is called. Use createDirectoriesOnDisk to
00509       /// accomplish that. The created file will be empty upon return from this
00510       /// function.
00511       /// @returns true if the file could not be created, false otherwise.
00512       /// @brief Create the file this Path refers to.
00513       bool createFileOnDisk(
00514         std::string* ErrMsg = 0 ///< Optional place to put error messages.
00515       );
00516 
00517       /// This is like createFile except that it creates a temporary file. A
00518       /// unique temporary file name is generated based on the contents of
00519       /// \p this before the call. The new name is assigned to \p this and the
00520       /// file is created.  Note that this will both change the Path object
00521       /// *and* create the corresponding file. This function will ensure that
00522       /// the newly generated temporary file name is unique in the file system.
00523       /// @returns true if the file couldn't be created, false otherwise.
00524       /// @brief Create a unique temporary file
00525       bool createTemporaryFileOnDisk(
00526         bool reuse_current = false, ///< When set to true, this parameter
00527           ///< indicates that if the current file name does not exist then
00528           ///< it will be used without modification.
00529         std::string* ErrMsg = 0 ///< Optional place to put error messages
00530       );
00531 
00532       /// This method renames the file referenced by \p this as \p newName. The
00533       /// file referenced by \p this must exist. The file referenced by
00534       /// \p newName does not need to exist.
00535       /// @returns true on error, false otherwise
00536       /// @brief Rename one file as another.
00537       bool renamePathOnDisk(const Path& newName, std::string* ErrMsg);
00538 
00539       /// This method attempts to destroy the file or directory named by the
00540       /// last component of the Path. If the Path refers to a directory and the
00541       /// \p destroy_contents is false, an attempt will be made to remove just
00542       /// the directory (the final Path component). If \p destroy_contents is
00543       /// true, an attempt will be made to remove the entire contents of the
00544       /// directory, recursively. If the Path refers to a file, the
00545       /// \p destroy_contents parameter is ignored.
00546       /// @param destroy_contents Indicates whether the contents of a destroyed
00547       /// @param Err An optional string to receive an error message.
00548       /// directory should also be destroyed (recursively).
00549       /// @returns false if the file/directory was destroyed, true on error.
00550       /// @brief Removes the file or directory from the filesystem.
00551       bool eraseFromDisk(bool destroy_contents = false,
00552                          std::string *Err = 0) const;
00553 
00554 
00555       /// MapInFilePages - This is a low level system API to map in the file
00556       /// that is currently opened as FD into the current processes' address
00557       /// space for read only access.  This function may return null on failure
00558       /// or if the system cannot provide the following constraints:
00559       ///  1) The pages must be valid after the FD is closed, until
00560       ///     UnMapFilePages is called.
00561       ///  2) Any padding after the end of the file must be zero filled, if
00562       ///     present.
00563       ///  3) The pages must be contiguous.
00564       ///
00565       /// This API is not intended for general use, clients should use
00566       /// MemoryBuffer::getFile instead.
00567       static const char *MapInFilePages(int FD, uint64_t FileSize);
00568 
00569       /// UnMapFilePages - Free pages mapped into the current process by
00570       /// MapInFilePages.
00571       ///
00572       /// This API is not intended for general use, clients should use
00573       /// MemoryBuffer::getFile instead.
00574       static void UnMapFilePages(const char *Base, uint64_t FileSize);
00575 
00576     /// @}
00577     /// @name Data
00578     /// @{
00579     protected:
00580       mutable std::string path;   ///< Storage for the path name.
00581 
00582 
00583     /// @}
00584   };
00585 
00586   /// This class is identical to Path class except it allows you to obtain the
00587   /// file status of the Path as well. The reason for the distinction is one of
00588   /// efficiency. First, the file status requires additional space and the space
00589   /// is incorporated directly into PathWithStatus without an additional malloc.
00590   /// Second, obtaining status information is an expensive operation on most
00591   /// operating systems so we want to be careful and explicity about where we
00592   /// allow this operation in LLVM.
00593   /// @brief Path with file status class.
00594   class PathWithStatus : public Path {
00595     /// @name Constructors
00596     /// @{
00597     public:
00598       /// @brief Default constructor
00599       PathWithStatus() : Path(), status(), fsIsValid(false) {}
00600 
00601       /// @brief Copy constructor
00602       PathWithStatus(const PathWithStatus &that)
00603         : Path(static_cast<const Path&>(that)), status(that.status),
00604            fsIsValid(that.fsIsValid) {}
00605 
00606       /// This constructor allows construction from a Path object
00607       /// @brief Path constructor
00608       PathWithStatus(const Path &other)
00609         : Path(other), status(), fsIsValid(false) {}
00610 
00611       /// This constructor will accept a std::string as a path. No checking is
00612       /// done on this path to determine if it is valid. To determine validity
00613       /// of the path, use the isValid method.
00614       /// @brief Construct a Path from a string.
00615       explicit PathWithStatus(
00616         const std::string& p ///< The path to assign.
00617       ) : Path(p), status(), fsIsValid(false) {}
00618 
00619       /// This constructor will accept a character range as a path.  No checking
00620       /// is done on this path to determine if it is valid.  To determine
00621       /// validity of the path, use the isValid method.
00622       /// @brief Construct a Path from a string.
00623       explicit PathWithStatus(
00624         const char *StrStart,  ///< Pointer to the first character of the path
00625         unsigned StrLen        ///< Length of the path.
00626       ) : Path(StrStart, StrLen), status(), fsIsValid(false) {}
00627 
00628       /// Makes a copy of \p that to \p this.
00629       /// @returns \p this
00630       /// @brief Assignment Operator
00631       PathWithStatus &operator=(const PathWithStatus &that) {
00632         static_cast<Path&>(*this) = static_cast<const Path&>(that);
00633         status = that.status;
00634         fsIsValid = that.fsIsValid;
00635         return *this;
00636       }
00637 
00638       /// Makes a copy of \p that to \p this.
00639       /// @returns \p this
00640       /// @brief Assignment Operator
00641       PathWithStatus &operator=(const Path &that) {
00642         static_cast<Path&>(*this) = static_cast<const Path&>(that);
00643         fsIsValid = false;
00644         return *this;
00645       }
00646 
00647     /// @}
00648     /// @name Methods
00649     /// @{
00650     public:
00651       /// This function returns status information about the file. The type of
00652       /// path (file or directory) is updated to reflect the actual contents
00653       /// of the file system.
00654       /// @returns 0 on failure, with Error explaining why (if non-zero)
00655       /// @returns a pointer to a FileStatus structure on success.
00656       /// @brief Get file status.
00657       const FileStatus *getFileStatus(
00658         bool forceUpdate = false, ///< Force an update from the file system
00659         std::string *Error = 0    ///< Optional place to return an error msg.
00660       ) const;
00661 
00662     /// @}
00663     /// @name Data
00664     /// @{
00665     private:
00666       mutable FileStatus status; ///< Status information.
00667       mutable bool fsIsValid;    ///< Whether we've obtained it or not
00668 
00669     /// @}
00670   };
00671 
00672   /// This enumeration delineates the kinds of files that LLVM knows about.
00673   enum LLVMFileType {
00674     Unknown_FileType = 0,              ///< Unrecognized file
00675     Bitcode_FileType,                  ///< Bitcode file
00676     Archive_FileType,                  ///< ar style archive file
00677     ELF_Relocatable_FileType,          ///< ELF Relocatable object file
00678     ELF_Executable_FileType,           ///< ELF Executable image
00679     ELF_SharedObject_FileType,         ///< ELF dynamically linked shared lib
00680     ELF_Core_FileType,                 ///< ELF core image
00681     Mach_O_Object_FileType,            ///< Mach-O Object file
00682     Mach_O_Executable_FileType,        ///< Mach-O Executable
00683     Mach_O_FixedVirtualMemorySharedLib_FileType, ///< Mach-O Shared Lib, FVM
00684     Mach_O_Core_FileType,              ///< Mach-O Core File
00685     Mach_O_PreloadExectuable_FileType, ///< Mach-O Preloaded Executable
00686     Mach_O_DynamicallyLinkedSharedLib_FileType, ///< Mach-O dynlinked shared lib
00687     Mach_O_DynamicLinker_FileType,     ///< The Mach-O dynamic linker
00688     Mach_O_Bundle_FileType,            ///< Mach-O Bundle file
00689     Mach_O_DynamicallyLinkedSharedLibStub_FileType, ///< Mach-O Shared lib stub
00690     COFF_FileType                      ///< COFF object file or lib
00691   };
00692 
00693   /// This utility function allows any memory block to be examined in order
00694   /// to determine its file type.
00695   LLVMFileType IdentifyFileType(const char*magic, unsigned length);
00696 
00697   /// This function can be used to copy the file specified by Src to the
00698   /// file specified by Dest. If an error occurs, Dest is removed.
00699   /// @returns true if an error occurs, false otherwise
00700   /// @brief Copy one file to another.
00701   bool CopyFile(const Path& Dest, const Path& Src, std::string* ErrMsg);
00702 
00703   /// This is the OS-specific path separator: a colon on Unix or a semicolon
00704   /// on Windows.
00705   extern const char PathSeparator;
00706 }
00707 
00708 std::ostream& operator<<(std::ostream& strm, const sys::Path& aPath);
00709 inline std::ostream& operator<<(std::ostream& strm,
00710                                 const sys::PathWithStatus& aPath) {
00711   strm << static_cast<const sys::Path&>(aPath);
00712   return strm;
00713 }
00714 
00715 }
00716 
00717 #endif