[llvm] r314345 - Do not remove a target file in FileOutputBuffer::create().

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 27 14:19:24 PDT 2017


Author: ruiu
Date: Wed Sep 27 14:19:24 2017
New Revision: 314345

URL: http://llvm.org/viewvc/llvm-project?rev=314345&view=rev
Log:
Do not remove a target file in FileOutputBuffer::create().

FileOutputBuffer::create() attempts to remove a target file if the file
is a regular one, which results in an unexpected result in a failure
scenario.

If something goes wrong and the user of FileOutputBuffer decides to not
call commit(), it leaves nothing. An existing file is removed, and no
new file is created.

What we should do is to atomically replace an existing file with a new
file using rename(), so that it wouldn't remove an existing file without
creating a new one.

Differential Revision: https://reviews.llvm.org/D38283

Modified:
    llvm/trunk/lib/Support/FileOutputBuffer.cpp

Modified: llvm/trunk/lib/Support/FileOutputBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileOutputBuffer.cpp?rev=314345&r1=314344&r2=314345&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FileOutputBuffer.cpp (original)
+++ llvm/trunk/lib/Support/FileOutputBuffer.cpp Wed Sep 27 14:19:24 2017
@@ -65,13 +65,6 @@ FileOutputBuffer::create(StringRef FileP
       IsRegular = false;
   }
 
-  if (IsRegular) {
-    // Delete target file.
-    EC = sys::fs::remove(FilePath);
-    if (EC)
-      return EC;
-  }
-
   SmallString<128> TempFilePath;
   int FD;
   if (IsRegular) {
@@ -125,7 +118,7 @@ std::error_code FileOutputBuffer::commit
 
   std::error_code EC;
   if (IsRegular) {
-    // Rename file to final name.
+    // Atomically replace the existing file with the new one.
     EC = sys::fs::rename(Twine(TempPath), Twine(FinalPath));
     sys::DontRemoveFileOnSignal(TempPath);
   } else {




More information about the llvm-commits mailing list