diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2017-11-22 19:59:05 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2017-11-22 19:59:05 +0000 |
commit | fe161b9d961218996a33cfec135d77bc8c96afc7 (patch) | |
tree | 9cd8e23ebd791271f08a1b3f199aad2bac03489a | |
parent | 32c9de009a52a43db3a24c38f05d2543f3fd6911 (diff) | |
download | bcm5719-llvm-fe161b9d961218996a33cfec135d77bc8c96afc7.tar.gz bcm5719-llvm-fe161b9d961218996a33cfec135d77bc8c96afc7.zip |
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.
llvm-svn: 318867
-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; |