#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include 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 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 open( ByteSource& source, ReadLimits limits = {}, std::stop_token stop = {}, std::optional deadline = std::nullopt, const Void::Time::MonotonicClock& clock = Void::Time::SystemMonotonicClock::instance()); [[nodiscard]] static Void::Result::Result open_file( const std::filesystem::path& archive_path, ReadLimits limits = {}, std::stop_token stop = {}, std::optional 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> next_entry(); /// Read up to dst.size() decompressed bytes of the active regular /// file entry. Returns 0 on entry EOF. [[nodiscard]] Void::Result::Result read_chunk(std::span dst); /// Drain and discard remaining bytes of the active entry while /// still accounting decompressed bytes against policy limits. [[nodiscard]] Void::Result::Result 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 m_impl; explicit Reader(std::unique_ptr 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> list_entries(ByteSource& source, ReadLimits limits = {}, bool forensic = false); [[nodiscard]] Void::Result::Result> 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& 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 publish(std::string_view new_child_name); /// Explicitly discard an unpublished staging tree. [[nodiscard]] Void::Result::Result discard(); private: struct Impl; std::unique_ptr m_impl; explicit StagedTree(std::unique_ptr impl) noexcept; friend Void::Result::Result extract_staged(ByteSource& source, const std::filesystem::path& parent_directory, const StagedExtractionOptions& options); friend Void::Result::Result 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 extract_staged(ByteSource& source, const std::filesystem::path& parent_directory, const StagedExtractionOptions& options = {}); [[nodiscard]] Void::Result::Result 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 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 create_archive(ByteSink& sink, std::span entries, const CreationOptions& options = {}); [[nodiscard]] Void::Result::Result create_archive(const std::filesystem::path& output_path, std::span entries, const CreationOptions& options = {}); /// Canonical snake_case alias for `create_archive`. [[nodiscard]] Void::Result::Result create(ByteSink& sink, std::span entries, const CreationOptions& options = {}); [[nodiscard]] Void::Result::Result create(const std::filesystem::path& output_path, std::span 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 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