[Lldb-commits] [lldb] r371890 - [Support] Add overload writeFileAtomically(std::function Writer)

Jan Korous via lldb-commits lldb-commits at lists.llvm.org
Fri Sep 13 13:08:27 PDT 2019


Author: jkorous
Date: Fri Sep 13 13:08:27 2019
New Revision: 371890

URL: http://llvm.org/viewvc/llvm-project?rev=371890&view=rev
Log:
[Support] Add overload writeFileAtomically(std::function Writer)

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

Modified:
    lldb/trunk/tools/lldb-server/lldb-platform.cpp

Modified: lldb/trunk/tools/lldb-server/lldb-platform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-server/lldb-platform.cpp?rev=371890&r1=371889&r2=371890&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-server/lldb-platform.cpp (original)
+++ lldb/trunk/tools/lldb-server/lldb-platform.cpp Fri Sep 13 13:08:27 2019
@@ -22,6 +22,7 @@
 
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/raw_ostream.h"
 
 #include "Acceptor.h"
 #include "LLDBServerUtilities.h"
@@ -103,29 +104,34 @@ static Status save_socket_id_to_file(con
 
   llvm::SmallString<64> temp_file_path;
   temp_file_spec.AppendPathComponent("port-file.%%%%%%");
-  int FD;
-  auto err_code = llvm::sys::fs::createUniqueFile(temp_file_spec.GetPath(), FD,
-                                                  temp_file_path);
-  if (err_code)
-    return Status("Failed to create temp file: %s", err_code.message().c_str());
-
-  llvm::FileRemover tmp_file_remover(temp_file_path);
-
-  {
-    llvm::raw_fd_ostream temp_file(FD, true);
-    temp_file << socket_id;
-    temp_file.close();
-    if (temp_file.has_error())
-      return Status("Failed to write to port file.");
-  }
-
-  err_code = llvm::sys::fs::rename(temp_file_path, file_spec.GetPath());
-  if (err_code)
-    return Status("Failed to rename file %s to %s: %s", temp_file_path.c_str(),
-                  file_spec.GetPath().c_str(), err_code.message().c_str());
 
-  tmp_file_remover.releaseFile();
-  return Status();
+  Status status;
+  if (auto Err =
+          handleErrors(llvm::writeFileAtomically(
+                           temp_file_path, temp_file_spec.GetPath(), socket_id),
+                       [&status, &temp_file_path,
+                        &file_spec](const AtomicFileWriteError &E) {
+                         std::string ErrorMsgBuffer;
+                         llvm::raw_string_ostream S(ErrorMsgBuffer);
+                         E.log(S);
+
+                         switch (E.Error) {
+                         case atomic_write_error::failed_to_create_uniq_file:
+                           status = Status("Failed to create temp file: %s",
+                                           ErrorMsgBuffer.c_str());
+                         case atomic_write_error::output_stream_error:
+                           status = Status("Failed to write to port file.");
+                         case atomic_write_error::failed_to_rename_temp_file:
+                           status = Status("Failed to rename file %s to %s: %s",
+                                           ErrorMsgBuffer.c_str(),
+                                           file_spec.GetPath().c_str(),
+                                           ErrorMsgBuffer.c_str());
+                         }
+                       })) {
+    return Status("Failed to atomically write file %s",
+                  file_spec.GetPath().c_str());
+  }
+  return status;
 }
 
 // main




More information about the lldb-commits mailing list