aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/filesystem.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/zencore/filesystem.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/zencore/filesystem.cpp')
-rw-r--r--src/zencore/filesystem.cpp10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/zencore/filesystem.cpp b/src/zencore/filesystem.cpp
index 29ec14e0c..3e94b550f 100644
--- a/src/zencore/filesystem.cpp
+++ b/src/zencore/filesystem.cpp
@@ -811,11 +811,17 @@ WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t Buffer
hRes = Outfile.Write(DataPtr, gsl::narrow_cast<uint32_t>(WriteSize));
if (FAILED(hRes))
{
+ Outfile.Close();
+ std::error_code DummyEc;
+ std::filesystem::remove(Path, DummyEc);
ThrowSystemException(hRes, fmt::format("File write failed for '{}'", Path).c_str());
}
#else
if (write(Fd, DataPtr, WriteSize) != int64_t(WriteSize))
{
+ close(Fd);
+ std::error_code DummyEc;
+ std::filesystem::remove(Path, DummyEc);
ThrowLastError(fmt::format("File write failed for '{}'", Path));
}
#endif // ZEN_PLATFORM_WINDOWS
@@ -825,7 +831,9 @@ WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t Buffer
}
}
-#if !ZEN_PLATFORM_WINDOWS
+#if ZEN_PLATFORM_WINDOWS
+ Outfile.Close();
+#else
close(Fd);
#endif
}