Index: include/llvm/Support/FileSystem.h =================================================================== --- include/llvm/Support/FileSystem.h +++ include/llvm/Support/FileSystem.h @@ -662,9 +662,11 @@ SmallVectorImpl &ResultPath, unsigned Mode = all_read | all_write); -/// @brief Simpler version for clients that don't want an open file. +/// @brief Simpler version for clients that don't want an open file. An empty +/// file will still be created. std::error_code createUniqueFile(const Twine &Model, - SmallVectorImpl &ResultPath); + SmallVectorImpl &ResultPath, + unsigned Mode = all_read | all_write); /// @brief Create a file in the system temporary directory. /// @@ -678,13 +680,36 @@ int &ResultFD, SmallVectorImpl &ResultPath); -/// @brief Simpler version for clients that don't want an open file. +/// @brief Simpler version for clients that don't want an open file. An empty +/// file will still be created. std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, SmallVectorImpl &ResultPath); std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl &ResultPath); +/// @brief Get a unique name, not currently exisiting in the filesystem. Subject +/// to race conditions, prefer to use createUniqueFile instead. +/// +/// Similar to createUniqueFile, but instead of creating a file only +/// checks if it exists. This function is subject to race conditions, if you +/// want to use the returned name to actually create a file, use +/// createUniqueFile instead. +std::error_code getPotentiallyUniqueFileName(const Twine &Model, + SmallVectorImpl &ResultPath); + +/// @brief Get a unique temporary file name, not currently exisiting in the +/// filesystem. Subject to race conditions, prefer to use createTemporaryFile +/// instead. +/// +/// Similar to createTemporaryFile, but instead of creating a file only +/// checks if it exists. This function is subject to race conditions, if you +/// want to use the returned name to actually create a file, use +/// createTemporaryFile instead. +std::error_code +getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix, + SmallVectorImpl &ResultPath); + enum OpenFlags : unsigned { F_None = 0, Index: lib/Support/Path.cpp =================================================================== --- lib/Support/Path.cpp +++ lib/Support/Path.cpp @@ -754,9 +754,15 @@ } std::error_code createUniqueFile(const Twine &Model, - SmallVectorImpl &ResultPath) { - int Dummy; - return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name); + SmallVectorImpl &ResultPath, + unsigned Mode) { + int FD; + auto EC = createUniqueFile(Model, FD, ResultPath, Mode); + if (EC) + return EC; + // FD is only needed to avoid race conditions. Close it right away. + close(FD); + return EC; } static std::error_code @@ -787,8 +793,13 @@ std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, SmallVectorImpl &ResultPath) { - int Dummy; - return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name); + int FD; + auto EC = createTemporaryFile(Prefix, Suffix, FD, ResultPath); + if (EC) + return EC; + // FD is only needed to avoid race conditions. Close it right away. + close(FD); + return EC; } @@ -797,8 +808,22 @@ std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl &ResultPath) { int Dummy; - return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, - true, 0, FS_Dir); + return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true, 0, + FS_Dir); +} + +std::error_code +getPotentiallyUniqueFileName(const Twine &Model, + SmallVectorImpl &ResultPath) { + int Dummy; + return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name); +} + +std::error_code +getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix, + SmallVectorImpl &ResultPath) { + int Dummy; + return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name); } static std::error_code make_absolute(const Twine ¤t_directory, Index: tools/dsymutil/dsymutil.cpp =================================================================== --- tools/dsymutil/dsymutil.cpp +++ tools/dsymutil/dsymutil.cpp @@ -163,13 +163,13 @@ static std::error_code getUniqueFile(const llvm::Twine &Model, int &ResultFD, llvm::SmallVectorImpl &ResultPath) { - // If in NoOutput mode, use the createUniqueFile variant that + // If in NoOutput mode, use the getPotentiallyUniqueFileName that // doesn't open the file but still generates a somewhat unique // name. In the real usage scenario, we'll want to ensure that the // file is trully unique, and creating it is the only way to achieve // that. if (NoOutput) - return llvm::sys::fs::createUniqueFile(Model, ResultPath); + return llvm::sys::fs::getPotentiallyUniqueFileName(Model, ResultPath); return llvm::sys::fs::createUniqueFile(Model, ResultFD, ResultPath); }