diff options
-rw-r--r-- | llvm/lib/Support/Path.cpp | 6 | ||||
-rw-r--r-- | llvm/unittests/Support/Path.cpp | 21 |
2 files changed, 27 insertions, 0 deletions
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index b96396a3846..cb8e5a8d5f7 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -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); } diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index a798928e4e5..f624626f5e5 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -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; |