Make the sys::Path::GetTemporaryDirectory method not throw exceptions and

adjust users of it to compensate.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29831 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer 2006-08-22 19:01:30 +00:00
parent be4f88a8b8
commit 487447626c
4 changed files with 70 additions and 37 deletions

View File

@ -244,7 +244,12 @@ template<typename GraphType>
sys::Path WriteGraph(const GraphType &G,
const std::string& Name,
const std::string& Title = "") {
sys::Path Filename = sys::Path::GetTemporaryDirectory();;
std::string ErrMsg;
sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
if (Filename.isEmpty()) {
std::cerr << "Error: " << ErrMsg << "\n";
return Filename;
}
Filename.appendComponent(Name + ".dot");
Filename.makeUnique();
std::cerr << "Writing '" << Filename << "'... ";

View File

@ -97,10 +97,11 @@ namespace sys {
/// a "standard" place for the operating system. The directory is
/// guaranteed to be created on exit from this function. If the directory
/// cannot be created, the function will throw an exception.
/// @throws std::string indicating why the directory could not be created.
/// @returns an invalid path (empty) on error
/// @param ErrMsg Optional place for an error message if an error occurs
/// @brief Constrct a path to an new, unique, existing temporary
/// directory.
static Path GetTemporaryDirectory();
static Path GetTemporaryDirectory(std::string* ErrMsg);
/// Construct a vector of sys::Path that contains the "standard" system
/// library paths suitable for linking into programs. This function *must*
@ -171,7 +172,7 @@ namespace sys {
/// @throws std::string if \p unverified_path is not legal.
/// @param unverified_path The path to verify and assign.
/// @brief Construct a Path from a string.
explicit Path(const std::string& unverified_path);
explicit Path(const std::string& p) : path(p) {}
/// @}
/// @name Operators

View File

@ -63,16 +63,6 @@ inline bool lastIsSlash(const std::string& path) {
namespace llvm {
using namespace sys;
Path::Path(const std::string& unverified_path) : path(unverified_path) {
if (unverified_path.empty())
return;
if (this->isValid())
return;
// oops, not valid.
path.clear();
ThrowErrno(unverified_path + ": path is not valid");
}
bool
Path::isValid() const {
// Check some obvious things
@ -97,14 +87,17 @@ Path::GetRootDirectory() {
}
Path
Path::GetTemporaryDirectory() {
Path::GetTemporaryDirectory(std::string* ErrMsg ) {
#if defined(HAVE_MKDTEMP)
// The best way is with mkdtemp but that's not available on many systems,
// Linux and FreeBSD have it. Others probably won't.
char pathname[MAXPATHLEN];
strcpy(pathname,"/tmp/llvm_XXXXXX");
if (0 == mkdtemp(pathname))
ThrowErrno(std::string(pathname) + ": can't create temporary directory");
if (0 == mkdtemp(pathname)) {
MakeErrMsg(ErrMsg,
std::string(pathname) + ": can't create temporary directory");
return Path();
}
Path result;
result.set(pathname);
assert(result.isValid() && "mkdtemp didn't create a valid pathname!");
@ -118,12 +111,18 @@ Path::GetTemporaryDirectory() {
char pathname[MAXPATHLEN];
strcpy(pathname, "/tmp/llvm_XXXXXX");
int fd = 0;
if (-1 == (fd = mkstemp(pathname)))
ThrowErrno(std::string(pathname) + ": can't create temporary directory");
if (-1 == (fd = mkstemp(pathname))) {
MakeErrMsg(ErrMsg,
std::string(pathname) + ": can't create temporary directory");
return Path();
}
::close(fd);
::unlink(pathname); // start race condition, ignore errors
if (-1 == ::mkdir(pathname, S_IRWXU)) // end race condition
ThrowErrno(std::string(pathname) + ": can't create temporary directory");
if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
MakeErrMsg(ErrMsg,
std::string(pathname) + ": can't create temporary directory");
return Path();
}
Path result;
result.set(pathname);
assert(result.isValid() && "mkstemp didn't create a valid pathname!");
@ -137,10 +136,16 @@ Path::GetTemporaryDirectory() {
char pathname[MAXPATHLEN];
strcpy(pathname, "/tmp/llvm_XXXXXX");
char *TmpName = ::mktemp(pathname);
if (TmpName == 0)
ThrowErrno(std::string(TmpName) + ": can't create unique directory name");
if (-1 == ::mkdir(TmpName, S_IRWXU))
ThrowErrno(std::string(TmpName) + ": can't create temporary directory");
if (TmpName == 0) {
MakeErrMsg(ErrMsg,
std::string(TmpName) + ": can't create unique directory name");
return Path();
}
if (-1 == ::mkdir(TmpName, S_IRWXU)) {
MakeErrMsg(ErrMsg,
std::string(TmpName) + ": can't create temporary directory");
return Path();
}
Path result;
result.set(TmpName);
assert(result.isValid() && "mktemp didn't create a valid pathname!");
@ -160,8 +165,10 @@ Path::GetTemporaryDirectory() {
num++;
sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
} while ( 0 == access(pathname, F_OK ) );
if (-1 == ::mkdir(pathname, S_IRWXU))
ThrowErrno(std::string(pathname) + ": can't create temporary directory");
if (-1 == ::mkdir(pathname, S_IRWXU)) {
MakeErrMsg(ErrMsg,
std::string(pathname) + ": can't create temporary directory");
return Path();
Path result;
result.set(pathname);
assert(result.isValid() && "mkstemp didn't create a valid pathname!");

View File

@ -83,8 +83,6 @@ public:
, TempDir()
, AdditionalArgs()
{
TempDir = sys::Path::GetTemporaryDirectory();
sys::RemoveDirectoryOnSignal(TempDir);
AdditionalArgs.reserve(NUM_PHASES);
StringVector emptyVec;
for (unsigned i = 0; i < NUM_PHASES; ++i)
@ -196,12 +194,25 @@ private:
}
sys::Path MakeTempFile(const std::string& basename,
const std::string& suffix) {
const std::string& suffix,
std::string* ErrMsg) {
if (TempDir.isEmpty()) {
TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
if (TempDir.isEmpty())
return sys::Path();
sys::RemoveDirectoryOnSignal(TempDir);
}
sys::Path result(TempDir);
if (!result.appendComponent(basename))
throw basename + ": can't use this file name";
if (!result.appendSuffix(suffix))
throw suffix + ": can't use this file suffix";
if (!result.appendComponent(basename)) {
if (ErrMsg)
*ErrMsg = basename + ": can't use this file name";
return sys::Path();
}
if (!result.appendSuffix(suffix)) {
if (ErrMsg)
*ErrMsg = suffix + ": can't use this file suffix";
return sys::Path();
}
return result;
}
@ -700,7 +711,10 @@ public:
actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
}
} else {
sys::Path TempFile(MakeTempFile(I->first.getBasename(),"E"));
sys::Path TempFile(
MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
if (TempFile.isEmpty())
return 1;
actions.push_back(GetAction(cd,InFile,TempFile,
PREPROCESSING));
InFile = TempFile;
@ -731,7 +745,10 @@ public:
actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
}
} else {
sys::Path TempFile(MakeTempFile(I->first.getBasename(),"trans"));
sys::Path TempFile(
MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
if (TempFile.isEmpty())
return 1;
actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
InFile = TempFile;
}
@ -774,7 +791,10 @@ public:
actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
}
} else {
sys::Path TempFile(MakeTempFile(I->first.getBasename(),"opt"));
sys::Path TempFile(
MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
if (TempFile.isEmpty())
return 1;
actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
InFile = TempFile;
}