[PATCH] D66859: [index-while-buildling] FSUtil

Jan Korous via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 13:46:52 PDT 2019


jkorous updated this revision to Diff 217724.
jkorous edited the summary of this revision.
jkorous added a comment.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Addressed comments.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66859/new/

https://reviews.llvm.org/D66859

Files:
  llvm/include/llvm/Support/FileUtilities.h
  llvm/lib/Support/FileUtilities.cpp


Index: llvm/lib/Support/FileUtilities.cpp
===================================================================
--- llvm/lib/Support/FileUtilities.cpp
+++ llvm/lib/Support/FileUtilities.cpp
@@ -15,6 +15,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cctype>
 #include <cmath>
@@ -264,3 +265,37 @@
 
   return CompareFailed;
 }
+
+Error llvm::writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
+                                StringRef Buffer) {
+  SmallString<128> GeneratedUniqPath;
+  int TempFD;
+  if (const std::error_code Error = sys::fs::createUniqueFile(
+          TempPathModel.str(), TempFD, GeneratedUniqPath)) {
+    return createStringError(
+        Error, "failed to create temporary file with model \"%s\"",
+        TempPathModel.str().c_str());
+  }
+
+  raw_fd_ostream OS(TempFD, /*shouldClose=*/true);
+  OS.write(Buffer.data(), Buffer.size());
+  OS.close();
+  TempFD = -1;
+
+  if (OS.has_error()) {
+    const std::error_code Error = OS.error();
+    OS.clear_error();
+    return createStringError(Error, "failed to write to \"%s\"",
+                             GeneratedUniqPath.c_str());
+  }
+
+  if (const std::error_code Error =
+          sys::fs::rename(/*from=*/GeneratedUniqPath.c_str(),
+                          /*to=*/FinalPath.str().c_str())) {
+    return createStringError(Error, "failed to rename file \"%s\" to \"%s\"",
+                             GeneratedUniqPath.c_str(),
+                             FinalPath.str().c_str());
+  }
+
+  return Error::success();
+}
Index: llvm/include/llvm/Support/FileUtilities.h
===================================================================
--- llvm/include/llvm/Support/FileUtilities.h
+++ llvm/include/llvm/Support/FileUtilities.h
@@ -14,6 +14,8 @@
 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
 #define LLVM_SUPPORT_FILEUTILITIES_H
 
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Errc.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 
@@ -72,6 +74,11 @@
     /// will not be removed when the object is destroyed.
     void releaseFile() { DeleteIt = false; }
   };
+
+  /// Creates a unique file with name according to the given \p TempPathModel,
+  /// writes content of \p Buffer to the file and renames it to \p FinalPath.
+  llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
+                                  StringRef Buffer);
 } // End llvm namespace
 
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66859.217724.patch
Type: text/x-patch
Size: 2584 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190828/c4472b6b/attachment.bin>


More information about the llvm-commits mailing list