feat(system,config,sqlite): implement VL-001, VL-003, VL-010, VL-011

This commit is contained in:
faumaray committed 2026-11-26 20:45:52 +03:00
1 parent adfc8548dc
commit b50131f52e
14 files changed
+2660 -139

No files matched your search

+54
View File
@@ -194,6 +194,57 @@ int mode_ignore_sigterm(int argc, char** argv)
return 0;
}
int mode_grandchild_ignore_sigterm(int argc, char** argv)
{
if (argc < 3) {
return usage_error("grandchild-ignore-sigterm requires <marker_file>");
}
const char* marker_path = argv[2];
int ready_p[2]{-1, -1};
if (::pipe(ready_p) != 0) {
return 1;
}
const pid_t gc = ::fork();
if (gc < 0) {
(void)::close(ready_p[0]);
(void)::close(ready_p[1]);
return 1;
}
if (gc == 0) {
(void)::close(ready_p[0]);
std::signal(SIGTERM, SIG_IGN);
const int mfd = ::open(marker_path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644);
if (mfd < 0) {
(void)::close(ready_p[1]);
::_exit(1);
}
static constexpr char kTick[] = "tick\n";
(void)write_all_fd(mfd, kTick, sizeof(kTick) - 1);
static constexpr char kReady[] = "ready\n";
(void)write_all_fd(STDOUT_FILENO, kReady, sizeof(kReady) - 1);
const char one = '1';
(void)write_all_fd(ready_p[1], &one, 1);
(void)::close(ready_p[1]);
(void)::close(STDIN_FILENO);
(void)::close(STDOUT_FILENO);
(void)::close(STDERR_FILENO);
for (int iter = 0; iter < 500; ++iter) {
std::this_thread::sleep_for(std::chrono::milliseconds{10});
(void)write_all_fd(mfd, kTick, sizeof(kTick) - 1);
}
(void)::close(mfd);
::_exit(0);
}
(void)::close(ready_p[1]);
char dummy = 0;
bool eof = false;
(void)read_exact_fd(ready_p[0], &dummy, 1, eof);
(void)::close(ready_p[0]);
::_exit(0);
}
int mode_simultaneous_io(int argc, char** argv)
{
if (argc != 3) {
@@ -485,6 +536,9 @@ int main(int argc, char** argv)
if (mode == "ignore-sigterm") {
return mode_ignore_sigterm(argc, argv);
}
if (mode == "grandchild-ignore-sigterm") {
return mode_grandchild_ignore_sigterm(argc, argv);
}
if (mode == "simultaneous-io") {
return mode_simultaneous_io(argc, argv);
}
@@ -187,3 +187,89 @@ Void::Testing::Test argparse_help_contains_names()
Void::Testing::expect(h.find("--output") != std::string::npos);
Void::Testing::expect(h.find("A description.") != std::string::npos);
}
Void::Testing::Test argparse_subcommands_option_many_and_distinct_outcomes()
{
bool verbose = false;
std::vector<std::string> targets;
std::vector<std::string> profiles;
std::string compdb_mode;
bool build_selected = false;
bool use_selected = false;
Parser root{"vbs", "Void Build System"};
root.flag("--verbose", "-v", "verbose output", verbose);
auto& build_cmd =
root.subcommand("build", "Build project", [&] { build_selected = true; });
build_cmd.option_many("--target", "-t", "targets", targets);
build_cmd.option_many("--profile", "-p", "profiles", profiles);
auto& compdb_cmd = root.subcommand("compdb", "Compile database commands");
auto& use_cmd =
compdb_cmd.subcommand("use", "Select compdb", [&] { use_selected = true; });
use_cmd.positional("mode", "compdb mode", compdb_mode);
// 1. Subcommand + repeatable option_many
{
const Args a{{"vbs", "-v", "build", "--target", "app", "--target=lib",
"-p", "debug", "--profile", "release"}};
const auto res = root.parse_detailed(a.argc, a.argv);
Void::Testing::require(res.ok());
Void::Testing::expect(verbose);
Void::Testing::expect(build_selected);
Void::Testing::expect(!use_selected);
Void::Testing::expect_eq(res.matched_command_path.size(), std::size_t{1});
Void::Testing::expect_eq(res.matched_command_path[0],
std::string{"build"});
Void::Testing::expect_eq(targets.size(), std::size_t{2});
Void::Testing::expect_eq(targets[0], std::string{"app"});
Void::Testing::expect_eq(targets[1], std::string{"lib"});
Void::Testing::expect_eq(profiles.size(), std::size_t{2});
Void::Testing::expect_eq(profiles[0], std::string{"debug"});
Void::Testing::expect_eq(profiles[1], std::string{"release"});
}
// 2. Nested subcommands ("vbs compdb use active")
{
build_selected = false;
use_selected = false;
const Args a{{"vbs", "compdb", "use", "active"}};
const auto res = root.parse_detailed(a.argc, a.argv);
Void::Testing::require(res.ok());
Void::Testing::expect(!build_selected);
Void::Testing::expect(use_selected);
Void::Testing::expect_eq(res.matched_command_path.size(), std::size_t{2});
Void::Testing::expect_eq(res.matched_command_path[0],
std::string{"compdb"});
Void::Testing::expect_eq(res.matched_command_path[1], std::string{"use"});
Void::Testing::expect_eq(compdb_mode, std::string{"active"});
}
// 3. Help is ParseOutcome::Help (distinct from ParseOutcome::Error)
{
const Args a{{"vbs", "--help"}};
const auto res = root.parse_detailed(a.argc, a.argv);
Void::Testing::expect(res.is_help());
Void::Testing::expect(!res.is_error());
Void::Testing::expect(res.message.find("Commands:") != std::string::npos);
const Args sub_help{{"vbs", "build", "--help"}};
const auto sres = root.parse_detailed(sub_help.argc, sub_help.argv);
Void::Testing::expect(sres.is_help());
Void::Testing::expect(sres.message.find("--target") != std::string::npos);
}
// 4. Missing or unknown subcommand is ParseOutcome::Error without running callbacks
{
build_selected = false;
const Args missing_sub{{"vbs"}};
const auto mres = root.parse_detailed(missing_sub.argc, missing_sub.argv);
Void::Testing::expect(mres.is_error());
const Args bad_sub{{"vbs", "unknowncmd"}};
const auto bres = root.parse_detailed(bad_sub.argc, bad_sub.argv);
Void::Testing::expect(bres.is_error());
Void::Testing::expect(!build_selected);
}
}
@@ -0,0 +1,176 @@
#include <Void/System/DirectoryWatcher.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <chrono>
#include <filesystem>
#include <string>
#include <vector>
namespace
{
[[nodiscard]] std::vector<Void::FileSystem::DirectoryEvent>
collect_events(Void::FileSystem::DirectoryWatcher& watcher,
std::chrono::milliseconds budget)
{
std::vector<Void::FileSystem::DirectoryEvent> all;
const auto deadline = std::chrono::steady_clock::now() + budget;
while (std::chrono::steady_clock::now() < deadline) {
auto batch = watcher.poll(std::chrono::milliseconds{40});
Void::Testing::require(batch.has_value());
for (auto& ev : *batch) {
all.push_back(std::move(ev));
}
if (!all.empty() && batch->empty()) {
break;
}
}
return all;
}
} // namespace
Void::Testing::Test directory_watcher_tracks_recursive_trees_ignores_and_renames()
{
Void::FileSystem::TempDir tmp{"void_dir_watch_"};
const auto ignored_dir = tmp / ".git";
Void::Testing::require(
Void::FileSystem::ensure_directory(ignored_dir).has_value());
Void::FileSystem::DirectoryWatcherOptions opts;
opts.ignore = [](const std::filesystem::path& p, bool) {
for (const auto& part : p) {
if (part == ".git" || part == "build") {
return true;
}
}
return false;
};
auto w_res = Void::FileSystem::DirectoryWatcher::create(tmp.path(), opts);
Void::Testing::require(w_res.has_value());
auto watcher = std::move(*w_res);
Void::Testing::expect_eq(watcher.watch_count(), std::size_t{1});
// Ignored subtree changes must produce zero events
Void::Testing::require(
Void::FileSystem::write_file(ignored_dir / "HEAD", "ref").has_value());
auto ignored_events = watcher.poll(std::chrono::milliseconds{40});
Void::Testing::require(ignored_events.has_value());
Void::Testing::expect(ignored_events->empty());
// Create nested subdirectories + file immediately -> auto-watched recursively
const auto sub = tmp / "src/core";
Void::Testing::require(Void::FileSystem::ensure_directory(sub).has_value());
Void::Testing::require(
Void::FileSystem::write_file(sub / "main.cpp", "int main(){}")
.has_value());
const auto create_events =
collect_events(watcher, std::chrono::milliseconds{500});
const bool saw_file_create_or_mod = std::any_of(
create_events.begin(), create_events.end(),
[&](const Void::FileSystem::DirectoryEvent& ev) {
return ev.path.filename() == "main.cpp" &&
(ev.kind == Void::FileSystem::DirectoryEventKind::Created ||
ev.kind == Void::FileSystem::DirectoryEventKind::Modified);
});
Void::Testing::expect(saw_file_create_or_mod);
Void::Testing::expect(watcher.watch_count() >= 3U);
// Modify inside auto-watched nested directory
Void::Testing::require(
Void::FileSystem::write_file(sub / "main.cpp", "int main(){return 0;}")
.has_value());
const auto mod_events =
collect_events(watcher, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
mod_events.begin(), mod_events.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Modified &&
ev.path.filename() == "main.cpp";
}));
// File rename pairing (IN_MOVED_FROM / IN_MOVED_TO)
std::filesystem::rename(sub / "main.cpp", sub / "entry.cpp");
const auto ren_events =
collect_events(watcher, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
ren_events.begin(), ren_events.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Renamed &&
ev.old_path.filename() == "main.cpp" &&
ev.path.filename() == "entry.cpp";
}));
// Directory rename rewrites watch paths so modifications inside new path
// still report the new directory path.
const auto renamed_dir = tmp / "src/engine";
std::filesystem::rename(sub, renamed_dir);
(void)collect_events(watcher, std::chrono::milliseconds{300});
Void::Testing::require(
Void::FileSystem::write_file(renamed_dir / "entry.cpp", "// updated")
.has_value());
const auto post_move_events =
collect_events(watcher, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
post_move_events.begin(), post_move_events.end(),
[&](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Modified &&
ev.path == (watcher.root() / "src/engine/entry.cpp");
}));
// Directory replacement / deletion storm
std::filesystem::remove_all(tmp / "src");
const auto del_events =
collect_events(watcher, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
del_events.begin(), del_events.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind == Void::FileSystem::DirectoryEventKind::Deleted;
}));
}
Void::Testing::Test directory_watcher_signals_rescan_on_overflow_and_limit()
{
Void::FileSystem::TempDir tmp{"void_dir_ovf_"};
// 1. Simulated queue overflow emits RescanRequired and wake recovers
{
auto w_res = Void::FileSystem::DirectoryWatcher::create(tmp.path());
Void::Testing::require(w_res.has_value());
w_res->simulate_overflow_for_test();
auto evs = w_res->poll(std::chrono::milliseconds{100});
Void::Testing::require(evs.has_value());
Void::Testing::expect(std::any_of(
evs->begin(), evs->end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind ==
Void::FileSystem::DirectoryEventKind::RescanRequired;
}));
Void::Testing::require(w_res->rescan().has_value());
}
// 2. max_watches exhaustion emits RescanRequired
{
Void::FileSystem::DirectoryWatcherOptions tight;
tight.max_watches = 1U;
auto w_res =
Void::FileSystem::DirectoryWatcher::create(tmp.path(), tight);
Void::Testing::require(w_res.has_value());
Void::Testing::require(
Void::FileSystem::ensure_directory(tmp / "extra_dir").has_value());
const auto evs = collect_events(*w_res, std::chrono::milliseconds{400});
Void::Testing::expect(std::any_of(
evs.begin(), evs.end(),
[](const Void::FileSystem::DirectoryEvent& ev) {
return ev.kind ==
Void::FileSystem::DirectoryEventKind::RescanRequired;
}));
}
}
+116
View File
@@ -96,3 +96,119 @@ Void::Testing::Test remove_path_handles_missing_and_recursive()
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);
}
}
@@ -19,6 +19,7 @@
#include <fstream>
#include <stop_token>
#include <string>
#include <thread>
#include <vector>
namespace
@@ -294,3 +295,72 @@ Void::Testing::Test system_replace_file_temp_ownership_and_metrics()
expect(sample.valid);
expect(sample.ratio >= 0.0);
}
Void::Testing::Test system_terminate_reaps_entire_process_group_after_leader_exits()
{
Void::FileSystem::TempDir tmp{"void_sys_pgid_"};
const auto marker = tmp / "grandchild_ticks.txt";
Void::Process::SpawnOptions opts;
opts.executable = VOID_PROCESS_HELPER_PATH;
opts.arguments = {"grandchild-ignore-sigterm", marker.string()};
opts.new_process_group = true;
opts.stdin_pipe = false;
opts.stderr_pipe = false;
auto spawned = Void::Process::spawn(opts);
require(spawned.has_value());
// Wait for grandchild readiness signal on stdout.
std::string ready_buf;
std::array<char, 64> chunk{};
const auto wait_start = std::chrono::steady_clock::now();
while (ready_buf.find("ready\n") == std::string::npos) {
const ssize_t n = ::read(spawned->stdout_fd(), chunk.data(), chunk.size());
if (n > 0) {
ready_buf.append(chunk.data(), static_cast<std::size_t>(n));
continue;
}
if (n < 0 && errno == EINTR) {
continue;
}
if (std::chrono::steady_clock::now() - wait_start > std::chrono::seconds{5}) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds{5});
}
require(ready_buf.find("ready\n") != std::string::npos);
// Wait until direct leader has exited and been reaped, while the
// SIGTERM-ignoring grandchild continues running in the process group.
std::optional<Void::Process::ExitStatus> leader_status;
for (int i = 0; i < 200; ++i) {
auto w = spawned->try_wait();
require(w.has_value());
if (w->has_value()) {
leader_status = *w;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds{5});
}
require(leader_status.has_value());
expect(leader_status->exited);
expect_eq(leader_status->code, 0);
expect(spawned->process_group_alive());
// terminate(60ms) must send SIGTERM to -pgid, wait out grace since the
// grandchild ignores SIGTERM, escalate to SIGKILL, and not return until
// ESRCH confirms all process-group members are gone.
auto term = spawned->terminate(std::chrono::milliseconds{60});
require(term.has_value());
expect(!spawned->process_group_alive());
std::error_code ec;
const auto size_after_terminate = std::filesystem::file_size(marker, ec);
require(!ec);
expect(size_after_terminate > 0U);
std::this_thread::sleep_for(std::chrono::milliseconds{100});
const auto size_later = std::filesystem::file_size(marker, ec);
require(!ec);
expect_eq(size_later, size_after_terminate);
}