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
|
||||
Reference in new issue
Block a user