diff options
author | Jan Korous <jkorous@apple.com> | 2019-09-13 20:08:27 +0000 |
---|---|---|
committer | Jan Korous <jkorous@apple.com> | 2019-09-13 20:08:27 +0000 |
commit | f69c91780fbb0e9c0e95f70a079f578efdca0bfa (patch) | |
tree | 8c22ed25e585979a3fda35911273cf1e62de3707 /lldb/tools/lldb-server/lldb-platform.cpp | |
parent | c6ffefd2d1a95b7312741fbd3a9972e5f918173b (diff) | |
download | llvm-f69c91780fbb0e9c0e95f70a079f578efdca0bfa.zip llvm-f69c91780fbb0e9c0e95f70a079f578efdca0bfa.tar.gz llvm-f69c91780fbb0e9c0e95f70a079f578efdca0bfa.tar.bz2 |
[Support] Add overload writeFileAtomically(std::function Writer)
Differential Revision: https://reviews.llvm.org/D67424
llvm-svn: 371890
Diffstat (limited to 'lldb/tools/lldb-server/lldb-platform.cpp')
-rw-r--r-- | lldb/tools/lldb-server/lldb-platform.cpp | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/lldb/tools/lldb-server/lldb-platform.cpp b/lldb/tools/lldb-server/lldb-platform.cpp index ee438da..c837eea 100644 --- a/lldb/tools/lldb-server/lldb-platform.cpp +++ b/lldb/tools/lldb-server/lldb-platform.cpp @@ -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(const std::string &socket_id, 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 |