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