From fd3ecf91686b11feea078531baeddb60e6941116 Mon Sep 17 00:00:00 2001 From: aur-build Date: Tue, 17 Nov 2026 20:59:57 +0000 Subject: feat: SQLite schema, migrations, single writer actor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The §11 data model as four migrations, plus the §11a concurrency model: one writer thread owning the write connection (mutations arrive as closures over a crossbeam channel, so SQLITE_BUSY is not part of the design) and thread-local read-only connections under WAL. Invariants that are database guarantees rather than conventions: I7 partial unique index -> at most one pending_review per pkgbase I3 UNIQUE(pkgbase_id, identity_hash) -> no duplicate candidates I10 audit table rejects UPDATE and DELETE by trigger §16 current_accepted_release_id moves only by compare-and-swap Also: vercmp + dependency-string parsing (ALPM algorithm, no libalpm), hash helpers including recipe_hash over the whole AUR tree, the clock helper that stamps every write, and the config/root layout. Part of P0 (DESIGN.md §17). --- src/hash.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/hash.rs (limited to 'src/hash.rs') diff --git a/src/hash.rs b/src/hash.rs new file mode 100644 index 0000000..5243520 --- /dev/null +++ b/src/hash.rs @@ -0,0 +1,78 @@ +//! Hashing helpers. `sha256` hex strings are the currency of identity and +//! provenance throughout the system (DESIGN.md §3, §5.3). + +use sha2::{Digest, Sha256}; +use std::io::{self, Read, Write}; + +pub const EMPTY: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; + +pub fn hex(bytes: &[u8]) -> String { + let mut h = Sha256::new(); + h.update(bytes); + hex::encode(h.finalize()) +} + +pub fn hex_str(s: &str) -> String { + hex(s.as_bytes()) +} + +/// First `n` hex chars — enough for display, not for identity. +pub fn short(bytes: &[u8], n: usize) -> String { + let h = hex(bytes); + h[..n.min(h.len())].to_string() +} + +/// Stream-hash a reader, optionally teeing every byte into `sink`. +/// +/// Used to hash a download while writing it into the source cache, so the +/// bandwidth is paid for once (§5.3 "stream-hash, don't buffer"). +pub fn digest_reader(mut r: R, mut sink: Option<&mut dyn Write>) -> io::Result<(String, u64)> { + let mut h = Sha256::new(); + let mut buf = vec![0u8; 64 * 1024]; + let mut total = 0u64; + loop { + let n = r.read(&mut buf)?; + if n == 0 { + break; + } + h.update(&buf[..n]); + if let Some(s) = sink.as_mut() { + s.write_all(&buf[..n])?; + } + total += n as u64; + } + Ok((hex::encode(h.finalize()), total)) +} + +pub fn digest_file(path: &std::path::Path) -> io::Result<(String, u64)> { + let f = std::fs::File::open(path)?; + let mut br = io::BufReader::new(f); + digest_reader(&mut br, None) +} + +/// Hash of a canonical, sorted list of `(path, blob_hash)` pairs. +/// +/// This is `recipe_hash` (DESIGN.md §3): a digest over *every* file in the AUR +/// snapshot tree — PKGBUILD, .SRCINFO, *.install and any shipped patches or +/// assets — because a patch file shipped in the AUR repo is part of the recipe +/// and is a classic silent-change vector. +pub fn recipe_hash(entries: &[(String, String)]) -> String { + let mut sorted: Vec<&(String, String)> = entries.iter().collect(); + sorted.sort_unstable_by(|a, b| a.0.cmp(&b.0)); + let mut h = Sha256::new(); + for (path, blob) in &sorted { + h.update(path.as_bytes()); + h.update([0u8]); + h.update(blob.as_bytes()); + h.update([0u8]); + } + hex::encode(h.finalize()) +} + +/// Canonical JSON used for identity digests. Key order in serde_json maps is +/// not stable across object construction, so we serialize through a +/// `BTreeMap`-preserving path: callers pass already-canonical values (arrays of +/// objects with a fixed field order) and we hash the compact rendering. +pub fn canonical_json(v: &T) -> String { + serde_json::to_string(v).unwrap_or_else(|_| "{}".into()) +} -- cgit v1.2.3