[llvm] r318867 - Allow TempFile::discard to be called twice.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 22 11:59:05 PST 2017


Author: rafael
Date: Wed Nov 22 11:59:05 2017
New Revision: 318867

URL: http://llvm.org/viewvc/llvm-project?rev=318867&view=rev
Log:
Allow TempFile::discard to be called twice.

We already allowed keep+discard. It is important to be able to discard
a temporary if a rename fail. It is also convenient as it allows the
use of RAII for discarding.

Allow discarding twice for similar reasons.

Modified:
    llvm/trunk/lib/Support/Path.cpp
    llvm/trunk/unittests/Support/Path.cpp

Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=318867&r1=318866&r2=318867&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Wed Nov 22 11:59:05 2017
@@ -779,10 +779,16 @@ Error TempFile::discard() {
     RemoveEC = fs::remove(TmpName);
     sys::DontRemoveFileOnSignal(TmpName);
   }
+
+  if (!RemoveEC)
+    TmpName = "";
+
   if (FD != -1 && close(FD) == -1) {
     std::error_code EC = std::error_code(errno, std::generic_category());
     return errorCodeToError(EC);
   }
+  FD = -1;
+
   return errorCodeToError(RemoveEC);
 }
 

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=318867&r1=318866&r2=318867&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Wed Nov 22 11:59:05 2017
@@ -564,6 +564,27 @@ TEST_F(FileSystemTest, RealPath) {
   ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
 }
 
+TEST_F(FileSystemTest, TempFileKeepDiscard) {
+  // We can keep then discard.
+  auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
+  ASSERT_TRUE((bool)TempFileOrError);
+  fs::TempFile File = std::move(*TempFileOrError);
+  ASSERT_FALSE((bool)File.keep(TestDirectory + "/keep"));
+  ASSERT_FALSE((bool)File.discard());
+  ASSERT_TRUE(fs::exists(TestDirectory + "/keep"));
+  ASSERT_NO_ERROR(fs::remove(TestDirectory + "/keep"));
+}
+
+TEST_F(FileSystemTest, TempFileDiscardDiscard) {
+  // We can discard twice.
+  auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
+  ASSERT_TRUE((bool)TempFileOrError);
+  fs::TempFile File = std::move(*TempFileOrError);
+  ASSERT_FALSE((bool)File.discard());
+  ASSERT_FALSE((bool)File.discard());
+  ASSERT_FALSE(fs::exists(TestDirectory + "/keep"));
+}
+
 TEST_F(FileSystemTest, TempFiles) {
   // Create a temp file.
   int FileDescriptor;




More information about the llvm-commits mailing list