- 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.
417 lines
13 KiB
C++
417 lines
13 KiB
C++
#include <Void/System/Path.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#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()
|
|
{
|
|
Void::FileSystem::TempDir tmp;
|
|
const auto path = tmp / "data.txt";
|
|
{
|
|
std::ofstream f{path};
|
|
f << "hello\nworld";
|
|
}
|
|
auto r = Void::FileSystem::read_file(path);
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(*r, std::string{"hello\nworld"});
|
|
}
|
|
|
|
Void::Testing::Test read_file_fails_on_missing()
|
|
{
|
|
auto r = Void::FileSystem::read_file("/this/path/does/not/exist");
|
|
Void::Testing::expect(!r.has_value());
|
|
Void::Testing::expect(r.error().message().find("read_file") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
Void::Testing::Test write_file_creates_and_overwrites()
|
|
{
|
|
Void::FileSystem::TempDir tmp;
|
|
const auto path = tmp / "out.txt";
|
|
|
|
auto r1 = Void::FileSystem::write_file(path, "first");
|
|
Void::Testing::expect(r1.has_value());
|
|
|
|
auto r2 = Void::FileSystem::write_file(path, "second");
|
|
Void::Testing::expect(r2.has_value());
|
|
|
|
auto read_back = Void::FileSystem::read_file(path);
|
|
Void::Testing::expect(read_back.has_value());
|
|
Void::Testing::expect_eq(*read_back, std::string{"second"});
|
|
}
|
|
|
|
Void::Testing::Test atomic_write_replaces_target()
|
|
{
|
|
Void::FileSystem::TempDir tmp;
|
|
const auto path = tmp / "snapshot.json";
|
|
|
|
{
|
|
std::ofstream f{path};
|
|
f << "old contents";
|
|
}
|
|
|
|
auto r = Void::FileSystem::atomic_write(path, "new contents");
|
|
Void::Testing::expect(r.has_value());
|
|
|
|
auto read_back = Void::FileSystem::read_file(path);
|
|
Void::Testing::expect(read_back.has_value());
|
|
Void::Testing::expect_eq(*read_back, std::string{"new contents"});
|
|
|
|
// Staging file should not be left behind on success.
|
|
auto staging = path;
|
|
staging += ".tmp";
|
|
Void::Testing::expect(!std::filesystem::exists(staging));
|
|
}
|
|
|
|
Void::Testing::Test ensure_directory_creates_nested_dirs()
|
|
{
|
|
Void::FileSystem::TempDir tmp;
|
|
const auto nested = tmp / "a/b/c/d";
|
|
|
|
auto r = Void::FileSystem::ensure_directory(nested);
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect(std::filesystem::is_directory(nested));
|
|
|
|
// Idempotent — second call still succeeds.
|
|
auto r2 = Void::FileSystem::ensure_directory(nested);
|
|
Void::Testing::expect(r2.has_value());
|
|
}
|
|
|
|
Void::Testing::Test remove_path_handles_missing_and_recursive()
|
|
{
|
|
// Missing path is a no-op (success).
|
|
auto r1 = Void::FileSystem::remove_path("/nonexistent/path");
|
|
Void::Testing::expect(r1.has_value());
|
|
|
|
// Recursive removal.
|
|
Void::FileSystem::TempDir tmp;
|
|
const auto nested = tmp / "a/b";
|
|
(void)Void::FileSystem::ensure_directory(nested);
|
|
(void)Void::FileSystem::write_file(nested / "leaf.txt", "x");
|
|
|
|
auto r2 = Void::FileSystem::remove_path(tmp.path() / "a");
|
|
Void::Testing::expect(r2.has_value());
|
|
Void::Testing::expect(!std::filesystem::exists(tmp.path() / "a"));
|
|
}
|
|
|
|
Void::Testing::Test filesystem_file_lock_symlink_swap_reflink_and_timestamps()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_fs_vl010_"};
|
|
|
|
// 1. RAII advisory FileLock (shared & exclusive, blocking & try_lock)
|
|
{
|
|
const auto lock_path = tmp / "resource.lock";
|
|
auto ex = Void::FileSystem::FileLock::lock(
|
|
lock_path, Void::FileSystem::LockMode::Exclusive);
|
|
Void::Testing::require(ex.has_value());
|
|
Void::Testing::expect(ex->locked());
|
|
|
|
auto contended_ex = Void::FileSystem::FileLock::try_lock(
|
|
lock_path, Void::FileSystem::LockMode::Exclusive);
|
|
Void::Testing::require(contended_ex.has_value());
|
|
Void::Testing::expect(!contended_ex->has_value());
|
|
|
|
auto contended_sh = Void::FileSystem::FileLock::try_lock(
|
|
lock_path, Void::FileSystem::LockMode::Shared);
|
|
Void::Testing::require(contended_sh.has_value());
|
|
Void::Testing::expect(!contended_sh->has_value());
|
|
|
|
ex->unlock();
|
|
Void::Testing::expect(!ex->locked());
|
|
|
|
auto sh1 = Void::FileSystem::FileLock::try_lock(
|
|
lock_path, Void::FileSystem::LockMode::Shared);
|
|
auto sh2 = Void::FileSystem::FileLock::try_lock(
|
|
lock_path, Void::FileSystem::LockMode::Shared);
|
|
Void::Testing::require(sh1.has_value() && sh1->has_value());
|
|
Void::Testing::require(sh2.has_value() && sh2->has_value());
|
|
}
|
|
|
|
// 2. Atomic symlink swap over missing, existing, and dangling links
|
|
{
|
|
const auto link_path = tmp / "current";
|
|
Void::Testing::require(
|
|
Void::FileSystem::atomic_symlink_swap(link_path, "v1")
|
|
.has_value());
|
|
Void::Testing::expect_eq(
|
|
std::filesystem::read_symlink(link_path).string(),
|
|
std::string{"v1"});
|
|
|
|
Void::Testing::require(
|
|
Void::FileSystem::atomic_symlink_swap(link_path, "v2")
|
|
.has_value());
|
|
Void::Testing::expect_eq(
|
|
std::filesystem::read_symlink(link_path).string(),
|
|
std::string{"v2"});
|
|
}
|
|
|
|
// 3. reflink_copy_file isolates copied file (never hardlinked)
|
|
{
|
|
const auto src = tmp / "origin.bin";
|
|
const auto dst = tmp / "cloned.bin";
|
|
const std::string payload(128ULL * 1024ULL, 'K');
|
|
Void::Testing::require(
|
|
Void::FileSystem::write_file(src, payload).has_value());
|
|
|
|
auto cp = Void::FileSystem::reflink_copy_file(src, dst);
|
|
Void::Testing::require(cp.has_value());
|
|
Void::Testing::expect_eq(cp->bytes, payload.size());
|
|
Void::Testing::expect_eq(
|
|
Void::FileSystem::read_file(dst).value(), payload);
|
|
|
|
struct stat st_src {};
|
|
struct stat st_dst {};
|
|
Void::Testing::require(::lstat(src.c_str(), &st_src) == 0);
|
|
Void::Testing::require(::lstat(dst.c_str(), &st_dst) == 0);
|
|
Void::Testing::expect(st_src.st_ino != st_dst.st_ino);
|
|
Void::Testing::expect_eq(st_src.st_nlink, nlink_t{1});
|
|
Void::Testing::expect_eq(st_dst.st_nlink, nlink_t{1});
|
|
|
|
// overwrite == false must fail cleanly when dst already exists
|
|
Void::FileSystem::CopyOptions no_overwrite;
|
|
no_overwrite.overwrite = false;
|
|
Void::Testing::expect(
|
|
!Void::FileSystem::reflink_copy_file(src, dst, no_overwrite)
|
|
.has_value());
|
|
|
|
// Mutating dst must never alter src
|
|
Void::Testing::require(
|
|
Void::FileSystem::write_file(dst, "mutated").has_value());
|
|
Void::Testing::expect_eq(
|
|
Void::FileSystem::read_file(src).value(), payload);
|
|
}
|
|
|
|
// 4. Canonical timestamp setter with AT_SYMLINK_NOFOLLOW
|
|
{
|
|
const auto target = tmp / "real.txt";
|
|
const auto link = tmp / "alias.txt";
|
|
Void::Testing::require(
|
|
Void::FileSystem::write_file(target, "data").has_value());
|
|
Void::Testing::require(
|
|
Void::FileSystem::atomic_symlink_swap(link, target).has_value());
|
|
|
|
constexpr std::int64_t kTargetMtime = 1'700'000'000LL;
|
|
constexpr std::int64_t kLinkMtime = 1'600'000'123LL;
|
|
Void::Testing::require(
|
|
Void::FileSystem::set_canonical_mtime(target, kTargetMtime)
|
|
.has_value());
|
|
Void::Testing::require(
|
|
Void::FileSystem::set_canonical_mtime(link, kLinkMtime)
|
|
.has_value());
|
|
|
|
struct stat st_target {};
|
|
struct stat st_link {};
|
|
Void::Testing::require(::lstat(target.c_str(), &st_target) == 0);
|
|
Void::Testing::require(::lstat(link.c_str(), &st_link) == 0);
|
|
Void::Testing::expect_eq(static_cast<std::int64_t>(st_target.st_mtim.tv_sec),
|
|
kTargetMtime);
|
|
Void::Testing::expect_eq(static_cast<std::int64_t>(st_link.st_mtim.tv_sec),
|
|
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});
|
|
}
|