Index: include/llvm/Support/FileSystem.h =================================================================== --- include/llvm/Support/FileSystem.h +++ include/llvm/Support/FileSystem.h @@ -666,6 +666,26 @@ /// 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 @@ /// otherwise a platform-specific error_code. std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl &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 @@ /// running the assembler. std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, - SmallVectorImpl &ResultPath); + SmallVectorImpl &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 createUniqueDirectory(const Twine &Prefix, SmallVectorImpl &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)); } Index: lib/Support/Path.cpp =================================================================== --- lib/Support/Path.cpp +++ lib/Support/Path.cpp @@ -160,10 +160,11 @@ FS_Name }; -static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD, - SmallVectorImpl &ResultPath, - bool MakeAbsolute, unsigned Mode, - FSEntity Type) { +static std::error_code +createUniqueEntity(const Twine &Model, int &ResultFD, + SmallVectorImpl &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 @@ 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 createUniqueFile(const Twine &Model, int &ResultFd, SmallVectorImpl &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 @@ static std::error_code createTemporaryFile(const Twine &Model, int &ResultFD, - llvm::SmallVectorImpl &ResultPath, FSEntity Type) { + llvm::SmallVectorImpl &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 &ResultPath, FSEntity Type) { + llvm::SmallVectorImpl &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 &ResultPath) { - return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File); + SmallVectorImpl &ResultPath, + sys::fs::OpenFlags Flags) { + return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File, + Flags); } std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,