~FileRemover() {
if (DeleteIt)
try {
- Filename.destroyFile();
+ Filename.destroy();
} catch (...) {} // Ignore problems deleting the file.
}
bool isValid() const;
/// This function determines if the contents of the path name are
- /// empty. That is, the path has a zero length.
+ /// empty. That is, the path has a zero length. This does NOT determine if
+ /// if the file is empty. Use the getSize method for that.
/// @returns true iff the path is empty.
/// @brief Determines if the path name is empty (invalid).
bool isEmpty() const { return path.empty(); }
- /// This function determines if the path name in this object is intended
- /// to reference a legal file name (as opposed to a directory name). This
- /// function does not verify anything with the file system, it merely
- /// determines if the syntax of the path represents a file name or not.
+ /// This function determines if the object referenced by this path is
+ /// a file or not. This function accesses the under lying file system to
+ /// determine the type of entity referenced by the path.
/// @returns true if this path name references a file.
/// @brief Determines if the path name references a file.
bool isFile() const;
- /// This function determines if the path name in this object is intended
- /// to reference a legal directory name (as opposed to a file name). This
- /// function does not verify anything with the file system, it merely
- /// determines if the syntax of the path represents a directory name or
- /// not.
+ /// This function determines if the object referenced by this path is a
+ /// directory or not. This function accesses the underlying file system to
+ /// determine the type of entity referenced by the path.
/// @returns true if the path name references a directory
/// @brief Determines if the path name references a directory.
bool isDirectory() const;
bool isDynamicLibrary() const;
/// This function determines if the path name references an existing file
- /// or directory in the file system. Unlike isFile and isDirectory, this
- /// function actually checks for the existence of the file or directory.
- /// @returns true if the pathname references an existing file.
+ /// or directory in the file system.
+ /// @returns true if the pathname references an existing file or
+ /// directory.
/// @brief Determines if the path is a file or directory in
/// the file system.
bool exists() const;
/// This function determines if the path name references a readable file
- /// or directory in the file system. Unlike isFile and isDirectory, this
- /// function actually checks for the existence and readability (by the
- /// current program) of the file or directory.
+ /// or directory in the file system. This function checks for
+ /// the existence and readability (by the current program) of the file
+ /// or directory.
/// @returns true if the pathname references a readable file.
/// @brief Determines if the path is a readable file or directory
/// in the file system.
bool canRead() const;
/// This function determines if the path name references a writable file
- /// or directory in the file system. Unlike isFile and isDirectory, this
- /// function actually checks for the existence and writability (by the
- /// current program) of the file or directory.
+ /// or directory in the file system. This function checks for the
+ /// existence and writability (by the current program) of the file or
+ /// directory.
/// @returns true if the pathname references a writable file.
/// @brief Determines if the path is a writable file or directory
/// in the file system.
bool canWrite() const;
/// This function determines if the path name references an executable
- /// file in the file system. Unlike isFile and isDirectory, this
- /// function actually checks for the existence and executability (by
- /// the current program) of the file.
+ /// file in the file system. This function checks for the existence and
+ /// executability (by the current program) of the file.
/// @returns true if the pathname references an executable file.
/// @brief Determines if the path is an executable file in the file
/// system.
/// @brief Returns the path as a std::string.
const std::string& toString() const { return path; }
- /// This function returns the last component of the path name. If the
- /// isDirectory() function would return true then this returns the name
- /// of the last directory in the path. If the isFile() function would
- /// return true then this function returns the name of the file without
- /// any of the preceding directories.
+ /// This function returns the last component of the path name. The last
+ /// component is the file or directory name occuring after the last
+ /// directory separator.
/// @returns std::string containing the last component of the path name.
/// @brief Returns the last component of the path name.
std::string getLast() const;
- /// This function strips off the path and suffix of the file name and
- /// returns just the basename.
+ /// This function strips off the path and suffix of the file or directory
+ /// name and returns just the basename. For example /a/foo.bar would cause
+ /// this function to return "foo".
/// @returns std::string containing the basename of the path
/// @throws nothing
/// @brief Get the base name of the path
/// @brief Build a list of directory's contents.
bool getDirectoryContents(std::set<Path>& paths) const;
- /// This method attempts to destroy the directory named by the last in
- /// the Path name. If \p remove_contents is false, an attempt will be
- /// made to remove just the directory that this Path object refers to
- /// (the final Path component). If \p remove_contents is true, an attempt
- /// will be made to remove the entire contents of the directory,
- /// recursively.
+ /// This method attempts to destroy the file or directory named by the
+ /// last component of the Path. If the Path refers to a directory and the
+ /// \p destroy_contents is false, an attempt will be made to remove just
+ /// the directory (the final Path component). If \p destroy_contents is
+ /// true, an attempt will be made to remove the entire contents of the
+ /// directory, recursively. If the Path refers to a file, the
+ /// \p destroy_contents parameter is ignored.
/// @param destroy_contents Indicates whether the contents of a destroyed
/// directory should also be destroyed (recursively).
- /// @returns false if the Path does not refer to a directory, true
- /// otherwise.
+ /// @returns true if the file/directory was destroyed, false if the path
+ /// refers to something that is neither a file nor a directory.
/// @throws std::string if there is an error.
/// @brief Removes the file or directory from the filesystem.
- bool destroyDirectory( bool destroy_contents = false ) const;
-
- /// This method attempts to destroy the file named by the last item in the
- /// Path name.
- /// @returns false if the Path does not refer to a file, true otherwise.
- /// @throws std::string if there is an error.
- /// @brief Destroy the file this Path refers to.
- bool destroyFile() const;
+ bool destroy( bool destroy_contents = false ) const;
/// Obtain a 'C' string for the path name.
/// @returns a 'C' string containing the path name.
/// @brief Make the file readable;
void makeExecutable();
- /// This method attempts to set the Path object to \p unverified_path
- /// and interpret the name as a directory name. The \p unverified_path
- /// is verified. If verification succeeds then \p unverified_path
- /// is accepted as a directory and true is returned. Otherwise,
- /// the Path object remains unchanged and false is returned.
- /// @returns true if the path was set, false otherwise.
- /// @param unverified_path The path to be set in Path object.
- /// @throws nothing
- /// @brief Set a full path from a std::string
- bool setDirectory(const std::string& unverified_path);
-
- /// This method attempts to set the Path object to \p unverified_path
- /// and interpret the name as a file name. The \p unverified_path
- /// is verified. If verification succeeds then \p unverified_path
- /// is accepted as a file name and true is returned. Otherwise,
- /// the Path object remains unchanged and false is returned.
+ /// This method sets the Path object to \p unverified_path. This can fail
+ /// if the \p unverified_path does not pass the syntactic checks of the
+ /// isValid method. If verification fails, the Path object remains
+ /// unchanged and false is returned. Otherwise true is returned and the
+ /// Path object takes on the path value of \p unverified_path
/// @returns true if the path was set, false otherwise.
/// @param unverified_path The path to be set in Path object.
/// @throws nothing
/// @brief Set a full path from a std::string
- bool setFile(const std::string& unverified_path);
-
- /// The \p dirname is added to the end of the Path if it is a legal
- /// directory name for the operating system. The precondition for this
- /// function is that the Path must reference a directory name (i.e.
- /// isDirectory() returns true).
- /// @param dirname A string providing the directory name to
- /// be added to the end of the path.
- /// @returns false if the directory name could not be added
- /// @throws nothing
- /// @brief Adds the name of a directory to a Path.
- bool appendDirectory( const std::string& dirname );
+ bool set(const std::string& unverified_path);
- /// One directory component is removed from the Path name. The Path must
- /// refer to a non-root directory name (i.e. isDirectory() returns true
- /// but isRootDirectory() returns false). Upon exit, the Path will
- /// refer to the directory above it.
+ /// One path component is removed from the Path. If only one component is
+ /// present in the path, the Path object becomes empty. If the Path object
+ /// is empty, no change is made.
/// @throws nothing
- /// @returns false if the directory name could not be removed.
+ /// @returns false if the path component could not be removed.
/// @brief Removes the last directory component of the Path.
- bool elideDirectory();
-
- /// The \p filename is added to the end of the Path if it is a legal
- /// directory name for the operating system. The precondition for this
- /// function is that the Path reference a directory name (i.e.
- /// isDirectory() returns true).
- /// @throws nothing
- /// @returns false if the file name could not be added.
- /// @brief Appends the name of a file.
- bool appendFile( const std::string& filename );
+ bool eraseComponent();
- /// One file component is removed from the Path name. The Path must
- /// refer to a file (i.e. isFile() returns true). Upon exit,
- /// the Path will refer to the directory above it.
+ /// The \p component is added to the end of the Path if it is a legal
+ /// name for the operating system. A directory separator will be added if
+ /// needed.
/// @throws nothing
- /// @returns false if the file name could not be removed
- /// @brief Removes the last file component of the path.
- bool elideFile();
+ /// @returns false if the path component could not be added.
+ /// @brief Appends one path component to the Path.
+ bool appendComponent( const std::string& component );
/// A period and the \p suffix are appended to the end of the pathname.
/// The precondition for this function is that the Path reference a file
/// @brief Adds a period and the \p suffix to the end of the pathname.
bool appendSuffix(const std::string& suffix);
- /// The suffix of the filename is removed. The suffix begins with and
+ /// The suffix of the filename is erased. The suffix begins with and
/// includes the last . character in the filename after the last directory
/// separator and extends until the end of the name. If no . character is
/// after the last directory separator, then the file name is left
/// @returns false if there was no suffix to remove, true otherwise.
/// @throws nothing
/// @brief Remove the suffix from a path name.
- bool elideSuffix();
+ bool eraseSuffix();
/// The current Path name is made unique in the file system. Upon return,
/// the Path will have been changed to make a unique file in the file
/// @returns false if the Path does not refer to a file, true otherwise.
/// @throws std::string if there is an file system error.
/// @brief Rename one file as another.
- bool renameFile(const Path& newName);
+ bool rename(const Path& newName);
/// This method sets the access time, modification time, and permission
/// mode of the file associated with \p this as given by \p si.
member->next = 0;
member->prev = 0;
member->parent = this;
- member->path.setFile(pathname);
+ member->path.set(pathname);
member->info.fileSize = MemberSize;
member->info.modTime.fromEpochTime(atoi(Hdr->date));
unsigned int mode;
// Close up shop
FinalFile.close();
arch.close();
- TmpArchive.destroyFile();
+ TmpArchive.destroy();
} else {
// We don't have to insert the symbol table, so just renaming the temp
// file to the correct name will suffice.
- TmpArchive.renameFile(archPath);
+ TmpArchive.rename(archPath);
}
} catch (...) {
// Make sure we clean up.
if (TmpArchive.exists())
- TmpArchive.destroyFile();
+ TmpArchive.destroy();
throw;
}
}
member->next = 0;
member->prev = 0;
member->parent = this;
- member->path.setFile(pathname);
+ member->path.set(pathname);
member->info.fileSize = MemberSize;
member->info.modTime.fromEpochTime(atoi(Hdr->date));
unsigned int mode;
// Close up shop
FinalFile.close();
arch.close();
- TmpArchive.destroyFile();
+ TmpArchive.destroy();
} else {
// We don't have to insert the symbol table, so just renaming the temp
// file to the correct name will suffice.
- TmpArchive.renameFile(archPath);
+ TmpArchive.rename(archPath);
}
} catch (...) {
// Make sure we clean up.
if (TmpArchive.exists())
- TmpArchive.destroyFile();
+ TmpArchive.destroy();
throw;
}
}
if (SourceText == 0) { // Read the file in if we haven't already.
sys::Path tmpPath;
if (!Directory.empty())
- tmpPath.setDirectory(Directory);
- tmpPath.appendFile(BaseName);
+ tmpPath.set(Directory);
+ tmpPath.appendComponent(BaseName);
if (tmpPath.canRead())
SourceText = new SourceFile(tmpPath.toString(), Descriptor);
else
// If the source library's module id is in the dependent library list of the
// destination library, remove it since that module is now linked in.
sys::Path modId;
- modId.setFile(Src->getModuleIdentifier());
+ modId.set(Src->getModuleIdentifier());
if (!modId.isEmpty())
Dest->removeLibrary(modId.getBasename());
void
Linker::addPath(const sys::Path& path) {
- assert(path.isDirectory() && "Can only insert directories into the path");
LibPaths.push_back(path);
}
Linker::addPaths(const std::vector<std::string>& paths) {
for (unsigned i = 0; i != paths.size(); ++i) {
sys::Path aPath;
- aPath.setDirectory(paths[i]);
+ aPath.set(paths[i]);
LibPaths.push_back(aPath);
}
}
static inline sys::Path IsLibrary(const std::string& Name,
const sys::Path& Directory) {
- assert(Directory.isDirectory() && "Need to specify a directory");
sys::Path FullPath(Directory);
- FullPath.appendFile("lib" + Name);
- FullPath.appendSuffix("a");
- if (FullPath.isArchive())
- return FullPath;
+ // Make sure the directory actually is a directory in the file system.
+ if (FullPath.isDirectory())
+ {
+ // Try the libX.a form
+ FullPath.appendComponent("lib" + Name);
+ FullPath.appendSuffix("a");
+ if (FullPath.isArchive())
+ return FullPath;
+
+ // Try the libX.bca form
+ FullPath.eraseSuffix();
+ FullPath.appendSuffix("bca");
+ if (FullPath.isArchive())
+ return FullPath;
- FullPath.elideSuffix();
- FullPath.appendSuffix("bca");
- if (FullPath.isArchive())
- return FullPath;
+ // Try the libX.so form
+ FullPath.eraseSuffix();
+ FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
+ if (FullPath.isDynamicLibrary()) // Native shared library?
+ return FullPath;
+ if (FullPath.isBytecodeFile()) // .so file containing bytecode?
+ return FullPath;
- FullPath.elideSuffix();
- FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
- if (FullPath.isDynamicLibrary()) // Native shared library?
- return FullPath;
- if (FullPath.isBytecodeFile()) // .so file containing bytecode?
- return FullPath;
+ // Not found .. fall through
+ }
+ // Indicate that the library was not found in the directory.
FullPath.clear();
return FullPath;
}
// if ProgramPath contains at least one / character, indicating that it is a
// relative path to bugpoint itself.
sys::Path Result ( ProgramPath );
- Result.elideFile();
+ Result.eraseComponent();
if (!Result.isEmpty()) {
- Result.appendFile(ExeName);
+ Result.appendComponent(ExeName);
if (Result.canExecute())
return Result;
}
ErrorFile.close();
}
- ErrorFilename.destroyFile();
+ ErrorFilename.destroy();
throw ToolExecutionError(OS.str());
}
void LLC::compileProgram(const std::string &Bytecode) {
sys::Path OutputAsmFile;
OutputAsm(Bytecode, OutputAsmFile);
- OutputAsmFile.destroyFile();
+ OutputAsmFile.destroy();
}
int LLC::ExecuteProgram(const std::string &Bytecode,
void CBE::compileProgram(const std::string &Bytecode) {
sys::Path OutputCFile;
OutputC(Bytecode, OutputCFile);
- OutputCFile.destroyFile();
+ OutputCFile.destroy();
}
int CBE::ExecuteProgram(const std::string &Bytecode,
Path::GetLLVMConfigDir() {
Path result;
#ifdef LLVM_ETCDIR
- if (result.setDirectory(LLVM_ETCDIR))
+ if (result.set(LLVM_ETCDIR))
return result;
#endif
return GetLLVMDefaultConfigDir();
GetSystemLibraryPaths(LibPaths);
for (unsigned i = 0; i < LibPaths.size(); ++i) {
sys::Path FullPath(LibPaths[i]);
- FullPath.appendFile("lib" + name + LTDL_SHLIB_EXT);
+ FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
if (FullPath.isDynamicLibrary())
return FullPath;
- FullPath.elideSuffix();
+ FullPath.eraseSuffix();
FullPath.appendSuffix("a");
if (FullPath.isArchive())
return FullPath;
Path
Path::GetRootDirectory() {
Path result;
- result.setDirectory("/");
+ result.set("/");
return result;
}
if (0 == mkdtemp(pathname))
ThrowErrno(std::string(pathname) + ": can't create temporary directory");
Path result;
- result.setDirectory(pathname);
+ result.set(pathname);
assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
return result;
#elif defined(HAVE_MKSTEMP)
if (-1 == ::mkdir(pathname, S_IRWXU)) // end race condition
ThrowErrno(std::string(pathname) + ": can't create temporary directory");
Path result;
- result.setDirectory(pathname);
+ result.set(pathname);
assert(result.isValid() && "mkstemp didn't create a valid pathname!");
return result;
#elif defined(HAVE_MKTEMP)
if (-1 == ::mkdir(TmpName, S_IRWXU))
ThrowErrno(std::string(TmpName) + ": can't create temporary directory");
Path result;
- result.setDirectory(TmpName);
+ result.set(TmpName);
assert(result.isValid() && "mktemp didn't create a valid pathname!");
return result;
#else
if (-1 == ::mkdir(pathname, S_IRWXU))
ThrowErrno(std::string(pathname) + ": can't create temporary directory");
Path result;
- result.setDirectory(pathname);
+ result.set(pathname);
assert(result.isValid() && "mkstemp didn't create a valid pathname!");
return result;
#endif
Path tmpPath;
while( delim != 0 ) {
std::string tmp(at, size_t(delim-at));
- if (tmpPath.setDirectory(tmp))
+ if (tmpPath.set(tmp))
if (tmpPath.canRead())
Paths.push_back(tmpPath);
at = delim + 1;
delim = strchr(at, ':');
}
if (*at != 0)
- if (tmpPath.setDirectory(std::string(at)))
+ if (tmpPath.set(std::string(at)))
if (tmpPath.canRead())
Paths.push_back(tmpPath);
#ifdef LLVM_LIBDIR
{
Path tmpPath;
- if (tmpPath.setDirectory(LLVM_LIBDIR))
+ if (tmpPath.set(LLVM_LIBDIR))
if (tmpPath.canRead())
Paths.push_back(tmpPath);
}
const char* home = getenv("HOME");
if (home) {
Path result;
- if (result.setDirectory(home))
+ if (result.set(home))
return result;
}
return GetRootDirectory();
bool
Path::isFile() const {
- return (isValid() && path[path.length()-1] != '/');
+ struct stat buf;
+ if (0 != stat(path.c_str(), &buf)) {
+ ThrowErrno(path + ": can't determine type of path object: ");
+ }
+ return S_ISREG(buf.st_mode);
}
bool
Path::isDirectory() const {
- return (isValid() && path[path.length()-1] == '/');
+ struct stat buf;
+ if (0 != stat(path.c_str(), &buf)) {
+ ThrowErrno(path + ": can't determine type of path object: ");
+ }
+ return S_ISDIR(buf.st_mode);
}
std::string
info.user = buf.st_uid;
info.group = buf.st_gid;
info.isDir = S_ISDIR(buf.st_mode);
- if (info.isDir && path[path.length()-1] != '/')
- path += '/';
}
static bool AddPermissionBits(const std::string& Filename, int bits) {
ThrowErrno(aPath.path +
": can't determine file object type", stat_errno);
}
- if (S_ISDIR(buf.st_mode))
- aPath.path += "/";
result.insert(aPath);
}
}
}
bool
-Path::setDirectory(const std::string& a_path) {
- if (a_path.size() == 0)
+Path::set(const std::string& a_path) {
+ if (a_path.empty())
return false;
- Path save(*this);
+ std::string save(path);
path = a_path;
- size_t last = a_path.size() -1;
- if (a_path[last] != '/')
- path += '/';
if (!isValid()) {
- path = save.path;
+ path = save;
return false;
}
return true;
}
bool
-Path::setFile(const std::string& a_path) {
- if (a_path.size() == 0)
- return false;
- Path save(*this);
- path = a_path;
- size_t last = a_path.size() - 1;
- while (last > 0 && a_path[last] == '/')
- last--;
- path.erase(last+1);
- if (!isValid()) {
- path = save.path;
+Path::appendComponent(const std::string& name) {
+ if (name.empty())
return false;
+ std::string save(path);
+ if (!path.empty()) {
+ size_t last = path.size() - 1;
+ if (path[last] != '/')
+ path += '/';
}
- return true;
-}
-
-bool
-Path::appendDirectory(const std::string& dir) {
- if (isFile())
- return false;
- Path save(*this);
- path += dir;
- path += "/";
+ path += name;
if (!isValid()) {
- path = save.path;
+ path = save;
return false;
}
return true;
}
bool
-Path::elideDirectory() {
- if (isFile())
- return false;
+Path::eraseComponent() {
size_t slashpos = path.rfind('/',path.size());
- if (slashpos == 0 || slashpos == std::string::npos)
- return false;
+ if (slashpos == 0 || slashpos == std::string::npos) {
+ path.erase();
+ return true;
+ }
if (slashpos == path.size() - 1)
slashpos = path.rfind('/',slashpos-1);
- if (slashpos == std::string::npos)
- return false;
- path.erase(slashpos);
- return true;
-}
-
-bool
-Path::appendFile(const std::string& file) {
- if (!isDirectory())
- return false;
- Path save(*this);
- path += file;
- if (!isValid()) {
- path = save.path;
- return false;
+ if (slashpos == std::string::npos) {
+ path.erase();
+ return true;
}
- return true;
-}
-
-bool
-Path::elideFile() {
- if (isDirectory())
- return false;
- size_t slashpos = path.rfind('/',path.size());
- if (slashpos == std::string::npos)
- return false;
- path.erase(slashpos+1);
+ path.erase(slashpos);
return true;
}
bool
Path::appendSuffix(const std::string& suffix) {
- if (isDirectory())
- return false;
- Path save(*this);
+ std::string save(path);
path.append(".");
path.append(suffix);
if (!isValid()) {
- path = save.path;
+ path = save;
return false;
}
return true;
}
-bool
-Path::elideSuffix() {
- if (isDirectory()) return false;
+bool
+Path::eraseSuffix() {
+ std::string save(path);
size_t dotpos = path.rfind('.',path.size());
size_t slashpos = path.rfind('/',path.size());
- if (slashpos != std::string::npos && dotpos != std::string::npos &&
+ if (slashpos != std::string::npos &&
+ dotpos != std::string::npos &&
dotpos > slashpos) {
path.erase(dotpos, path.size()-dotpos);
- return true;
}
- return false;
+ if (!isValid()) {
+ path = save;
+ return false;
+ }
+ return true;
}
-
bool
Path::createDirectory( bool create_parents) {
- // Make sure we're dealing with a directory
- if (!isDirectory()) return false;
-
// Get a writeable copy of the path name
char pathname[MAXPATHLEN];
path.copy(pathname,MAXPATHLEN);
bool
Path::createFile() {
- // Make sure we're dealing with a file
- if (!isFile()) return false;
-
// Create the file
int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
if (fd < 0)
bool
Path::createTemporaryFile(bool reuse_current) {
- // Make sure we're dealing with a file
- if (!isFile())
- return false;
-
// Make this into a unique file name
makeUnique( reuse_current );
}
bool
-Path::destroyDirectory(bool remove_contents) const {
+Path::destroy(bool remove_contents) const {
// Make sure we're dealing with a directory
- if (!isDirectory()) return false;
-
- // If it doesn't exist, we're done.
- if (!exists()) return true;
-
- if (remove_contents) {
- // Recursively descend the directory to remove its content
- std::string cmd("/bin/rm -rf ");
- cmd += path;
- system(cmd.c_str());
- } else {
- // Otherwise, try to just remove the one directory
- char pathname[MAXPATHLEN];
- path.copy(pathname,MAXPATHLEN);
- int lastchar = path.length() - 1 ;
- if (pathname[lastchar] == '/')
- pathname[lastchar] = 0;
- else
- pathname[lastchar+1] = 0;
- if ( 0 != rmdir(pathname))
- ThrowErrno(std::string(pathname) + ": can't destroy directory");
+ if (isFile()) {
+ if (0 != unlink(path.c_str()))
+ ThrowErrno(path + ": can't destroy file");
+ } else if (isDirectory()) {
+ if (remove_contents) {
+ // Recursively descend the directory to remove its content
+ std::string cmd("/bin/rm -rf ");
+ cmd += path;
+ system(cmd.c_str());
+ } else {
+ // Otherwise, try to just remove the one directory
+ char pathname[MAXPATHLEN];
+ path.copy(pathname,MAXPATHLEN);
+ int lastchar = path.length() - 1 ;
+ if (pathname[lastchar] == '/')
+ pathname[lastchar] = 0;
+ else
+ pathname[lastchar+1] = 0;
+ if ( 0 != rmdir(pathname))
+ ThrowErrno(std::string(pathname) + ": can't destroy directory");
+ }
}
+ else
+ return false;
return true;
}
bool
-Path::destroyFile() const {
- if (!isFile()) return false;
- if (0 != unlink(path.c_str()))
- ThrowErrno(path + ": can't destroy file");
- return true;
-}
-
-bool
-Path::renameFile(const Path& newName) {
- if (!isFile()) return false;
- if (0 != rename(path.c_str(), newName.c_str()))
+Path::rename(const Path& newName) {
+ if (0 != ::rename(path.c_str(), newName.c_str()))
ThrowErrno(std::string("can't rename '") + path + "' as '" +
newName.toString() + "' ");
return true;
bool
Path::setStatusInfo(const StatusInfo& si) const {
- if (!isFile()) return false;
struct utimbuf utb;
utb.actime = si.modTime.toPosixTime();
utb.modtime = utb.actime;
if (progName.length() == 0) // no program
return Path();
Path temp;
- if (!temp.setFile(progName)) // invalid name
+ if (!temp.set(progName)) // invalid name
return Path();
// FIXME: have to check for absolute filename - we cannot assume anything
// about "." being in $PATH
// Check to see if this first directory contains the executable...
Path FilePath;
- if (FilePath.setDirectory(std::string(PathStr,Colon))) {
- FilePath.appendFile(progName);
+ if (FilePath.set(std::string(PathStr,Colon))) {
+ FilePath.appendComponent(progName);
if (FilePath.canExecute())
return FilePath; // Found the executable!
}
if (DirectoriesToRemove != 0)
while (!DirectoriesToRemove->empty()) {
- DirectoriesToRemove->back().destroyDirectory(true);
+ DirectoriesToRemove->back().destroy(true);
DirectoriesToRemove->pop_back();
}
bool
Path::isFile() const {
- return (isValid() && path[path.length()-1] != '/');
+ WIN32_FILE_ATTRIBUTE_DATA fi;
+ if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
+ ThrowError(std::string(path) + ": Can't get status: ");
+ return fi.dwFileAttributes & FILE_ATTRIBUTE_NORMAL;
}
bool
Path::isDirectory() const {
- return (isValid() && path[path.length()-1] == '/');
+ WIN32_FILE_ATTRIBUTE_DATA fi;
+ if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi))
+ ThrowError(std::string(path) + ": Can't get status: ");
+ return fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
}
std::string
if (BD.runPasses(Prefix, PfxOutput))
return KeepPrefix;
- PrefixOutput.setFile(PfxOutput);
+ PrefixOutput.set(PfxOutput);
OrigProgram = BD.Program;
BD.Program = ParseInputFile(PrefixOutput.toString());
<< PrefixOutput << "'!\n";
exit(1);
}
- PrefixOutput.destroyFile();
+ PrefixOutput.destroy();
}
std::cout << "Checking to see if these passes crash: "
exit(1);
// Remove the intermediate C file
- OutputCFile.destroyFile();
+ OutputCFile.destroy();
return "./" + SharedObjectFile;
}
// If we're checking the program exit code, assume anything nonzero is bad.
if (CheckProgramExitCode && ProgramExitedNonzero) {
- Output.destroyFile();
+ Output.destroy();
if (RemoveBytecode)
- sys::Path(BytecodeFile).destroyFile();
+ sys::Path(BytecodeFile).destroy();
return true;
}
}
// Remove the generated output.
- Output.destroyFile();
+ Output.destroy();
// Remove the bytecode file if we are supposed to.
- if (RemoveBytecode) sys::Path(BytecodeFile).destroyFile();
+ if (RemoveBytecode)
+ sys::Path(BytecodeFile).destroy();
return FilesDifferent;
}
// If the prefix maintains the predicate by itself, only keep the prefix!
if (BD.diffProgram(BytecodeResult)) {
std::cout << " nope.\n";
- sys::Path(BytecodeResult).destroyFile();
+ sys::Path(BytecodeResult).destroy();
return KeepPrefix;
}
std::cout << " yup.\n"; // No miscompilation!
<< BytecodeResult << "'!\n";
exit(1);
}
- sys::Path(BytecodeResult).destroyFile(); // No longer need the file on disk
+ sys::Path(BytecodeResult).destroy(); // No longer need the file on disk
// Don't check if there are no passes in the suffix.
if (Suffix.empty())
std::cerr << ": still failing!\n";
else
std::cerr << ": didn't fail.\n";
- TestModuleBC.destroyFile();
- SafeModuleBC.destroyFile();
- sys::Path(SharedObject).destroyFile();
+ TestModuleBC.destroy();
+ SafeModuleBC.destroy();
+ sys::Path(SharedObject).destroy();
return Result;
}
// If we are supposed to delete the bytecode file or if the passes crashed,
// remove it now. This may fail if the file was never created, but that's ok.
if (DeleteOutput || !ExitedOK)
- sys::Path(OutputFilename).destroyFile();
+ sys::Path(OutputFilename).destroy();
#ifndef PLATFORMINDEPENDENT
if (!Quiet) {
<< BytecodeResult << "'!\n";
exit(1);
}
- sys::Path(BytecodeResult).destroyFile(); // No longer need the file on disk
+ sys::Path(BytecodeResult).destroy(); // No longer need the file on disk
return Ret;
}
ErrorFile.close();
}
- ErrorFilename.destroyFile();
+ ErrorFilename.destroy();
throw ToolExecutionError(OS.str());
}
void LLC::compileProgram(const std::string &Bytecode) {
sys::Path OutputAsmFile;
OutputAsm(Bytecode, OutputAsmFile);
- OutputAsmFile.destroyFile();
+ OutputAsmFile.destroy();
}
int LLC::ExecuteProgram(const std::string &Bytecode,
void CBE::compileProgram(const std::string &Bytecode) {
sys::Path OutputCFile;
OutputC(Bytecode, OutputCFile);
- OutputCFile.destroyFile();
+ OutputCFile.destroy();
}
int CBE::ExecuteProgram(const std::string &Bytecode,
static bool isBytecodeLPath(const std::string &LibPath) {
bool isBytecodeLPath = false;
- // Make sure the -L path has a '/' character
- // because llvm-g++ passes them without the ending
- // '/' char and sys::Path doesn't think it is a
- // directory (see: sys::Path::isDirectory) without it
- std::string dir = LibPath;
- if ( dir[dir.length()-1] != '/' )
- dir.append("/");
-
- sys::Path LPath(dir);
+ sys::Path LPath(LibPath);
+
+ // Make sure its a directory
+ if (!LPath.isDirectory())
+ return isBytecodeLPath;
// Grab the contents of the -L path
std::set<sys::Path> Files;
if (!SaveTemps) {
// Remove the assembly language file.
- AssemblyFile.destroyFile();
+ AssemblyFile.destroy();
// Remove the bytecode language file.
- sys::Path(RealBytecodeOutput).destroyFile();
+ sys::Path(RealBytecodeOutput).destroy();
}
} else if (NativeCBE) {
if (!SaveTemps) {
// Remove the assembly language file.
- CFile.destroyFile();
+ CFile.destroy();
// Remove the bytecode language file.
- sys::Path(RealBytecodeOutput).destroyFile();
+ sys::Path(RealBytecodeOutput).destroy();
}
} else if (!LinkAsLibrary) {
void buildPaths(bool checkExistence = true) {
for (unsigned i = 0; i < Members.size(); i++) {
sys::Path aPath;
- if (!aPath.setFile(Members[i]))
+ if (!aPath.set(Members[i]))
throw std::string("File member name invalid: ") + Members[i];
if (checkExistence) {
if (!aPath.exists())
// Make sure the intervening directories are created
if (I->hasPath()) {
sys::Path dirs(I->getPath());
- dirs.elideFile();
+ dirs.eraseComponent();
dirs.createDirectory(/*create_parents=*/true);
}
// Check the path name of the archive
sys::Path ArchivePath;
- if (!ArchivePath.setFile(ArchiveName))
+ if (!ArchivePath.set(ArchiveName))
throw std::string("Archive name invalid: ") + ArchiveName;
// Create or open the archive object.
gcc, envp);
// Remove the assembly language file.
- AssemblyFile.destroyFile();
+ AssemblyFile.destroy();
} else if (NativeCBE) {
sys::Path CFile (OutputFilename);
CFile.appendSuffix("cbe.c");
GenerateNative(OutputFilename, CFile.toString(), Libraries, gcc, envp);
// Remove the assembly language file.
- CFile.destroyFile();
+ CFile.destroy();
} else {
EmitShellScript(argv);
//
static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
sys::Path Filename;
- if (!Filename.setFile(FN)) {
+ if (!Filename.set(FN)) {
std::cerr << "Invalid file name: '" << FN << "'\n";
return std::auto_ptr<Module>();
}
// Check the path name of the archive
sys::Path ArchivePath;
- if (!ArchivePath.setFile(ArchiveName))
+ if (!ArchivePath.set(ArchiveName))
throw std::string("Archive name invalid: ") + ArchiveName;
// Make sure it exists, we don't create empty archives
StringVector::const_iterator E = paths.end();
while (I != E) {
sys::Path tmp;
- tmp.setDirectory(*I);
+ tmp.set(*I);
IncludePaths.push_back(tmp);
++I;
}
StringVector::const_iterator E = paths.end();
while (I != E) {
sys::Path tmp;
- tmp.setDirectory(*I);
+ tmp.set(*I);
LibraryPaths.push_back(tmp);
++I;
}
void cleanup() {
if (!isSet(KEEP_TEMPS_FLAG)) {
if (TempDir.isDirectory() && TempDir.canWrite())
- TempDir.destroyDirectory(/*remove_contents=*/true);
+ TempDir.destroy(/*remove_contents=*/true);
} else {
std::cout << "Temporary files are in " << TempDir << "\n";
}
sys::Path MakeTempFile(const std::string& basename,
const std::string& suffix) {
sys::Path result(TempDir);
- if (!result.appendFile(basename))
+ if (!result.appendComponent(basename))
throw basename + ": can't use this file name";
if (!result.appendSuffix(suffix))
throw suffix + ": can't use this file suffix";
llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
bool native = false) {
sys::Path fullpath;
- fullpath.setFile(link_item);
+ fullpath.set(link_item);
if (fullpath.canRead())
return fullpath;
for (PathVector::iterator PI = LibraryPaths.begin(),
PE = LibraryPaths.end(); PI != PE; ++PI) {
- fullpath.setDirectory(PI->toString());
- fullpath.appendFile(link_item);
+ fullpath.set(PI->toString());
+ fullpath.appendComponent(link_item);
if (fullpath.canRead())
return fullpath;
if (native) {
fullpath.appendSuffix("bc");
if (fullpath.canRead())
return fullpath;
- fullpath.elideSuffix();
+ fullpath.eraseSuffix();
fullpath.appendSuffix("o");
if (fullpath.canRead())
return fullpath;
fullpath = *PI;
- fullpath.appendFile(std::string("lib") + link_item);
+ fullpath.appendComponent(std::string("lib") + link_item);
fullpath.appendSuffix("a");
if (fullpath.canRead())
return fullpath;
- fullpath.elideSuffix();
+ fullpath.eraseSuffix();
fullpath.appendSuffix("so");
if (fullpath.canRead())
return fullpath;
/// The output of the translator is an LLVM Assembly program
/// We need to translate it to bytecode
Action* action = new Action();
- action->program.setFile("llvm-as");
+ action->program.set("llvm-as");
action->args.push_back(InFile.toString());
action->args.push_back("-o");
InFile.appendSuffix("bc");
/// The output of the optimizer is an LLVM Assembly program
/// We need to translate it to bytecode with llvm-as
Action* action = new Action();
- action->program.setFile("llvm-as");
+ action->program.set("llvm-as");
action->args.push_back(InFile.toString());
action->args.push_back("-f");
action->args.push_back("-o");
Action* action = new Action();
if (isSet(EMIT_NATIVE_FLAG)) {
// Use llc to get the native assembly file
- action->program.setFile("llc");
+ action->program.set("llc");
action->args.push_back(InFile.toString());
action->args.push_back("-f");
action->args.push_back("-o");
actions.push_back(action);
} else {
// Just convert back to llvm assembly with llvm-dis
- action->program.setFile("llvm-dis");
+ action->program.set("llvm-dis");
action->args.push_back(InFile.toString());
action->args.push_back("-f");
action->args.push_back("-o");
// Set up the linking action with llvm-ld
Action* link = new Action();
- link->program.setFile("llvm-ld");
+ link->program.set("llvm-ld");
// Add in the optimization level requested
switch (optLevel) {
{
std::string progname;
if (parseProgramName(progname))
- action.program.setFile(progname);
+ action.program.set(progname);
else
error("Expecting a program name");
// Try the environment variable
const char* conf = getenv("LLVM_CONFIG_DIR");
if (conf) {
- confFile.setDirectory(conf);
- confFile.appendFile(ftype);
+ confFile.set(conf);
+ confFile.appendComponent(ftype);
if (!confFile.canRead())
throw std::string("Configuration file for '") + ftype +
"' is not available.";
// Try the user's home directory
confFile = sys::Path::GetUserHomeDirectory();
if (!confFile.isEmpty()) {
- confFile.appendDirectory(".llvm");
- confFile.appendDirectory("etc");
- confFile.appendFile(ftype);
+ confFile.appendComponent(".llvm");
+ confFile.appendComponent("etc");
+ confFile.appendComponent(ftype);
if (!confFile.canRead())
confFile.clear();
}
if (confFile.isEmpty()) {
// Okay, try the LLVM installation directory
confFile = sys::Path::GetLLVMConfigDir();
- confFile.appendFile(ftype);
+ confFile.appendComponent(ftype);
if (!confFile.canRead()) {
// Okay, try the "standard" place
confFile = sys::Path::GetLLVMDefaultConfigDir();
- confFile.appendFile(ftype);
+ confFile.appendComponent(ftype);
if (!confFile.canRead()) {
throw std::string("Configuration file for '") + ftype +
"' is not available.";
}
} else {
confFile = configDir;
- confFile.appendFile(ftype);
+ confFile.appendComponent(ftype);
if (!confFile.canRead())
throw std::string("Configuration file for '") + ftype +
"' is not available.";