[PATCH] D112710: [Support] [Windows] Use RemoveFileOnSignal if unable to use the delete-on-close flag

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 28 05:27:19 PDT 2021


mstorsjo created this revision.
mstorsjo added reviewers: akhuang, rnk.
Herald added subscribers: dexonsmith, hiraditya.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

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

I left this out from D111875 <https://reviews.llvm.org/D111875> because I was worried it would end up too
messy and didn't want to hold up the rest of it on it, but it turns out
it should be pretty straightforward.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112710

Files:
  llvm/lib/Support/Path.cpp


Index: llvm/lib/Support/Path.cpp
===================================================================
--- llvm/lib/Support/Path.cpp
+++ llvm/lib/Support/Path.cpp
@@ -1216,9 +1216,7 @@
   std::error_code RemoveEC;
   if (Remove && !TmpName.empty()) {
     RemoveEC = fs::remove(TmpName);
-#ifndef _WIN32
     sys::DontRemoveFileOnSignal(TmpName);
-#endif
     if (!RemoveEC)
       TmpName = "";
   } else {
@@ -1264,8 +1262,8 @@
     if (RenameEC)
       remove(TmpName);
   }
-  sys::DontRemoveFileOnSignal(TmpName);
 #endif
+  sys::DontRemoveFileOnSignal(TmpName);
 
   if (!RenameEC)
     TmpName = "";
@@ -1287,9 +1285,8 @@
   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 = "";
 
@@ -1313,17 +1310,20 @@
   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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112710.382996.patch
Type: text/x-patch
Size: 1601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211028/a42fc59f/attachment.bin>


More information about the llvm-commits mailing list