152 lines
5.6 KiB
C++
152 lines
5.6 KiB
C++
#include <Void/Build/Package/Archive.hpp>
|
|
|
|
#include <Void/Archive/Archive.hpp>
|
|
#include <Void/Build/Platform/FileOps.hpp>
|
|
#include <Void/Build/Util/Sha256.hpp>
|
|
#include <Void/System/Types.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <format>
|
|
#include <set>
|
|
|
|
namespace Void::Build::Package {
|
|
namespace {
|
|
|
|
Void::Result::Error make_error(Void::System::SystemErrorCode code, std::string msg, int err = 0) {
|
|
return Void::System::make_system_error(code, std::move(msg), err);
|
|
}
|
|
|
|
bool is_excluded_package_path(const std::filesystem::path &rel) {
|
|
for (const auto &part : rel) {
|
|
const auto s = part.string();
|
|
if (s == ".git" || s == ".vbs" || s == "build" || s == ".serena" || s == ".cache" ||
|
|
s == "keys" || s == ".env" || s == "WG.conf") {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Void::Result::Result<std::string, Void::Result::Error>
|
|
create_deterministic_tar(const std::filesystem::path &output_tar,
|
|
std::vector<ArchiveEntrySpec> entries,
|
|
std::int64_t reproducibility_epoch) {
|
|
std::error_code ec;
|
|
if (output_tar.has_parent_path()) {
|
|
std::filesystem::create_directories(output_tar.parent_path(), ec);
|
|
}
|
|
|
|
std::ranges::sort(entries, {}, &ArchiveEntrySpec::archive_path);
|
|
|
|
std::vector<Void::Archive::CreateEntry> v_entries;
|
|
v_entries.reserve(entries.size());
|
|
for (const auto &e : entries) {
|
|
Void::Archive::CreateEntry ce;
|
|
ce.path = e.archive_path;
|
|
ce.type = Void::Archive::EntryType::RegularFile;
|
|
ce.executable = (e.mode & 0111u) != 0;
|
|
ce.source_file = e.source_file;
|
|
ce.mode = e.mode & 0777u;
|
|
v_entries.push_back(std::move(ce));
|
|
}
|
|
|
|
Void::Archive::CreationOptions opts;
|
|
opts.compression = Void::Archive::ArchiveCompression::None;
|
|
opts.reproducible = true;
|
|
opts.canonical_mtime = reproducibility_epoch;
|
|
opts.canonical_uid = 0;
|
|
opts.canonical_gid = 0;
|
|
opts.prefer_builtin_tar = true;
|
|
|
|
// Delegate deterministic PAX/USTAR tar creation directly to Void::Archive (VL-006).
|
|
auto created = Void::Archive::create(output_tar, v_entries, opts);
|
|
if (!created) {
|
|
return std::unexpected{std::move(created.error())};
|
|
}
|
|
return Util::sha256_file_hex(output_tar);
|
|
}
|
|
|
|
Void::Result::Result<std::string, Void::Result::Error>
|
|
create_source_package(const std::filesystem::path &project_root,
|
|
const std::filesystem::path &output_tar, std::int64_t reproducibility_epoch) {
|
|
std::error_code ec;
|
|
const auto canon = std::filesystem::weakly_canonical(project_root, ec);
|
|
if (ec || !std::filesystem::is_directory(canon)) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("create_source_package: '{}' is not a directory", project_root.string()))};
|
|
}
|
|
|
|
std::vector<ArchiveEntrySpec> entries;
|
|
std::filesystem::recursive_directory_iterator it{
|
|
canon, std::filesystem::directory_options::skip_permission_denied, ec};
|
|
for (; !ec && it != std::filesystem::recursive_directory_iterator{}; it.increment(ec)) {
|
|
const auto rel = it->path().lexically_relative(canon);
|
|
if (is_excluded_package_path(rel)) {
|
|
if (it->is_directory(ec)) {
|
|
it.disable_recursion_pending();
|
|
}
|
|
continue;
|
|
}
|
|
auto meta = Platform::stat_lstat(it->path());
|
|
if (!meta || !meta->exists || meta->is_symlink) {
|
|
continue;
|
|
}
|
|
if (meta->is_regular) {
|
|
entries.push_back({rel.lexically_normal().generic_string(), it->path(),
|
|
(meta->mode & 0111u) ? 0755u : 0644u});
|
|
}
|
|
}
|
|
return create_deterministic_tar(output_tar, std::move(entries), reproducibility_epoch);
|
|
}
|
|
|
|
Void::Result::Result<ArchiveExtractionStats, Void::Result::Error>
|
|
extract_safe_tar(const std::filesystem::path &tar_path,
|
|
const std::filesystem::path &destination_dir, const ArchiveLimits &limits) {
|
|
std::error_code ec;
|
|
const auto parent = destination_dir.has_parent_path() ? destination_dir.parent_path()
|
|
: std::filesystem::path{"."};
|
|
std::filesystem::create_directories(parent, ec);
|
|
if (ec) {
|
|
return std::unexpected{make_error(
|
|
Void::System::map_errno_to_system_code(ec.value()),
|
|
std::format("extract_safe_tar: failed creating parent '{}'", parent.string()),
|
|
ec.value())};
|
|
}
|
|
|
|
// Delegate safe staged extraction (rejecting traversal, absolute paths, symlinks,
|
|
// hardlinks, duplicates, and quota overflows) directly to Void::Archive (VL-006).
|
|
Void::Archive::StagedExtractionOptions opts;
|
|
opts.limits.max_entries = limits.max_entries;
|
|
opts.limits.max_file_bytes = limits.max_file_bytes;
|
|
opts.limits.max_total_bytes = limits.max_total_bytes;
|
|
opts.preserve_executable_bit = true;
|
|
|
|
auto staged = Void::Archive::extract_staged(tar_path, parent, opts);
|
|
if (!staged) {
|
|
return std::unexpected{std::move(staged.error())};
|
|
}
|
|
|
|
ArchiveExtractionStats stats;
|
|
stats.entries = staged->entries();
|
|
stats.total_bytes = staged->bytes();
|
|
for (const auto &info : staged->inventory()) {
|
|
if (info.type == Void::Archive::EntryType::RegularFile) {
|
|
stats.extracted_files.emplace_back(info.path);
|
|
}
|
|
}
|
|
|
|
if (std::filesystem::exists(destination_dir, ec)) {
|
|
std::filesystem::remove_all(destination_dir, ec);
|
|
}
|
|
auto pub = staged->publish(destination_dir.filename().string());
|
|
if (!pub) {
|
|
return std::unexpected{std::move(pub.error())};
|
|
}
|
|
return stats;
|
|
}
|
|
|
|
} // namespace Void::Build::Package
|