On error, close the temporary file descriptor.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 16 Jul 2013 16:00:32 +0000 (16:00 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 16 Jul 2013 16:00:32 +0000 (16:00 +0000)
With this change llvm-ar can remove the temporary file on windows too.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186423 91177308-0d34-0410-b5e6-96231b3b80d8

test/Object/archive-error-tmp.txt [new file with mode: 0644]
tools/llvm-ar/llvm-ar.cpp

diff --git a/test/Object/archive-error-tmp.txt b/test/Object/archive-error-tmp.txt
new file mode 100644 (file)
index 0000000..0618986
--- /dev/null
@@ -0,0 +1,9 @@
+REQUIRES: shell
+
+Test that no temporary file is left behind on error.
+
+RUN: rm -rf %t
+RUN: mkdir %t
+RUN: not llvm-ar rc %t/foo.a .
+RUN: rmdir %t
+
index 32bcb49a1db9a64c86ba939817035e0f847fad1b..d65f9ecc9fe51a8b784bf554f353f37ee433744d 100644 (file)
@@ -41,10 +41,13 @@ using namespace llvm;
 static StringRef ToolName;
 
 static const char *TemporaryOutput;
+static int TmpArchiveFD = -1;
 
 // fail - Show the error message and exit.
 LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
   outs() << ToolName << ": " << Error << ".\n";
+  if (TmpArchiveFD != -1)
+    close(TmpArchiveFD);
   if (TemporaryOutput)
     sys::fs::remove(TemporaryOutput);
   exit(1);
@@ -526,7 +529,6 @@ static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
 
 static void performWriteOperation(ArchiveOperation Operation,
                                   object::Archive *OldArchive) {
-  int TmpArchiveFD;
   SmallString<128> TmpArchive;
   failIfError(sys::fs::createUniqueFile(ArchiveName + ".temp-archive-%%%%%%%.a",
                                         TmpArchiveFD, TmpArchive));