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