From e04f789f46adbbf2ec5f48ead03c67250987500f Mon Sep 17 00:00:00 2001 From: Frederic Riss Date: Wed, 5 Aug 2015 23:33:50 +0000 Subject: [PATCH] [dsymutil] Do not create temporary files in -no-output mode. The files were never written to and then deleted, but they were created nonetheless. To prevent that, create a wrapper around the 2 variants of createUniqueFile and use the one that only does an access(Exists) call to check for name unicity in -no-output mode. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244172 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/dsymutil/dsymutil.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tools/dsymutil/dsymutil.cpp b/tools/dsymutil/dsymutil.cpp index 581da4ebfa0..cf5e6ed99a5 100644 --- a/tools/dsymutil/dsymutil.cpp +++ b/tools/dsymutil/dsymutil.cpp @@ -79,21 +79,35 @@ static opt InputIsYAMLDebugMap( init(false), cat(DsymCategory)); } +static std::error_code getUniqueFile(const llvm::Twine &Model, int &ResultFD, + llvm::SmallVectorImpl &ResultPath) { + // If in NoOutput mode, use the createUniqueFile variant 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::createUniqueFile(Model, ResultFD, ResultPath); +} + static std::string getOutputFileName(llvm::StringRef InputFile, bool TempFile = false) { if (TempFile) { - std::string OutputFile = (InputFile + ".tmp%%%%%%.dwarf").str(); + llvm::Twine OutputFile = InputFile + ".tmp%%%%%%.dwarf"; int FD; llvm::SmallString<128> UniqueFile; - if (auto EC = llvm::sys::fs::createUniqueFile(OutputFile, FD, UniqueFile)) { + if (auto EC = getUniqueFile(OutputFile, FD, UniqueFile)) { llvm::errs() << "error: failed to create temporary outfile '" << OutputFile << "': " << EC.message() << '\n'; return ""; } llvm::sys::RemoveFileOnSignal(UniqueFile); - // Close the file immediately. We know it is unique. It will be - // reopened and written to later. - llvm::raw_fd_ostream CloseImmediately(FD, true /* shouldClose */, true); + if (!NoOutput) { + // Close the file immediately. We know it is unique. It will be + // reopened and written to later. + llvm::raw_fd_ostream CloseImmediately(FD, true /* shouldClose */, true); + } return UniqueFile.str(); } -- 2.34.1