Add a createUniqueFile function and switch llvm's users of unique_file.

This function is complementary to createTemporaryFile. It handles the case were
the unique file is *not* temporary: we will rename it in the end. Since we
will rename it, the file has to be in the same filesystem as the final
destination and we don't prepend the system temporary directory.

This has a small semantic difference from unique_file: the default mode is 0666.
This matches the behavior of most unix tools. For example, with this change
lld now produces files with the same permissions as ld. I will add a test
of this change when I port clang over to createUniqueFile (next commit).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185726 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2013-07-05 21:01:08 +00:00
parent cd56727b55
commit 200c748a86
9 changed files with 60 additions and 21 deletions

View File

@ -575,6 +575,35 @@ error_code unique_file(const Twine &model, int &result_fd,
error_code unique_file(const Twine &Model, SmallVectorImpl<char> &ResultPath, error_code unique_file(const Twine &Model, SmallVectorImpl<char> &ResultPath,
bool MakeAbsolute = true); bool MakeAbsolute = true);
/// @brief Create a uniquely named file.
///
/// Generates a unique path suitable for a temporary file and then opens it as a
/// file. The name is based on \a model with '%' replaced by a random char in
/// [0-9a-f]. If \a model is not an absolute path, a suitable temporary
/// directory will be prepended.
///
/// Example: clang-%%-%%-%%-%%-%%.s => clang-a0-b1-c2-d3-e4.s
///
/// This is an atomic operation. Either the file is created and opened, or the
/// file system is left untouched.
///
/// The intendend use is for files that are to be kept, possibly after
/// renaming them. For example, when running 'clang -c foo.o', the file can
/// be first created as foo-abc123.o and then renamed.
///
/// @param Model Name to base unique path off of.
/// @param ResultFD Set to the opened file's file descriptor.
/// @param ResultPath Set to the opened file's absolute path.
/// @returns errc::success if Result{FD,Path} have been successfully set,
/// otherwise a platform specific error_code.
error_code createUniqueFile(const Twine &Model, int &ResultFD,
SmallVectorImpl<char> &ResultPath,
unsigned Mode = all_read | all_write);
/// @brief Simpler version for clients that don't want an open file.
error_code createUniqueFile(const Twine &Model,
SmallVectorImpl<char> &ResultPath);
/// @brief Create a file in the system temporary directory. /// @brief Create a file in the system temporary directory.
/// ///
/// The filename is of the form prefix-random_chars.suffix. Since the directory /// The filename is of the form prefix-random_chars.suffix. Since the directory

View File

@ -65,8 +65,8 @@ error_code FileOutputBuffer::create(StringRef FilePath,
// Create new file in same directory but with random name. // Create new file in same directory but with random name.
SmallString<128> TempFilePath; SmallString<128> TempFilePath;
int FD; int FD;
EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%", EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%",
FD, TempFilePath, false, 0644); FD, TempFilePath);
if (EC) if (EC)
return EC; return EC;

View File

@ -78,10 +78,9 @@ LockFileManager::LockFileManager(StringRef FileName)
UniqueLockFileName += "-%%%%%%%%"; UniqueLockFileName += "-%%%%%%%%";
int UniqueLockFileID; int UniqueLockFileID;
if (error_code EC if (error_code EC
= sys::fs::unique_file(UniqueLockFileName.str(), = sys::fs::createUniqueFile(UniqueLockFileName.str(),
UniqueLockFileID, UniqueLockFileID,
UniqueLockFileName, UniqueLockFileName)) {
/*makeAbsolute=*/false)) {
Error = EC; Error = EC;
return; return;
} }

View File

@ -654,6 +654,17 @@ error_code unique_file(const Twine &Model, SmallVectorImpl<char> &ResultPath,
return createUniqueEntity(Model, Dummy, ResultPath, MakeAbsolute, 0, FS_Name); return createUniqueEntity(Model, Dummy, ResultPath, MakeAbsolute, 0, FS_Name);
} }
error_code createUniqueFile(const Twine &Model, int &ResultFd,
SmallVectorImpl<char> &ResultPath, unsigned Mode) {
return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
}
error_code createUniqueFile(const Twine &Model,
SmallVectorImpl<char> &ResultPath) {
int Dummy;
return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
}
static error_code createTemporaryFile(const Twine &Model, int &ResultFD, static error_code createTemporaryFile(const Twine &Model, int &ResultFD,
llvm::SmallVectorImpl<char> &ResultPath, llvm::SmallVectorImpl<char> &ResultPath,
FSEntity Type) { FSEntity Type) {

View File

@ -267,7 +267,7 @@ void BugDriver::compileProgram(Module *M, std::string *Error) const {
// Emit the program to a bitcode file... // Emit the program to a bitcode file...
SmallString<128> BitcodeFile; SmallString<128> BitcodeFile;
int BitcodeFD; int BitcodeFD;
error_code EC = sys::fs::unique_file( error_code EC = sys::fs::createUniqueFile(
OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile); OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
if (EC) { if (EC) {
errs() << ToolName << ": Error making unique filename: " << EC.message() errs() << ToolName << ": Error making unique filename: " << EC.message()
@ -306,7 +306,7 @@ std::string BugDriver::executeProgram(const Module *Program,
// Emit the program to a bitcode file... // Emit the program to a bitcode file...
SmallString<128> UniqueFilename; SmallString<128> UniqueFilename;
int UniqueFD; int UniqueFD;
error_code EC = sys::fs::unique_file( error_code EC = sys::fs::createUniqueFile(
OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename); OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
if (EC) { if (EC) {
errs() << ToolName << ": Error making unique filename: " errs() << ToolName << ": Error making unique filename: "
@ -332,7 +332,7 @@ std::string BugDriver::executeProgram(const Module *Program,
// Check to see if this is a valid output filename... // Check to see if this is a valid output filename...
SmallString<128> UniqueFile; SmallString<128> UniqueFile;
error_code EC = sys::fs::unique_file(OutputFile, UniqueFile); error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
if (EC) { if (EC) {
errs() << ToolName << ": Error making unique filename: " errs() << ToolName << ": Error making unique filename: "
<< EC.message() << "\n"; << EC.message() << "\n";

View File

@ -365,8 +365,8 @@ Module *BugDriver::ExtractMappedBlocksFromModule(const
Module *M) { Module *M) {
SmallString<128> Filename; SmallString<128> Filename;
int FD; int FD;
error_code EC = sys::fs::unique_file(OutputPrefix + "-extractblocks%%%%%%%", error_code EC = sys::fs::createUniqueFile(
FD, Filename); OutputPrefix + "-extractblocks%%%%%%%", FD, Filename);
if (EC) { if (EC) {
outs() << "*** Basic Block extraction failed!\n"; outs() << "*** Basic Block extraction failed!\n";
errs() << "Error creating temporary file: " << EC.message() << "\n"; errs() << "Error creating temporary file: " << EC.message() << "\n";

View File

@ -124,8 +124,8 @@ bool BugDriver::runPasses(Module *Program,
// setup the output file name // setup the output file name
outs().flush(); outs().flush();
SmallString<128> UniqueFilename; SmallString<128> UniqueFilename;
error_code EC = error_code EC = sys::fs::createUniqueFile(
sys::fs::unique_file(OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename); OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename);
if (EC) { if (EC) {
errs() << getToolName() << ": Error making unique filename: " errs() << getToolName() << ": Error making unique filename: "
<< EC.message() << "\n"; << EC.message() << "\n";
@ -136,8 +136,8 @@ bool BugDriver::runPasses(Module *Program,
// set up the input file name // set up the input file name
SmallString<128> InputFilename; SmallString<128> InputFilename;
int InputFD; int InputFD;
EC = sys::fs::unique_file(OutputPrefix + "-input-%%%%%%%.bc", InputFD, EC = sys::fs::createUniqueFile(OutputPrefix + "-input-%%%%%%%.bc", InputFD,
InputFilename); InputFilename);
if (EC) { if (EC) {
errs() << getToolName() << ": Error making unique filename: " errs() << getToolName() << ": Error making unique filename: "
<< EC.message() << "\n"; << EC.message() << "\n";

View File

@ -478,7 +478,7 @@ GCC::FileType LLC::OutputCode(const std::string &Bitcode,
SmallString<128> UniqueFile; SmallString<128> UniqueFile;
error_code EC = error_code EC =
sys::fs::unique_file(Bitcode + "-%%%%%%%" + Suffix, UniqueFile); sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);
if (EC) { if (EC) {
errs() << "Error making unique filename: " << EC.message() << "\n"; errs() << "Error making unique filename: " << EC.message() << "\n";
exit(1); exit(1);
@ -715,7 +715,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
SmallString<128> OutputBinary; SmallString<128> OutputBinary;
error_code EC = error_code EC =
sys::fs::unique_file(ProgramFile+ "-%%%%%%%.gcc.exe", OutputBinary); sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.gcc.exe", OutputBinary);
if (EC) { if (EC) {
errs() << "Error making unique filename: " << EC.message() << "\n"; errs() << "Error making unique filename: " << EC.message() << "\n";
exit(1); exit(1);
@ -824,8 +824,8 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
const std::vector<std::string> &ArgsForGCC, const std::vector<std::string> &ArgsForGCC,
std::string &Error) { std::string &Error) {
SmallString<128> UniqueFilename; SmallString<128> UniqueFilename;
error_code EC = sys::fs::unique_file(InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, error_code EC = sys::fs::createUniqueFile(
UniqueFilename); InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);
if (EC) { if (EC) {
errs() << "Error making unique filename: " << EC.message() << "\n"; errs() << "Error making unique filename: " << EC.message() << "\n";
exit(1); exit(1);

View File

@ -260,8 +260,8 @@ bool Archive::writeToDisk(bool TruncateNames, std::string *ErrMsg) {
// Create a temporary file to store the archive in // Create a temporary file to store the archive in
int TmpArchiveFD; int TmpArchiveFD;
SmallString<128> TmpArchive; SmallString<128> TmpArchive;
error_code EC = sys::fs::unique_file("temp-archive-%%%%%%%.a", TmpArchiveFD, error_code EC = sys::fs::createUniqueFile("temp-archive-%%%%%%%.a",
TmpArchive, true, sys::fs::all_read | sys::fs::all_write); TmpArchiveFD, TmpArchive);
if (EC) if (EC)
return true; return true;