<div dir="ltr">It should replace an existing file both on Unix and Windows. On Unix, it uses rename() which replaces a destination file if exists. On Windows, it uses MoveFileExW with MOVEFILE_REPLACE_EXISTING flag, so it should replace a file too.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 26, 2017 at 4:30 PM, Zachary Turner <span dir="ltr"><<a href="mailto:zturner@google.com" target="_blank">zturner@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I don’t think this will replace an existing file will it?  It looks like it will fail if the file already exists <br><div class="HOEnZb"><div class="h5"><div class="gmail_quote"><div dir="ltr">On Tue, Sep 26, 2017 at 3:31 PM Rui Ueyama via Phabricator <<a href="mailto:reviews@reviews.llvm.org" target="_blank">reviews@reviews.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">ruiu created this revision.<br>
Herald added a subscriber: hiraditya.<br>
<br>
FileOutputBuffer::create() attempts to remove a target file if the file<br>
is a regular one, which results in an unexpected result in a failure<br>
scenario.<br>
<br>
If something goes wrong and the user of FileOutputBuffer decides to not<br>
call commit(), it leaves nothing. An existing file was removed, and no<br>
new file was created.<br>
<br>
What we should do is to atomically replace an existing file with a new<br>
file using rename(), so that it wouldn't remove an existing file without<br>
creating a new one.<br>
<br>
<br>
<a href="https://reviews.llvm.org/D38283" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D38283</a><br>
<br>
Files:<br>
  llvm/lib/Support/<wbr>FileOutputBuffer.cpp<br>
<br>
<br>
Index: llvm/lib/Support/<wbr>FileOutputBuffer.cpp<br>
==============================<wbr>==============================<wbr>=======<br>
--- llvm/lib/Support/<wbr>FileOutputBuffer.cpp<br>
+++ llvm/lib/Support/<wbr>FileOutputBuffer.cpp<br>
@@ -65,13 +65,6 @@<br>
       IsRegular = false;<br>
   }<br>
<br>
-  if (IsRegular) {<br>
-    // Delete target file.<br>
-    EC = sys::fs::remove(FilePath);<br>
-    if (EC)<br>
-      return EC;<br>
-  }<br>
-<br>
   SmallString<128> TempFilePath;<br>
   int FD;<br>
   if (IsRegular) {<br>
@@ -125,7 +118,7 @@<br>
<br>
   std::error_code EC;<br>
   if (IsRegular) {<br>
-    // Rename file to final name.<br>
+    // Atomically replace the existing file with the new one.<br>
     EC = sys::fs::rename(Twine(<wbr>TempPath), Twine(FinalPath));<br>
     sys::DontRemoveFileOnSignal(<wbr>TempPath);<br>
   } else {<br>
<br>
<br>
</blockquote></div>
</div></div></blockquote></div><br></div>