From e11874f6c5e33f5c2d634bfd33e1ae67c93f9b74 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 5 Jul 2013 19:56:49 +0000 Subject: [PATCH] Add a higher level createTemporaryFile function. This function is inspired by clang's Driver::GetTemporaryPath. It hides the pattern used for uniquing and requires simple file names that are always placed in the system temporary directory. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185716 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/FileSystem.h | 16 +++++++++++++++ lib/Support/Path.cpp | 34 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/llvm/Support/FileSystem.h b/include/llvm/Support/FileSystem.h index 3244517644e..d8f79ba69d6 100644 --- a/include/llvm/Support/FileSystem.h +++ b/include/llvm/Support/FileSystem.h @@ -575,6 +575,22 @@ error_code unique_file(const Twine &model, int &result_fd, error_code unique_file(const Twine &Model, SmallVectorImpl &ResultPath, bool MakeAbsolute = true); +/// @brief Create a file in the system temporary directory. +/// +/// The filename is of the form prefix-random_chars.suffix. Since the directory +/// is not know to the caller, Prefix and Suffix cannot have path separators. +/// The files are created with mode 0600. +/// +/// This should be used for things like a temporary .s that is removed after +/// running the assembler. +error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, + int &ResultFD, + SmallVectorImpl &ResultPath); + +/// @brief Simpler version for clients that don't want an open file. +error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, + SmallVectorImpl &ResultPath); + error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl &ResultPath); diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 5c28c55f267..36fb9f3a4bf 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -654,6 +654,40 @@ error_code unique_file(const Twine &Model, SmallVectorImpl &ResultPath, return createUniqueEntity(Model, Dummy, ResultPath, MakeAbsolute, 0, FS_Name); } +static error_code createTemporaryFile(const Twine &Model, int &ResultFD, + llvm::SmallVectorImpl &ResultPath, + FSEntity Type) { + SmallString<128> Storage; + StringRef P = Model.toNullTerminatedStringRef(Storage); + assert(P.find_first_of(separators) == 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); +} + +static error_code +createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, + llvm::SmallVectorImpl &ResultPath, + FSEntity Type) { + return createTemporaryFile(Prefix + "-%%%%%%." + Suffix, ResultFD, ResultPath, + Type); +} + + +error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, + int &ResultFD, + SmallVectorImpl &ResultPath) { + return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File); +} + +error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, + SmallVectorImpl &ResultPath) { + int Dummy; + return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name); +} + + // This is a mkdtemp with a different pattern. We use createUniqueEntity mostly // for consistency. We should try using mkdtemp. error_code createUniqueDirectory(const Twine &Prefix, -- 2.34.1