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