For PR789:

Make the sys::Path::getFileStatus function more efficient by having it
return a pointer to the FileStatus structure rather than copy it. Adjust
uses of the function accordingly. Also, fix some memory issues in sys::Path.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35476 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer
2007-03-29 19:05:44 +00:00
parent f735f7b3ac
commit 8475ec068c
16 changed files with 123 additions and 92 deletions

View File

@@ -74,6 +74,8 @@ PROJ_VERSION := 1.0
endif endif
endif endif
LLVMMAKE := $(LLVM_SRC_ROOT)/make
PROJ_bindir := $(DESTDIR)$(PROJ_prefix)/bin PROJ_bindir := $(DESTDIR)$(PROJ_prefix)/bin
PROJ_libdir := $(DESTDIR)$(PROJ_prefix)/lib PROJ_libdir := $(DESTDIR)$(PROJ_prefix)/lib
PROJ_datadir := $(DESTDIR)$(PROJ_prefix)/share PROJ_datadir := $(DESTDIR)$(PROJ_prefix)/share

View File

@@ -1760,3 +1760,5 @@ printvars::
$(Echo) "Module : " '$(Module)' $(Echo) "Module : " '$(Module)'
$(Echo) "FilesToConfig: " '$(FilesToConfigPATH)' $(Echo) "FilesToConfig: " '$(FilesToConfigPATH)'
$(Echo) "SubDirs : " '$(SubDirs)' $(Echo) "SubDirs : " '$(SubDirs)'
$(Echo) "ProjLibsPaths: " '$(ProjLibsPaths)'
$(Echo) "ProjLibsOptions: " '$(ProjLibsOptions)'

View File

@@ -166,6 +166,7 @@ namespace sys {
/// @brief Construct an empty (and invalid) path. /// @brief Construct an empty (and invalid) path.
Path() : path(), status(0) {} Path() : path(), status(0) {}
~Path() { delete status; } ~Path() { delete status; }
Path(const Path &that) : path(that.path), status(0) {}
/// This constructor will accept a std::string as a path. No checking is /// 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 /// done on this path to determine if it is valid. To determine validity
@@ -183,6 +184,9 @@ namespace sys {
/// @brief Assignment Operator /// @brief Assignment Operator
Path &operator=(const Path &that) { Path &operator=(const Path &that) {
path = that.path; path = that.path;
if (status)
delete status;
status = 0;
return *this; return *this;
} }
@@ -223,9 +227,11 @@ namespace sys {
/// @brief Determine if a path is syntactically valid or not. /// @brief Determine if a path is syntactically valid or not.
bool isValid() const; bool isValid() const;
/// This function determines if the contents of the path name are /// This function determines if the contents of the path name are empty.
/// empty. That is, the path has a zero length. This does NOT determine if /// That is, the path name has a zero length. This does NOT determine if
/// if the file is empty. Use the getSize method for that. /// if the file is empty. To get the length of the file itself, Use the
/// getFileStatus() method and then the getSize() on the returned
/// FileStatus object
/// @returns true iff the path is empty. /// @returns true iff the path is empty.
/// @brief Determines if the path name is empty (invalid). /// @brief Determines if the path name is empty (invalid).
bool isEmpty() const { return path.empty(); } bool isEmpty() const { return path.empty(); }
@@ -357,13 +363,13 @@ namespace sys {
/// This function returns status information about the file. The type of /// This function returns status information about the file. The type of
/// path (file or directory) is updated to reflect the actual contents /// path (file or directory) is updated to reflect the actual contents
/// of the file system. This returns false on success, or true on error /// of the file system.
/// and fills in the specified error string if specified. /// @returns 0 on failure, with Error explaining why (if non-zero)
/// @returns a pointer to a FileStatus structure on success.
/// @brief Get file status. /// @brief Get file status.
bool getFileStatus( const FileStatus *getFileStatus(
FileStatus &Status, ///< The resulting file status bool forceUpdate = false, ///< Force an update from the file system
bool forceUpdate = false, ///< Force an update from the file system std::string *Error = 0 ///< Optional place to return an error msg.
std::string *Error = 0 ///< Optional place to return an error msg.
) const; ) const;
/// @} /// @}

View File

@@ -116,7 +116,10 @@ bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
path.getMagicNumber(magic,4); path.getMagicNumber(magic,4);
signature = magic.c_str(); signature = magic.c_str();
std::string err; std::string err;
if (path.getFileStatus(info, false, ErrMsg)) const sys::FileStatus *FSinfo = path.getFileStatus(false, ErrMsg);
if (FSinfo)
info = *FSinfo;
else
return true; return true;
} }

View File

@@ -163,7 +163,10 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where,
mbr->data = 0; mbr->data = 0;
mbr->path = filePath; mbr->path = filePath;
if (mbr->path.getFileStatus(mbr->info, false, ErrMsg)) const sys::FileStatus *FSInfo = mbr->path.getFileStatus(false, ErrMsg);
if (FSInfo)
mbr->info = *FSInfo;
else
return true; return true;
unsigned flags = 0; unsigned flags = 0;

View File

@@ -116,7 +116,10 @@ bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
path.getMagicNumber(magic,4); path.getMagicNumber(magic,4);
signature = magic.c_str(); signature = magic.c_str();
std::string err; std::string err;
if (path.getFileStatus(info, false, ErrMsg)) const sys::FileStatus *FSinfo = path.getFileStatus(false, ErrMsg);
if (FSinfo)
info = *FSinfo;
else
return true; return true;
} }

View File

@@ -163,7 +163,10 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where,
mbr->data = 0; mbr->data = 0;
mbr->path = filePath; mbr->path = filePath;
if (mbr->path.getFileStatus(mbr->info, false, ErrMsg)) const sys::FileStatus *FSInfo = mbr->path.getFileStatus(false, ErrMsg);
if (FSInfo)
mbr->info = *FSInfo;
else
return true; return true;
unsigned flags = 0; unsigned flags = 0;

View File

@@ -194,9 +194,10 @@ void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo,
ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) { ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
assert(M && "Cannot create program information with a null module!"); assert(M && "Cannot create program information with a null module!");
sys::FileStatus Stat; const sys::FileStatus *Stat;
if (!sys::Path(M->getModuleIdentifier()).getFileStatus(Stat)) Stat = sys::Path(M->getModuleIdentifier()).getFileStatus();
ProgramTimeStamp = Stat.getTimestamp(); if (Stat)
ProgramTimeStamp = Stat->getTimestamp();
SourceFilesIsComplete = false; SourceFilesIsComplete = false;
SourceFunctionsIsComplete = false; SourceFunctionsIsComplete = false;

View File

@@ -152,16 +152,17 @@ int llvm::DiffFilesWithTolerance(const sys::Path &FileA,
const sys::Path &FileB, const sys::Path &FileB,
double AbsTol, double RelTol, double AbsTol, double RelTol,
std::string *Error) { std::string *Error) {
sys::FileStatus FileAStat, FileBStat; const sys::FileStatus *FileAStat = FileA.getFileStatus(false, Error);
if (FileA.getFileStatus(FileAStat, false, Error)) if (!FileAStat)
return 2; return 2;
if (FileB.getFileStatus(FileBStat, false, Error)) const sys::FileStatus *FileBStat = FileB.getFileStatus(false, Error);
if (!FileBStat)
return 2; return 2;
// Check for zero length files because some systems croak when you try to // Check for zero length files because some systems croak when you try to
// mmap an empty file. // mmap an empty file.
size_t A_size = FileAStat.getSize(); size_t A_size = FileAStat->getSize();
size_t B_size = FileBStat.getSize(); size_t B_size = FileBStat->getSize();
// If they are both zero sized then they're the same // If they are both zero sized then they're the same
if (A_size == 0 && B_size == 0) if (A_size == 0 && B_size == 0)

View File

@@ -333,9 +333,10 @@ bool
Path::canExecute() const { Path::canExecute() const {
if (0 != access(path.c_str(), R_OK | X_OK )) if (0 != access(path.c_str(), R_OK | X_OK ))
return false; return false;
struct stat st; if (const FileStatus *fs = getFileStatus(true, 0)) {
int r = stat(path.c_str(), &st); if (!S_ISREG(fs->mode))
if (r != 0 || !S_ISREG(st.st_mode)) return false;
} else
return false; return false;
return true; return true;
} }
@@ -362,12 +363,14 @@ Path::getLast() const {
return path.substr(pos+1); return path.substr(pos+1);
} }
bool const FileStatus*
Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const { Path::getFileStatus(bool update, std::string *ErrStr) const {
if (status == 0 || update) { if (status == 0 || update) {
struct stat buf; struct stat buf;
if (0 != stat(path.c_str(), &buf)) if (0 != stat(path.c_str(), &buf)) {
return MakeErrMsg(ErrStr, path + ": can't get status of file"); MakeErrMsg(ErrStr, path + ": can't get status of file");
return 0;
}
if (status == 0) if (status == 0)
status = new FileStatus; status = new FileStatus;
status->fileSize = buf.st_size; status->fileSize = buf.st_size;
@@ -379,8 +382,7 @@ Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const {
status->isDir = S_ISDIR(buf.st_mode); status->isDir = S_ISDIR(buf.st_mode);
status->isFile = S_ISREG(buf.st_mode); status->isFile = S_ISREG(buf.st_mode);
} }
info = *status; return status;
return false;
} }
static bool AddPermissionBits(const Path &File, int bits) { static bool AddPermissionBits(const Path &File, int bits) {
@@ -392,12 +394,12 @@ static bool AddPermissionBits(const Path &File, int bits) {
umask(mask); // Restore the umask. umask(mask); // Restore the umask.
// Get the file's current mode. // Get the file's current mode.
FileStatus Stat; if (const FileStatus *fs = File.getFileStatus()) {
if (File.getFileStatus(Stat)) return false; // Change the file to have whichever permissions bits from 'bits'
// that the umask would not disable.
// Change the file to have whichever permissions bits from 'bits' if ((chmod(File.c_str(), (fs->getMode() | (bits & ~mask)))) == -1)
// that the umask would not disable. return false;
if ((chmod(File.c_str(), (Stat.getMode() | (bits & ~mask)))) == -1) } else
return false; return false;
return true; return true;
@@ -593,24 +595,25 @@ Path::createTemporaryFileOnDisk(bool reuse_current, std::string* ErrMsg) {
bool bool
Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
FileStatus Status; FileStatus Status;
if (getFileStatus(Status, ErrStr)) if (const FileStatus *Status = getFileStatus(false, ErrStr)) {
// Note: this check catches strange situations. In all cases, LLVM should
// only be involved in the creation and deletion of regular files. This
// check ensures that what we're trying to erase is a regular file. It
// effectively prevents LLVM from erasing things like /dev/null, any block
// special file, or other things that aren't "regular" files.
if (Status->isFile) {
if (unlink(path.c_str()) != 0)
return MakeErrMsg(ErrStr, path + ": can't destroy file");
return false;
}
if (!Status->isDir) {
if (ErrStr) *ErrStr = "not a file or directory";
return true;
}
} else
return true; return true;
// Note: this check catches strange situations. In all cases, LLVM should only
// be involved in the creation and deletion of regular files. This check
// ensures that what we're trying to erase is a regular file. It effectively
// prevents LLVM from erasing things like /dev/null, any block special file,
// or other things that aren't "regular" files.
if (Status.isFile) {
if (unlink(path.c_str()) != 0)
return MakeErrMsg(ErrStr, path + ": can't destroy file");
return false;
}
if (!Status.isDir) {
if (ErrStr) *ErrStr = "not a file or directory";
return true;
}
if (remove_contents) { if (remove_contents) {
// Recursively descend the directory to remove its contents. // Recursively descend the directory to remove its contents.
std::string cmd = "/bin/rm -rf " + path; std::string cmd = "/bin/rm -rf " + path;
@@ -629,7 +632,7 @@ Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
if (rmdir(pathname) != 0) if (rmdir(pathname) != 0)
return MakeErrMsg(ErrStr, return MakeErrMsg(ErrStr,
std::string(pathname) + ": can't destroy directory"); std::string(pathname) + ": can't erase directory");
return false; return false;
} }

View File

@@ -168,8 +168,10 @@ bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
// RemoveDirectoryOnSignal - The public API // RemoveDirectoryOnSignal - The public API
bool sys::RemoveDirectoryOnSignal(const sys::Path& path, std::string* ErrMsg) { bool sys::RemoveDirectoryOnSignal(const sys::Path& path, std::string* ErrMsg) {
// Not a directory? // Not a directory?
sys::FileStatus Status; const sys::FileStatus *Status = path.getFileStatus(false, ErrMsg);
if (path.getFileStatus(Status) || !Status.isDir) { if (!Status)
return true;
if (!Status->isDir) {
if (ErrMsg) if (ErrMsg)
*ErrMsg = path.toString() + " is not a directory"; *ErrMsg = path.toString() + " is not a directory";
return true; return true;

View File

@@ -306,13 +306,15 @@ Path::getLast() const {
return path.substr(pos+1); return path.substr(pos+1);
} }
bool const FileStatus *
Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const { Path::getFileStatus(bool update, std::string *ErrStr) const {
if (status == 0 || update) { if (status == 0 || update) {
WIN32_FILE_ATTRIBUTE_DATA fi; WIN32_FILE_ATTRIBUTE_DATA fi;
if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &fi)) {
return MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) + MakeErrMsg(ErrStr, "getStatusInfo():" + std::string(path) +
": Can't get status: "); ": Can't get status: ");
return 0;
}
if (status == 0) if (status == 0)
status = new FileStatus; status = new FileStatus;
@@ -337,8 +339,7 @@ Path::getFileStatus(FileStatus &info, bool update, std::string *ErrStr) const {
status->isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; status->isDir = fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
} }
info = *status; return status;
return false;
} }
bool Path::makeReadableOnDisk(std::string* ErrMsg) { bool Path::makeReadableOnDisk(std::string* ErrMsg) {
@@ -369,11 +370,10 @@ bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
bool bool
Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const { Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
FileStatus Status; const FileStatus *Status = getFileStatus(false, ErrMsg);
if (getFileStatus(Status, ErrMsg)) if (!Status)
return true; return true;
if (!Status->isDir) {
if (!Status.isDir) {
MakeErrMsg(ErrMsg, path + ": not a directory"); MakeErrMsg(ErrMsg, path + ": not a directory");
return true; return true;
} }
@@ -567,11 +567,11 @@ Path::createFileOnDisk(std::string* ErrMsg) {
bool bool
Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const { Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
FileStatus Status; const FileStatus *Status = getFileStatus(false, ErrStr);
if (getFileStatus(Status, ErrStr)) if (!Status)
return false; return false;
if (Status.isFile) { if (Status->isFile) {
DWORD attr = GetFileAttributes(path.c_str()); DWORD attr = GetFileAttributes(path.c_str());
// If it doesn't exist, we're done. // If it doesn't exist, we're done.
@@ -588,7 +588,7 @@ Path::eraseFromDisk(bool remove_contents, std::string *ErrStr) const {
if (!DeleteFile(path.c_str())) if (!DeleteFile(path.c_str()))
return MakeErrMsg(ErrStr, path + ": Can't destroy file: "); return MakeErrMsg(ErrStr, path + ": Can't destroy file: ");
return false; return false;
} else if (Status.isDir) { } else if (Status->isDir) {
// If it doesn't exist, we're done. // If it doesn't exist, we're done.
if (!exists()) if (!exists())
return false; return false;

View File

@@ -101,8 +101,10 @@ bool sys::RemoveFileOnSignal(const sys::Path &Filename, std::string* ErrMsg) {
// RemoveDirectoryOnSignal - The public API // RemoveDirectoryOnSignal - The public API
bool sys::RemoveDirectoryOnSignal(const sys::Path& path, std::string* ErrMsg) { bool sys::RemoveDirectoryOnSignal(const sys::Path& path, std::string* ErrMsg) {
// Not a directory? // Not a directory?
sys::FileStatus Status; const sys::FileStatus *Status = path.getFileStatus(false, ErrMsg);
if (path.getFileStatus(Status) || !Status.isDir) { if (!Status)
return true;
if (!Status->isDir) {
if (ErrMsg) if (ErrMsg)
*ErrMsg = path.toString() + " is not a directory"; *ErrMsg = path.toString() + " is not a directory";
return true; return true;

View File

@@ -281,16 +281,16 @@ recurseDirectories(const sys::Path& path,
for (std::set<sys::Path>::iterator I = content.begin(), E = content.end(); for (std::set<sys::Path>::iterator I = content.begin(), E = content.end();
I != E; ++I) { I != E; ++I) {
// Make sure it exists and is a directory // Make sure it exists and is a directory
sys::FileStatus Status; const sys::FileStatus *Status = I->getFileStatus(false, ErrMsg);
if (I->getFileStatus(Status)) { if (!Status)
if (Status.isDir) { return true;
std::set<sys::Path> moreResults; if (Status->isDir) {
if (recurseDirectories(*I, moreResults, ErrMsg)) std::set<sys::Path> moreResults;
return true; if (recurseDirectories(*I, moreResults, ErrMsg))
result.insert(moreResults.begin(), moreResults.end()); return true;
} else { result.insert(moreResults.begin(), moreResults.end());
} else {
result.insert(*I); result.insert(*I);
}
} }
} }
} }
@@ -308,11 +308,11 @@ bool buildPaths(bool checkExistence, std::string* ErrMsg) {
if (checkExistence) { if (checkExistence) {
if (!aPath.exists()) if (!aPath.exists())
throw std::string("File does not exist: ") + Members[i]; throw std::string("File does not exist: ") + Members[i];
sys::FileStatus si;
std::string Err; std::string Err;
if (aPath.getFileStatus(si, false, &Err)) const sys::FileStatus *si = aPath.getFileStatus(false, &Err);
if (!si)
throw Err; throw Err;
if (si.isDir) { if (si->isDir) {
std::set<sys::Path> dirpaths; std::set<sys::Path> dirpaths;
if (recurseDirectories(aPath, dirpaths, ErrMsg)) if (recurseDirectories(aPath, dirpaths, ErrMsg))
return true; return true;
@@ -644,14 +644,14 @@ doReplaceOrInsert(std::string* ErrMsg) {
} }
if (found != remaining.end()) { if (found != remaining.end()) {
sys::FileStatus si;
std::string Err; std::string Err;
if (found->getFileStatus(si, false, &Err)) const sys::FileStatus *si = found->getFileStatus(false, &Err);
if (!si)
return true; return true;
if (si.isDir) { if (si->isDir) {
if (OnlyUpdate) { if (OnlyUpdate) {
// Replace the item only if it is newer. // Replace the item only if it is newer.
if (si.modTime > I->getModTime()) if (si->modTime > I->getModTime())
if (I->replaceWith(*found, ErrMsg)) if (I->replaceWith(*found, ErrMsg))
return true; return true;
} else { } else {

View File

@@ -50,11 +50,11 @@ void CLIDebugger::startProgramRunning() {
// If the program has been modified, reload it! // If the program has been modified, reload it!
sys::Path Program(Dbg.getProgramPath()); sys::Path Program(Dbg.getProgramPath());
sys::FileStatus Status;
std::string Err; std::string Err;
if (Program.getFileStatus(Status, &Err)) const sys::FileStatus *Status = Program.getFileStatus(false, &Err);
if (!Status)
throw Err; throw Err;
if (TheProgramInfo->getProgramTimeStamp() != Status.getTimestamp()) { if (TheProgramInfo->getProgramTimeStamp() != Status->getTimestamp()) {
std::cout << "'" << Program << "' has changed; re-reading program.\n"; std::cout << "'" << Program << "' has changed; re-reading program.\n";
// Unload an existing program. This kills the program if necessary. // Unload an existing program. This kills the program if necessary.

View File

@@ -195,8 +195,8 @@ private:
void cleanup() { void cleanup() {
if (!isSet(KEEP_TEMPS_FLAG)) { if (!isSet(KEEP_TEMPS_FLAG)) {
sys::FileStatus Status; const sys::FileStatus *Status = TempDir.getFileStatus();
if (!TempDir.getFileStatus(Status) && Status.isDir) if (Status && Status->isDir)
TempDir.eraseFromDisk(/*remove_contents=*/true); TempDir.eraseFromDisk(/*remove_contents=*/true);
} else { } else {
std::cout << "Temporary files are in " << TempDir << "\n"; std::cout << "Temporary files are in " << TempDir << "\n";