aboutsummaryrefslogtreecommitdiff
path: root/src/zenstore/cas.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2024-03-13 10:33:40 +0100
committerGitHub Enterprise <[email protected]>2024-03-13 10:33:40 +0100
commit5fd1bdfafcd1e44162a1a4a978de775660c378a6 (patch)
treeb92b9cc595477075bd02320d0948c2f11e0c0717 /src/zenstore/cas.cpp
parentupdates to signing (diff)
downloadzen-5fd1bdfafcd1e44162a1a4a978de775660c378a6.tar.xz
zen-5fd1bdfafcd1e44162a1a4a978de775660c378a6.zip
fix potential partially written files (#2)
* Make sure WriteFile() does not leave incomplete files * use TemporaryFile and MoveTemporaryIntoPlace to avoid leaving partial files on error
Diffstat (limited to 'src/zenstore/cas.cpp')
-rw-r--r--src/zenstore/cas.cpp14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/zenstore/cas.cpp b/src/zenstore/cas.cpp
index f1a141ca0..4f137744b 100644
--- a/src/zenstore/cas.cpp
+++ b/src/zenstore/cas.cpp
@@ -214,9 +214,19 @@ CasImpl::UpdateManifest()
ZEN_TRACE("Writing new manifest to '{}'", ManifestPath);
- BasicFile Marker;
- Marker.Open(ManifestPath.c_str(), BasicFile::Mode::kTruncate);
+ TemporaryFile Marker;
+ std::error_code Ec;
+ Marker.CreateTemporary(ManifestPath.parent_path(), Ec);
+ if (Ec)
+ {
+ throw std::system_error(Ec, fmt::format("Failed to create temp file for cas manifest at '{}'", ManifestPath));
+ }
Marker.Write(m_ManifestObject.GetBuffer(), 0);
+ Marker.MoveTemporaryIntoPlace(ManifestPath, Ec);
+ if (Ec)
+ {
+ throw std::system_error(Ec, fmt::format("Failed to move temp file '{}' to '{}'", Marker.GetPath(), ManifestPath));
+ }
}
CasStore::InsertResult