Skip to content

Commit

Permalink
[Support] [Windows] Use RemoveFileOnSignal if unable to use the delet…
Browse files Browse the repository at this point in the history
…e-on-close flag

This takes care of cleaning up the temp files on crashes. It doesn't
handle cleanup when explicitly killed though.

Differential Revision: https://reviews.llvm.org/D112710
  • Loading branch information
mstorsjo committed Nov 3, 2021
1 parent 7ff943a commit a39eba7
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions llvm/lib/Support/Path.cpp
Expand Up @@ -1212,9 +1212,7 @@ Error TempFile::discard() {
std::error_code RemoveEC;
if (Remove && !TmpName.empty()) {
RemoveEC = fs::remove(TmpName);
#ifndef _WIN32
sys::DontRemoveFileOnSignal(TmpName);
#endif
if (!RemoveEC)
TmpName = "";
} else {
Expand Down Expand Up @@ -1260,8 +1258,8 @@ Error TempFile::keep(const Twine &Name) {
if (RenameEC)
remove(TmpName);
}
sys::DontRemoveFileOnSignal(TmpName);
#endif
sys::DontRemoveFileOnSignal(TmpName);

if (!RenameEC)
TmpName = "";
Expand All @@ -1283,9 +1281,8 @@ Error TempFile::keep() {
auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
if (std::error_code EC = setDeleteDisposition(H, false))
return errorCodeToError(EC);
#else
sys::DontRemoveFileOnSignal(TmpName);
#endif
sys::DontRemoveFileOnSignal(TmpName);

TmpName = "";

Expand All @@ -1309,17 +1306,20 @@ Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode,
TempFile Ret(ResultPath, FD);
#ifdef _WIN32
auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
bool SetSignalHandler = false;
if (std::error_code EC = setDeleteDisposition(H, true)) {
Ret.RemoveOnClose = true;
SetSignalHandler = true;
}
#else
if (sys::RemoveFileOnSignal(ResultPath)) {
bool SetSignalHandler = true;
#endif
if (SetSignalHandler && sys::RemoveFileOnSignal(ResultPath)) {
// Make sure we delete the file when RemoveFileOnSignal fails.
consumeError(Ret.discard());
std::error_code EC(errc::operation_not_permitted);
return errorCodeToError(EC);
}
#endif
return std::move(Ret);
}
} // namespace fs
Expand Down

0 comments on commit a39eba7

Please sign in to comment.