aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/basicfile.cpp
diff options
context:
space:
mode:
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;
}