220 lines
8.4 KiB
C++
220 lines
8.4 KiB
C++
#pragma once
|
|
|
|
#include <Void/Archive/Types.hpp>
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <stop_token>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Archive
|
|
{
|
|
struct ExtractionOptions {
|
|
std::size_t maxEntries{10'000};
|
|
std::uint64_t maxFileBytes{512ULL * 1024 * 1024};
|
|
std::uint64_t maxTotalBytes{2ULL * 1024 * 1024 * 1024};
|
|
bool overwriteExisting{false};
|
|
};
|
|
|
|
struct ExtractionResult {
|
|
std::size_t entries{0};
|
|
std::uint64_t bytes{0};
|
|
};
|
|
|
|
/// Returns true if Void::Archive was built with LibArchive support (required
|
|
/// for .tar.zst and .tar.gz compression/decompression; uncompressed USTAR/PAX
|
|
/// .tar reading and writing work with zero external dependencies).
|
|
[[nodiscard]] bool has_libarchive_backend() noexcept;
|
|
|
|
/// Extract a tar/tar.gz/tar.zst archive incrementally into `destination`
|
|
/// without invoking an external process. Preserved 1.x incremental API;
|
|
/// callers requiring all-or-nothing directory visibility should use
|
|
/// `extract_staged` + `StagedTree::publish`.
|
|
[[nodiscard]] Void::Result::Result<ExtractionResult>
|
|
extract(const std::filesystem::path& archivePath,
|
|
const std::filesystem::path& destination,
|
|
const ExtractionOptions& options = {});
|
|
|
|
/// Caller-driven bounded tar/tar.zst reader. Maintains one active entry at
|
|
/// a time; callers must drain (`read_chunk` returning 0) or call
|
|
/// `skip_entry()` before advancing with `next_entry()`.
|
|
class Reader
|
|
{
|
|
public:
|
|
|
|
~Reader();
|
|
Reader(Reader&&) noexcept;
|
|
Reader& operator=(Reader&&) noexcept;
|
|
Reader(const Reader&) = delete;
|
|
Reader& operator=(const Reader&) = delete;
|
|
|
|
[[nodiscard]] static Void::Result::Result<Reader> open(
|
|
ByteSource& source, ReadLimits limits = {},
|
|
std::stop_token stop = {},
|
|
std::optional<Void::Time::MonotonicClock::time_point> deadline =
|
|
std::nullopt,
|
|
const Void::Time::MonotonicClock& clock =
|
|
Void::Time::SystemMonotonicClock::instance());
|
|
|
|
[[nodiscard]] static Void::Result::Result<Reader> open_file(
|
|
const std::filesystem::path& archive_path,
|
|
ReadLimits limits = {}, std::stop_token stop = {},
|
|
std::optional<Void::Time::MonotonicClock::time_point> deadline =
|
|
std::nullopt,
|
|
const Void::Time::MonotonicClock& clock =
|
|
Void::Time::SystemMonotonicClock::instance());
|
|
|
|
/// Advance to the next archive entry. Returns std::nullopt on clean
|
|
/// EOF.
|
|
[[nodiscard]] Void::Result::Result<std::optional<EntryInfo>>
|
|
next_entry();
|
|
|
|
/// Read up to dst.size() decompressed bytes of the active regular
|
|
/// file entry. Returns 0 on entry EOF.
|
|
[[nodiscard]] Void::Result::Result<std::size_t>
|
|
read_chunk(std::span<std::byte> dst);
|
|
|
|
/// Drain and discard remaining bytes of the active entry while
|
|
/// still accounting decompressed bytes against policy limits.
|
|
[[nodiscard]] Void::Result::Result<std::uint64_t> skip_entry();
|
|
|
|
[[nodiscard]] std::size_t entries_seen() const noexcept;
|
|
[[nodiscard]] std::uint64_t total_decompressed_bytes() const noexcept;
|
|
[[nodiscard]] std::uint64_t total_compressed_bytes() const noexcept;
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
explicit Reader(std::unique_ptr<Impl> impl) noexcept;
|
|
};
|
|
|
|
/// Bounded metadata listing of archive entries. Normal mode (`forensic =
|
|
/// false`) rejects unsafe/duplicate/conflicting entries. Opt-in forensic
|
|
/// mode reports rejected entries without writing to disk.
|
|
[[nodiscard]] Void::Result::Result<std::vector<EntryInfo>>
|
|
list_entries(ByteSource& source, ReadLimits limits = {},
|
|
bool forensic = false);
|
|
|
|
[[nodiscard]] Void::Result::Result<std::vector<EntryInfo>>
|
|
list_entries(const std::filesystem::path& archive_path,
|
|
ReadLimits limits = {}, bool forensic = false);
|
|
|
|
/// Move-only handle owning an unpublished private staging directory beneath
|
|
/// a pinned parent directory. Unlinks only names under its staging
|
|
/// descriptor without following links if destroyed before publication.
|
|
class StagedTree
|
|
{
|
|
public:
|
|
|
|
StagedTree() noexcept = default;
|
|
~StagedTree() noexcept;
|
|
StagedTree(StagedTree&&) noexcept;
|
|
StagedTree& operator=(StagedTree&&) noexcept;
|
|
StagedTree(const StagedTree&) = delete;
|
|
StagedTree& operator=(const StagedTree&) = delete;
|
|
|
|
[[nodiscard]] bool valid() const noexcept;
|
|
[[nodiscard]] bool published() const noexcept;
|
|
[[nodiscard]] std::string_view staging_id() const noexcept;
|
|
[[nodiscard]] const std::filesystem::path&
|
|
staging_root() const noexcept;
|
|
[[nodiscard]] const std::filesystem::path&
|
|
tree_path() const noexcept;
|
|
[[nodiscard]] std::size_t entries() const noexcept;
|
|
[[nodiscard]] std::uint64_t bytes() const noexcept;
|
|
[[nodiscard]] const std::vector<EntryInfo>&
|
|
inventory() const noexcept;
|
|
|
|
/// Publish the staged tree into a previously absent immediate child
|
|
/// of the pinned parent directory using atomic no-replace rename
|
|
/// (`renameat2(..., RENAME_NOREPLACE)`). Never overwrites existing
|
|
/// directories or falls back to a racy existence check.
|
|
[[nodiscard]] Void::Result::Result<PublicationReceipt>
|
|
publish(std::string_view new_child_name);
|
|
|
|
/// Explicitly discard an unpublished staging tree.
|
|
[[nodiscard]] Void::Result::Result<void> discard();
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
explicit StagedTree(std::unique_ptr<Impl> impl) noexcept;
|
|
|
|
friend Void::Result::Result<StagedTree>
|
|
extract_staged(ByteSource& source,
|
|
const std::filesystem::path& parent_directory,
|
|
const StagedExtractionOptions& options);
|
|
friend Void::Result::Result<StagedTree>
|
|
extract_staged_isolated(
|
|
const std::filesystem::path& archive_path,
|
|
const std::filesystem::path& parent_directory,
|
|
const IsolationOptions& options);
|
|
};
|
|
|
|
/// Extract a tar/tar.zst stream into an exclusive 0700 staging directory
|
|
/// beneath `parent_directory`, fsync files and directories bottom-up, write
|
|
/// a bounded completion inventory, and return an unpublished StagedTree.
|
|
[[nodiscard]] Void::Result::Result<StagedTree>
|
|
extract_staged(ByteSource& source,
|
|
const std::filesystem::path& parent_directory,
|
|
const StagedExtractionOptions& options = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<StagedTree>
|
|
extract_staged(const std::filesystem::path& archive_path,
|
|
const std::filesystem::path& parent_directory,
|
|
const StagedExtractionOptions& options = {});
|
|
|
|
/// Remove abandoned staging directories under `parent_directory` that carry
|
|
/// a valid exclusive operation marker owned by the current user and are at
|
|
/// least `min_age` old. Never touches directories without a valid marker.
|
|
[[nodiscard]] Void::Result::Result<std::size_t>
|
|
cleanup_stale_staging(const std::filesystem::path& parent_directory,
|
|
std::chrono::seconds min_age =
|
|
std::chrono::seconds{300});
|
|
|
|
/// Create a bounded, optionally reproducible tar, tar.gz, or tar.zst
|
|
/// archive from regular file and directory entries. Sorts normalized
|
|
/// paths, rejects duplicates/conflicts, and opens source files with O_NOFOLLOW.
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
create_archive(ByteSink& sink, std::span<const CreateEntry> entries,
|
|
const CreationOptions& options = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
create_archive(const std::filesystem::path& output_path,
|
|
std::span<const CreateEntry> entries,
|
|
const CreationOptions& options = {});
|
|
|
|
/// Canonical snake_case alias for `create_archive`.
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
create(ByteSink& sink, std::span<const CreateEntry> entries,
|
|
const CreationOptions& options = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
create(const std::filesystem::path& output_path,
|
|
std::span<const CreateEntry> entries,
|
|
const CreationOptions& options = {});
|
|
|
|
/// Hardened untrusted-input profile: extracts the archive in an isolated
|
|
/// System child process with bounded execution time and RSS checks, then
|
|
/// independently verifies the staged tree and inventory in the parent
|
|
/// before returning StagedTree.
|
|
[[nodiscard]] Void::Result::Result<StagedTree>
|
|
extract_staged_isolated(const std::filesystem::path& archive_path,
|
|
const std::filesystem::path& parent_directory,
|
|
const IsolationOptions& options);
|
|
|
|
/// Entry point invoked by the isolated archive decoder worker binary.
|
|
[[nodiscard]] int run_isolated_worker_main(int argc, char** argv);
|
|
|
|
} // namespace Void::Archive
|