Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

743 lines
35 KiB

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