662 lines
27 KiB
C++
662 lines
27 KiB
C++
#include <Void/Build/Snapshot/Snapshot.hpp>
|
|
|
|
#include <Void/Build/Platform/FileOps.hpp>
|
|
#include <Void/Build/Util/Json.hpp>
|
|
#include <Void/Build/Util/Sha256.hpp>
|
|
#include <Void/System/Types.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cerrno>
|
|
#include <dirent.h>
|
|
#include <fcntl.h>
|
|
#include <format>
|
|
#include <functional>
|
|
#include <set>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
namespace Void::Build::Snapshot {
|
|
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_name(std::string_view name, const std::vector<std::string> &excluded) {
|
|
if (name == ".env" || name.starts_with(".env.")) {
|
|
return name != ".env.example";
|
|
}
|
|
if (name.ends_with(".pem") || name.ends_with(".key")) {
|
|
return true;
|
|
}
|
|
return std::ranges::contains(excluded, name);
|
|
}
|
|
|
|
bool path_has_excluded_component(const std::filesystem::path &rel_path,
|
|
const std::vector<std::string> &excluded) {
|
|
for (const auto &part : rel_path) {
|
|
const auto s = part.string();
|
|
if (s.empty() || s == ".") {
|
|
continue;
|
|
}
|
|
if (is_excluded_name(s, excluded)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Void::Result::Result<std::vector<std::string>, Void::Result::Error>
|
|
read_sorted_dir_entries(const std::filesystem::path &dir_path,
|
|
const std::vector<std::string> &excluded) {
|
|
const int fd = ::open(dir_path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
|
if (fd < 0) {
|
|
const int err = errno;
|
|
return std::unexpected{make_error(
|
|
Void::System::map_errno_to_system_code(err),
|
|
std::format("snapshot: cannot open directory '{}'", dir_path.string()), err)};
|
|
}
|
|
DIR *dir = ::fdopendir(fd);
|
|
if (!dir) {
|
|
const int err = errno;
|
|
(void)::close(fd);
|
|
return std::unexpected{
|
|
make_error(Void::System::map_errno_to_system_code(err),
|
|
std::format("snapshot: fdopendir failed for '{}'", dir_path.string()), err)};
|
|
}
|
|
std::vector<std::string> names;
|
|
errno = 0;
|
|
while (const struct dirent *ent = ::readdir(dir)) {
|
|
const std::string_view name{ent->d_name};
|
|
if (name == "." || name == "..") {
|
|
continue;
|
|
}
|
|
if (is_excluded_name(name, excluded)) {
|
|
continue;
|
|
}
|
|
names.emplace_back(name);
|
|
}
|
|
const int read_err = errno;
|
|
(void)::closedir(dir);
|
|
if (read_err != 0) {
|
|
return std::unexpected{make_error(
|
|
Void::System::map_errno_to_system_code(read_err),
|
|
std::format("snapshot: readdir failed for '{}'", dir_path.string()), read_err)};
|
|
}
|
|
std::ranges::sort(names);
|
|
return names;
|
|
}
|
|
|
|
std::string normalize_logical(const std::filesystem::path &rel) {
|
|
std::string s = rel.lexically_normal().generic_string();
|
|
if (s == ".") {
|
|
return "";
|
|
}
|
|
if (s.starts_with("./")) {
|
|
s.erase(0, 2);
|
|
}
|
|
return s;
|
|
}
|
|
|
|
std::string compute_snapshot_id(const InputSnapshot &snap) {
|
|
Util::Sha256Hasher hasher;
|
|
hasher.update_length_prefixed("vbs-snapshot-v1");
|
|
hasher.update_u64(static_cast<std::uint64_t>(snap.reproducibility_epoch));
|
|
hasher.update_u64(snap.entries.size());
|
|
for (const auto &[logical, entry] : snap.entries) {
|
|
hasher.update_length_prefixed(logical);
|
|
hasher.update_u64(static_cast<std::uint64_t>(entry.kind));
|
|
hasher.update_length_prefixed(entry.sha256);
|
|
hasher.update_u64(entry.size);
|
|
hasher.update_u64(entry.mode & 0777u);
|
|
hasher.update_length_prefixed(entry.symlink_target);
|
|
}
|
|
hasher.update_u64(snap.memberships.size());
|
|
for (const auto &[dir, mem] : snap.memberships) {
|
|
hasher.update_length_prefixed(dir);
|
|
hasher.update_u64(mem.entries.size());
|
|
for (const auto &name : mem.entries) {
|
|
hasher.update_length_prefixed(name);
|
|
}
|
|
}
|
|
hasher.update_u64(snap.lookup_guards.size());
|
|
for (const auto &guard : snap.lookup_guards) {
|
|
hasher.update_u64(guard.search_roots.size());
|
|
for (const auto &r : guard.search_roots) {
|
|
hasher.update_length_prefixed(r);
|
|
}
|
|
hasher.update_length_prefixed(guard.lookup_name);
|
|
hasher.update_u64(static_cast<std::uint64_t>(guard.matched_root_index + 1));
|
|
hasher.update_length_prefixed(guard.matched_sha256);
|
|
}
|
|
return hasher.finalize_hex();
|
|
}
|
|
|
|
Void::Result::Result<LookupGuard, Void::Result::Error>
|
|
evaluate_one_guard(const std::filesystem::path &project_root,
|
|
const std::vector<std::filesystem::path> &allowed_external_roots,
|
|
const std::vector<std::string> &search_roots, const std::string &lookup_name) {
|
|
LookupGuard guard;
|
|
guard.search_roots = search_roots;
|
|
guard.lookup_name = lookup_name;
|
|
guard.matched_root_index = -1;
|
|
|
|
for (std::size_t i = 0; i < search_roots.size(); ++i) {
|
|
const std::filesystem::path root_path{search_roots[i]};
|
|
const auto abs_root = root_path.is_absolute() ? root_path : (project_root / root_path);
|
|
const auto candidate = abs_root / lookup_name;
|
|
auto meta = Platform::stat_lstat(candidate);
|
|
if (!meta) {
|
|
return std::unexpected{meta.error()};
|
|
}
|
|
if (!meta->exists) {
|
|
continue;
|
|
}
|
|
auto confined =
|
|
Platform::resolve_confined_path(project_root, candidate, allowed_external_roots);
|
|
if (!confined) {
|
|
return std::unexpected{confined.error()};
|
|
}
|
|
auto target_meta = Platform::stat_lstat(*confined);
|
|
if (!target_meta) {
|
|
return std::unexpected{target_meta.error()};
|
|
}
|
|
if (target_meta->exists && target_meta->is_regular) {
|
|
auto digest = Util::sha256_file_hex(*confined);
|
|
if (!digest) {
|
|
return std::unexpected{digest.error()};
|
|
}
|
|
guard.matched_root_index = static_cast<int>(i);
|
|
guard.matched_sha256 = std::move(*digest);
|
|
return guard;
|
|
}
|
|
}
|
|
return guard;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Void::Result::Result<InputSnapshot, Void::Result::Error> capture_snapshot(
|
|
const SnapshotOptions &options, const std::vector<std::filesystem::path> &explicit_files,
|
|
const std::vector<std::filesystem::path> &directory_roots,
|
|
const std::vector<std::pair<std::vector<std::string>, std::string>> &lookup_queries) {
|
|
std::error_code ec;
|
|
const auto canonical_root = std::filesystem::weakly_canonical(options.project_root, ec);
|
|
if (ec || !std::filesystem::is_directory(canonical_root)) {
|
|
return std::unexpected{
|
|
make_error(Void::System::SystemErrorCode::not_found,
|
|
std::format("snapshot: project root '{}' is not a directory",
|
|
options.project_root.string()))};
|
|
}
|
|
|
|
const int attempts = std::max(1, options.max_revalidate_retries);
|
|
for (int attempt = 0; attempt < attempts; ++attempt) {
|
|
InputSnapshot snap;
|
|
snap.project_root = canonical_root;
|
|
snap.reproducibility_epoch = options.reproducibility_epoch;
|
|
|
|
std::map<std::string, std::filesystem::path, std::less<>> source_files_to_verify;
|
|
std::map<std::string, std::filesystem::path, std::less<>> dirs_to_verify;
|
|
|
|
auto capture_single_file = [&](const std::filesystem::path &raw_path)
|
|
-> Void::Result::Result<void, Void::Result::Error> {
|
|
const auto abs_path = raw_path.is_absolute() ? raw_path : (canonical_root / raw_path);
|
|
auto lmeta = Platform::stat_lstat(abs_path);
|
|
if (!lmeta) {
|
|
return std::unexpected{lmeta.error()};
|
|
}
|
|
if (!lmeta->exists) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("snapshot: required input '{}' does not exist", raw_path.string()),
|
|
ENOENT)};
|
|
}
|
|
auto confined = Platform::resolve_confined_path(canonical_root, abs_path,
|
|
options.allowed_external_roots);
|
|
if (!confined) {
|
|
return std::unexpected{confined.error()};
|
|
}
|
|
std::filesystem::path rel = abs_path.lexically_relative(canonical_root);
|
|
std::string logical = normalize_logical(rel);
|
|
if (logical.empty() || logical.starts_with("..")) {
|
|
logical = "external/" + Util::sha256_hex(confined->string()).substr(0, 16) + "/" +
|
|
confined->filename().string();
|
|
}
|
|
|
|
if (lmeta->is_symlink) {
|
|
SnapshotEntry sym;
|
|
sym.logical_path = logical;
|
|
sym.kind = EntryKind::Symlink;
|
|
sym.symlink_target = lmeta->symlink_target;
|
|
sym.mode = lmeta->mode;
|
|
// Hash the resolved target content if it is a regular file so retargeting or
|
|
// editing target bytes invalidates the snapshot.
|
|
auto target_meta = Platform::stat_lstat(*confined);
|
|
if (!target_meta) {
|
|
return std::unexpected{target_meta.error()};
|
|
}
|
|
if (target_meta->exists && target_meta->is_regular) {
|
|
auto digest = Util::sha256_file_hex(*confined);
|
|
if (!digest) {
|
|
return std::unexpected{digest.error()};
|
|
}
|
|
sym.sha256 = std::move(*digest);
|
|
sym.size = target_meta->size;
|
|
source_files_to_verify[logical] = *confined;
|
|
}
|
|
snap.entries[logical] = std::move(sym);
|
|
return {};
|
|
}
|
|
|
|
if (!lmeta->is_regular) {
|
|
return std::unexpected{
|
|
make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
std::format("snapshot: '{}' is not a regular file or symlink",
|
|
abs_path.string()))};
|
|
}
|
|
|
|
SnapshotEntry entry;
|
|
entry.logical_path = logical;
|
|
entry.kind = EntryKind::RegularFile;
|
|
entry.size = lmeta->size;
|
|
entry.mode = lmeta->mode;
|
|
|
|
if (options.materialize) {
|
|
const auto staging_blob =
|
|
options.snapshot_store_dir / "blobs" /
|
|
(Util::sha256_hex(logical + "#" + std::to_string(attempt)) + ".stage");
|
|
auto copied = Platform::copy_isolated(*confined, staging_blob, entry.mode);
|
|
if (!copied) {
|
|
return std::unexpected{copied.error()};
|
|
}
|
|
auto digest = Util::sha256_file_hex(staging_blob);
|
|
if (!digest) {
|
|
std::filesystem::remove(staging_blob, ec);
|
|
return std::unexpected{digest.error()};
|
|
}
|
|
entry.sha256 = std::move(*digest);
|
|
const auto final_blob =
|
|
options.snapshot_store_dir / "blobs" / entry.sha256.substr(0, 2) / entry.sha256;
|
|
std::filesystem::create_directories(final_blob.parent_path(), ec);
|
|
if (!std::filesystem::exists(final_blob, ec)) {
|
|
(void)Platform::set_canonical_mtime(staging_blob,
|
|
options.reproducibility_epoch);
|
|
std::filesystem::rename(staging_blob, final_blob, ec);
|
|
if (ec) {
|
|
std::filesystem::remove(staging_blob, ec);
|
|
}
|
|
} else {
|
|
std::filesystem::remove(staging_blob, ec);
|
|
}
|
|
entry.materialized_path = final_blob;
|
|
} else {
|
|
auto digest = Util::sha256_file_hex(*confined);
|
|
if (!digest) {
|
|
return std::unexpected{digest.error()};
|
|
}
|
|
entry.sha256 = std::move(*digest);
|
|
}
|
|
|
|
source_files_to_verify[logical] = *confined;
|
|
snap.entries[logical] = std::move(entry);
|
|
return {};
|
|
};
|
|
|
|
std::function<Void::Result::Result<void, Void::Result::Error>(
|
|
const std::filesystem::path &, std::set<std::pair<std::uint64_t, std::uint64_t>> &)>
|
|
walk_dir;
|
|
walk_dir = [&](const std::filesystem::path &dir_path,
|
|
std::set<std::pair<std::uint64_t, std::uint64_t>> &visited_dirs)
|
|
-> Void::Result::Result<void, Void::Result::Error> {
|
|
auto confined = Platform::resolve_confined_path(canonical_root, dir_path,
|
|
options.allowed_external_roots);
|
|
if (!confined) {
|
|
return std::unexpected{confined.error()};
|
|
}
|
|
auto meta = Platform::stat_lstat(*confined);
|
|
if (!meta) {
|
|
return std::unexpected{meta.error()};
|
|
}
|
|
if (!meta->exists) {
|
|
// Record empty membership for absent search roots so creation invalidates.
|
|
const std::string logical_dir =
|
|
normalize_logical(dir_path.lexically_relative(canonical_root));
|
|
snap.memberships[logical_dir] = DirectoryMembership{logical_dir, {}};
|
|
dirs_to_verify[logical_dir] = dir_path;
|
|
return {};
|
|
}
|
|
if (!meta->is_directory) {
|
|
return {};
|
|
}
|
|
if (!visited_dirs.insert({meta->dev, meta->ino}).second) {
|
|
return {};
|
|
}
|
|
|
|
auto entries_res = read_sorted_dir_entries(*confined, options.excluded_names);
|
|
if (!entries_res) {
|
|
return std::unexpected{entries_res.error()};
|
|
}
|
|
const std::string logical_dir =
|
|
normalize_logical(dir_path.lexically_relative(canonical_root));
|
|
snap.memberships[logical_dir] = DirectoryMembership{logical_dir, *entries_res};
|
|
dirs_to_verify[logical_dir] = *confined;
|
|
|
|
for (const auto &name : *entries_res) {
|
|
const auto child = dir_path / name;
|
|
const auto child_rel = child.lexically_relative(canonical_root);
|
|
if (path_has_excluded_component(child_rel, options.excluded_names)) {
|
|
continue;
|
|
}
|
|
auto cmeta = Platform::stat_lstat(child);
|
|
if (!cmeta) {
|
|
return std::unexpected{cmeta.error()};
|
|
}
|
|
if (!cmeta->exists) {
|
|
continue;
|
|
}
|
|
if (cmeta->is_directory) {
|
|
auto sub = walk_dir(child, visited_dirs);
|
|
if (!sub) {
|
|
return sub;
|
|
}
|
|
} else if (cmeta->is_regular || cmeta->is_symlink) {
|
|
auto cap = capture_single_file(child);
|
|
if (!cap) {
|
|
return cap;
|
|
}
|
|
}
|
|
}
|
|
return {};
|
|
};
|
|
|
|
for (const auto &file : explicit_files) {
|
|
auto r = capture_single_file(file);
|
|
if (!r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
}
|
|
std::set<std::pair<std::uint64_t, std::uint64_t>> visited_dirs;
|
|
for (const auto &root : directory_roots) {
|
|
const auto abs_dir = root.is_absolute() ? root : (canonical_root / root);
|
|
auto r = walk_dir(abs_dir, visited_dirs);
|
|
if (!r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
}
|
|
for (const auto &[search_roots, lookup_name] : lookup_queries) {
|
|
auto guard = evaluate_one_guard(canonical_root, options.allowed_external_roots,
|
|
search_roots, lookup_name);
|
|
if (!guard) {
|
|
return std::unexpected{guard.error()};
|
|
}
|
|
snap.lookup_guards.push_back(std::move(*guard));
|
|
}
|
|
|
|
// Revalidation pass before sealing: detect concurrent mutations during capture.
|
|
bool mutated = false;
|
|
for (const auto &[logical, src_path] : source_files_to_verify) {
|
|
auto rehash = Util::sha256_file_hex(src_path);
|
|
if (!rehash || *rehash != snap.entries.at(logical).sha256) {
|
|
mutated = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!mutated) {
|
|
for (const auto &[logical_dir, dir_path] : dirs_to_verify) {
|
|
auto meta = Platform::stat_lstat(dir_path);
|
|
if (!meta) {
|
|
mutated = true;
|
|
break;
|
|
}
|
|
if (!meta->exists) {
|
|
if (!snap.memberships.at(logical_dir).entries.empty()) {
|
|
mutated = true;
|
|
break;
|
|
}
|
|
continue;
|
|
}
|
|
auto cur = read_sorted_dir_entries(dir_path, options.excluded_names);
|
|
if (!cur || *cur != snap.memberships.at(logical_dir).entries) {
|
|
mutated = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (mutated) {
|
|
continue;
|
|
}
|
|
|
|
snap.snapshot_id = compute_snapshot_id(snap);
|
|
|
|
// Materialize a per-snapshot logical tree using private copies/reflinks (no hardlinks)
|
|
// with canonical mtime so compiler builds see isolated, immutable bytes.
|
|
if (options.materialize) {
|
|
snap.materialized_root = options.snapshot_store_dir / "trees" / snap.snapshot_id;
|
|
const auto seal_marker = snap.materialized_root / ".sealed";
|
|
if (!std::filesystem::exists(seal_marker, ec)) {
|
|
const auto tmp_tree = options.snapshot_store_dir / "trees" /
|
|
(snap.snapshot_id + ".staging_" +
|
|
std::to_string(static_cast<long long>(::getpid())));
|
|
std::filesystem::remove_all(tmp_tree, ec);
|
|
std::filesystem::create_directories(tmp_tree, ec);
|
|
|
|
for (auto &[logical, entry] : snap.entries) {
|
|
const auto dst = tmp_tree / logical;
|
|
std::filesystem::create_directories(dst.parent_path(), ec);
|
|
if (entry.kind == EntryKind::RegularFile && !entry.materialized_path.empty()) {
|
|
auto copied =
|
|
Platform::copy_isolated(entry.materialized_path, dst, entry.mode);
|
|
if (!copied) {
|
|
std::filesystem::remove_all(tmp_tree, ec);
|
|
return std::unexpected{copied.error()};
|
|
}
|
|
(void)Platform::set_canonical_mtime(dst, options.reproducibility_epoch);
|
|
} else if (entry.kind == EntryKind::Symlink) {
|
|
if (!entry.sha256.empty()) {
|
|
// Materialize symlinked regular file target inside snapshot tree
|
|
// safely.
|
|
const auto src_target = source_files_to_verify.find(logical);
|
|
if (src_target != source_files_to_verify.end()) {
|
|
auto copied =
|
|
Platform::copy_isolated(src_target->second, dst, entry.mode);
|
|
if (!copied) {
|
|
std::filesystem::remove_all(tmp_tree, ec);
|
|
return std::unexpected{copied.error()};
|
|
}
|
|
(void)Platform::set_canonical_mtime(dst,
|
|
options.reproducibility_epoch);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
(void)Platform::atomic_write_file(tmp_tree / ".sealed", snap.snapshot_id + "\n",
|
|
false);
|
|
std::filesystem::rename(tmp_tree, snap.materialized_root, ec);
|
|
if (ec) {
|
|
// Another concurrent worker may have sealed the identical content-addressed
|
|
// tree.
|
|
std::filesystem::remove_all(tmp_tree, ec);
|
|
if (!std::filesystem::exists(seal_marker, ec)) {
|
|
return std::unexpected{
|
|
make_error(Void::System::SystemErrorCode::io_error,
|
|
std::format("snapshot: failed publishing tree '{}'",
|
|
snap.materialized_root.string()))};
|
|
}
|
|
}
|
|
}
|
|
for (auto &[logical, entry] : snap.entries) {
|
|
entry.materialized_path = snap.materialized_root / logical;
|
|
}
|
|
(void)write_snapshot_manifest(snap, options.snapshot_store_dir);
|
|
}
|
|
|
|
return snap;
|
|
}
|
|
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::busy,
|
|
std::format("snapshot: source tree remained unstable across {} revalidation retries",
|
|
attempts))};
|
|
}
|
|
|
|
Void::Result::Result<bool, Void::Result::Error> is_snapshot_fresh(const InputSnapshot &snapshot,
|
|
const SnapshotOptions &options) {
|
|
for (const auto &[logical, entry] : snapshot.entries) {
|
|
if (logical.starts_with("external/")) {
|
|
continue;
|
|
}
|
|
const auto live_path = snapshot.project_root / logical;
|
|
auto meta = Platform::stat_lstat(live_path);
|
|
if (!meta) {
|
|
return std::unexpected{meta.error()};
|
|
}
|
|
if (!meta->exists) {
|
|
return false;
|
|
}
|
|
if (entry.kind == EntryKind::Symlink) {
|
|
if (!meta->is_symlink || meta->symlink_target != entry.symlink_target) {
|
|
return false;
|
|
}
|
|
auto confined = Platform::resolve_confined_path(snapshot.project_root, live_path,
|
|
options.allowed_external_roots);
|
|
if (!confined) {
|
|
return std::unexpected{confined.error()};
|
|
}
|
|
if (!entry.sha256.empty()) {
|
|
auto rehash = Util::sha256_file_hex(*confined);
|
|
if (!rehash || *rehash != entry.sha256) {
|
|
return false;
|
|
}
|
|
}
|
|
} else if (entry.kind == EntryKind::RegularFile) {
|
|
if (!meta->is_regular || meta->size != entry.size) {
|
|
return false;
|
|
}
|
|
// Content verification: NEVER shortcut on mtime/size alone (catches restored-mtime
|
|
// edits!).
|
|
auto rehash = Util::sha256_file_hex(live_path);
|
|
if (!rehash) {
|
|
return std::unexpected{rehash.error()};
|
|
}
|
|
if (*rehash != entry.sha256) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const auto &[logical_dir, mem] : snapshot.memberships) {
|
|
const auto live_dir =
|
|
logical_dir.empty() ? snapshot.project_root : (snapshot.project_root / logical_dir);
|
|
auto meta = Platform::stat_lstat(live_dir);
|
|
if (!meta) {
|
|
return std::unexpected{meta.error()};
|
|
}
|
|
if (!meta->exists) {
|
|
if (!mem.entries.empty()) {
|
|
return false;
|
|
}
|
|
continue;
|
|
}
|
|
if (!meta->is_directory) {
|
|
return false;
|
|
}
|
|
auto live_entries = read_sorted_dir_entries(live_dir, options.excluded_names);
|
|
if (!live_entries) {
|
|
return std::unexpected{live_entries.error()};
|
|
}
|
|
if (*live_entries != mem.entries) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
for (const auto &guard : snapshot.lookup_guards) {
|
|
auto live = evaluate_one_guard(snapshot.project_root, options.allowed_external_roots,
|
|
guard.search_roots, guard.lookup_name);
|
|
if (!live) {
|
|
return std::unexpected{live.error()};
|
|
}
|
|
if (live->matched_root_index != guard.matched_root_index ||
|
|
live->matched_sha256 != guard.matched_sha256) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
Void::Result::Result<SnapshotEntry, Void::Result::Error>
|
|
seal_generated_artifact(const std::filesystem::path &staged_file, std::string logical_path,
|
|
const std::filesystem::path &snapshot_store_dir,
|
|
std::int64_t reproducibility_epoch) {
|
|
auto meta = Platform::stat_lstat(staged_file);
|
|
if (!meta) {
|
|
return std::unexpected{meta.error()};
|
|
}
|
|
if (!meta->exists || !meta->is_regular) {
|
|
return std::unexpected{
|
|
make_error(Void::System::SystemErrorCode::not_found,
|
|
std::format("seal_generated_artifact: '{}' is missing or not a regular file",
|
|
staged_file.string()))};
|
|
}
|
|
auto digest = Util::sha256_file_hex(staged_file);
|
|
if (!digest) {
|
|
return std::unexpected{digest.error()};
|
|
}
|
|
|
|
const auto dst = snapshot_store_dir / "generated" / digest->substr(0, 2) / *digest;
|
|
std::error_code ec;
|
|
if (!std::filesystem::exists(dst, ec)) {
|
|
auto copied = Platform::copy_isolated(staged_file, dst, meta->mode);
|
|
if (!copied) {
|
|
return std::unexpected{copied.error()};
|
|
}
|
|
(void)Platform::set_canonical_mtime(dst, reproducibility_epoch);
|
|
}
|
|
|
|
SnapshotEntry entry;
|
|
entry.logical_path = std::move(logical_path);
|
|
entry.kind = EntryKind::RegularFile;
|
|
entry.sha256 = std::move(*digest);
|
|
entry.size = meta->size;
|
|
entry.mode = meta->mode;
|
|
entry.materialized_path = dst;
|
|
return entry;
|
|
}
|
|
|
|
Void::Result::Result<std::filesystem::path, Void::Result::Error>
|
|
write_snapshot_manifest(const InputSnapshot &snapshot,
|
|
const std::filesystem::path &snapshot_store_dir) {
|
|
using Util::json_array;
|
|
using Util::json_quote;
|
|
|
|
std::string json =
|
|
"{\"schema_version\":1,\"snapshot_id\":" + json_quote(snapshot.snapshot_id) +
|
|
",\"reproducibility_epoch\":" + std::to_string(snapshot.reproducibility_epoch) +
|
|
",\"entries\":";
|
|
std::vector<const SnapshotEntry *> entry_list;
|
|
for (const auto &[k, v] : snapshot.entries) {
|
|
entry_list.push_back(&v);
|
|
}
|
|
json += json_array(entry_list, [](const SnapshotEntry *e) {
|
|
return "{\"logical_path\":" + json_quote(e->logical_path) +
|
|
",\"sha256\":" + json_quote(e->sha256) + ",\"size\":" + std::to_string(e->size) +
|
|
",\"symlink_target\":" + json_quote(e->symlink_target) + '}';
|
|
});
|
|
json += ",\"lookup_guards\":";
|
|
json += json_array(snapshot.lookup_guards, [](const LookupGuard &g) {
|
|
return "{\"lookup_name\":" + json_quote(g.lookup_name) +
|
|
",\"matched_root_index\":" + std::to_string(g.matched_root_index) +
|
|
",\"matched_sha256\":" + json_quote(g.matched_sha256) + '}';
|
|
});
|
|
json += "}\n";
|
|
|
|
const auto manifest_path = snapshot_store_dir / "manifests" / (snapshot.snapshot_id + ".json");
|
|
auto written = Platform::atomic_write_file(manifest_path, json, true);
|
|
if (!written) {
|
|
return std::unexpected{written.error()};
|
|
}
|
|
return manifest_path;
|
|
}
|
|
|
|
} // namespace Void::Build::Snapshot
|