The Windows read_file_bounded path was implemented together with #3 using a single CreateFileW handle. This adds the #9 acceptance coverage and documents the contract: - The size is checked before allocation and every chunk is re-checked, so growth after the initial size check still fails with limit_exceeded and never returns partial contents. - Windows shares read/write/delete, so another process may append, rename or replace the path while a background read is pending; the read continues from the opened handle and sees one coherent version. - Tests: empty, binary, Unicode path, exact limit, over limit before reading, and (POSIX) a stream that grows past the limit mid-read.
493 lines
16 KiB
C++
493 lines
16 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});
|
|
}
|
|
|
|
// Issue #9: read_file_bounded uses one handle, checks the cap before
|
|
// allocation and again on every chunk, and handles empty/binary/Unicode paths.
|
|
Void::Testing::Test read_file_bounded_handles_empty_binary_and_limits()
|
|
{
|
|
ScopedTestDir dir{"bounded"};
|
|
|
|
const auto empty = dir.path / "empty.bin";
|
|
seed(empty, "");
|
|
auto empty_result = Void::FileSystem::read_file_bounded(empty);
|
|
Void::Testing::require(empty_result.has_value());
|
|
Void::Testing::expect(empty_result->empty());
|
|
|
|
const std::string binary{"\x00\x01\x02\xff\x00", 5};
|
|
const auto binary_path = dir.path / "binary.bin";
|
|
seed(binary_path, binary);
|
|
auto binary_result = Void::FileSystem::read_file_bounded(binary_path);
|
|
Void::Testing::require(binary_result.has_value());
|
|
Void::Testing::expect_eq(*binary_result, binary);
|
|
|
|
const auto unicode = dir.path / std::filesystem::path{u8"данные-δ.bin"};
|
|
seed(unicode, "unicode payload");
|
|
auto unicode_result = Void::FileSystem::read_file_bounded(unicode);
|
|
Void::Testing::require(unicode_result.has_value());
|
|
Void::Testing::expect_eq(*unicode_result, std::string{"unicode payload"});
|
|
|
|
const auto data = dir.path / "data.bin";
|
|
seed(data, std::string(16, 'x'));
|
|
|
|
// Exactly at the limit is accepted.
|
|
auto exact = Void::FileSystem::read_file_bounded(data, {.max_bytes = 16});
|
|
Void::Testing::require(exact.has_value());
|
|
Void::Testing::expect_eq(exact->size(), std::size_t{16});
|
|
|
|
// One byte over is rejected by the pre-allocation size check.
|
|
auto over = Void::FileSystem::read_file_bounded(data, {.max_bytes = 15});
|
|
Void::Testing::require(!over.has_value());
|
|
Void::Testing::expect(over.error().message().find("limit") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
// A stream that keeps producing data after open must be cut off by the
|
|
// per-chunk check even though it has no initial size to validate.
|
|
Void::Testing::Test read_file_bounded_rejects_growth_past_limit()
|
|
{
|
|
ScopedTestDir dir{"bounded_growth"};
|
|
const auto fifo = dir.path / "stream";
|
|
Void::Testing::require(::mkfifo(fifo.c_str(), 0600) == 0);
|
|
|
|
std::thread writer{[&fifo] {
|
|
const int fd = ::open(fifo.c_str(), O_WRONLY);
|
|
if (fd < 0) {
|
|
return;
|
|
}
|
|
const std::string payload(64, 'z');
|
|
std::size_t offset = 0;
|
|
while (offset < payload.size()) {
|
|
const ssize_t written =
|
|
::write(fd, payload.data() + offset, payload.size() - offset);
|
|
if (written <= 0) {
|
|
break;
|
|
}
|
|
offset += static_cast<std::size_t>(written);
|
|
}
|
|
(void)::close(fd);
|
|
}};
|
|
|
|
auto result = Void::FileSystem::read_file_bounded(fifo, {.max_bytes = 8});
|
|
writer.join();
|
|
|
|
Void::Testing::require(!result.has_value());
|
|
Void::Testing::expect(result.error().message().find("limit") !=
|
|
std::string::npos);
|
|
}
|
|
#endif
|