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: cfe/trunk/lib/Frontend/ASTUnit.cpp cfe/trunk/lib/Serialization/GlobalModuleIndex.cpp Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=371890&r1=371889&r2=371890&view=diff ============================================================================== --- cfe/trunk/lib/Frontend/ASTUnit.cpp (original) +++ cfe/trunk/lib/Frontend/ASTUnit.cpp Fri Sep 13 13:08:27 2019 @@ -84,6 +84,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/FileUtilities.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Timer.h" #include "llvm/Support/VirtualFileSystem.h" @@ -2301,26 +2302,19 @@ bool ASTUnit::Save(StringRef File) { SmallString<128> TempPath; TempPath = File; TempPath += "-%%%%%%%%"; - int fd; - if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath)) - return true; - // FIXME: Can we somehow regenerate the stat cache here, or do we need to // unconditionally create a stat cache when we parse the file? - llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true); - - serialize(Out); - Out.close(); - if (Out.has_error()) { - Out.clear_error(); - return true; - } - if (llvm::sys::fs::rename(TempPath, File)) { - llvm::sys::fs::remove(TempPath); + if (llvm::Error Err = llvm::writeFileAtomically( + TempPath, File, [this](llvm::raw_ostream &Out) { + return serialize(Out) ? llvm::make_error<llvm::StringError>( + "ASTUnit serialization failed", + llvm::inconvertibleErrorCode()) + : llvm::Error::success(); + })) { + consumeError(std::move(Err)); return true; } - return false; } Modified: cfe/trunk/lib/Serialization/GlobalModuleIndex.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/GlobalModuleIndex.cpp?rev=371890&r1=371889&r2=371890&view=diff ============================================================================== --- cfe/trunk/lib/Serialization/GlobalModuleIndex.cpp (original) +++ cfe/trunk/lib/Serialization/GlobalModuleIndex.cpp Fri Sep 13 13:08:27 2019 @@ -10,7 +10,6 @@ // //===----------------------------------------------------------------------===// - #include "ASTReaderInternals.h" #include "clang/Basic/FileManager.h" #include "clang/Lex/HeaderSearch.h" @@ -21,10 +20,12 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Bitstream/BitstreamReader.h" #include "llvm/Bitstream/BitstreamWriter.h" #include "llvm/Support/DJB.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/FileUtilities.h" #include "llvm/Support/LockFileManager.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/OnDiskHashTable.h" @@ -912,37 +913,9 @@ GlobalModuleIndex::writeIndex(FileManage "failed writing index"); } - // Write the global index file to a temporary file. - llvm::SmallString<128> IndexTmpPath; - int TmpFD; - if (llvm::sys::fs::createUniqueFile(IndexPath + "-%%%%%%%%", TmpFD, - IndexTmpPath)) - return llvm::createStringError(std::errc::io_error, - "failed creating unique file"); - - // Open the temporary global index file for output. - llvm::raw_fd_ostream Out(TmpFD, true); - if (Out.has_error()) - return llvm::createStringError(Out.error(), "failed outputting to stream"); - - // Write the index. - Out.write(OutputBuffer.data(), OutputBuffer.size()); - Out.close(); - if (Out.has_error()) - return llvm::createStringError(Out.error(), "failed writing to stream"); - - // Remove the old index file. It isn't relevant any more. - llvm::sys::fs::remove(IndexPath); - - // Rename the newly-written index file to the proper name. - if (std::error_code Err = llvm::sys::fs::rename(IndexTmpPath, IndexPath)) { - // Remove the file on failure, don't check whether removal succeeded. - llvm::sys::fs::remove(IndexTmpPath); - return llvm::createStringError(Err, "failed renaming file \"%s\" to \"%s\"", - IndexTmpPath.c_str(), IndexPath.c_str()); - } - - return llvm::Error::success(); + return llvm::writeFileAtomically( + (IndexPath + "-%%%%%%%%").str(), IndexPath, + llvm::StringRef(OutputBuffer.data(), OutputBuffer.size())); } namespace { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits