aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/basicfile.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2025-03-10 18:33:24 +0100
committerGitHub Enterprise <[email protected]>2025-03-10 18:33:24 +0100
commit7de3d4218ee5969af6147f9ab20bda538a136d9a (patch)
tree77e10f8e3a165275bbb0bfa516eb65a854cd75ec /src/zencore/basicfile.cpp
parentpartial block fetch (#298) (diff)
downloadzen-7de3d4218ee5969af6147f9ab20bda538a136d9a.tar.xz
zen-7de3d4218ee5969af6147f9ab20bda538a136d9a.zip
pick up existing cache (#299)
- Improvement: Scavenge .zen temp folders for existing data (downloaded, decompressed or written) from previous failed run - Improvement: Faster abort during stream compression - Improvement: Try to move downloaded blobs with rename if possible avoiding an extra disk write - Improvement: Only clean temp folders on successful or cancelled build - keep it if download fails
Diffstat (limited to 'src/zencore/basicfile.cpp')
-rw-r--r--src/zencore/basicfile.cpp37
1 files changed, 24 insertions, 13 deletions
diff --git a/src/zencore/basicfile.cpp b/src/zencore/basicfile.cpp
index 6e879ca0d..95876cff4 100644
--- a/src/zencore/basicfile.cpp
+++ b/src/zencore/basicfile.cpp
@@ -858,7 +858,7 @@ BasicFileWriter::Flush()
}
IoBuffer
-WriteToTempFile(const CompositeBuffer& Buffer, const std::filesystem::path& Path)
+WriteToTempFile(CompositeBuffer&& Buffer, const std::filesystem::path& Path)
{
TemporaryFile Temp;
std::error_code Ec;
@@ -868,6 +868,7 @@ WriteToTempFile(const CompositeBuffer& Buffer, const std::filesystem::path& Path
throw std::system_error(Ec, fmt::format("Failed to create temp file for blob at '{}'", Path));
}
+ uint64_t BufferSize = Buffer.GetSize();
{
uint64_t Offset = 0;
static const uint64_t BufferingSize = 256u * 1024u;
@@ -899,21 +900,31 @@ WriteToTempFile(const CompositeBuffer& Buffer, const std::filesystem::path& Path
Temp.MoveTemporaryIntoPlace(Path, Ec);
if (Ec)
{
- IoBuffer TmpBuffer = IoBufferBuilder::MakeFromFile(Path);
- if (TmpBuffer)
+ Ec.clear();
+ BasicFile OpenTemp(Path, BasicFile::Mode::kDelete, Ec);
+ if (Ec)
{
- IoHash ExistingHash = IoHash::HashBuffer(TmpBuffer);
- const IoHash ExpectedHash = IoHash::HashBuffer(Buffer);
- if (ExistingHash == ExpectedHash)
- {
- TmpBuffer.SetDeleteOnClose(true);
- return TmpBuffer;
- }
+ throw std::system_error(Ec, fmt::format("Failed to move temp file to '{}'", Path));
}
- throw std::system_error(Ec, fmt::format("Failed to move temp file to '{}'", Path));
- }
+ if (OpenTemp.FileSize() != BufferSize)
+ {
+ throw std::runtime_error(fmt::format("Failed to move temp file to '{}' - mismatching file size already exists", Path));
+ }
+ IoBuffer TmpBuffer(IoBuffer::File, OpenTemp.Detach(), 0, BufferSize, true);
- IoBuffer TmpBuffer = IoBufferBuilder::MakeFromFile(Path);
+ IoHash ExistingHash = IoHash::HashBuffer(TmpBuffer);
+ const IoHash ExpectedHash = IoHash::HashBuffer(Buffer);
+ if (ExistingHash != ExpectedHash)
+ {
+ throw std::runtime_error(fmt::format("Failed to move temp file to '{}' - mismatching file hash already exists", Path));
+ }
+ Buffer = CompositeBuffer{};
+ TmpBuffer.SetDeleteOnClose(true);
+ return TmpBuffer;
+ }
+ Buffer = CompositeBuffer{};
+ BasicFile OpenTemp(Path, BasicFile::Mode::kDelete);
+ IoBuffer TmpBuffer(IoBuffer::File, OpenTemp.Detach(), 0, BufferSize, true);
TmpBuffer.SetDeleteOnClose(true);
return TmpBuffer;
}