diff options
author | Zachary Turner <zturner@google.com> | 2018-06-28 18:49:09 +0000 |
---|---|---|
committer | Zachary Turner <zturner@google.com> | 2018-06-28 18:49:09 +0000 |
commit | 1adca7c4a5119fb79757a5a9635c9e01ec996ced (patch) | |
tree | e1f12ba1e6575501af5af7abccb1e285e08aad3d /llvm/lib/Support/Path.cpp | |
parent | c09b5e31d778ad6cb96b5cbc8d1153e5b9b23cf8 (diff) | |
download | bcm5719-llvm-1adca7c4a5119fb79757a5a9635c9e01ec996ced.tar.gz bcm5719-llvm-1adca7c4a5119fb79757a5a9635c9e01ec996ced.zip |
Add a flag to FileOutputBuffer that allows modification.
FileOutputBuffer creates a temp file and on commit atomically
renames the temp file to the destination file. Sometimes we
want to modify an existing file in place, but still have the
atomicity guarantee. To do this we can initialize the contents
of the temp file from the destination file (if it exists), that
way the resulting FileOutputBuffer can have only selective
bytes modified. Committing will then atomically replace the
destination file as desired.
llvm-svn: 335902
Diffstat (limited to 'llvm/lib/Support/Path.cpp')
-rw-r--r-- | llvm/lib/Support/Path.cpp | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index cc507874b00..4a15ebd3f55 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -927,16 +927,7 @@ std::error_code create_directories(const Twine &Path, bool IgnoreExisting, return create_directory(P, IgnoreExisting, Perms); } -std::error_code copy_file(const Twine &From, const Twine &To) { - int ReadFD, WriteFD; - if (std::error_code EC = openFileForRead(From, ReadFD, OF_None)) - return EC; - if (std::error_code EC = - openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) { - close(ReadFD); - return EC; - } - +static std::error_code copy_file_internal(int ReadFD, int WriteFD) { const size_t BufSize = 4096; char *Buf = new char[BufSize]; int BytesRead = 0, BytesWritten = 0; @@ -953,8 +944,6 @@ std::error_code copy_file(const Twine &From, const Twine &To) { if (BytesWritten < 0) break; } - close(ReadFD); - close(WriteFD); delete[] Buf; if (BytesRead < 0 || BytesWritten < 0) @@ -962,6 +951,36 @@ std::error_code copy_file(const Twine &From, const Twine &To) { return std::error_code(); } +std::error_code copy_file(const Twine &From, const Twine &To) { + int ReadFD, WriteFD; + if (std::error_code EC = openFileForRead(From, ReadFD, OF_None)) + return EC; + if (std::error_code EC = + openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) { + close(ReadFD); + return EC; + } + + std::error_code EC = copy_file_internal(ReadFD, WriteFD); + + close(ReadFD); + close(WriteFD); + + return EC; +} + +std::error_code copy_file(const Twine &From, int ToFD) { + int ReadFD; + if (std::error_code EC = openFileForRead(From, ReadFD, OF_None)) + return EC; + + std::error_code EC = copy_file_internal(ReadFD, ToFD); + + close(ReadFD); + + return EC; +} + ErrorOr<MD5::MD5Result> md5_contents(int FD) { MD5 Hash; |