For PR351:

* Fix implementation and documentation about LLVMGCCDIR/bytecode-libs
* Add the makeUnique method, replacement for getUniqueFilename in Support.
* Add the sys::CopyFile function, replacement for CopyFile in Support.
* Move GetLLVMConfigDir() into generic code area since its generic.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@18947 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer
2004-12-15 01:50:13 +00:00
parent 67f6d3ad30
commit c29befb554
6 changed files with 270 additions and 45 deletions

View File

@ -100,7 +100,8 @@ namespace sys {
/// Construct a vector of sys::Path that contains the "standard" bytecode /// Construct a vector of sys::Path that contains the "standard" bytecode
/// library paths suitable for linking into an llvm program. This function /// library paths suitable for linking into an llvm program. This function
/// *must* return the value of LLVM_LIB_SEARCH_PATH as well as the values /// *must* return the value of LLVM_LIB_SEARCH_PATH as well as the values
/// of LLVM_LIBDIR and LLVMGCCDIR/bytecode-libs. It also must provide the /// of LLVM_LIBDIR and LLVMGCCDIR/lib (if --with-llvmgccdir was specified
/// when LLVM was configured). It also must provide the
/// System library paths as returned by GetSystemLibraryPaths. /// System library paths as returned by GetSystemLibraryPaths.
/// @brief Construct a list of directories in which bytecode could be /// @brief Construct a list of directories in which bytecode could be
/// found. /// found.
@ -487,6 +488,14 @@ namespace sys {
/// @brief Remove the suffix from a path name. /// @brief Remove the suffix from a path name.
bool elideSuffix(); bool elideSuffix();
/// 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
/// system or it will not have been changed if the current path name is
/// already unique.
/// @throws std::string if an unrecoverable error occurs.
/// @brief Make the current path name unique in the file system.
void makeUnique();
/// This method attempts to create a directory in the file system with the /// This method attempts to create a directory in the file system with the
/// same name as the Path object. The \p create_parents parameter controls /// same name as the Path object. The \p create_parents parameter controls
/// whether intermediate directories are created or not. if \p /// whether intermediate directories are created or not. if \p
@ -577,6 +586,12 @@ namespace sys {
/// This utility function allows any memory block to be examined in order /// This utility function allows any memory block to be examined in order
/// to determine its file type. /// to determine its file type.
LLVMFileType IdentifyFileType(const char*magic, unsigned length); LLVMFileType IdentifyFileType(const char*magic, unsigned length);
/// This function can be used to copy the file specified by Src to the
/// file specified by Dest. If an error occurs, Dest is removed.
/// @throws std::string if an error opening or writing the files occurs.
/// @brief Copy one file to another.
void CopyFiles(const Path& Dest, const Path& Src);
} }
inline std::ostream& operator<<(std::ostream& strm, const sys::Path& aPath) { inline std::ostream& operator<<(std::ostream& strm, const sys::Path& aPath) {

View File

@ -23,6 +23,14 @@ using namespace sys;
//=== independent code. //=== independent code.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
Path
Path::GetLLVMConfigDir() {
Path result;
if (result.setDirectory(LLVM_ETCDIR))
return result;
return GetLLVMDefaultConfigDir();
}
LLVMFileType LLVMFileType
sys::IdentifyFileType(const char*magic, unsigned length) { sys::IdentifyFileType(const char*magic, unsigned length) {
assert(magic && "Invalid magic number string"); assert(magic && "Invalid magic number string");

View File

@ -87,7 +87,7 @@ Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
} }
#ifdef LLVMGCCDIR #ifdef LLVMGCCDIR
{ {
Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/"); Path tmpPath(std::string(LLVMGCCDIR) + "lib/");
if (tmpPath.readable()) if (tmpPath.readable())
Paths.push_back(tmpPath); Paths.push_back(tmpPath);
} }
@ -108,14 +108,6 @@ Path::GetLLVMDefaultConfigDir() {
return Path("/etc/llvm/"); return Path("/etc/llvm/");
} }
Path
Path::GetLLVMConfigDir() {
Path result;
if (result.setDirectory(LLVM_ETCDIR))
return result;
return GetLLVMDefaultConfigDir();
}
Path Path
Path::GetUserHomeDirectory() { Path::GetUserHomeDirectory() {
const char* home = getenv("HOME"); const char* home = getenv("HOME");
@ -491,20 +483,19 @@ Path::createFile() {
bool bool
Path::createTemporaryFile() { Path::createTemporaryFile() {
// Make sure we're dealing with a file // Make sure we're dealing with a file
if (!isFile()) return false; if (!isFile())
return false;
// Append the filename filler // Make this into a unique file name
char pathname[MAXPATHLEN]; makeUnique();
path.copy(pathname,MAXPATHLEN);
pathname[path.length()] = 0; // create the file
strcat(pathname,"XXXXXX"); int outFile = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
int fd = ::mkstemp(pathname); if (outFile != -1) {
if (fd < 0) { ::close(outFile);
ThrowErrno(path + ": Can't create temporary file"); return true;
} }
path = pathname; return false;
::close(fd);
return true;
} }
bool bool
@ -565,6 +556,93 @@ Path::setStatusInfo(const StatusInfo& si) const {
return true; return true;
} }
void
CopyFile(const sys::Path &Dest, const sys::Path &Src) {
int inFile = -1;
int outFile = -1;
try {
inFile = ::open(Src.c_str(), O_RDONLY);
if (inFile == -1)
ThrowErrno("Cannnot open source file to copy: " + Src.toString());
outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
if (outFile == -1)
ThrowErrno("Cannnot create destination file for copy: " +Dest.toString());
char Buffer[16*1024];
while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
if (Amt == -1) {
if (errno != EINTR && errno != EAGAIN)
ThrowErrno("Can't read source file: " + Src.toString());
} else {
char *BufPtr = Buffer;
while (Amt) {
ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
if (AmtWritten == -1) {
if (errno != EINTR && errno != EAGAIN)
ThrowErrno("Can't write destination file: " + Dest.toString());
} else {
Amt -= AmtWritten;
BufPtr += AmtWritten;
}
}
}
}
::close(inFile);
::close(outFile);
} catch (...) {
if (inFile != -1)
::close(inFile);
if (outFile != -1)
::close(outFile);
throw;
}
}
void
Path::makeUnique() {
if (!exists())
return; // File doesn't exist already, just use it!
// Append an XXXXXX pattern to the end of the file for use with mkstemp,
// mktemp or our own implementation.
char *FNBuffer = (char*) alloca(path.size()+8);
path.copy(FNBuffer,path.size());
strcpy(FNBuffer+path.size(), "-XXXXXX");
#if defined(HAVE_MKSTEMP)
int TempFD;
if ((TempFD = mkstemp(FNBuffer)) == -1) {
ThrowErrno("Cannot make unique filename for '" + path + "'");
}
// We don't need to hold the temp file descriptor... we will trust that no one
// will overwrite/delete the file before we can open it again.
close(TempFD);
// Save the name
path = FNBuffer;
#elif defined(HAVE_MKTEMP)
// If we don't have mkstemp, use the old and obsolete mktemp function.
if (mktemp(FNBuffer) == 0) {
ThrowErrno("Cannot make unique filename for '" + path + "'");
}
// Save the name
path = FNBuffer;
#else
// Okay, looks like we have to do it all by our lonesome.
static unsigned FCounter = 0;
unsigned offset = path.size() + 1;
while ( FCounter < 999999 && exists()) {
sprintf(FNBuffer+offset,"%06u",++FCounter);
path = FNBuffer;
}
if (FCounter > 999999)
throw std::string("Cannot make unique filename for '" + path + "'");
#endif
}
} }
// vim: sw=2 // vim: sw=2

View File

@ -87,7 +87,7 @@ Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
} }
#ifdef LLVMGCCDIR #ifdef LLVMGCCDIR
{ {
Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/"); Path tmpPath(std::string(LLVMGCCDIR) + "lib/");
if (tmpPath.readable()) if (tmpPath.readable())
Paths.push_back(tmpPath); Paths.push_back(tmpPath);
} }
@ -108,14 +108,6 @@ Path::GetLLVMDefaultConfigDir() {
return Path("/etc/llvm/"); return Path("/etc/llvm/");
} }
Path
Path::GetLLVMConfigDir() {
Path result;
if (result.setDirectory(LLVM_ETCDIR))
return result;
return GetLLVMDefaultConfigDir();
}
Path Path
Path::GetUserHomeDirectory() { Path::GetUserHomeDirectory() {
const char* home = getenv("HOME"); const char* home = getenv("HOME");
@ -491,20 +483,19 @@ Path::createFile() {
bool bool
Path::createTemporaryFile() { Path::createTemporaryFile() {
// Make sure we're dealing with a file // Make sure we're dealing with a file
if (!isFile()) return false; if (!isFile())
return false;
// Append the filename filler // Make this into a unique file name
char pathname[MAXPATHLEN]; makeUnique();
path.copy(pathname,MAXPATHLEN);
pathname[path.length()] = 0; // create the file
strcat(pathname,"XXXXXX"); int outFile = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
int fd = ::mkstemp(pathname); if (outFile != -1) {
if (fd < 0) { ::close(outFile);
ThrowErrno(path + ": Can't create temporary file"); return true;
} }
path = pathname; return false;
::close(fd);
return true;
} }
bool bool
@ -565,6 +556,93 @@ Path::setStatusInfo(const StatusInfo& si) const {
return true; return true;
} }
void
CopyFile(const sys::Path &Dest, const sys::Path &Src) {
int inFile = -1;
int outFile = -1;
try {
inFile = ::open(Src.c_str(), O_RDONLY);
if (inFile == -1)
ThrowErrno("Cannnot open source file to copy: " + Src.toString());
outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
if (outFile == -1)
ThrowErrno("Cannnot create destination file for copy: " +Dest.toString());
char Buffer[16*1024];
while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
if (Amt == -1) {
if (errno != EINTR && errno != EAGAIN)
ThrowErrno("Can't read source file: " + Src.toString());
} else {
char *BufPtr = Buffer;
while (Amt) {
ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
if (AmtWritten == -1) {
if (errno != EINTR && errno != EAGAIN)
ThrowErrno("Can't write destination file: " + Dest.toString());
} else {
Amt -= AmtWritten;
BufPtr += AmtWritten;
}
}
}
}
::close(inFile);
::close(outFile);
} catch (...) {
if (inFile != -1)
::close(inFile);
if (outFile != -1)
::close(outFile);
throw;
}
}
void
Path::makeUnique() {
if (!exists())
return; // File doesn't exist already, just use it!
// Append an XXXXXX pattern to the end of the file for use with mkstemp,
// mktemp or our own implementation.
char *FNBuffer = (char*) alloca(path.size()+8);
path.copy(FNBuffer,path.size());
strcpy(FNBuffer+path.size(), "-XXXXXX");
#if defined(HAVE_MKSTEMP)
int TempFD;
if ((TempFD = mkstemp(FNBuffer)) == -1) {
ThrowErrno("Cannot make unique filename for '" + path + "'");
}
// We don't need to hold the temp file descriptor... we will trust that no one
// will overwrite/delete the file before we can open it again.
close(TempFD);
// Save the name
path = FNBuffer;
#elif defined(HAVE_MKTEMP)
// If we don't have mkstemp, use the old and obsolete mktemp function.
if (mktemp(FNBuffer) == 0) {
ThrowErrno("Cannot make unique filename for '" + path + "'");
}
// Save the name
path = FNBuffer;
#else
// Okay, looks like we have to do it all by our lonesome.
static unsigned FCounter = 0;
unsigned offset = path.size() + 1;
while ( FCounter < 999999 && exists()) {
sprintf(FNBuffer+offset,"%06u",++FCounter);
path = FNBuffer;
}
if (FCounter > 999999)
throw std::string("Cannot make unique filename for '" + path + "'");
#endif
}
} }
// vim: sw=2 // vim: sw=2

View File

@ -158,7 +158,7 @@ Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
} }
#ifdef LLVMGCCDIR #ifdef LLVMGCCDIR
{ {
Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/"); Path tmpPath(std::string(LLVMGCCDIR) + "lib/");
if (tmpPath.readable()) if (tmpPath.readable())
Paths.push_back(tmpPath); Paths.push_back(tmpPath);
} }
@ -584,6 +584,29 @@ bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
return true; return true;
} }
void
CopyFile(const sys::Path &Dest, const sys::Path &Src) {
if (!::CopyFile(Src.c_str(), Dest.c_str(), false))
ThrowError("Can't copy '" + Src.toString() +
"' to '" + Dest.toString() + "'");
}
void
Path::makeUnique() {
if (!exists())
return; // File doesn't exist already, just use it!
Path dir (*this);
dir.elideFile();
std::string fname = this->getLast();
char* newName = alloca(MAX_PATH+1);
if (!GetTempFileName(dir.c_str(), fname.c_str(), 0, newName))
ThrowError("Cannot make unique filename for '" + path + "'");
path = newName;
}
} }
} }

View File

@ -158,7 +158,7 @@ Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
} }
#ifdef LLVMGCCDIR #ifdef LLVMGCCDIR
{ {
Path tmpPath(std::string(LLVMGCCDIR) + "bytecode-libs/"); Path tmpPath(std::string(LLVMGCCDIR) + "lib/");
if (tmpPath.readable()) if (tmpPath.readable())
Paths.push_back(tmpPath); Paths.push_back(tmpPath);
} }
@ -584,6 +584,29 @@ bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
return true; return true;
} }
void
CopyFile(const sys::Path &Dest, const sys::Path &Src) {
if (!::CopyFile(Src.c_str(), Dest.c_str(), false))
ThrowError("Can't copy '" + Src.toString() +
"' to '" + Dest.toString() + "'");
}
void
Path::makeUnique() {
if (!exists())
return; // File doesn't exist already, just use it!
Path dir (*this);
dir.elideFile();
std::string fname = this->getLast();
char* newName = alloca(MAX_PATH+1);
if (!GetTempFileName(dir.c_str(), fname.c_str(), 0, newName))
ThrowError("Cannot make unique filename for '" + path + "'");
path = newName;
}
} }
} }