Change Path::getStatusInfo to return a boolean and error string on an error
instead of throwing an exception. This reduces the amount of code that is
exposed to exceptions (e.g. FileUtilities), though it is clearly only one step
along the way.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29395 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Archive/Archive.cpp b/lib/Archive/Archive.cpp
index 66b9d70..3bb9a05 100644
--- a/lib/Archive/Archive.cpp
+++ b/lib/Archive/Archive.cpp
@@ -104,12 +104,14 @@
flags &= ~HasLongFilenameFlag;
// Get the signature and status info
- std::string magic;
const char* signature = (const char*) data;
+ std::string magic;
if (!signature) {
path.getMagicNumber(magic,4);
signature = magic.c_str();
- path.getStatusInfo(info);
+ std::string err;
+ if (path.getFileStatus(info, &err))
+ throw err;
}
// Determine what kind of file it is
diff --git a/lib/Archive/ArchiveWriter.cpp b/lib/Archive/ArchiveWriter.cpp
index 390fd12..52ba99e 100644
--- a/lib/Archive/ArchiveWriter.cpp
+++ b/lib/Archive/ArchiveWriter.cpp
@@ -159,7 +159,9 @@
mbr->data = 0;
mbr->path = filePath;
- mbr->path.getStatusInfo(mbr->info);
+ std::string err;
+ if (mbr->path.getFileStatus(mbr->info, &err))
+ throw err;
unsigned flags = 0;
bool hasSlash = filePath.toString().find('/') != std::string::npos;
diff --git a/lib/Bytecode/Archive/Archive.cpp b/lib/Bytecode/Archive/Archive.cpp
index 66b9d70..3bb9a05 100644
--- a/lib/Bytecode/Archive/Archive.cpp
+++ b/lib/Bytecode/Archive/Archive.cpp
@@ -104,12 +104,14 @@
flags &= ~HasLongFilenameFlag;
// Get the signature and status info
- std::string magic;
const char* signature = (const char*) data;
+ std::string magic;
if (!signature) {
path.getMagicNumber(magic,4);
signature = magic.c_str();
- path.getStatusInfo(info);
+ std::string err;
+ if (path.getFileStatus(info, &err))
+ throw err;
}
// Determine what kind of file it is
diff --git a/lib/Bytecode/Archive/ArchiveWriter.cpp b/lib/Bytecode/Archive/ArchiveWriter.cpp
index 390fd12..52ba99e 100644
--- a/lib/Bytecode/Archive/ArchiveWriter.cpp
+++ b/lib/Bytecode/Archive/ArchiveWriter.cpp
@@ -159,7 +159,9 @@
mbr->data = 0;
mbr->path = filePath;
- mbr->path.getStatusInfo(mbr->info);
+ std::string err;
+ if (mbr->path.getFileStatus(mbr->info, &err))
+ throw err;
unsigned flags = 0;
bool hasSlash = filePath.toString().find('/') != std::string::npos;
diff --git a/lib/Debugger/ProgramInfo.cpp b/lib/Debugger/ProgramInfo.cpp
index 66d38f7..b60f5fc 100644
--- a/lib/Debugger/ProgramInfo.cpp
+++ b/lib/Debugger/ProgramInfo.cpp
@@ -196,8 +196,9 @@
ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
assert(M && "Cannot create program information with a null module!");
- sys::Path modulePath(M->getModuleIdentifier());
- ProgramTimeStamp = modulePath.getTimestamp();
+ sys::FileStatus Stat;
+ if (!sys::Path(M->getModuleIdentifier()).getFileStatus(Stat))
+ ProgramTimeStamp = Stat.getTimestamp();
SourceFilesIsComplete = false;
SourceFunctionsIsComplete = false;
diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp
index a7f42dd..d4608cc 100644
--- a/lib/Support/FileUtilities.cpp
+++ b/lib/Support/FileUtilities.cpp
@@ -146,19 +146,23 @@
const sys::Path &FileB,
double AbsTol, double RelTol,
std::string *Error) {
+ sys::FileStatus FileAStat, FileBStat;
+ if (FileA.getFileStatus(FileAStat, Error) ||
+ FileB.getFileStatus(FileBStat, Error))
+ return 2;
+ // Check for zero length files because some systems croak when you try to
+ // mmap an empty file.
+ size_t A_size = FileAStat.getSize();
+ size_t B_size = FileBStat.getSize();
+
+ // If they are both zero sized then they're the same
+ if (A_size == 0 && B_size == 0)
+ return 0;
+ // If only one of them is zero sized then they can't be the same
+ if ((A_size == 0 || B_size == 0))
+ return 1;
+
try {
- // Check for zero length files because some systems croak when you try to
- // mmap an empty file.
- size_t A_size = FileA.getSize();
- size_t B_size = FileB.getSize();
-
- // If they are both zero sized then they're the same
- if (A_size == 0 && B_size == 0)
- return 0;
- // If only one of them is zero sized then they can't be the same
- if ((A_size == 0 || B_size == 0))
- return 1;
-
// Now its safe to mmap the files into memory becasue both files
// have a non-zero size.
sys::MappedFile F1(FileA);
diff --git a/lib/System/Unix/Path.inc b/lib/System/Unix/Path.inc
index 4ca4753..a0d76b0 100644
--- a/lib/System/Unix/Path.inc
+++ b/lib/System/Unix/Path.inc
@@ -386,21 +386,22 @@
return path.substr(pos+1);
}
-void
-Path::getStatusInfo(StatusInfo& info) const {
+bool
+Path::getFileStatus(FileStatus &info, std::string *ErrStr) const {
struct stat buf;
- if (0 != stat(path.c_str(), &buf)) {
- ThrowErrno(path + ": can't determine type of path object: ");
- }
+ if (0 != stat(path.c_str(), &buf))
+ return GetErrno(path + ": can't determine type of path object: ", ErrStr);
info.fileSize = buf.st_size;
info.modTime.fromEpochTime(buf.st_mtime);
info.mode = buf.st_mode;
info.user = buf.st_uid;
info.group = buf.st_gid;
- info.isDir = S_ISDIR(buf.st_mode);
+ info.isDir = S_ISDIR(buf.st_mode);
+ info.isFile = S_ISREG(buf.st_mode);
+ return false;
}
-static bool AddPermissionBits(const std::string& Filename, int bits) {
+static bool AddPermissionBits(const Path &File, int bits) {
// Get the umask value from the operating system. We want to use it
// when changing the file's permissions. Since calling umask() sets
// the umask and returns its old value, we must call it a second
@@ -409,30 +410,29 @@
umask(mask); // Restore the umask.
// Get the file's current mode.
- struct stat st;
- if ((stat(Filename.c_str(), &st)) == -1)
- return false;
+ FileStatus Stat;
+ if (File.getFileStatus(Stat)) return false;
// Change the file to have whichever permissions bits from 'bits'
// that the umask would not disable.
- if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
+ if ((chmod(File.c_str(), (Stat.getMode() | (bits & ~mask)))) == -1)
return false;
return true;
}
void Path::makeReadableOnDisk() {
- if (!AddPermissionBits(path,0444))
+ if (!AddPermissionBits(*this, 0444))
ThrowErrno(path + ": can't make file readable");
}
void Path::makeWriteableOnDisk() {
- if (!AddPermissionBits(path,0222))
+ if (!AddPermissionBits(*this, 0222))
ThrowErrno(path + ": can't make file writable");
}
void Path::makeExecutableOnDisk() {
- if (!AddPermissionBits(path,0111))
+ if (!AddPermissionBits(*this, 0111))
ThrowErrno(path + ": can't make file executable");
}
@@ -642,7 +642,7 @@
}
bool
-Path::setStatusInfoOnDisk(const StatusInfo& si) const {
+Path::setStatusInfoOnDisk(const FileStatus &si) const {
struct utimbuf utb;
utb.actime = si.modTime.toPosixTime();
utb.modtime = utb.actime;