feat(System): transactional file replacement on Windows x64 (#3)
- replace_file/atomic_write gain a Win32 implementation: exclusive CREATE_NEW staging in the destination directory, WriteFile loop, FlushFileBuffers for durable_sync, MoveFileExW(MOVEFILE_REPLACE_EXISTING [| MOVEFILE_WRITE_THROUGH]) for atomic publication and a parent-directory flush for durability. - read_file_bounded/write_file implemented on Windows so the contract is usable and testable there. - Documented Windows guarantees per durability level, including the application-crash vs power-loss distinction and post-rename failure reporting (sync_parent + visible_on_disk + durability_unknown). - Added a portable contract test (first creation, replacement, Unicode name, pre-publication faults preserving old contents, post-publication fault reporting, no staging leftovers, live destination never deleted) and a concurrent-writer atomicity test.
This commit is contained in:
1 parent
7b0fea6095
commit
51ceb081ac
2 files changed
+739
-20
No files matched your search
@@ -3,8 +3,17 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
Void::Testing::Test read_file_returns_contents()
|
||||
{
|
||||
@@ -212,3 +221,196 @@ Void::Testing::Test filesystem_file_lock_symlink_swap_reflink_and_timestamps()
|
||||
kLinkMtime);
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
/// Portable scoped temp directory (TempDir is POSIX-only today, but the
|
||||
/// replace_file contract must also run on Windows x64).
|
||||
struct ScopedTestDir
|
||||
{
|
||||
std::filesystem::path path;
|
||||
|
||||
explicit ScopedTestDir(std::string_view label)
|
||||
{
|
||||
path = std::filesystem::temp_directory_path() /
|
||||
std::format("void_path_{}_{:016x}",
|
||||
label,
|
||||
Void::FileSystem::detail::next_temp_entropy());
|
||||
std::filesystem::create_directories(path);
|
||||
}
|
||||
|
||||
~ScopedTestDir()
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(path, ec);
|
||||
}
|
||||
|
||||
ScopedTestDir(const ScopedTestDir&) = delete;
|
||||
ScopedTestDir& operator=(const ScopedTestDir&) = delete;
|
||||
};
|
||||
|
||||
[[nodiscard]] std::string slurp(const std::filesystem::path& path)
|
||||
{
|
||||
std::ifstream in{path, std::ios::binary};
|
||||
return std::string{std::istreambuf_iterator<char>{in},
|
||||
std::istreambuf_iterator<char>{}};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t entry_count(const std::filesystem::path& dir)
|
||||
{
|
||||
std::size_t count = 0;
|
||||
for (const auto& entry : std::filesystem::directory_iterator{dir}) {
|
||||
(void)entry;
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void seed(const std::filesystem::path& path, std::string_view content)
|
||||
{
|
||||
std::ofstream out{path, std::ios::binary | std::ios::trunc};
|
||||
out.write(content.data(), static_cast<std::streamsize>(content.size()));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// Contract for replace_file/atomic_write that must hold on every supported
|
||||
// platform: pre-publication failures preserve the old file, post-publication
|
||||
// failures report visibility + unknown durability, and staging files never
|
||||
// outlive a call or endanger the live destination.
|
||||
Void::Testing::Test replace_file_contract_durability_and_faults()
|
||||
{
|
||||
ScopedTestDir dir{"contract"};
|
||||
const auto target = dir.path / "slot.bin";
|
||||
|
||||
// First creation: there is no previous destination.
|
||||
{
|
||||
auto r = Void::FileSystem::replace_file(target, std::string_view{"first"});
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(slurp(target), std::string{"first"});
|
||||
Void::Testing::expect_eq(entry_count(dir.path), std::size_t{1});
|
||||
}
|
||||
|
||||
// Replacement of an existing destination.
|
||||
{
|
||||
auto r = Void::FileSystem::replace_file(target, std::string_view{"second"});
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(slurp(target), std::string{"second"});
|
||||
}
|
||||
|
||||
// Failure while staging (before publication): old contents stay readable
|
||||
// and intact, and the staging file is cleaned up.
|
||||
{
|
||||
seed(target, "A");
|
||||
Void::FileSystem::ReplaceOptions opts;
|
||||
opts.fault_hook = [](Void::FileSystem::ReplacePhase phase)
|
||||
-> std::optional<int> {
|
||||
if (phase == Void::FileSystem::ReplacePhase::write_stage) {
|
||||
return EIO;
|
||||
}
|
||||
return std::nullopt;
|
||||
};
|
||||
auto r =
|
||||
Void::FileSystem::replace_file(target, std::string_view{"B"}, opts);
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect(r.error().phase ==
|
||||
Void::FileSystem::ReplacePhase::write_stage);
|
||||
Void::Testing::expect(!r.error().visible_on_disk);
|
||||
Void::Testing::expect(!r.error().durability_unknown);
|
||||
Void::Testing::expect_eq(slurp(target), std::string{"A"});
|
||||
Void::Testing::expect_eq(entry_count(dir.path), std::size_t{1});
|
||||
}
|
||||
|
||||
// Failure injected immediately before the publish operation: the old file
|
||||
// must remain completely intact.
|
||||
{
|
||||
seed(target, "A");
|
||||
Void::FileSystem::ReplaceOptions opts;
|
||||
opts.fault_hook = [](Void::FileSystem::ReplacePhase phase)
|
||||
-> std::optional<int> {
|
||||
if (phase == Void::FileSystem::ReplacePhase::rename) {
|
||||
return EIO;
|
||||
}
|
||||
return std::nullopt;
|
||||
};
|
||||
auto r =
|
||||
Void::FileSystem::replace_file(target, std::string_view{"B"}, opts);
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect(r.error().phase ==
|
||||
Void::FileSystem::ReplacePhase::rename);
|
||||
Void::Testing::expect(!r.error().visible_on_disk);
|
||||
Void::Testing::expect(!r.error().durability_unknown);
|
||||
Void::Testing::expect_eq(slurp(target), std::string{"A"});
|
||||
Void::Testing::expect_eq(entry_count(dir.path), std::size_t{1});
|
||||
}
|
||||
|
||||
// Failure after visibility changed: the new file is visible, durability is
|
||||
// explicitly unknown, and the live destination is never deleted.
|
||||
{
|
||||
seed(target, "A");
|
||||
Void::FileSystem::ReplaceOptions opts;
|
||||
opts.fault_hook = [](Void::FileSystem::ReplacePhase phase)
|
||||
-> std::optional<int> {
|
||||
if (phase == Void::FileSystem::ReplacePhase::sync_parent) {
|
||||
return EIO;
|
||||
}
|
||||
return std::nullopt;
|
||||
};
|
||||
auto r =
|
||||
Void::FileSystem::replace_file(target, std::string_view{"B"}, opts);
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect(r.error().phase ==
|
||||
Void::FileSystem::ReplacePhase::sync_parent);
|
||||
Void::Testing::expect(r.error().visible_on_disk);
|
||||
Void::Testing::expect(r.error().durability_unknown);
|
||||
Void::Testing::expect_eq(slurp(target), std::string{"B"});
|
||||
Void::Testing::expect_eq(entry_count(dir.path), std::size_t{1});
|
||||
}
|
||||
|
||||
// atomic_write is the convenience wrapper over the same publication path.
|
||||
{
|
||||
auto r = Void::FileSystem::atomic_write(target, "via atomic_write");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(slurp(target), std::string{"via atomic_write"});
|
||||
}
|
||||
|
||||
// Unicode destination name (UTF-8 on POSIX, UTF-16 on Windows).
|
||||
{
|
||||
const auto unicode = dir.path / std::filesystem::path{u8"слот-δ.txt"};
|
||||
auto r =
|
||||
Void::FileSystem::replace_file(unicode, std::string_view{"unicode"});
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(slurp(unicode), std::string{"unicode"});
|
||||
}
|
||||
}
|
||||
|
||||
// Concurrent writers must never expose partial contents and must not leave
|
||||
// staging files behind (exclusive temporary names + atomic publication).
|
||||
Void::Testing::Test replace_file_concurrent_writers_are_atomic()
|
||||
{
|
||||
ScopedTestDir dir{"concurrent"};
|
||||
const auto target = dir.path / "slot.bin";
|
||||
seed(target, "initial");
|
||||
|
||||
std::vector<std::thread> writers;
|
||||
for (int t = 0; t < 4; ++t) {
|
||||
writers.emplace_back([&target, t] {
|
||||
const std::string payload(4096,
|
||||
static_cast<char>('a' + t));
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
(void)Void::FileSystem::replace_file(
|
||||
target, std::string_view{payload});
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& writer : writers) {
|
||||
writer.join();
|
||||
}
|
||||
|
||||
const std::string content = slurp(target);
|
||||
Void::Testing::expect_eq(content.size(), std::size_t{4096});
|
||||
Void::Testing::expect(content.find_first_not_of(content.front()) ==
|
||||
std::string::npos);
|
||||
Void::Testing::expect_eq(entry_count(dir.path), std::size_t{1});
|
||||
}
|
||||
Reference in new issue
Block a user