[dsymutil] Do not create temporary files in -no-output mode.
authorFrederic Riss <friss@apple.com>
Wed, 5 Aug 2015 23:33:50 +0000 (23:33 +0000)
committerFrederic Riss <friss@apple.com>
Wed, 5 Aug 2015 23:33:50 +0000 (23:33 +0000)
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

index 581da4ebfa06f1893b16e48497b7cd0f0e759557..cf5e6ed99a5f71da58984676288de74d6aa0b7c4 100644 (file)
@@ -79,21 +79,35 @@ static opt<bool> InputIsYAMLDebugMap(
     init(false), cat(DsymCategory));
 }
 
+static std::error_code getUniqueFile(const llvm::Twine &Model, int &ResultFD,
+                                     llvm::SmallVectorImpl<char> &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();
   }