diff options
| author | Dan Engelbrecht <[email protected]> | 2024-05-02 17:01:09 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-05-02 17:01:09 +0200 |
| commit | 375fa71cb816acb25bd2eaf24ef5cc292a1f2c36 (patch) | |
| tree | 7d8e20b99bd65af37f75cb95f18c0c0001e58dcd /src/zenutil/basicfile.cpp | |
| parent | batch cache put (#67) (diff) | |
| download | zen-375fa71cb816acb25bd2eaf24ef5cc292a1f2c36.tar.xz zen-375fa71cb816acb25bd2eaf24ef5cc292a1f2c36.zip | |
use write and move in place for safer writing of files (#70)
* use write and move in place for safer writing of files
Diffstat (limited to 'src/zenutil/basicfile.cpp')
| -rw-r--r-- | src/zenutil/basicfile.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/zenutil/basicfile.cpp b/src/zenutil/basicfile.cpp index f553fe5a0..d837c2caf 100644 --- a/src/zenutil/basicfile.cpp +++ b/src/zenutil/basicfile.cpp @@ -560,6 +560,26 @@ TemporaryFile::MoveTemporaryIntoPlace(std::filesystem::path FinalFileName, std:: ////////////////////////////////////////////////////////////////////////// +void +TemporaryFile::SafeWriteFile(std::filesystem::path Path, MemoryView Data) +{ + TemporaryFile TempFile; + std::error_code Ec; + TempFile.CreateTemporary(Path.parent_path(), Ec); + if (Ec) + { + throw std::system_error(Ec, fmt::format("Failed to create temp file for file at '{}'", Path)); + } + TempFile.Write(Data, 0); + TempFile.MoveTemporaryIntoPlace(Path, Ec); + if (Ec) + { + throw std::system_error(Ec, fmt::format("Failed to move temp file '{}' to '{}'", TempFile.GetPath(), Path)); + } +} + +////////////////////////////////////////////////////////////////////////// + LockFile::LockFile() { } |