#include #include #include #include #include #include #include #include #include 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 create_deterministic_tar(const std::filesystem::path &output_tar, std::vector 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 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 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()))}; } // The archive must never contain itself (or its in-progress staging file) when the output // lives inside the project tree, e.g. a custom build directory that is not named `build`. std::error_code output_ec; const auto canonical_output = std::filesystem::weakly_canonical(output_tar, output_ec); const bool have_output = !output_ec; const auto is_output_path = [&](const std::filesystem::path &path) { if (!have_output) { return false; } std::error_code candidate_ec; return std::filesystem::weakly_canonical(path, candidate_ec) == canonical_output; }; std::vector 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_output_path(it->path())) { continue; } 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); } namespace { void zip_put_u16(std::string &out, std::uint16_t value) { out.push_back(static_cast(value & 0xFFu)); out.push_back(static_cast((value >> 8) & 0xFFu)); } void zip_put_u32(std::string &out, std::uint32_t value) { for (int shift = 0; shift < 32; shift += 8) { out.push_back(static_cast((value >> shift) & 0xFFu)); } } std::uint32_t zip_crc32(std::string_view data) { static const auto table = [] { std::array t{}; for (std::uint32_t i = 0; i < 256; ++i) { std::uint32_t c = i; for (int k = 0; k < 8; ++k) { c = (c & 1u) != 0u ? (0xEDB88320u ^ (c >> 1)) : (c >> 1); } t[i] = c; } return t; }(); std::uint32_t crc = 0xFFFFFFFFu; for (const char ch : data) { crc = table[(crc ^ static_cast(ch)) & 0xFFu] ^ (crc >> 8); } return crc ^ 0xFFFFFFFFu; } } // namespace Void::Result::Result create_stored_zip(const std::filesystem::path &output_zip, const std::vector &entries) { struct Record { std::string name; std::uint32_t crc{0}; std::uint32_t size{0}; std::uint32_t offset{0}; std::uint32_t mode{0644}; }; std::string local; std::vector records; for (const auto &entry : entries) { auto content = Void::FileSystem::read_file(entry.source_file); if (!content) { return std::unexpected{content.error()}; } const auto crc = zip_crc32(*content); const auto size = static_cast(content->size()); records.push_back( {entry.archive_path, crc, size, static_cast(local.size()), entry.mode}); zip_put_u32(local, 0x04034b50u); zip_put_u16(local, 20); // version needed zip_put_u16(local, 0); // flags zip_put_u16(local, 0); // stored zip_put_u16(local, 0); // time zip_put_u16(local, 0x21); // 1980-01-01 zip_put_u32(local, crc); zip_put_u32(local, size); zip_put_u32(local, size); zip_put_u16(local, static_cast(entry.archive_path.size())); zip_put_u16(local, 0); // extra local += entry.archive_path; local += *content; } std::string central; for (const auto &record : records) { zip_put_u32(central, 0x02014b50u); zip_put_u16(central, 0x031e); // made by UNIX zip_put_u16(central, 20); zip_put_u16(central, 0); zip_put_u16(central, 0); zip_put_u16(central, 0); zip_put_u16(central, 0x21); zip_put_u32(central, record.crc); zip_put_u32(central, record.size); zip_put_u32(central, record.size); zip_put_u16(central, static_cast(record.name.size())); zip_put_u16(central, 0); // extra zip_put_u16(central, 0); // comment zip_put_u16(central, 0); // disk zip_put_u16(central, 0); // internal attrs zip_put_u32(central, (record.mode & 0777u) << 16); zip_put_u32(central, record.offset); central += record.name; } std::string eocd; zip_put_u32(eocd, 0x06054b50u); zip_put_u16(eocd, 0); zip_put_u16(eocd, 0); zip_put_u16(eocd, static_cast(records.size())); zip_put_u16(eocd, static_cast(records.size())); zip_put_u32(eocd, static_cast(central.size())); zip_put_u32(eocd, static_cast(local.size())); zip_put_u16(eocd, 0); return Platform::atomic_write_file(output_zip, local + central + eocd, true); } Void::Result::Result 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