feat(System): harden OwnedFd/Child process spawn, full-duplex bounds, durable replace_file, safe TempFile/TempDir, and SystemOnly consumer (M-SYSTEM-01, M-SYSTEM-02, M-SYSTEM-05)
This commit is contained in:
1 parent
3f67b1ecb7
commit
fcdbc063b2
14 files changed
+3538
-890
No files matched your search
+133
-81
@@ -1,9 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <system_error>
|
||||
@@ -12,113 +16,161 @@
|
||||
namespace Void::FileSystem
|
||||
{
|
||||
|
||||
struct ListDirectoryOptions {
|
||||
std::size_t max_entries{65536};
|
||||
};
|
||||
|
||||
/// 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);
|
||||
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)
|
||||
file_size(const std::filesystem::path& path)
|
||||
{
|
||||
std::error_code ec;
|
||||
auto sz = std::filesystem::file_size(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::warn("FileSystem", "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);
|
||||
std::error_code ec;
|
||||
auto sz = std::filesystem::file_size(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::warn("FileSystem",
|
||||
"file_size: cannot stat '{}': {}",
|
||||
path.string(),
|
||||
ec.message());
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
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)
|
||||
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::warn("FileSystem", "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);
|
||||
std::error_code ec;
|
||||
auto t = std::filesystem::last_write_time(path, ec);
|
||||
if (ec) {
|
||||
Void::Log::warn("FileSystem",
|
||||
"last_modified: cannot stat '{}': {}",
|
||||
path.string(),
|
||||
ec.message());
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
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).
|
||||
/// List entries in a directory up to `options.max_entries`.
|
||||
[[nodiscard]] inline Void::Result::
|
||||
Result<std::vector<std::filesystem::path>, Void::Result::Error>
|
||||
list_directory(const std::filesystem::path& path)
|
||||
Result<std::vector<std::filesystem::path>, Void::Result::Error>
|
||||
list_directory_bounded(const std::filesystem::path& path,
|
||||
ListDirectoryOptions options = {})
|
||||
{
|
||||
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::warn("FileSystem", "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::debug("FileSystem", "list_directory: '{}' ({} entries)",
|
||||
path.string(), entries.size());
|
||||
return Void::Result::ok(std::move(entries));
|
||||
std::error_code ec;
|
||||
std::vector<std::filesystem::path> entries;
|
||||
for (const auto& entry : std::filesystem::directory_iterator{path, ec}) {
|
||||
if (ec) {
|
||||
break;
|
||||
}
|
||||
if (entries.size() >= options.max_entries) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::limit_exceeded,
|
||||
std::format(
|
||||
"FileSystem::list_directory: '{}' exceeded max_entries ({})",
|
||||
path.string(),
|
||||
options.max_entries))};
|
||||
}
|
||||
entries.push_back(entry.path());
|
||||
}
|
||||
if (ec) {
|
||||
Void::Log::warn("FileSystem",
|
||||
"list_directory: cannot iterate '{}': {}",
|
||||
path.string(),
|
||||
ec.message());
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
std::format("FileSystem::list_directory: cannot iterate '{}': {}",
|
||||
path.string(),
|
||||
ec.message()))};
|
||||
}
|
||||
Void::Log::debug("FileSystem",
|
||||
"list_directory: '{}' ({} entries)",
|
||||
path.string(),
|
||||
entries.size());
|
||||
return Void::Result::ok(std::move(entries));
|
||||
}
|
||||
|
||||
/// List all entries in a directory (bounded at 65,536 default).
|
||||
[[nodiscard]] inline Void::Result::
|
||||
Result<std::vector<std::filesystem::path>, Void::Result::Error>
|
||||
list_directory(const std::filesystem::path& path)
|
||||
{
|
||||
return list_directory_bounded(path, ListDirectoryOptions{});
|
||||
}
|
||||
|
||||
/// 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)
|
||||
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::warn("FileSystem", "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::debug("FileSystem", "copy_file: '{}' -> '{}'", src.string(),
|
||||
dst.string());
|
||||
return {};
|
||||
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::warn("FileSystem",
|
||||
"copy_file: '{}' -> '{}': {}",
|
||||
src.string(),
|
||||
dst.string(),
|
||||
ec.message());
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
std::format("FileSystem::copy_file: '{}' -> '{}': {}",
|
||||
src.string(),
|
||||
dst.string(),
|
||||
ec.message()))};
|
||||
}
|
||||
Void::Log::debug("FileSystem",
|
||||
"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)
|
||||
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::warn("FileSystem", "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::debug("FileSystem", "copy_dir: '{}' -> '{}'", src.string(),
|
||||
dst.string());
|
||||
return {};
|
||||
std::error_code ec;
|
||||
std::filesystem::copy(
|
||||
src, dst, std::filesystem::copy_options::recursive, ec);
|
||||
if (ec) {
|
||||
Void::Log::warn("FileSystem",
|
||||
"copy_dir: '{}' -> '{}': {}",
|
||||
src.string(),
|
||||
dst.string(),
|
||||
ec.message());
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::map_errno_to_system_code(ec.value()),
|
||||
std::format("FileSystem::copy_dir: '{}' -> '{}': {}",
|
||||
src.string(),
|
||||
dst.string(),
|
||||
ec.message()))};
|
||||
}
|
||||
Void::Log::debug("FileSystem",
|
||||
"copy_dir: '{}' -> '{}'",
|
||||
src.string(),
|
||||
dst.string());
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace Void::FileSystem
|
||||
Reference in new issue
Block a user