#pragma once #include #include #include #include #include #include #include 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 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); } /// Return the last-write time of a path. [[nodiscard]] inline Void::Result:: Result 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); } /// List all entries in a directory (non-recursive, filesystem order). [[nodiscard]] inline Void::Result:: Result, Void::Result::Error> list_directory(const std::filesystem::path& path) { std::error_code ec; std::vector 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)); } /// Copy a single file. Set overwrite=true to allow replacing an existing dst. [[nodiscard]] inline Void::Result::Result 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 {}; } /// Recursively copy a directory tree from src to dst. /// dst must not already exist; its parent must be accessible. [[nodiscard]] inline Void::Result::Result 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 {}; } } // namespace Void::FileSystem