//
// The LLVM Compiler Infrastructure
//
-// This file was developed by Reid Spencer and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#define LLVM_SYSTEM_PATH_H
#include "llvm/System/TimeValue.h"
+#include "llvm/Support/raw_ostream.h"
#include <set>
#include <string>
#include <vector>
/// platform independent and eliminates many of the unix-specific fields.
/// However, to support llvm-ar, the mode, user, and group fields are
/// retained. These pertain to unix security and may not have a meaningful
- /// value on non-Unix platforms. However, the other fields fields should
- /// always be applicable on all platforms. The structure is filled in by
+ /// value on non-Unix platforms. However, the other fields fields should
+ /// always be applicable on all platforms. The structure is filled in by
/// the PathWithStatus class.
/// @brief File status structure
class FileStatus {
FileStatus() : fileSize(0), modTime(0,0), mode(0777), user(999),
group(999), uniqueID(0), isDir(false), isFile(false) { }
-
+
TimeValue getTimestamp() const { return modTime; }
uint64_t getSize() const { return fileSize; }
uint32_t getMode() const { return mode; }
/// @brief Construct a path to the current user's "home" directory
static Path GetUserHomeDirectory();
+ /// Construct a path to the current directory for the current process.
+ /// @returns The current working directory.
+ /// @brief Returns the current working directory.
+ static Path GetCurrentDirectory();
+
/// Return the suffix commonly used on file names that contain a shared
/// object, shared archive, or dynamic link library. Such files are
/// linked at runtime into a process and their code images are shared
/// @brief Return the dynamic link library suffix.
static std::string GetDLLSuffix();
+ /// GetMainExecutable - Return the path to the main executable, given the
+ /// value of argv[0] from program startup and the address of main itself.
+ static Path GetMainExecutable(const char *argv0, void *MainAddr);
+
/// This is one of the very few ways in which a path can be constructed
/// with a syntactically invalid name. The only *legal* invalid name is an
/// empty one. Other invalid names are not permitted. Empty paths are
/// This constructor will accept a std::string as a path. No checking is
/// done on this path to determine if it is valid. To determine validity
- /// of the path, use the isValid method.
+ /// of the path, use the isValid method.
/// @param p The path to assign.
/// @brief Construct a Path from a string.
- explicit Path(const std::string& p) : path(p) {}
+ explicit Path(const std::string& p);
/// This constructor will accept a character range as a path. No checking
/// is done on this path to determine if it is valid. To determine
- /// validity of the path, use the isValid method.
+ /// validity of the path, use the isValid method.
/// @param StrStart A pointer to the first character of the path name
/// @param StrLen The length of the path name at StrStart
/// @brief Construct a Path from a string.
- explicit Path(const char *StrStart, unsigned StrLen)
- : path(StrStart, StrStart+StrLen) {}
-
+ Path(const char *StrStart, unsigned StrLen);
+
/// @}
/// @name Operators
/// @{
return *this;
}
+ /// Makes a copy of \p that to \p this.
+ /// @param \p that A std::string denoting the path
+ /// @returns \p this
+ /// @brief Assignment Operator
+ Path &operator=(const std::string &that);
+
/// Compares \p this Path with \p that Path for equality.
/// @returns true if \p this and \p that refer to the same thing.
/// @brief Equality Operator
- bool operator==(const Path &that) const {
- return 0 == path.compare(that.path);
- }
+ bool operator==(const Path &that) const;
/// Compares \p this Path with \p that Path for inequality.
/// @returns true if \p this and \p that refer to different things.
/// @brief Inequality Operator
- bool operator!=(const Path &that) const {
- return 0 != path.compare(that.path);
- }
+ bool operator!=(const Path &that) const;
/// Determines if \p this Path is less than \p that Path. This is required
/// so that Path objects can be placed into ordered collections (e.g.
/// the std::string::compare method.
/// @returns true if \p this path is lexicographically less than \p that.
/// @brief Less Than Operator
- bool operator<(const Path& that) const {
- return 0 > path.compare(that.path);
- }
+ bool operator<(const Path& that) const;
/// @}
/// @name Path Accessors
/// @brief Determine if a path is syntactically valid or not.
bool isValid() const;
- /// This function determines if the contents of the path name are empty.
+ /// This function determines if the contents of the path name are empty.
/// That is, the path name has a zero length. This does NOT determine if
- /// if the file is empty. To get the length of the file itself, Use the
- /// PathWithStatus::getFileStatus() method and then the getSize() method
+ /// if the file is empty. To get the length of the file itself, Use the
+ /// PathWithStatus::getFileStatus() method and then the getSize() method
/// on the returned FileStatus object.
/// @returns true iff the path is empty.
/// @brief Determines if the path name is empty (invalid).
/// @brief Get the base name of the path
std::string getBasename() const;
+ /// This function strips off the suffix of the path beginning with the
+ /// path separator ('/' on Unix, '\' on Windows) and returns the result.
+ std::string getDirname() const;
+
+ /// This function strips off the path and basename(up to and
+ /// including the last dot) of the file or directory name and
+ /// returns just the suffix. For example /a/foo.bar would cause
+ /// this function to return "bar".
+ /// @returns std::string containing the suffix of the path
+ /// @brief Get the suffix of the path
+ std::string getSuffix() const;
+
/// Obtain a 'C' string for the path name.
/// @returns a 'C' string containing the path name.
/// @brief Returns the path as a C string.
const char *c_str() const { return path.c_str(); }
+ /// size - Return the length in bytes of this path name.
+ size_t size() const { return path.size(); }
+
+ /// empty - Returns true if the path is empty.
+ unsigned empty() const { return path.empty(); }
+
/// @}
/// @name Disk Accessors
/// @{
bool isRootDirectory() const;
/// This function determines if the path name is absolute, as opposed to
- /// relative.
+ /// relative.
/// @brief Determine if the path is absolute.
bool isAbsolute() const;
+ /// This function determines if the path name is absolute, as opposed to
+ /// relative.
+ /// @brief Determine if the path is absolute.
+ static bool isAbsolute(const char *NameStart, unsigned NameLen);
+
/// This function opens the file associated with the path name provided by
/// the Path object and reads its magic number. If the magic number at the
/// start of the file matches \p magic, true is returned. In all other
/// bitcode files.
/// @brief Determine if the path references a bitcode file.
bool isBitcodeFile() const;
-
+
/// This function determines if the path name in the object references a
/// native Dynamic Library (shared library, shared object) by looking at
/// the file's magic number. The Path object must reference a file, not a
/// the file system.
bool exists() const;
+ /// This function determines if the path name refences an
+ /// existing directory.
+ /// @returns true if the pathname references an existing directory.
+ /// @brief Determins if the path is a directory in the file system.
+ bool isDirectory() const;
+
/// This function determines if the path name references a readable file
/// or directory in the file system. This function checks for
/// the existence and readability (by the current program) of the file
/// @brief Make the current path name unique in the file system.
bool makeUnique( bool reuse_current /*= true*/, std::string* ErrMsg );
+ /// The current Path name is made absolute by prepending the
+ /// current working directory if necessary.
+ void makeAbsolute();
+
/// @}
/// @name Disk Mutators
/// @{
/// created. The created directory will have no entries.
/// @returns true if the directory could not be created, false otherwise
/// @brief Create the directory this Path refers to.
- bool createDirectoryOnDisk(
- bool create_parents = false, ///< Determines whether non-existent
- ///< directory components other than the last one (the "parents")
+ bool createDirectoryOnDisk(
+ bool create_parents = false, ///< Determines whether non-existent
+ ///< directory components other than the last one (the "parents")
///< are created or not.
std::string* ErrMsg = 0 ///< Optional place to put error messages.
);
/// file is created. Note that this will both change the Path object
/// *and* create the corresponding file. This function will ensure that
/// the newly generated temporary file name is unique in the file system.
- /// @returns true if the file couldn't be created, false otherwise.
+ /// @returns true if the file couldn't be created, false otherwise.
/// @brief Create a unique temporary file
bool createTemporaryFileOnDisk(
- bool reuse_current = false, ///< When set to true, this parameter
- ///< indicates that if the current file name does not exist then
+ bool reuse_current = false, ///< When set to true, this parameter
+ ///< indicates that if the current file name does not exist then
///< it will be used without modification.
std::string* ErrMsg = 0 ///< Optional place to put error messages
);
/// @brief Removes the file or directory from the filesystem.
bool eraseFromDisk(bool destroy_contents = false,
std::string *Err = 0) const;
+
+
+ /// MapInFilePages - This is a low level system API to map in the file
+ /// that is currently opened as FD into the current processes' address
+ /// space for read only access. This function may return null on failure
+ /// or if the system cannot provide the following constraints:
+ /// 1) The pages must be valid after the FD is closed, until
+ /// UnMapFilePages is called.
+ /// 2) Any padding after the end of the file must be zero filled, if
+ /// present.
+ /// 3) The pages must be contiguous.
+ ///
+ /// This API is not intended for general use, clients should use
+ /// MemoryBuffer::getFile instead.
+ static const char *MapInFilePages(int FD, uint64_t FileSize);
+
+ /// UnMapFilePages - Free pages mapped into the current process by
+ /// MapInFilePages.
+ ///
+ /// This API is not intended for general use, clients should use
+ /// MemoryBuffer::getFile instead.
+ static void UnMapFilePages(const char *Base, uint64_t FileSize);
+
/// @}
/// @name Data
/// @{
protected:
mutable std::string path; ///< Storage for the path name.
+
/// @}
};
class PathWithStatus : public Path {
/// @name Constructors
/// @{
- public:
+ public:
/// @brief Default constructor
PathWithStatus() : Path(), status(), fsIsValid(false) {}
/// @brief Copy constructor
- PathWithStatus(const PathWithStatus &that)
- : Path(static_cast<const Path&>(that)), status(that.status),
+ PathWithStatus(const PathWithStatus &that)
+ : Path(static_cast<const Path&>(that)), status(that.status),
fsIsValid(that.fsIsValid) {}
/// This constructor allows construction from a Path object
/// @brief Path constructor
- PathWithStatus(const Path &other)
+ PathWithStatus(const Path &other)
: Path(other), status(), fsIsValid(false) {}
/// This constructor will accept a std::string as a path. No checking is
/// done on this path to determine if it is valid. To determine validity
- /// of the path, use the isValid method.
+ /// of the path, use the isValid method.
/// @brief Construct a Path from a string.
explicit PathWithStatus(
const std::string& p ///< The path to assign.
/// This constructor will accept a character range as a path. No checking
/// is done on this path to determine if it is valid. To determine
- /// validity of the path, use the isValid method.
+ /// validity of the path, use the isValid method.
/// @brief Construct a Path from a string.
explicit PathWithStatus(
const char *StrStart, ///< Pointer to the first character of the path
/// @returns true if an error occurs, false otherwise
/// @brief Copy one file to another.
bool CopyFile(const Path& Dest, const Path& Src, std::string* ErrMsg);
+
+ /// This is the OS-specific path separator: a colon on Unix or a semicolon
+ /// on Windows.
+ extern const char PathSeparator;
+}
+
+inline raw_ostream& operator<<(raw_ostream& strm, const sys::Path& aPath) {
+ strm << aPath.toString();
+ return strm;
+}
+
+inline raw_ostream& operator<<(raw_ostream& strm,
+ const sys::PathWithStatus& aPath) {
+ strm << static_cast<const sys::Path&>(aPath);
+ return strm;
}
std::ostream& operator<<(std::ostream& strm, const sys::Path& aPath);
-inline std::ostream& operator<<(std::ostream& strm,
+inline std::ostream& operator<<(std::ostream& strm,
const sys::PathWithStatus& aPath) {
strm << static_cast<const sys::Path&>(aPath);
return strm;