Add OpenFlags to the create(Unique|Temporary)File interfaces.

This will allow a future F_Delete flag to be specified when we want
the file to be automatically deleted on close.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319117 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2017-11-27 23:44:11 +00:00
parent 9ee906e259
commit fc69100d8e
2 changed files with 44 additions and 36 deletions

View File

@ -666,6 +666,26 @@ bool status_known(const basic_file_status &s);
/// platform-specific error_code.
std::error_code status_known(const Twine &path, bool &result);
enum OpenFlags : unsigned {
F_None = 0,
/// F_Excl - When opening a file, this flag makes raw_fd_ostream
/// report an error if the file already exists.
F_Excl = 1,
/// F_Append - When opening a file, if it already exists append to the
/// existing file instead of returning an error. This may not be specified
/// with F_Excl.
F_Append = 2,
/// The file should be opened in text mode on platforms that make this
/// distinction.
F_Text = 4,
/// Open the file for read and write.
F_RW = 8
};
/// @brief Create a uniquely named file.
///
/// Generates a unique path suitable for a temporary file and then opens it as a
@ -689,7 +709,8 @@ std::error_code status_known(const Twine &path, bool &result);
/// otherwise a platform-specific error_code.
std::error_code createUniqueFile(const Twine &Model, int &ResultFD,
SmallVectorImpl<char> &ResultPath,
unsigned Mode = all_read | all_write);
unsigned Mode = all_read | all_write,
sys::fs::OpenFlags Flags = sys::fs::F_RW);
/// @brief Simpler version for clients that don't want an open file.
std::error_code createUniqueFile(const Twine &Model,
@ -743,7 +764,8 @@ public:
/// running the assembler.
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
int &ResultFD,
SmallVectorImpl<char> &ResultPath);
SmallVectorImpl<char> &ResultPath,
sys::fs::OpenFlags Flags = sys::fs::F_RW);
/// @brief Simpler version for clients that don't want an open file.
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
@ -752,26 +774,6 @@ std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
std::error_code createUniqueDirectory(const Twine &Prefix,
SmallVectorImpl<char> &ResultPath);
enum OpenFlags : unsigned {
F_None = 0,
/// F_Excl - When opening a file, this flag makes raw_fd_ostream
/// report an error if the file already exists.
F_Excl = 1,
/// F_Append - When opening a file, if it already exists append to the
/// existing file instead of returning an error. This may not be specified
/// with F_Excl.
F_Append = 2,
/// The file should be opened in text mode on platforms that make this
/// distinction.
F_Text = 4,
/// Open the file for read and write.
F_RW = 8
};
inline OpenFlags operator|(OpenFlags A, OpenFlags B) {
return OpenFlags(unsigned(A) | unsigned(B));
}

View File

@ -160,10 +160,11 @@ enum FSEntity {
FS_Name
};
static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD,
SmallVectorImpl<char> &ResultPath,
bool MakeAbsolute, unsigned Mode,
FSEntity Type) {
static std::error_code
createUniqueEntity(const Twine &Model, int &ResultFD,
SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
unsigned Mode, FSEntity Type,
sys::fs::OpenFlags Flags = sys::fs::F_None) {
SmallString<128> ModelStorage;
Model.toVector(ModelStorage);
@ -196,7 +197,7 @@ retry_random_path:
case FS_File: {
if (std::error_code EC =
sys::fs::openFileForWrite(Twine(ResultPath.begin()), ResultFD,
sys::fs::F_RW | sys::fs::F_Excl, Mode)) {
Flags | sys::fs::F_Excl, Mode)) {
if (EC == errc::file_exists)
goto retry_random_path;
return EC;
@ -750,8 +751,9 @@ std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
SmallVectorImpl<char> &ResultPath,
unsigned Mode) {
return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
unsigned Mode, sys::fs::OpenFlags Flags) {
return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File,
Flags);
}
std::error_code createUniqueFile(const Twine &Model,
@ -845,28 +847,32 @@ Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) {
static std::error_code
createTemporaryFile(const Twine &Model, int &ResultFD,
llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type,
sys::fs::OpenFlags Flags) {
SmallString<128> Storage;
StringRef P = Model.toNullTerminatedStringRef(Storage);
assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
"Model must be a simple filename.");
// Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
return createUniqueEntity(P.begin(), ResultFD, ResultPath,
true, owner_read | owner_write, Type);
return createUniqueEntity(P.begin(), ResultFD, ResultPath, true,
owner_read | owner_write, Type, Flags);
}
static std::error_code
createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type,
sys::fs::OpenFlags Flags = sys::fs::F_None) {
const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Type);
Type, Flags);
}
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
int &ResultFD,
SmallVectorImpl<char> &ResultPath) {
return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
SmallVectorImpl<char> &ResultPath,
sys::fs::OpenFlags Flags) {
return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File,
Flags);
}
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,