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
This commit is contained in:
Rafael Espindola 2017-11-22 19:59:05 +00:00
parent 32c9de009a
commit fe161b9d96
2 changed files with 27 additions and 0 deletions

View File

@ -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);
}

View File

@ -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;