752 lines
31 KiB
C++
752 lines
31 KiB
C++
#include <Void/Build/Install/Installer.hpp>
|
|
|
|
#include <Void/Build/Package/Archive.hpp>
|
|
#include <Void/Build/Platform/FileOps.hpp>
|
|
#include <Void/Build/Provenance/Signing.hpp>
|
|
#include <Void/Build/Util/Json.hpp>
|
|
#include <Void/Build/Util/Sha256.hpp>
|
|
#include <Void/Core/Scope.hpp>
|
|
#include <Void/Data/StrictJson.hpp>
|
|
#include <Void/System/Path.hpp>
|
|
#include <Void/System/Types.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <format>
|
|
#include <set>
|
|
#include <unistd.h>
|
|
|
|
namespace Void::Build::Install {
|
|
namespace {
|
|
|
|
Void::Result::Error make_error(Void::System::SystemErrorCode code, std::string msg) {
|
|
return Void::System::make_system_error(code, std::move(msg));
|
|
}
|
|
|
|
bool is_safe_relative_dest(const std::filesystem::path &rel) {
|
|
if (rel.empty() || rel.is_absolute()) {
|
|
return false;
|
|
}
|
|
const auto norm = rel.lexically_normal();
|
|
for (const auto &part : norm) {
|
|
if (part == "..") {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string default_dest_for_kind(TargetKind kind) {
|
|
switch (kind) {
|
|
case TargetKind::Executable:
|
|
case TargetKind::Test:
|
|
return "bin";
|
|
case TargetKind::StaticLibrary:
|
|
case TargetKind::SharedLibrary:
|
|
case TargetKind::ObjectLibrary:
|
|
return "lib";
|
|
case TargetKind::HeaderOnly:
|
|
case TargetKind::CustomAction:
|
|
return "share";
|
|
}
|
|
return "share";
|
|
}
|
|
|
|
std::string escape_cmake_str(std::string_view s) {
|
|
std::string out;
|
|
for (const char c : s) {
|
|
if (c == '\\' || c == '"' || c == '$' || c == ';') {
|
|
out.push_back('\\');
|
|
}
|
|
out.push_back(c);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
void put_u16(std::string &out, std::uint16_t value) {
|
|
out.push_back(static_cast<char>(value & 0xFFu));
|
|
out.push_back(static_cast<char>((value >> 8) & 0xFFu));
|
|
}
|
|
|
|
void put_u32(std::string &out, std::uint32_t value) {
|
|
for (int shift = 0; shift < 32; shift += 8) {
|
|
out.push_back(static_cast<char>((value >> shift) & 0xFFu));
|
|
}
|
|
}
|
|
|
|
std::uint32_t crc32_bytes(std::string_view data) {
|
|
static const auto table = [] {
|
|
std::array<std::uint32_t, 256> 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<std::uint8_t>(ch)) & 0xFFu] ^ (crc >> 8);
|
|
}
|
|
return crc ^ 0xFFFFFFFFu;
|
|
}
|
|
|
|
// Stored (uncompressed) deterministic ZIP archive of the installed files.
|
|
Void::Result::Result<void, Void::Result::Error>
|
|
write_stored_zip(const std::filesystem::path &output,
|
|
const std::vector<Package::ArchiveEntrySpec> &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<Record> 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 = crc32_bytes(*content);
|
|
const auto size = static_cast<std::uint32_t>(content->size());
|
|
records.push_back(
|
|
{entry.archive_path, crc, size, static_cast<std::uint32_t>(local.size()), entry.mode});
|
|
put_u32(local, 0x04034b50u);
|
|
put_u16(local, 20); // version needed
|
|
put_u16(local, 0); // flags
|
|
put_u16(local, 0); // stored
|
|
put_u16(local, 0); // time
|
|
put_u16(local, 0x21); // 1980-01-01
|
|
put_u32(local, crc);
|
|
put_u32(local, size);
|
|
put_u32(local, size);
|
|
put_u16(local, static_cast<std::uint16_t>(entry.archive_path.size()));
|
|
put_u16(local, 0); // extra
|
|
local += entry.archive_path;
|
|
local += *content;
|
|
}
|
|
|
|
std::string central;
|
|
for (const auto &record : records) {
|
|
put_u32(central, 0x02014b50u);
|
|
put_u16(central, 0x031e); // made by UNIX
|
|
put_u16(central, 20);
|
|
put_u16(central, 0);
|
|
put_u16(central, 0);
|
|
put_u16(central, 0);
|
|
put_u16(central, 0x21);
|
|
put_u32(central, record.crc);
|
|
put_u32(central, record.size);
|
|
put_u32(central, record.size);
|
|
put_u16(central, static_cast<std::uint16_t>(record.name.size()));
|
|
put_u16(central, 0); // extra
|
|
put_u16(central, 0); // comment
|
|
put_u16(central, 0); // disk
|
|
put_u16(central, 0); // internal attrs
|
|
put_u32(central, (record.mode & 0777u) << 16);
|
|
put_u32(central, record.offset);
|
|
central += record.name;
|
|
}
|
|
|
|
std::string eocd;
|
|
put_u32(eocd, 0x06054b50u);
|
|
put_u16(eocd, 0);
|
|
put_u16(eocd, 0);
|
|
put_u16(eocd, static_cast<std::uint16_t>(records.size()));
|
|
put_u16(eocd, static_cast<std::uint16_t>(records.size()));
|
|
put_u32(eocd, static_cast<std::uint32_t>(central.size()));
|
|
put_u32(eocd, static_cast<std::uint32_t>(local.size()));
|
|
put_u16(eocd, 0);
|
|
|
|
std::string zip = local + central + eocd;
|
|
return Platform::atomic_write_file(output, zip, true);
|
|
}
|
|
|
|
void append_ar_member(std::string &out, std::string_view name, std::string_view data) {
|
|
std::string header;
|
|
std::string padded_name{name};
|
|
padded_name.push_back('/');
|
|
header.reserve(60);
|
|
header += padded_name;
|
|
header.resize(16, ' ');
|
|
header += std::format("{:<12}", 0); // mtime
|
|
header += std::format("{:<6}", 0); // uid
|
|
header += std::format("{:<6}", 0); // gid
|
|
header += std::format("{:<8}", 100644); // mode
|
|
header += std::format("{:<10}", data.size()); // size
|
|
header += "`\n";
|
|
out += header;
|
|
out += data;
|
|
if (data.size() % 2 != 0) {
|
|
out.push_back('\n');
|
|
}
|
|
}
|
|
|
|
// Debian package: ar container with debian-binary, control.tar and data.tar (uncompressed).
|
|
Void::Result::Result<void, Void::Result::Error>
|
|
write_deb_package(const std::filesystem::path &output, const InstallOptions &options,
|
|
const Graph::BuildGraph &graph,
|
|
const std::vector<Package::ArchiveEntrySpec> &entries) {
|
|
const auto scratch = output.parent_path() / (output.filename().string() + ".members");
|
|
std::filesystem::remove_all(scratch);
|
|
std::error_code ec;
|
|
std::filesystem::create_directories(scratch, ec);
|
|
auto cleanup = Void::Scope::on_exit([&] { std::filesystem::remove_all(scratch, ec); });
|
|
|
|
const auto data_tar = scratch / "data.tar";
|
|
auto data = Package::create_deterministic_tar(data_tar, entries, 0);
|
|
if (!data) {
|
|
return std::unexpected{data.error()};
|
|
}
|
|
|
|
std::uint64_t installed_size = 0;
|
|
for (const auto &entry : entries) {
|
|
if (const auto meta = Platform::stat_lstat(entry.source_file); meta && meta->exists) {
|
|
installed_size += meta->size;
|
|
}
|
|
}
|
|
std::string deb_arch = graph.profile.architecture;
|
|
std::ranges::transform(deb_arch, deb_arch.begin(), [](char c) {
|
|
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
});
|
|
if (deb_arch == "x86_64") {
|
|
deb_arch = "amd64";
|
|
} else if (deb_arch == "aarch64" || deb_arch == "arm64") {
|
|
deb_arch = "arm64";
|
|
}
|
|
const std::string control_text =
|
|
"Package: " + options.package_name + "\n" + "Version: " + options.package_version + "\n" +
|
|
"Architecture: " + deb_arch + "\n" +
|
|
"Maintainer: " +
|
|
(options.maintainer.empty() ? "Void Build <noreply@localhost>" : options.maintainer) +
|
|
"\n" + "Installed-Size: " + std::to_string(installed_size / 1024) + "\n" +
|
|
"Section: misc\nPriority: optional\n" + "Description: " +
|
|
(options.package_description.empty() ? options.package_name
|
|
: options.package_description) +
|
|
"\n";
|
|
|
|
const auto control_file = scratch / "control";
|
|
if (auto wrote = Platform::atomic_write_file(control_file, control_text, false); !wrote) {
|
|
return std::unexpected{wrote.error()};
|
|
}
|
|
const auto control_tar = scratch / "control.tar";
|
|
auto control = Package::create_deterministic_tar(
|
|
control_tar, {{"control", control_file, 0644u}}, 0);
|
|
if (!control) {
|
|
return std::unexpected{control.error()};
|
|
}
|
|
|
|
auto data_bytes = Void::FileSystem::read_file(data_tar);
|
|
if (!data_bytes) {
|
|
return std::unexpected{data_bytes.error()};
|
|
}
|
|
auto control_bytes = Void::FileSystem::read_file(control_tar);
|
|
if (!control_bytes) {
|
|
return std::unexpected{control_bytes.error()};
|
|
}
|
|
|
|
std::string deb = "!<arch>\n";
|
|
append_ar_member(deb, "debian-binary", "2.0\n");
|
|
append_ar_member(deb, "control.tar", *control_bytes);
|
|
append_ar_member(deb, "data.tar", *data_bytes);
|
|
return Platform::atomic_write_file(output, deb, true);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Void::Result::Result<InstallReport, Void::Result::Error>
|
|
install_project(const Graph::ProjectDescription &description, const Graph::BuildGraph &graph,
|
|
const InstallOptions &options) {
|
|
if (options.prefix.empty()) {
|
|
return std::unexpected{
|
|
make_error(Void::System::SystemErrorCode::invalid_argument, "install prefix is empty")};
|
|
}
|
|
if (options.package_name.empty()) {
|
|
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
"install package_name is empty")};
|
|
}
|
|
|
|
std::error_code ec;
|
|
const auto prefix_abs = std::filesystem::weakly_canonical(options.prefix, ec);
|
|
const auto proj_root = graph.project_root;
|
|
if (Platform::path_is_within(proj_root, prefix_abs) &&
|
|
!Platform::path_is_within(graph.profile_build_dir, prefix_abs) && prefix_abs == proj_root) {
|
|
return std::unexpected{make_error(Void::System::SystemErrorCode::permission_denied,
|
|
"refusing to install directly onto project source root")};
|
|
}
|
|
|
|
// Build complete list of staged copy operations and detect destination collisions upfront!
|
|
struct StagedItem {
|
|
std::filesystem::path src;
|
|
std::filesystem::path rel_dest;
|
|
std::uint32_t mode{0644};
|
|
std::string target_name;
|
|
};
|
|
std::vector<StagedItem> items;
|
|
std::set<std::filesystem::path> dest_set;
|
|
|
|
auto add_item =
|
|
[&](const std::filesystem::path &src, const std::filesystem::path &rel_dest,
|
|
std::uint32_t mode,
|
|
std::string target_name = {}) -> Void::Result::Result<void, Void::Result::Error> {
|
|
if (!is_safe_relative_dest(rel_dest)) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::permission_denied,
|
|
std::format("install destination '{}' escapes prefix", rel_dest.string()))};
|
|
}
|
|
const auto norm = rel_dest.lexically_normal();
|
|
if (!dest_set.insert(norm).second) {
|
|
return std::unexpected{
|
|
make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
std::format("install destination collision on '{}'", norm.string()))};
|
|
}
|
|
if (!std::filesystem::is_regular_file(src, ec)) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("install source artifact '{}' does not exist (build target first)",
|
|
src.string()))};
|
|
}
|
|
items.push_back({src, norm, mode, std::move(target_name)});
|
|
return {};
|
|
};
|
|
|
|
// 1. Installed targets.
|
|
struct ExportedTarget {
|
|
std::string name;
|
|
TargetKind kind;
|
|
std::filesystem::path rel_artifact;
|
|
std::vector<std::string> public_defines;
|
|
std::vector<std::string> public_compile_opts;
|
|
std::vector<std::string> local_deps;
|
|
};
|
|
std::vector<ExportedTarget> exported_targets;
|
|
std::set<std::string> bundled_library_names;
|
|
std::vector<std::filesystem::path> bundled_libraries;
|
|
|
|
for (const auto &trec : description.targets) {
|
|
if (!trec.install_rule) {
|
|
continue;
|
|
}
|
|
if (options.component && trec.install_rule->component != *options.component) {
|
|
continue;
|
|
}
|
|
const std::string dest_dir = trec.install_rule->destination.empty()
|
|
? default_dest_for_kind(trec.kind)
|
|
: trec.install_rule->destination;
|
|
const auto sum_it =
|
|
std::ranges::find(graph.targets, trec.name, &Graph::ResolvedTargetSummary::name);
|
|
if (sum_it == graph.targets.end()) {
|
|
continue;
|
|
}
|
|
|
|
ExportedTarget et;
|
|
et.name = trec.name;
|
|
et.kind = trec.kind;
|
|
for (const auto &d : trec.defines) {
|
|
if (d.visibility != Visibility::Private) {
|
|
et.public_defines.push_back(d.value);
|
|
}
|
|
}
|
|
for (const auto &o : trec.scoped_compile_options) {
|
|
if (o.visibility != Visibility::Private) {
|
|
et.public_compile_opts.push_back(o.value);
|
|
}
|
|
}
|
|
for (const auto &l : trec.links) {
|
|
if (l.kind == LinkKind::Local &&
|
|
(l.visibility != Visibility::Private || trec.kind == TargetKind::StaticLibrary)) {
|
|
et.local_deps.push_back(l.target);
|
|
}
|
|
}
|
|
|
|
if (!sum_it->primary_outputs.empty()) {
|
|
const auto &art = sum_it->primary_outputs.front();
|
|
const auto rel_dest = std::filesystem::path{dest_dir} / art.filename();
|
|
const std::uint32_t mode =
|
|
(trec.kind == TargetKind::Executable || trec.kind == TargetKind::SharedLibrary)
|
|
? 0755u
|
|
: 0644u;
|
|
if (auto r = add_item(art, rel_dest, mode, trec.name); !r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
et.rel_artifact = rel_dest;
|
|
}
|
|
// Bundle runtime shared libraries so the prefix is self-contained.
|
|
if (options.bundle_runtime_dependencies) {
|
|
for (const auto &library_path : sum_it->runtime_shared_libraries) {
|
|
const std::string filename = library_path.filename().string();
|
|
if (!bundled_library_names.insert(filename).second) {
|
|
continue;
|
|
}
|
|
const auto rel_dest = std::filesystem::path{"lib"} / filename;
|
|
if (auto r = add_item(library_path, rel_dest, 0755u, trec.name); !r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
bundled_libraries.push_back(library_path);
|
|
}
|
|
}
|
|
exported_targets.push_back(std::move(et));
|
|
}
|
|
|
|
// 2. Project file & directory install rules (headers/data).
|
|
for (const auto &rule : description.install_rules) {
|
|
if (options.component && rule.component != *options.component) {
|
|
continue;
|
|
}
|
|
const auto src_abs = std::filesystem::path{rule.source_path}.is_absolute()
|
|
? std::filesystem::path{rule.source_path}
|
|
: (proj_root / rule.source_path);
|
|
if (rule.is_directory) {
|
|
if (!std::filesystem::is_directory(src_abs, ec)) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("install directory '{}' does not exist", src_abs.string()))};
|
|
}
|
|
for (const auto &ent : std::filesystem::recursive_directory_iterator(src_abs, ec)) {
|
|
if (ent.is_regular_file(ec)) {
|
|
const auto rel = ent.path().lexically_relative(src_abs);
|
|
if (auto r = add_item(ent.path(), std::filesystem::path{rule.destination} / rel,
|
|
0644u);
|
|
!r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
const auto rel_dest = std::filesystem::path{rule.destination} / src_abs.filename();
|
|
if (auto r = add_item(src_abs, rel_dest, 0644u); !r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Stage everything privately first so failed installs never leave broken metadata.
|
|
const auto stage_root = graph.profile_build_dir /
|
|
std::format("install-stage-{}", static_cast<long long>(::getpid()));
|
|
std::filesystem::remove_all(stage_root, ec);
|
|
std::filesystem::create_directories(stage_root, ec);
|
|
auto stage_guard = Void::Scope::on_exit([&] { std::filesystem::remove_all(stage_root, ec); });
|
|
|
|
std::vector<std::filesystem::path> owned_rel_files;
|
|
for (const auto &item : items) {
|
|
const auto staged = stage_root / item.rel_dest;
|
|
auto copied = Platform::copy_isolated(item.src, staged, item.mode);
|
|
if (!copied) {
|
|
return std::unexpected{copied.error()};
|
|
}
|
|
owned_rel_files.push_back(item.rel_dest);
|
|
}
|
|
|
|
// 4. Generate relocatable native package metadata (ZERO build/snapshot paths!).
|
|
std::optional<std::filesystem::path> native_rel;
|
|
if (options.export_native_metadata) {
|
|
using Util::json_array;
|
|
using Util::json_quote;
|
|
|
|
native_rel = std::filesystem::path{"share"} / "vbs" / options.package_name / "package.json";
|
|
std::string pjson =
|
|
"{\"schema_version\":1,\"name\":" + json_quote(options.package_name) +
|
|
",\"version\":" + json_quote(options.package_version) +
|
|
",\"profile\":" + json_quote(graph.profile.name) +
|
|
",\"compiler\":" + json_quote(compiler_name(graph.profile.compiler)) +
|
|
",\"os\":" + json_quote(graph.profile.os) +
|
|
",\"architecture\":" + json_quote(graph.profile.architecture) +
|
|
",\"include_dir\":\"include\",\"targets\":" +
|
|
json_array(exported_targets,
|
|
[](const ExportedTarget &et) {
|
|
return "{\"name\":" + json_quote(et.name) +
|
|
",\"kind\":" + json_quote(target_kind_name(et.kind)) +
|
|
",\"artifact\":" + json_quote(et.rel_artifact.generic_string()) +
|
|
",\"defines\":" + json_array(et.public_defines, json_quote) +
|
|
",\"compile_options\":" +
|
|
json_array(et.public_compile_opts, json_quote) +
|
|
",\"dependencies\":" + json_array(et.local_deps, json_quote) +
|
|
'}';
|
|
}) +
|
|
"}\n";
|
|
if (auto w = Platform::atomic_write_file(stage_root / *native_rel, pjson, false); !w) {
|
|
return std::unexpected{w.error()};
|
|
}
|
|
owned_rel_files.push_back(*native_rel);
|
|
}
|
|
|
|
// 5. Generate relocatable CMake Config + Targets files using ${_vbs_prefix} relative paths!
|
|
std::optional<std::filesystem::path> cmake_rel;
|
|
if (options.export_cmake_config) {
|
|
const auto cmake_dir = std::filesystem::path{"lib"} / "cmake" / options.package_name;
|
|
cmake_rel = cmake_dir / (options.package_name + "Config.cmake");
|
|
const auto targets_rel = cmake_dir / (options.package_name + "Targets.cmake");
|
|
|
|
std::string cfg_cmake =
|
|
"# Relocatable CMake config generated by VoidBuildSystem\n"
|
|
"get_filename_component(_vbs_prefix \"${CMAKE_CURRENT_LIST_DIR}/../../..\" ABSOLUTE)\n"
|
|
"include(\"${CMAKE_CURRENT_LIST_DIR}/" +
|
|
options.package_name + "Targets.cmake\")\n";
|
|
|
|
std::string tgt_cmake =
|
|
"# Relocatable CMake imported targets generated by VoidBuildSystem\n"
|
|
"get_filename_component(_vbs_prefix \"${CMAKE_CURRENT_LIST_DIR}/../../..\" ABSOLUTE)\n";
|
|
for (const auto &et : exported_targets) {
|
|
const std::string full = options.package_name + "::" + et.name;
|
|
std::string cm_type = "STATIC";
|
|
if (et.kind == TargetKind::SharedLibrary) {
|
|
cm_type = "SHARED";
|
|
} else if (et.kind == TargetKind::HeaderOnly) {
|
|
cm_type = "INTERFACE";
|
|
} else if (et.kind == TargetKind::Executable) {
|
|
continue;
|
|
}
|
|
tgt_cmake += "if(NOT TARGET " + full + ")\n";
|
|
tgt_cmake += " add_library(" + full + " " + cm_type + " IMPORTED)\n";
|
|
tgt_cmake += " set_target_properties(" + full + " PROPERTIES\n";
|
|
if (!et.rel_artifact.empty()) {
|
|
tgt_cmake += " IMPORTED_LOCATION \"${_vbs_prefix}/" +
|
|
escape_cmake_str(et.rel_artifact.generic_string()) + "\"\n";
|
|
}
|
|
tgt_cmake += " INTERFACE_INCLUDE_DIRECTORIES \"${_vbs_prefix}/include\"\n";
|
|
if (!et.public_defines.empty()) {
|
|
tgt_cmake += " INTERFACE_COMPILE_DEFINITIONS \"";
|
|
for (std::size_t i = 0; i < et.public_defines.size(); ++i) {
|
|
if (i > 0) {
|
|
tgt_cmake += ";";
|
|
}
|
|
tgt_cmake += escape_cmake_str(et.public_defines[i]);
|
|
}
|
|
tgt_cmake += "\"\n";
|
|
}
|
|
if (!et.local_deps.empty()) {
|
|
tgt_cmake += " INTERFACE_LINK_LIBRARIES \"";
|
|
for (std::size_t i = 0; i < et.local_deps.size(); ++i) {
|
|
if (i > 0) {
|
|
tgt_cmake += ";";
|
|
}
|
|
tgt_cmake += options.package_name + "::" + et.local_deps[i];
|
|
}
|
|
tgt_cmake += "\"\n";
|
|
}
|
|
tgt_cmake += " )\nendif()\n";
|
|
}
|
|
|
|
if (auto w = Platform::atomic_write_file(stage_root / *cmake_rel, cfg_cmake, false); !w) {
|
|
return std::unexpected{w.error()};
|
|
}
|
|
if (auto w = Platform::atomic_write_file(stage_root / targets_rel, tgt_cmake, false); !w) {
|
|
return std::unexpected{w.error()};
|
|
}
|
|
owned_rel_files.push_back(*cmake_rel);
|
|
owned_rel_files.push_back(targets_rel);
|
|
}
|
|
|
|
// 6. Publish staged files into prefix atomically and write ownership manifest.
|
|
std::filesystem::create_directories(prefix_abs, ec);
|
|
InstallReport report;
|
|
report.prefix = prefix_abs;
|
|
|
|
for (const auto &rel : owned_rel_files) {
|
|
const auto staged = stage_root / rel;
|
|
const auto final_dst = prefix_abs / rel;
|
|
auto meta = Platform::stat_lstat(staged);
|
|
auto copied = Platform::copy_isolated(staged, final_dst, meta ? meta->mode : 0644u);
|
|
if (!copied) {
|
|
return std::unexpected{copied.error()};
|
|
}
|
|
report.installed_files.push_back(final_dst);
|
|
}
|
|
|
|
report.manifest_path = prefix_abs / ".vbs-install-manifest.json";
|
|
if (native_rel) {
|
|
report.native_package_file = prefix_abs / *native_rel;
|
|
}
|
|
if (cmake_rel) {
|
|
report.cmake_config_file = prefix_abs / *cmake_rel;
|
|
}
|
|
|
|
const std::string manifest_json =
|
|
"{\"schema_version\":1,\"package\":" + Util::json_quote(options.package_name) +
|
|
",\"files\":" +
|
|
Util::json_array(
|
|
owned_rel_files,
|
|
[](const std::filesystem::path &p) { return Util::json_quote(p.generic_string()); }) +
|
|
"}\n";
|
|
auto mw = Platform::atomic_write_file(report.manifest_path, manifest_json, true);
|
|
if (!mw) {
|
|
return std::unexpected{mw.error()};
|
|
}
|
|
report.bundled_libraries = std::move(bundled_libraries);
|
|
|
|
// 7. Optional deterministic installer archive of the published prefix.
|
|
if (options.archive_path) {
|
|
std::vector<Package::ArchiveEntrySpec> archive_entries;
|
|
archive_entries.reserve(report.installed_files.size());
|
|
for (const auto &file : report.installed_files) {
|
|
const auto meta = Platform::stat_lstat(file);
|
|
archive_entries.push_back(
|
|
{file.lexically_relative(prefix_abs).generic_string(), file,
|
|
meta ? (meta->mode & 0777u) : 0644u});
|
|
}
|
|
|
|
if (options.archive_format == "zip") {
|
|
if (auto zipped = write_stored_zip(*options.archive_path, archive_entries); !zipped) {
|
|
return std::unexpected{zipped.error()};
|
|
}
|
|
} else if (options.archive_format == "deb") {
|
|
if (auto packed = write_deb_package(*options.archive_path, options, graph,
|
|
archive_entries);
|
|
!packed) {
|
|
return std::unexpected{packed.error()};
|
|
}
|
|
} else {
|
|
const bool archive_as_script = options.archive_format == "sh";
|
|
const auto archive_target = archive_as_script
|
|
? stage_root / "installer-payload.tar"
|
|
: std::filesystem::path{*options.archive_path};
|
|
auto archived = Package::create_deterministic_tar(archive_target,
|
|
std::move(archive_entries), 0);
|
|
if (!archived) {
|
|
return std::unexpected{archived.error()};
|
|
}
|
|
|
|
if (archive_as_script) {
|
|
// Self-extracting installer: a POSIX sh header followed by the tar payload.
|
|
const auto make_header = [&](std::size_t payload_line) {
|
|
return "#!/bin/sh\n"
|
|
"# VoidBuildSystem self-extracting installer for " +
|
|
options.package_name + " " + options.package_version +
|
|
"\n"
|
|
"# Usage: sh <installer> [--prefix DIR] (default: ${PREFIX:-/usr/local})\n"
|
|
"set -eu\n"
|
|
"prefix=\"${PREFIX:-/usr/local}\"\n"
|
|
"if [ \"${1:-}\" = \"--prefix\" ]; then\n"
|
|
" prefix=\"${2:?--prefix requires a directory}\"\n"
|
|
"fi\n"
|
|
"tmp=\"$(mktemp -d)\"\n"
|
|
"trap 'rm -rf \"$tmp\"' EXIT\n"
|
|
"tail -n +" +
|
|
std::to_string(payload_line) +
|
|
" \"$0\" > \"$tmp/payload.tar\"\n"
|
|
"mkdir -p \"$prefix\"\n"
|
|
"tar -x -f \"$tmp/payload.tar\" -C \"$prefix\"\n"
|
|
"echo \"installed " +
|
|
options.package_name +
|
|
" to $prefix\"\n"
|
|
"exit 0\n";
|
|
};
|
|
std::size_t header_lines = 0;
|
|
for (const char c : make_header(0)) {
|
|
if (c == '\n') {
|
|
++header_lines;
|
|
}
|
|
}
|
|
const std::string header = make_header(header_lines + 1);
|
|
|
|
auto payload = Void::FileSystem::read_file(archive_target);
|
|
if (!payload) {
|
|
return std::unexpected{payload.error()};
|
|
}
|
|
std::string script = header;
|
|
script += *payload;
|
|
if (auto written =
|
|
Platform::atomic_write_file(*options.archive_path, script, true, 0755);
|
|
!written) {
|
|
return std::unexpected{written.error()};
|
|
}
|
|
}
|
|
}
|
|
report.archive_path = *options.archive_path;
|
|
|
|
// Optional detached signature and checksums over the final archive bytes.
|
|
if (options.signing_seed_path) {
|
|
auto bytes = Void::FileSystem::read_file(*options.archive_path);
|
|
if (!bytes) {
|
|
return std::unexpected{bytes.error()};
|
|
}
|
|
report.archive_sha256 = Util::sha256_hex(*bytes);
|
|
report.checksum_path = options.archive_path->string() + ".sha256";
|
|
if (auto written = Platform::atomic_write_file(
|
|
*report.checksum_path,
|
|
report.archive_sha256 + " " + options.archive_path->filename().string() + "\n",
|
|
true);
|
|
!written) {
|
|
return std::unexpected{written.error()};
|
|
}
|
|
|
|
auto signature = Provenance::sign_message(
|
|
std::as_bytes(std::span<const char>{bytes->data(), bytes->size()}),
|
|
*options.signing_seed_path);
|
|
if (!signature) {
|
|
return std::unexpected{signature.error()};
|
|
}
|
|
auto public_key = Provenance::public_key_from_seed(*options.signing_seed_path);
|
|
if (!public_key) {
|
|
return std::unexpected{public_key.error()};
|
|
}
|
|
report.signature_path = options.archive_path->string() + ".sig";
|
|
if (auto written = Platform::atomic_write_file(*report.signature_path,
|
|
*signature + "\n", true);
|
|
!written) {
|
|
return std::unexpected{written.error()};
|
|
}
|
|
report.public_key_path = options.archive_path->string() + ".pub";
|
|
if (auto written = Platform::atomic_write_file(*report.public_key_path,
|
|
*public_key + "\n", true);
|
|
!written) {
|
|
return std::unexpected{written.error()};
|
|
}
|
|
}
|
|
}
|
|
return report;
|
|
}
|
|
|
|
Void::Result::Result<std::size_t, Void::Result::Error>
|
|
uninstall_prefix(const std::filesystem::path &prefix) {
|
|
const auto manifest_path = prefix / ".vbs-install-manifest.json";
|
|
auto text = Void::FileSystem::read_file(manifest_path);
|
|
if (!text) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("no VBS install manifest found at '{}'", manifest_path.string()))};
|
|
}
|
|
auto parsed = Void::Json::Strict::parse(*text);
|
|
if (!parsed) {
|
|
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
"corrupt VBS install manifest")};
|
|
}
|
|
const auto *root = std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
|
|
if (!root) {
|
|
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
"invalid VBS install manifest")};
|
|
}
|
|
|
|
std::size_t removed = 0;
|
|
std::error_code ec;
|
|
const auto canon_prefix = std::filesystem::weakly_canonical(prefix, ec);
|
|
const auto fit = root->find("files");
|
|
if (fit != root->end()) {
|
|
if (const auto *arr = std::get_if<Void::Json::Strict::Value::Array>(&fit->second.value)) {
|
|
for (const auto &v : *arr) {
|
|
if (const auto *s = std::get_if<std::string>(&v.value)) {
|
|
const std::filesystem::path rel{*s};
|
|
if (!is_safe_relative_dest(rel)) {
|
|
continue;
|
|
}
|
|
const auto target = (canon_prefix / rel).lexically_normal();
|
|
if (Platform::path_is_within(canon_prefix, target) &&
|
|
std::filesystem::is_regular_file(target, ec)) {
|
|
std::filesystem::remove(target, ec);
|
|
if (!ec) {
|
|
++removed;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
std::filesystem::remove(manifest_path, ec);
|
|
return removed;
|
|
}
|
|
|
|
} // namespace Void::Build::Install
|