177 lines
6.2 KiB
C++
177 lines
6.2 KiB
C++
#pragma once
|
|
|
|
#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>
|
|
#include <vector>
|
|
|
|
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);
|
|
}
|
|
|
|
/// 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::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)
|
|
{
|
|
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 entries in a directory up to `options.max_entries`.
|
|
[[nodiscard]] inline Void::Result::
|
|
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;
|
|
}
|
|
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)
|
|
{
|
|
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.
|
|
[[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::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
|