Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,159 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::FileSystem
|
||||
{
|
||||
/// Read the entire contents of a file as a string.
|
||||
///
|
||||
/// Failure modes wrapped in `Result`:
|
||||
/// - File does not exist
|
||||
/// - File cannot be opened (permissions, etc.)
|
||||
/// - I/O error during read
|
||||
[[nodiscard]] inline Void::Result::Result<std::string, Void::Result::Error>
|
||||
read_file(const std::filesystem::path& path)
|
||||
{
|
||||
std::ifstream f{path, std::ios::binary};
|
||||
if (!f.is_open()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("read_file: cannot open '{}'", path.string()));
|
||||
return Void::Result::fail(std::format(
|
||||
"FileSystem::read_file: cannot open '{}'", path.string()));
|
||||
}
|
||||
std::string content{std::istreambuf_iterator<char>(f),
|
||||
std::istreambuf_iterator<char>()};
|
||||
if (f.bad()) {
|
||||
Void::Log::log(Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("read_file: I/O error reading '{}'",
|
||||
path.string()));
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::read_file: I/O error reading '{}'",
|
||||
path.string()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("read_file: '{}' ({} bytes)", path.string(),
|
||||
content.size()));
|
||||
return Void::Result::ok(std::move(content));
|
||||
}
|
||||
|
||||
/// Write `content` to `path`, overwriting any existing file.
|
||||
///
|
||||
/// This is NOT crash-safe — a process killed mid-write may leave a
|
||||
/// truncated file. Use `atomic_write` when you need durability.
|
||||
[[nodiscard]] inline Void::Result::Result<void, Void::Result::Error>
|
||||
write_file(const std::filesystem::path& path, std::string_view content)
|
||||
{
|
||||
std::ofstream f{path, std::ios::binary | std::ios::trunc};
|
||||
if (!f.is_open()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("write_file: cannot open '{}'", path.string()));
|
||||
return Void::Result::fail(std::format(
|
||||
"FileSystem::write_file: cannot open '{}'", path.string()));
|
||||
}
|
||||
f.write(content.data(), static_cast<std::streamsize>(content.size()));
|
||||
f.flush();
|
||||
if (!f.good()) {
|
||||
Void::Log::log(Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("write_file: I/O error writing '{}'",
|
||||
path.string()));
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::write_file: I/O error writing '{}'",
|
||||
path.string()));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Crash-safe write: stage `content` in a sibling `.tmp` file, then
|
||||
/// atomically rename onto `path`. The rename is atomic on POSIX
|
||||
/// within the same filesystem; std::filesystem::rename uses the
|
||||
/// platform-appropriate primitive (`ReplaceFileW` on Windows).
|
||||
///
|
||||
/// Note: this does NOT fsync the staged file before rename. A
|
||||
/// process or kernel crash between write and rename may leave the
|
||||
/// `.tmp` file behind, but the original `path` is never corrupted.
|
||||
[[nodiscard]] inline Void::Result::Result<void, Void::Result::Error>
|
||||
atomic_write(const std::filesystem::path& path,
|
||||
std::string_view content)
|
||||
{
|
||||
auto staging = path;
|
||||
staging += ".tmp";
|
||||
if (auto r = write_file(staging, content); !r) {
|
||||
return r;
|
||||
}
|
||||
std::error_code ec;
|
||||
std::filesystem::rename(staging, path, ec);
|
||||
if (ec) {
|
||||
std::error_code ignore;
|
||||
std::filesystem::remove(staging, ignore);
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("atomic_write: rename failed for '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
return Void::Result::fail(std::format(
|
||||
"FileSystem::atomic_write: rename failed for '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("atomic_write: '{}' committed ({} bytes)",
|
||||
path.string(), content.size()));
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Create `path` as a directory, including any missing parents.
|
||||
/// Succeeds (no-op) if `path` already exists as a directory.
|
||||
[[nodiscard]] inline Void::Result::Result<void, Void::Result::Error>
|
||||
ensure_directory(const std::filesystem::path& path)
|
||||
{
|
||||
std::error_code ec;
|
||||
if (std::filesystem::is_directory(path, ec)) {
|
||||
return {};
|
||||
}
|
||||
std::filesystem::create_directories(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("ensure_directory: cannot create '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
return Void::Result::fail(std::format(
|
||||
"FileSystem::ensure_directory: cannot create '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("ensure_directory: created '{}'", path.string()));
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Remove a file or directory (recursively for directories).
|
||||
/// Succeeds (no-op) if the path doesn't exist.
|
||||
[[nodiscard]] inline Void::Result::Result<void, Void::Result::Error>
|
||||
remove_path(const std::filesystem::path& path)
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("remove_path: cannot remove '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::remove_path: cannot remove '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace Void::FileSystem
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Process
|
||||
{
|
||||
|
||||
/// Captured output from a completed subprocess.
|
||||
struct Output {
|
||||
int exit_code{0};
|
||||
std::string out; ///< captured stdout
|
||||
std::string err; ///< captured stderr
|
||||
};
|
||||
|
||||
/// Run an executable synchronously, capturing stdout and stderr separately.
|
||||
///
|
||||
/// @param executable Path to the binary (searched via PATH when relative).
|
||||
/// @param args Additional arguments passed after argv[0].
|
||||
/// @param stdin_data Data written to the process's stdin before reading.
|
||||
/// @param timeout Wall-clock limit; zero means no limit. On expiry the
|
||||
/// process receives SIGTERM and the call returns.
|
||||
///
|
||||
/// Returns an error only when process creation itself fails (pipe or fork
|
||||
/// failure). A process that exits non-zero still yields a successful Result
|
||||
/// with the appropriate exit_code. A process whose executable is not found
|
||||
/// exits with code 127 (POSIX convention).
|
||||
[[nodiscard]] Void::Result::Result<Output, Void::Result::Error>
|
||||
run(const std::filesystem::path& executable,
|
||||
std::vector<std::string> args = {},
|
||||
std::string_view stdin_data = {},
|
||||
std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
|
||||
|
||||
/// Run a command through /bin/sh -c "command".
|
||||
[[nodiscard]] Void::Result::Result<Output, Void::Result::Error>
|
||||
shell(std::string_view command,
|
||||
std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
|
||||
|
||||
} // namespace Void::Process
|
||||
@@ -0,0 +1,137 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <system_error>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::FileSystem
|
||||
{
|
||||
|
||||
/// Return true if path exists (file, directory, or symlink).
|
||||
[[nodiscard]] inline bool exists(const std::filesystem::path& path) noexcept
|
||||
{
|
||||
std::error_code ec;
|
||||
return std::filesystem::exists(path, ec);
|
||||
}
|
||||
|
||||
/// Return the size of a regular file in bytes.
|
||||
[[nodiscard]] inline Void::Result::Result<std::uintmax_t, Void::Result::Error>
|
||||
file_size(const std::filesystem::path& path)
|
||||
{
|
||||
std::error_code ec;
|
||||
auto sz = std::filesystem::file_size(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("file_size: cannot stat '{}': {}", path.string(),
|
||||
ec.message()));
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::file_size: cannot stat '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
return Void::Result::ok(sz);
|
||||
}
|
||||
|
||||
/// Return the last-write time of a path.
|
||||
[[nodiscard]] inline Void::Result::
|
||||
Result<std::filesystem::file_time_type, Void::Result::Error>
|
||||
last_modified(const std::filesystem::path& path)
|
||||
{
|
||||
std::error_code ec;
|
||||
auto t = std::filesystem::last_write_time(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("last_modified: cannot stat '{}': {}", path.string(),
|
||||
ec.message()));
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::last_modified: cannot stat '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
return Void::Result::ok(t);
|
||||
}
|
||||
|
||||
/// List all entries in a directory (non-recursive, filesystem order).
|
||||
[[nodiscard]] inline Void::Result::
|
||||
Result<std::vector<std::filesystem::path>, Void::Result::Error>
|
||||
list_directory(const std::filesystem::path& path)
|
||||
{
|
||||
std::error_code ec;
|
||||
std::vector<std::filesystem::path> entries;
|
||||
for (const auto& entry :
|
||||
std::filesystem::directory_iterator{path, ec}) {
|
||||
if (ec) {
|
||||
break;
|
||||
}
|
||||
entries.push_back(entry.path());
|
||||
}
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("list_directory: cannot iterate '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::list_directory: cannot iterate '{}': {}",
|
||||
path.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("list_directory: '{}' ({} entries)",
|
||||
path.string(), entries.size()));
|
||||
return Void::Result::ok(std::move(entries));
|
||||
}
|
||||
|
||||
/// Copy a single file. Set overwrite=true to allow replacing an existing dst.
|
||||
[[nodiscard]] inline Void::Result::Result<void, Void::Result::Error>
|
||||
copy_file(const std::filesystem::path& src,
|
||||
const std::filesystem::path& dst,
|
||||
bool overwrite = false)
|
||||
{
|
||||
std::error_code ec;
|
||||
auto opts = overwrite ? std::filesystem::copy_options::overwrite_existing
|
||||
: std::filesystem::copy_options::none;
|
||||
std::filesystem::copy_file(src, dst, opts, ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("copy_file: '{}' -> '{}': {}", src.string(),
|
||||
dst.string(), ec.message()));
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::copy_file: '{}' -> '{}': {}",
|
||||
src.string(), dst.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("copy_file: '{}' -> '{}'", src.string(),
|
||||
dst.string()));
|
||||
return {};
|
||||
}
|
||||
|
||||
/// Recursively copy a directory tree from src to dst.
|
||||
/// dst must not already exist; its parent must be accessible.
|
||||
[[nodiscard]] inline Void::Result::Result<void, Void::Result::Error>
|
||||
copy_dir(const std::filesystem::path& src,
|
||||
const std::filesystem::path& dst)
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::copy(src, dst, std::filesystem::copy_options::recursive,
|
||||
ec);
|
||||
if (ec) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Warning, "FileSystem",
|
||||
std::format("copy_dir: '{}' -> '{}': {}", src.string(),
|
||||
dst.string(), ec.message()));
|
||||
return Void::Result::fail(
|
||||
std::format("FileSystem::copy_dir: '{}' -> '{}': {}",
|
||||
src.string(), dst.string(), ec.message()));
|
||||
}
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("copy_dir: '{}' -> '{}'", src.string(),
|
||||
dst.string()));
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace Void::FileSystem
|
||||
@@ -0,0 +1,129 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::FileSystem
|
||||
{
|
||||
/// RAII scoped directory under `std::filesystem::temp_directory_path()`.
|
||||
///
|
||||
/// The directory is created with a unique name on construction and
|
||||
/// recursively removed on destruction. Useful in tests that need a
|
||||
/// scratch area without manual cleanup.
|
||||
///
|
||||
/// Construction throws `std::system_error` if the directory cannot
|
||||
/// be created. Use `TempDir::open(prefix)` for a non-throwing
|
||||
/// `Result`-returning factory.
|
||||
class TempDir
|
||||
{
|
||||
public:
|
||||
|
||||
explicit TempDir(std::string_view prefix = "void_test_")
|
||||
: m_path(make_unique_dir(prefix))
|
||||
{
|
||||
}
|
||||
|
||||
~TempDir() noexcept
|
||||
{
|
||||
if (!m_path.empty()) {
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("TempDir: removed '{}'", m_path.string()));
|
||||
std::error_code ignore;
|
||||
std::filesystem::remove_all(m_path, ignore);
|
||||
}
|
||||
}
|
||||
|
||||
TempDir(const TempDir&) = delete;
|
||||
TempDir& operator=(const TempDir&) = delete;
|
||||
|
||||
TempDir(TempDir&& other) noexcept
|
||||
: m_path(std::exchange(other.m_path, {}))
|
||||
{
|
||||
}
|
||||
|
||||
TempDir& operator=(TempDir&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
if (!m_path.empty()) {
|
||||
std::error_code ignore;
|
||||
std::filesystem::remove_all(m_path, ignore);
|
||||
}
|
||||
m_path = std::exchange(other.m_path, {});
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::filesystem::path& path() const noexcept
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
/// Build a path inside the temp dir.
|
||||
/// Example: `auto log_path = tmp / "test.log";`
|
||||
[[nodiscard]] std::filesystem::path
|
||||
operator/(std::string_view relative) const
|
||||
{
|
||||
return m_path / relative;
|
||||
}
|
||||
|
||||
/// Non-throwing factory.
|
||||
[[nodiscard]] static Void::Result::Result<TempDir,
|
||||
Void::Result::Error>
|
||||
open(std::string_view prefix = "void_test_")
|
||||
{
|
||||
try {
|
||||
return Void::Result::ok(TempDir{prefix});
|
||||
} catch (const std::exception& e) {
|
||||
return Void::Result::fail(std::string{e.what()});
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::filesystem::path m_path;
|
||||
|
||||
static std::filesystem::path
|
||||
make_unique_dir(std::string_view prefix)
|
||||
{
|
||||
static std::atomic<std::uint64_t> counter{0};
|
||||
const auto base = std::filesystem::temp_directory_path();
|
||||
for (int attempt = 0; attempt < 16; ++attempt) {
|
||||
const auto seed =
|
||||
static_cast<std::uint64_t>(
|
||||
std::chrono::steady_clock::now()
|
||||
.time_since_epoch()
|
||||
.count()) ^
|
||||
(counter.fetch_add(1, std::memory_order_relaxed)
|
||||
<< 16) ^
|
||||
static_cast<std::uint64_t>(std::hash<std::thread::id>{}(
|
||||
std::this_thread::get_id()));
|
||||
auto candidate =
|
||||
base / std::format("{}{:016x}", prefix, seed);
|
||||
std::error_code ec;
|
||||
if (std::filesystem::create_directory(candidate, ec)) {
|
||||
Void::Log::log(Void::Log::Level::Debug, "FileSystem",
|
||||
std::format("TempDir: created '{}'",
|
||||
candidate.string()));
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
throw std::system_error(
|
||||
std::make_error_code(std::errc::file_exists),
|
||||
"TempDir: failed to create unique directory after 16 "
|
||||
"attempts");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Void::FileSystem
|
||||
Reference in new issue
Block a user