From f69c91780fbb0e9c0e95f70a079f578efdca0bfa Mon Sep 17 00:00:00 2001 From: Jan Korous Date: Fri, 13 Sep 2019 20:08:27 +0000 Subject: [Support] Add overload writeFileAtomically(std::function Writer) Differential Revision: https://reviews.llvm.org/D67424 llvm-svn: 371890 --- llvm/lib/Support/FileUtilities.cpp | 63 ++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 16 deletions(-) (limited to 'llvm/lib/Support/FileUtilities.cpp') diff --git a/llvm/lib/Support/FileUtilities.cpp b/llvm/lib/Support/FileUtilities.cpp index b933692..d11fbb5 100644 --- a/llvm/lib/Support/FileUtilities.cpp +++ b/llvm/lib/Support/FileUtilities.cpp @@ -12,7 +12,9 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/FileUtilities.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/SmallString.h" +#include "llvm/Support/Error.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" @@ -266,36 +268,65 @@ int llvm::DiffFilesWithTolerance(StringRef NameA, return CompareFailed; } -Error llvm::writeFileAtomically(StringRef TempPathModel, StringRef FinalPath, - StringRef Buffer) { +void llvm::AtomicFileWriteError::log(raw_ostream &OS) const { + OS << "atomic_write_error: "; + switch (Error) { + case atomic_write_error::failed_to_create_uniq_file: + OS << "failed_to_create_uniq_file"; + return; + case atomic_write_error::output_stream_error: + OS << "output_stream_error"; + return; + case atomic_write_error::failed_to_rename_temp_file: + OS << "failed_to_rename_temp_file"; + return; + } + llvm_unreachable("unknown atomic_write_error value in " + "failed_to_rename_temp_file::log()"); +} + +llvm::Error llvm::writeFileAtomically(StringRef TempPathModel, + StringRef FinalPath, StringRef Buffer) { + return writeFileAtomically(TempPathModel, FinalPath, + [&Buffer](llvm::raw_ostream &OS) { + OS.write(Buffer.data(), Buffer.size()); + return llvm::Error::success(); + }); +} + +llvm::Error llvm::writeFileAtomically( + StringRef TempPathModel, StringRef FinalPath, + std::function Writer) { 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()); + if (sys::fs::createUniqueFile(TempPathModel.str(), TempFD, + GeneratedUniqPath)) { + return llvm::make_error( + atomic_write_error::failed_to_create_uniq_file); } + llvm::FileRemover RemoveTmpFileOnFail(GeneratedUniqPath); raw_fd_ostream OS(TempFD, /*shouldClose=*/true); - OS.write(Buffer.data(), Buffer.size()); - OS.close(); - TempFD = -1; + if (llvm::Error Err = Writer(OS)) { + return Err; + } + OS.close(); 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()); + return llvm::make_error( + atomic_write_error::output_stream_error); } 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 llvm::make_error( + atomic_write_error::failed_to_rename_temp_file); } + RemoveTmpFileOnFail.releaseFile(); return Error::success(); } + +char llvm::AtomicFileWriteError::ID; -- cgit v1.1