#include #include #include #include #include #include #include #include #include #include namespace Void::Build::Package { namespace { using JsonVal = Void::Json::Strict::Value; Void::Result::Error make_error(Void::System::SystemErrorCode code, std::string msg) { return Void::System::make_system_error(code, std::move(msg)); } const JsonVal *find_key(const JsonVal::Object &obj, const std::string &k) { const auto it = obj.find(k); return it == obj.end() ? nullptr : &it->second; } std::string get_str(const JsonVal::Object &obj, const std::string &k) { const auto *v = find_key(obj, k); if (!v) { return {}; } const auto *s = std::get_if(&v->value); return s ? *s : std::string{}; } std::int64_t get_i64(const JsonVal::Object &obj, const std::string &k, std::int64_t def = 0) { const auto *v = find_key(obj, k); if (!v) { return def; } const auto *i = std::get_if(&v->value); return i ? *i : def; } bool get_bool(const JsonVal::Object &obj, const std::string &k, bool def = false) { const auto *v = find_key(obj, k); if (!v) { return def; } const auto *b = std::get_if(&v->value); return b ? *b : def; } std::vector get_str_array(const JsonVal::Object &obj, const std::string &k) { std::vector out; const auto *v = find_key(obj, k); if (!v) { return out; } if (const auto *arr = std::get_if(&v->value)) { for (const auto &item : *arr) { if (const auto *s = std::get_if(&item.value)) { out.push_back(*s); } } } return out; } // Strips embedded URL credentials (user:pass@host) before persisting to vbs.lock! std::string strip_url_credentials(std::string_view url) { const auto scheme = url.find("://"); if (scheme == std::string_view::npos) { return std::string{url}; } const auto after_scheme = scheme + 3; const auto slash = url.find('/', after_scheme); const auto at = url.find('@', after_scheme); if (at != std::string_view::npos && (slash == std::string_view::npos || at < slash)) { return std::string{url.substr(0, after_scheme)} + std::string{url.substr(at + 1)}; } return std::string{url}; } std::string serialize_contexts_body(const std::map> &ctxs) { using Util::json_array; using Util::json_quote; std::string out{"{"}; bool first = true; for (const auto &[key, ctx] : ctxs) { if (!first) { out += ','; } first = false; out += json_quote(key) + ":{\"context_key\":" + json_quote(ctx.context_key) + ",\"profile_name\":" + json_quote(ctx.profile_name) + ",\"compiler\":" + json_quote(ctx.compiler) + ",\"os\":" + json_quote(ctx.os) + ",\"architecture\":" + json_quote(ctx.architecture) + ",\"machine_role\":" + json_quote(ctx.machine_role) + ",\"declaration_fingerprint\":" + json_quote(ctx.declaration_fingerprint) + ",\"entries\":"; out += json_array(ctx.entries, [](const LockedSourceEntry &e) { return "{\"alias\":" + json_quote(e.alias) + ",\"source_kind\":" + json_quote(e.source_kind) + ",\"registry_authority\":" + json_quote(strip_url_credentials(e.registry_authority)) + ",\"package_name\":" + json_quote(e.package_name) + ",\"version\":" + json_quote(e.version) + ",\"location\":" + json_quote(strip_url_credentials(e.location)) + ",\"revision\":" + json_quote(e.revision) + ",\"sha256\":" + json_quote(e.sha256) + ",\"builder\":" + json_quote(e.builder) + ",\"features\":" + json_array(e.features, json_quote) + ",\"build_dependency\":" + std::string{e.build_dependency ? "true" : "false"} + (e.signature ? ",\"signature\":" + json_quote(*e.signature) : std::string{}) + (e.public_key ? ",\"public_key\":" + json_quote(*e.public_key) : std::string{}) + '}'; }); out += '}'; } out += '}'; return out; } } // namespace std::string make_lock_context_key(const ProfileContext &profile, std::string_view machine_role) { return std::format("{}:{}:{}:{}:{}", profile.name, compiler_name(profile.compiler), profile.os, profile.architecture, machine_role); } std::string compute_declaration_fingerprint(const ProfileContext &profile, std::string_view machine_role, const std::vector &declarations) { Util::Sha256Hasher h; h.update_length_prefixed("vbs-lock-decl-v1"); h.update_length_prefixed(profile.name); h.update_length_prefixed(compiler_name(profile.compiler)); h.update_length_prefixed(profile.os); h.update_length_prefixed(profile.architecture); h.update_length_prefixed(machine_role); std::vector sorted; for (const auto &nd : declarations) { const bool want_build = (machine_role == "build"); if (nd.record.build_dependency == want_build) { sorted.push_back(&nd.record); } } std::ranges::sort(sorted, [](const DependencyRecord *a, const DependencyRecord *b) { return a->name < b->name; }); h.update_u64(sorted.size()); for (const auto *d : sorted) { h.update_length_prefixed(d->name); h.update_u64(static_cast(d->source_kind)); h.update_length_prefixed(strip_url_credentials(d->location)); h.update_length_prefixed(d->revision); h.update_length_prefixed(d->package); h.update_length_prefixed(d->version); h.update_length_prefixed(d->builder); h.update_length_prefixed(strip_url_credentials(d->registry_authority)); h.update_length_prefixed(d->sha256); h.update_u64(d->features.size()); for (const auto &f : d->features) { h.update_length_prefixed(f); } h.update_u64(d->options.size()); for (const auto &[k, opt] : d->options) { h.update_length_prefixed(k); std::visit( [&](const auto &v) { using T = std::remove_cvref_t; if constexpr (std::same_as) { h.update_length_prefixed("s:" + v); } else if constexpr (std::same_as) { h.update_length_prefixed(v ? "b:1" : "b:0"); } else { h.update_length_prefixed("i:" + std::to_string(v)); } }, opt.value); } } return h.finalize_hex(); } std::string serialize_lockfile(const LockfileDocument &doc) { const std::string body = serialize_contexts_body(doc.contexts); const std::string rev = Util::sha256_hex(body); return "{\"schema_version\":" + std::to_string(doc.schema_version) + ",\"revision_hash\":" + Util::json_quote(rev) + ",\"contexts\":" + body + "}\n"; } Void::Result::Result parse_lockfile(std::string_view json_text) { if (!Util::is_valid_utf8(json_text)) { return std::unexpected{ make_error(Void::System::SystemErrorCode::invalid_argument, "lockfile invalid UTF-8")}; } auto parsed = Void::Json::Strict::parse(json_text); if (!parsed) { return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument, "malformed JSON in vbs.lock")}; } const auto *root = std::get_if(&parsed->value); if (!root) { return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument, "vbs.lock root must be a JSON object")}; } LockfileDocument doc; doc.schema_version = static_cast(get_i64(*root, "schema_version", 0)); if (doc.schema_version != kLockfileSchemaVersion) { return std::unexpected{ make_error(Void::System::SystemErrorCode::invalid_argument, std::format("unsupported vbs.lock schema_version {}", doc.schema_version))}; } doc.revision_hash = get_str(*root, "revision_hash"); if (const auto *ctxs_v = find_key(*root, "contexts")) { const auto *cobj = std::get_if(&ctxs_v->value); if (!cobj) { return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument, "vbs.lock contexts must be an object")}; } for (const auto &[key, cv] : *cobj) { const auto *co = std::get_if(&cv.value); if (!co) { continue; } LockContextRecord rec; rec.context_key = get_str(*co, "context_key"); rec.profile_name = get_str(*co, "profile_name"); rec.compiler = get_str(*co, "compiler"); rec.os = get_str(*co, "os"); rec.architecture = get_str(*co, "architecture"); rec.machine_role = get_str(*co, "machine_role"); rec.declaration_fingerprint = get_str(*co, "declaration_fingerprint"); if (const auto *ev = find_key(*co, "entries")) { if (const auto *earr = std::get_if(&ev->value)) { for (const auto &item : *earr) { if (const auto *eo = std::get_if(&item.value)) { LockedSourceEntry e; e.alias = get_str(*eo, "alias"); e.source_kind = get_str(*eo, "source_kind"); e.registry_authority = get_str(*eo, "registry_authority"); e.package_name = get_str(*eo, "package_name"); e.version = get_str(*eo, "version"); e.location = get_str(*eo, "location"); e.revision = get_str(*eo, "revision"); e.sha256 = get_str(*eo, "sha256"); e.builder = get_str(*eo, "builder"); e.features = get_str_array(*eo, "features"); e.build_dependency = get_bool(*eo, "build_dependency", false); if (const auto sig = get_str(*eo, "signature"); !sig.empty()) { e.signature = sig; } if (const auto key = get_str(*eo, "public_key"); !key.empty()) { e.public_key = key; } rec.entries.push_back(std::move(e)); } } } } doc.contexts[key] = std::move(rec); } } // Verify revision hash integrity against canonical body! const std::string expected_rev = Util::sha256_hex(serialize_contexts_body(doc.contexts)); if (!doc.revision_hash.empty() && doc.revision_hash != expected_rev) { return std::unexpected{make_error( Void::System::SystemErrorCode::invalid_argument, "vbs.lock revision_hash does not match contents (corrupted or hand-edited lockfile)")}; } doc.revision_hash = expected_rev; return doc; } Void::Result::Result, Void::Result::Error> load_pinned_lock(const std::filesystem::path &lock_path) { auto meta = Platform::stat_lstat(lock_path); if (!meta) { return std::unexpected{meta.error()}; } if (!meta->exists) { return std::nullopt; } auto text = Void::FileSystem::read_file_bounded(lock_path, {64ULL * 1024ULL * 1024ULL}); if (!text) { return std::unexpected{text.error()}; } auto parsed = parse_lockfile(*text); if (!parsed) { return std::unexpected{parsed.error()}; } PinnedLock pinned; pinned.revision_hash = parsed->revision_hash; pinned.raw_json = std::move(*text); pinned.document = std::move(*parsed); return pinned; } Void::Result::Result validate_locked_context(const LockfileDocument &doc, std::string_view context_key, std::string_view expected_declaration_fingerprint) { const auto it = doc.contexts.find(context_key); if (it == doc.contexts.end()) { return std::unexpected{make_error( Void::System::SystemErrorCode::invalid_argument, std::format("vbs.lock has no resolution for context '{}' (run 'vbs deps resolve')", context_key))}; } if (it->second.declaration_fingerprint != expected_declaration_fingerprint) { return std::unexpected{make_error( Void::System::SystemErrorCode::invalid_argument, std::format( "vbs.lock context '{}' is incompatible with current dependency declarations " "(run 'vbs deps resolve' or 'vbs deps update')", context_key))}; } return &it->second; } Void::Result::Result publish_lock_context_cas(const std::filesystem::path &lock_path, std::string_view expected_base_revision, const LockContextRecord &updated_context, bool locked_mode) { if (locked_mode) { return std::unexpected{make_error(Void::System::SystemErrorCode::permission_denied, "--locked forbids creating or modifying vbs.lock")}; } const auto mutex_path = lock_path.parent_path() / ".vbs" / "lockfile.mutex"; auto mutex = Platform::FileLock::acquire(mutex_path, Platform::LockMode::Exclusive); if (!mutex) { return std::unexpected{mutex.error()}; } LockfileDocument current; std::string current_rev; auto meta = Platform::stat_lstat(lock_path); if (!meta) { return std::unexpected{meta.error()}; } if (meta->exists) { auto text = Void::FileSystem::read_file_bounded(lock_path, {64ULL * 1024ULL * 1024ULL}); if (!text) { return std::unexpected{text.error()}; } auto parsed = parse_lockfile(*text); if (!parsed) { return std::unexpected{parsed.error()}; } current = std::move(*parsed); current_rev = current.revision_hash; } // Compare-and-swap revision check (D-025): if another profile updated the lock concurrently, // allow merging ONLY if the other update did NOT touch our context_key; if it modified our // context_key or base mismatch occurred, report busy conflict for caller re-resolution! if (current_rev != expected_base_revision) { const auto existing_ctx = current.contexts.find(updated_context.context_key); if (existing_ctx != current.contexts.end() && existing_ctx->second.declaration_fingerprint != updated_context.declaration_fingerprint) { return std::unexpected{make_error( Void::System::SystemErrorCode::busy, std::format("concurrent vbs.lock revision conflict (expected '{}', found '{}'); " "re-resolve and retry", expected_base_revision, current_rev))}; } } current.contexts[updated_context.context_key] = updated_context; const std::string serialized = serialize_lockfile(current); auto reparsed = parse_lockfile(serialized); if (!reparsed) { return std::unexpected{reparsed.error()}; } auto wrote = Platform::atomic_write_file(lock_path, serialized, true); if (!wrote) { return std::unexpected{wrote.error()}; } PinnedLock pinned; pinned.revision_hash = reparsed->revision_hash; pinned.raw_json = serialized; pinned.document = std::move(*reparsed); return pinned; } } // namespace Void::Build::Package