LLVM API Documentation
00001 //===- llvm/System/Path.h - Path Operating System Concept -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by Reid Spencer and is distributed under the 00006 // University of Illinois Open Source 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 00022 namespace llvm { 00023 namespace sys { 00024 00025 /// This class provides an abstraction for the path to a file or directory 00026 /// in the operating system's filesystem and provides various basic operations 00027 /// on it. Note that this class only represents the name of a path to a file 00028 /// or directory which may or may not be valid for a given machine's file 00029 /// system. A Path ensures that the name it encapsulates is syntactical valid 00030 /// for the operating system it is running on but does not ensure correctness 00031 /// for any particular file system. A Path either references a file or a 00032 /// directory and the distinction is consistently maintained. Most operations 00033 /// on the class have invariants that require the Path object to be either a 00034 /// file path or a directory path, but not both. Those operations will also 00035 /// leave the object as either a file path or object path. There is exactly 00036 /// one invalid Path which is the empty path. The class should never allow any 00037 /// other syntactically invalid non-empty path name to be assigned. Empty 00038 /// paths are required in order to indicate an error result. If the path is 00039 /// empty, the isValid operation will return false. All operations will fail 00040 /// if isValid is false. Operations that change the path will either return 00041 /// false if it would cause a syntactically invalid path name (in which case 00042 /// the Path object is left unchanged) or throw an std::string exception 00043 /// indicating the error. 00044 /// @since 1.4 00045 /// @brief An abstraction for operating system paths. 00046 class Path { 00047 /// @name Types 00048 /// @{ 00049 public: 00050 /// This structure provides basic file system information about a file. It 00051 /// is patterned after the stat(2) Unix operating system call but made 00052 /// platform independent and eliminates many of the unix-specific fields. 00053 /// However, to support llvm-ar, the mode, user, and group fields are 00054 /// retained. These pertain to unix security and may not have a meaningful 00055 /// value on non-Unix platforms. However, the fileSize and modTime fields 00056 /// should always be applicabe on all platforms. The structure is 00057 /// filled in by the getStatusInfo method. 00058 /// @brief File status structure 00059 struct StatusInfo { 00060 StatusInfo() : fileSize(0), modTime(0,0), mode(0777), user(999), 00061 group(999), isDir(false) { } 00062 size_t fileSize; ///< Size of the file in bytes 00063 TimeValue modTime; ///< Time of file's modification 00064 uint32_t mode; ///< Mode of the file, if applicable 00065 uint32_t user; ///< User ID of owner, if applicable 00066 uint32_t group; ///< Group ID of owner, if applicable 00067 bool isDir; ///< True if this is a directory. 00068 }; 00069 00070 /// @} 00071 /// @name Constructors 00072 /// @{ 00073 public: 00074 /// Construct a path to the root directory of the file system. The root 00075 /// directory is a top level directory above which there are no more 00076 /// directories. For example, on UNIX, the root directory is /. On Windows 00077 /// it is C:\. Other operating systems may have different notions of 00078 /// what the root directory is. 00079 /// @throws nothing 00080 static Path GetRootDirectory(); 00081 00082 /// Construct a path to a unique temporary directory that is created in 00083 /// a "standard" place for the operating system. The directory is 00084 /// guaranteed to be created on exit from this function. If the directory 00085 /// cannot be created, the function will throw an exception. 00086 /// @throws std::string indicating why the directory could not be created. 00087 /// @brief Constrct a path to an new, unique, existing temporary 00088 /// directory. 00089 static Path GetTemporaryDirectory(); 00090 00091 /// Determine the platform-specific location of a library by first 00092 /// searching a list of library paths, then searching a list of "well 00093 /// known" paths for the platform. T 00094 /// @returns a valid Path object if the library was found, an invalid 00095 /// one otherwise. 00096 /// @throws nothing 00097 /// @brief Locate a library in a platform specific manner. 00098 static Path GetLibraryPath(const std::string& basename, 00099 const std::vector<std::string>& LibPaths); 00100 /// 00101 /// Construct a path to the first system library directory. The 00102 /// implementation of Path on a given platform must ensure that this 00103 /// directory both exists and also contains standard system libraries 00104 /// suitable for linking into programs. 00105 /// @throws nothing 00106 /// @brief Construct a path to the first system library directory 00107 static Path GetSystemLibraryPath1(); 00108 00109 /// Construct a path to the second system library directory. The 00110 /// implementation of Path on a given platform must ensure that this 00111 /// directory both exists and also contains standard system libraries 00112 /// suitable for linking into programs. Note that the "second" system 00113 /// library directory may or may not be different from the first. 00114 /// @throws nothing 00115 /// @brief Construct a path to the second system library directory 00116 static Path GetSystemLibraryPath2(); 00117 00118 /// Construct a path to the default LLVM configuration directory. The 00119 /// implementation must ensure that this is a well-known (same on many 00120 /// systems) directory in which llvm configuration files exist. For 00121 /// example, on Unix, the /etc/llvm directory has been selected. 00122 /// @throws nothing 00123 /// @brief Construct a path to the default LLVM configuration directory 00124 static Path GetLLVMDefaultConfigDir(); 00125 00126 /// Construct a path to the LLVM installed configuration directory. The 00127 /// implementation must ensure that this refers to the "etc" directory of 00128 /// the LLVM installation. This is the location where configuration files 00129 /// will be located for a particular installation of LLVM on a machine. 00130 /// @throws nothing 00131 /// @brief Construct a path to the LLVM installed configuration directory 00132 static Path GetLLVMConfigDir(); 00133 00134 /// Construct a path to the current user's home directory. The 00135 /// implementation must use an operating system specific mechanism for 00136 /// determining the user's home directory. For example, the environment 00137 /// variable "HOME" could be used on Unix. If a given operating system 00138 /// does not have the concept of a user's home directory, this static 00139 /// constructor must provide the same result as GetRootDirectory. 00140 /// @throws nothing 00141 /// @brief Construct a path to the current user's "home" directory 00142 static Path GetUserHomeDirectory(); 00143 00144 /// Return the suffix commonly used on file names that contain a shared 00145 /// object, shared archive, or dynamic link library. Such files are 00146 /// linked at runtime into a process and their code images are shared 00147 /// between processes. 00148 /// @returns The dynamic link library suffix for the current platform. 00149 /// @brief Return the dynamic link library suffix. 00150 static std::string GetDLLSuffix(); 00151 00152 /// This is one of the very few ways in which a path can be constructed 00153 /// with a syntactically invalid name. The only *legal* invalid name is an 00154 /// empty one. Other invalid names are not permitted. Empty paths are 00155 /// provided so that they can be used to indicate null or error results in 00156 /// other lib/System functionality. 00157 /// @throws nothing 00158 /// @brief Construct an empty (and invalid) path. 00159 Path() : path() {} 00160 00161 /// This constructor will accept a std::string as a path but if verifies 00162 /// that the path string has a legal syntax for the operating system on 00163 /// which it is running. This allows a path to be taken in from outside 00164 /// the program. However, if the path is not valid, the Path object will 00165 /// be set to an empty string and an exception will be thrown. 00166 /// @throws std::string if the path string is not legal. 00167 /// @param unverified_path The path to verify and assign. 00168 /// @brief Construct a Path from a string. 00169 explicit Path(std::string unverified_path); 00170 00171 /// @} 00172 /// @name Operators 00173 /// @{ 00174 public: 00175 /// Makes a copy of \p that to \p this. 00176 /// @returns \p this 00177 /// @throws nothing 00178 /// @brief Assignment Operator 00179 Path & operator = ( const Path & that ) { 00180 path = that.path; 00181 return *this; 00182 } 00183 00184 /// Compares \p this Path with \p that Path for equality. 00185 /// @returns true if \p this and \p that refer to the same thing. 00186 /// @throws nothing 00187 /// @brief Equality Operator 00188 bool operator == (const Path& that) const { 00189 return 0 == path.compare(that.path) ; 00190 } 00191 00192 /// Compares \p this Path with \p that Path for inequality. 00193 /// @returns true if \p this and \p that refer to different things. 00194 /// @throws nothing 00195 /// @brief Inequality Operator 00196 bool operator !=( const Path & that ) const { 00197 return 0 != path.compare( that.path ); 00198 } 00199 00200 /// Determines if \p this Path is less than \p that Path. This is required 00201 /// so that Path objects can be placed into ordered collections (e.g. 00202 /// std::map). The comparison is done lexicographically as defined by 00203 /// the std::string::compare method. 00204 /// @returns true if \p this path is lexicographically less than \p that. 00205 /// @throws nothing 00206 /// @brief Less Than Operator 00207 bool operator< (const Path& that) const { 00208 return 0 > path.compare( that.path ); 00209 } 00210 00211 /// @} 00212 /// @name Accessors 00213 /// @{ 00214 public: 00215 /// This function will use an operating system specific algorithm to 00216 /// determine if the current value of \p this is a syntactically valid 00217 /// path name for the operating system. The path name does not need to 00218 /// exist, validity is simply syntactical. Empty paths are always invalid. 00219 /// @returns true iff the path name is syntactically legal for the 00220 /// host operating system. 00221 /// @brief Determine if a path is syntactically valid or not. 00222 bool isValid() const; 00223 00224 /// This function determines if the contents of the path name are 00225 /// empty. That is, the path has a zero length. 00226 /// @returns true iff the path is empty. 00227 /// @brief Determines if the path name is empty (invalid). 00228 bool isEmpty() const { return path.empty(); } 00229 00230 /// This function determines if the path name in this object is intended 00231 /// to reference a legal file name (as opposed to a directory name). This 00232 /// function does not verify anything with the file system, it merely 00233 /// determines if the syntax of the path represents a file name or not. 00234 /// @returns true if this path name references a file. 00235 /// @brief Determines if the path name references a file. 00236 bool isFile() const; 00237 00238 /// This function determines if the path name in this object is intended 00239 /// to reference a legal directory name (as opposed to a file name). This 00240 /// function does not verify anything with the file system, it merely 00241 /// determines if the syntax of the path represents a directory name or 00242 /// not. 00243 /// @returns true if the path name references a directory 00244 /// @brief Determines if the path name references a directory. 00245 bool isDirectory() const; 00246 00247 /// This function determines if the path name in this object references 00248 /// the root (top level directory) of the file system. The details of what 00249 /// is considered the "root" may vary from system to system so this method 00250 /// will do the necessary checking. 00251 /// @returns true iff the path name references the root directory. 00252 /// @brief Determines if the path references the root directory. 00253 bool isRootDirectory() const; 00254 00255 /// This function opens the file associated with the path name provided by 00256 /// the Path object and reads its magic number. If the magic number at the 00257 /// start of the file matches \p magic, true is returned. In all other 00258 /// cases (file not found, file not accessible, etc.) it returns false. 00259 /// @returns true if the magic number of the file matches \p magic. 00260 /// @brief Determine if file has a specific magic number 00261 bool hasMagicNumber(const std::string& magic) const; 00262 00263 /// This function retrieves the first \p len bytes of the file associated 00264 /// with \p this. These bytes are returned as the "magic number" in the 00265 /// \p Magic parameter. 00266 /// @returns true if the Path is a file and the magic number is retrieved, 00267 /// false otherwise. 00268 /// @brief Get the file's magic number. 00269 bool getMagicNumber(std::string& Magic, unsigned len) const; 00270 00271 /// This function determines if the path name in the object references an 00272 /// archive file by looking at its magic number. 00273 /// @returns true if the file starts with the magic number for an archive 00274 /// file. 00275 /// @brief Determine if the path references an archive file. 00276 bool isArchive() const; 00277 00278 /// This function determines if the path name in the object references an 00279 /// LLVM Bytecode file by looking at its magic number. 00280 /// @returns true if the file starts with the magic number for LLVM 00281 /// bytecode files. 00282 /// @brief Determine if the path references a bytecode file. 00283 bool isBytecodeFile() const; 00284 00285 /// This function determines if the path name references an existing file 00286 /// or directory in the file system. Unlike isFile and isDirectory, this 00287 /// function actually checks for the existence of the file or directory. 00288 /// @returns true if the pathname references an existing file. 00289 /// @brief Determines if the path is a file or directory in 00290 /// the file system. 00291 bool exists() const; 00292 00293 /// This function determines if the path name references a readable file 00294 /// or directory in the file system. Unlike isFile and isDirectory, this 00295 /// function actually checks for the existence and readability (by the 00296 /// current program) of the file or directory. 00297 /// @returns true if the pathname references a readable file. 00298 /// @brief Determines if the path is a readable file or directory 00299 /// in the file system. 00300 bool readable() const; 00301 00302 /// This function determines if the path name references a writable file 00303 /// or directory in the file system. Unlike isFile and isDirectory, this 00304 /// function actually checks for the existence and writability (by the 00305 /// current program) of the file or directory. 00306 /// @returns true if the pathname references a writable file. 00307 /// @brief Determines if the path is a writable file or directory 00308 /// in the file system. 00309 bool writable() const; 00310 00311 /// This function determines if the path name references an executable 00312 /// file in the file system. Unlike isFile and isDirectory, this 00313 /// function actually checks for the existence and executability (by 00314 /// the current program) of the file. 00315 /// @returns true if the pathname references an executable file. 00316 /// @brief Determines if the path is an executable file in the file 00317 /// system. 00318 bool executable() const; 00319 00320 /// This function returns the current contents of the path as a 00321 /// std::string. This allows the underlying path string to be manipulated 00322 /// by other software. 00323 /// @returns std::string containing the path name. 00324 /// @brief Returns the path as a std::string. 00325 std::string get() const { return path; } 00326 00327 /// This function returns the last component of the path name. If the 00328 /// isDirectory() function would return true then this returns the name 00329 /// of the last directory in the path. If the isFile() function would 00330 /// return true then this function returns the name of the file without 00331 /// any of the preceding directories. 00332 /// @returns std::string containing the last component of the path name. 00333 /// @brief Returns the last component of the path name. 00334 std::string getLast() const; 00335 00336 /// This function strips off the path and suffix of the file name and 00337 /// returns just the basename. 00338 /// @returns std::string containing the basename of the path 00339 /// @throws nothing 00340 /// @brief Get the base name of the path 00341 std::string getBasename() const; 00342 00343 /// This function builds a list of paths that are the names of the 00344 /// files and directories in a directory. 00345 /// @returns false if \p this is not a directory, true otherwise 00346 /// @throws std::string if the directory cannot be searched 00347 /// @brief Build a list of directory's contents. 00348 bool getDirectoryContents(std::set<Path>& paths) const; 00349 00350 /// Obtain a 'C' string for the path name. 00351 /// @returns a 'C' string containing the path name. 00352 /// @brief Returns the path as a C string. 00353 const char* const c_str() const { return path.c_str(); } 00354 00355 /// @} 00356 /// @name Mutators 00357 /// @{ 00358 public: 00359 /// The path name is cleared and becomes empty. This is an invalid 00360 /// path name but is the *only* invalid path name. This is provided 00361 /// so that path objects can be used to indicate the lack of a 00362 /// valid path being found. 00363 void clear() { path.clear(); } 00364 00365 /// This function returns status information about the file. The type of 00366 /// path (file or directory) is updated to reflect the actual contents 00367 /// of the file system. If the file does not exist, false is returned. 00368 /// For other (hard I/O) errors, a std::string is throwing indicating the 00369 /// problem. 00370 /// @throws std::string if an error occurs. 00371 /// @brief Get file status. 00372 void getStatusInfo(StatusInfo& info) const; 00373 00374 /// This method attempts to set the Path object to \p unverified_path 00375 /// and interpret the name as a directory name. The \p unverified_path 00376 /// is verified. If verification succeeds then \p unverified_path 00377 /// is accepted as a directory and true is returned. Otherwise, 00378 /// the Path object remains unchanged and false is returned. 00379 /// @returns true if the path was set, false otherwise. 00380 /// @param unverified_path The path to be set in Path object. 00381 /// @throws nothing 00382 /// @brief Set a full path from a std::string 00383 bool setDirectory(const std::string& unverified_path); 00384 00385 /// This method attempts to set the Path object to \p unverified_path 00386 /// and interpret the name as a file name. The \p unverified_path 00387 /// is verified. If verification succeeds then \p unverified_path 00388 /// is accepted as a file name and true is returned. Otherwise, 00389 /// the Path object remains unchanged and false is returned. 00390 /// @returns true if the path was set, false otherwise. 00391 /// @param unverified_path The path to be set in Path object. 00392 /// @throws nothing 00393 /// @brief Set a full path from a std::string 00394 bool setFile(const std::string& unverified_path); 00395 00396 /// The \p dirname is added to the end of the Path if it is a legal 00397 /// directory name for the operating system. The precondition for this 00398 /// function is that the Path must reference a directory name (i.e. 00399 /// isDirectory() returns true). 00400 /// @param dirname A string providing the directory name to 00401 /// be added to the end of the path. 00402 /// @returns false if the directory name could not be added 00403 /// @throws nothing 00404 /// @brief Adds the name of a directory to a Path. 00405 bool appendDirectory( const std::string& dirname ); 00406 00407 /// One directory component is removed from the Path name. The Path must 00408 /// refer to a non-root directory name (i.e. isDirectory() returns true 00409 /// but isRootDirectory() returns false). Upon exit, the Path will 00410 /// refer to the directory above it. 00411 /// @throws nothing 00412 /// @returns false if the directory name could not be removed. 00413 /// @brief Removes the last directory component of the Path. 00414 bool elideDirectory(); 00415 00416 /// The \p filename is added to the end of the Path if it is a legal 00417 /// directory name for the operating system. The precondition for this 00418 /// function is that the Path reference a directory name (i.e. 00419 /// isDirectory() returns true). 00420 /// @throws nothing 00421 /// @returns false if the file name could not be added. 00422 /// @brief Appends the name of a file. 00423 bool appendFile( const std::string& filename ); 00424 00425 /// One file component is removed from the Path name. The Path must 00426 /// refer to a file (i.e. isFile() returns true). Upon exit, 00427 /// the Path will refer to the directory above it. 00428 /// @throws nothing 00429 /// @returns false if the file name could not be removed 00430 /// @brief Removes the last file component of the path. 00431 bool elideFile(); 00432 00433 /// A period and the \p suffix are appended to the end of the pathname. 00434 /// The precondition for this function is that the Path reference a file 00435 /// name (i.e. isFile() returns true). If the Path is not a file, no 00436 /// action is taken and the function returns false. If the path would 00437 /// become invalid for the host operating system, false is returned. 00438 /// @returns false if the suffix could not be added, true if it was. 00439 /// @throws nothing 00440 /// @brief Adds a period and the \p suffix to the end of the pathname. 00441 bool appendSuffix(const std::string& suffix); 00442 00443 /// The suffix of the filename is removed. The suffix begins with and 00444 /// includes the last . character in the filename after the last directory 00445 /// separator and extends until the end of the name. If no . character is 00446 /// after the last directory separator, then the file name is left 00447 /// unchanged (i.e. it was already without a suffix) but the function 00448 /// returns false. 00449 /// @returns false if there was no suffix to remove, true otherwise. 00450 /// @throws nothing 00451 /// @brief Remove the suffix from a path name. 00452 bool elideSuffix(); 00453 00454 /// This method attempts to create a directory in the file system with the 00455 /// same name as the Path object. The \p create_parents parameter controls 00456 /// whether intermediate directories are created or not. if \p 00457 /// create_parents is true, then an attempt will be made to create all 00458 /// intermediate directories. If \p create_parents is false, then only the 00459 /// final directory component of the Path name will be created. The 00460 /// created directory will have no entries. 00461 /// @returns false if the Path does not reference a directory, true 00462 /// otherwise. 00463 /// @param create_parents Determines whether non-existent directory 00464 /// components other than the last one (the "parents") are created or not. 00465 /// @throws std::string if an error occurs. 00466 /// @brief Create the directory this Path refers to. 00467 bool createDirectory( bool create_parents = false ); 00468 00469 /// This method attempts to create a file in the file system with the same 00470 /// name as the Path object. The intermediate directories must all exist 00471 /// at the time this method is called. Use createDirectories to 00472 /// accomplish that. The created file will be empty upon return from this 00473 /// function. 00474 /// @returns false if the Path does not reference a file, true otherwise. 00475 /// @throws std::string if an error occurs. 00476 /// @brief Create the file this Path refers to. 00477 bool createFile(); 00478 00479 /// This is like createFile except that it creates a temporary file. A 00480 /// unique temporary file name is generated based on the contents of 00481 /// \p this before the call. The new name is assigned to \p this and the 00482 /// file is created. Note that this will both change the Path object 00483 /// *and* create the corresponding file. This function will ensure that 00484 /// the newly generated temporary file name is unique in the file system. 00485 /// @throws std::string if there is an error 00486 /// @brief Create a unique temporary file 00487 bool createTemporaryFile(); 00488 00489 /// This method attempts to destroy the directory named by the last in 00490 /// the Path name. If \p remove_contents is false, an attempt will be 00491 /// made to remove just the directory that this Path object refers to 00492 /// (the final Path component). If \p remove_contents is true, an attempt 00493 /// will be made to remove the entire contents of the directory, 00494 /// recursively. 00495 /// @param destroy_contents Indicates whether the contents of a destroyed 00496 /// directory should also be destroyed (recursively). 00497 /// @returns false if the Path does not refer to a directory, true 00498 /// otherwise. 00499 /// @throws std::string if there is an error. 00500 /// @brief Removes the file or directory from the filesystem. 00501 bool destroyDirectory( bool destroy_contents = false ); 00502 00503 /// This method attempts to destroy the file named by the last item in the 00504 /// Path name. 00505 /// @returns false if the Path does not refer to a file, true otherwise. 00506 /// @throws std::string if there is an error. 00507 /// @brief Destroy the file this Path refers to. 00508 bool destroyFile(); 00509 00510 /// This method renames the file referenced by \p this as \p newName. Both 00511 /// files must exist before making this call. 00512 /// @returns false if the Path does not refer to a file, true otherwise. 00513 /// @throws std::string if there is an file system error. 00514 /// @brief Rename one file as another. 00515 bool renameFile(const Path& newName); 00516 00517 /// This method sets the access time, modification time, and permission 00518 /// mode of the file associated with \p this as given by \p si. 00519 /// @returns false if the Path does not refer to a file, true otherwise. 00520 /// @throws std::string if the file could not be modified 00521 /// @brief Set file times and mode. 00522 bool setStatusInfo(const StatusInfo& si ) const ; 00523 00524 /// @} 00525 /// @name Data 00526 /// @{ 00527 private: 00528 mutable std::string path; ///< Storage for the path name. 00529 00530 /// @} 00531 }; 00532 00533 /// This enumeration delineates the kinds of files that LLVM knows about. 00534 enum LLVMFileType { 00535 UnknownFileType = 0, ///< Unrecognized file 00536 BytecodeFileType = 1, ///< Uncompressed bytecode file 00537 CompressedBytecodeFileType = 2, ///< Compressed bytecode file 00538 ArchiveFileType = 3, ///< ar style archive file 00539 }; 00540 00541 /// This utility function allows any memory block to be examined in order 00542 /// to determine its file type. 00543 LLVMFileType IdentifyFileType(const char*magic, unsigned length); 00544 } 00545 00546 } 00547 00548 // vim: sw=2 00549 00550 #endif