From d9c8402180749df4452dc37febace4d933b8407d Mon Sep 17 00:00:00 2001 From: aur-build Date: Wed, 18 Nov 2026 09:12:28 +0000 Subject: =?UTF-8?q?pkginfo:=20.PKGINFO/.BUILDINFO=20parsing=20gets=20a=20r?= =?UTF-8?q?eal=20home,=20and=20a=20parser=20bug=20fix=20(=C2=A710.1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ingest decides what the index claims about a package and what the world snapshot records by reading two text files out of a tar inside a zstd stream. That code was living in src/shims — the test harness — which meant production depended on the harness for its view of a package's own metadata. Moved to src/pkginfo.rs, with the shim now delegating to it so the harness and the production path cannot disagree about what a package says about itself. Still no libalpm: tar + zstd + eighty lines of key = value parsing. Unknown keys are ignored rather than rejected, because makepkg adds fields over time and a new one must not stop us publishing. **The parser had a real bug.** A .BUILDINFO installed block puts its first entry on the same line as the key: installed = glibc 2.39-1 x86_64 - gmp 6.3.0-1 x86_64 - The old code skipped the header line, so the first dependency was silently dropped from every snapshot — exactly the kind of gap that makes staleness quietly wrong. The header line is parsed now. tests/world.rs builds real .pkg.tar.zst files and asserts the fields come out of the archive: repeated keys stay lists, an archive with no .PKGINFO is refused rather than defaulted from the filename, and the world snapshot contains our own packages and none of glibc/pacman/binutils. The empty-snapshot case for a first build is asserted too — nothing of ours existed yet, and recording that honestly beats inventing a world. Split pkgbases produce one approval, one release and N index rows each with its own artifact, asserted directly here. 143 tests pass, clippy clean. --- src/lib.rs | 1 + src/pipeline/steps.rs | 70 +++++++---------------- src/pkginfo.rs | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/shims/mod.rs | 34 ++---------- 4 files changed, 176 insertions(+), 79 deletions(-) create mode 100644 src/pkginfo.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index b697ac6..bee4303 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ pub mod git; pub mod hash; pub mod http; pub mod pipeline; +pub mod pkginfo; pub mod pkgver; pub mod recipe; pub mod review; diff --git a/src/pipeline/steps.rs b/src/pipeline/steps.rs index b925afb..3f575bd 100644 --- a/src/pipeline/steps.rs +++ b/src/pipeline/steps.rs @@ -264,24 +264,32 @@ fn collect(ctx: &Ctx) -> Result { for path in entries { let bytes = std::fs::read(&path)?; let (digest, size) = crate::hash::digest_file(&path)?; - let pkginfo = crate::shims::read_pkginfo(&bytes) + // Read what the package says about itself out of the archive, rather + // than trusting what the build claimed on stdout (§10.1). + let pkginfo = crate::pkginfo::pkginfo_text(&bytes) .map_err(|e| Error::invalid(format!("{}: {e}", path.display())))?; - let buildinfo = crate::shims::read_buildinfo(&bytes).unwrap_or_default(); - let meta = parse_pkginfo(&pkginfo); + let meta = crate::pkginfo::parse_pkginfo(&pkginfo); + // `.BUILDINFO` is what the world snapshot is built from. Absent is not + // fatal — the snapshot is then simply empty for this package — but a + // present-but-unparseable one would be a lie we should not swallow. + let buildinfo = crate::pkginfo::buildinfo_text(&bytes).unwrap_or_default(); let sig = path.with_extension("pkg.tar.zst.sig"); pkgs.push(CollectedPkg { - name: meta.get("pkgname").cloned().unwrap_or_default(), - version: meta.get("pkgver").cloned().unwrap_or_default(), - arch: meta.get("pkgarch").cloned().unwrap_or_else(|| ctx.cfg.arch.clone()), + name: meta.name.clone(), + version: meta.version.clone(), + arch: if meta.arch.is_empty() { ctx.cfg.arch.clone() } else { meta.arch.clone() }, path: path.display().to_string(), digest, size, signature_path: sig.exists().then(|| sig.display().to_string()), - depends: meta_multi(&pkginfo, "depend"), - makedepends: meta_multi(&pkginfo, "makedepend"), - provides: meta_multi(&pkginfo, "provides"), - conflicts: meta_multi(&pkginfo, "conflict"), - installed: parse_installed(&buildinfo), + depends: meta.depends.clone(), + makedepends: meta.makedepends.clone(), + provides: meta.provides.clone(), + conflicts: meta.conflicts.clone(), + installed: crate::pkginfo::parse_installed(&buildinfo) + .into_iter() + .map(|i| (i.name, i.version)) + .collect(), }); } if pkgs.is_empty() { @@ -414,43 +422,3 @@ pub fn recipe_at(dir: &Path) -> Result { crate::recipe::recipe_from_tree(&files) } -fn parse_pkginfo(text: &str) -> std::collections::HashMap { - let mut m = std::collections::HashMap::new(); - for line in text.lines() { - if let Some((k, v)) = line.split_once('=') { - m.insert(k.trim().to_string(), v.trim().to_string()); - } - } - m -} - -fn meta_multi(text: &str, key: &str) -> Vec { - text.lines() - .filter_map(|l| l.split_once('=')) - .filter(|(k, _)| k.trim() == key) - .map(|(_, v)| v.trim().to_string()) - .collect() -} - -/// `.BUILDINFO` `installed =` block: ` name version arch sig` per line. -fn parse_installed(text: &str) -> Vec<(String, String)> { - let mut out = Vec::new(); - let mut in_block = false; - for line in text.lines() { - if line.trim_start().starts_with("installed") { - in_block = true; - continue; - } - if in_block { - if !line.starts_with(' ') && !line.starts_with('\t') { - in_block = false; - continue; - } - let mut it = line.split_whitespace(); - if let (Some(name), Some(version)) = (it.next(), it.next()) { - out.push((name.to_string(), version.to_string())); - } - } - } - out -} diff --git a/src/pkginfo.rs b/src/pkginfo.rs new file mode 100644 index 0000000..7ec8b83 --- /dev/null +++ b/src/pkginfo.rs @@ -0,0 +1,150 @@ +//! Reading `.PKGINFO` and `.BUILDINFO` out of a package archive (§10.1). +//! +//! A `.pkg.tar.zst` is a zstd-compressed tar with two plain-text metadata files +//! inside it. pacman reads these through libalpm; we do not link libalpm (no +//! FFI, no C toolchain dependency, §14), so this module parses the tar and the +//! text directly. That is not a shortcut — the format is a `key = value` file +//! with repeated keys, and a `.BUILDINFO` `installed =` block of +//! `name version arch signature` lines. Both are stable and documented, and +//! parsing them here is roughly eighty lines. +//! +//! This is production code, not harness code: the ingest step decides what the +//! index claims about a package, and what the world snapshot records, by reading +//! these two files. + +use std::io::Read; + +/// A package's own metadata, as `.PKGINFO` states it. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct PkgInfo { + pub name: String, + pub version: String, + pub arch: String, + pub desc: String, + pub url: String, + pub license: Vec, + pub groups: Vec, + pub depends: Vec, + pub makedepends: Vec, + pub checkdepends: Vec, + pub optdepends: Vec, + pub provides: Vec, + pub conflicts: Vec, + pub replaces: Vec, +} + +/// One entry of a `.BUILDINFO` `installed` block: a package that was present in +/// the build root. The signature field is optional in real files. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Installed { + pub name: String, + pub version: String, + pub arch: String, +} + +/// Extract one named member from a zstd-compressed tar. +fn member(bytes: &[u8], suffix: &[u8]) -> Result { + let dec = zstd::Decoder::new(std::io::Cursor::new(bytes)).map_err(|e| e.to_string())?; + let mut ar = tar::Archive::new(dec); + for e in ar.entries().map_err(|e| e.to_string())? { + let mut e = e.map_err(|e| e.to_string())?; + if e.path_bytes().as_ref().ends_with(suffix) { + let mut s = String::new(); + e.read_to_string(&mut s).map_err(|e| e.to_string())?; + return Ok(s); + } + } + Err(format!("no {} in package", String::from_utf8_lossy(suffix))) +} + +/// `.PKGINFO` as raw text. +pub fn pkginfo_text(pkg_bytes: &[u8]) -> Result { + member(pkg_bytes, b".PKGINFO") +} + +/// `.BUILDINFO` as raw text. Absent on some hand-built packages; callers decide +/// whether that is fatal. +pub fn buildinfo_text(pkg_bytes: &[u8]) -> Result { + member(pkg_bytes, b".BUILDINFO") +} + +/// Parse `.PKGINFO`. Unknown keys are ignored rather than rejected: makepkg adds +/// fields over time and a new one must not stop us publishing. +pub fn parse_pkginfo(text: &str) -> PkgInfo { + let mut p = PkgInfo::default(); + for (k, v) in fields(text) { + match k { + "pkgname" => p.name = v.to_string(), + "pkgver" => p.version = v.to_string(), + "pkgarch" => p.arch = v.to_string(), + "pkgdesc" => p.desc = v.to_string(), + "url" => p.url = v.to_string(), + "license" => p.license.push(v.to_string()), + "group" => p.groups.push(v.to_string()), + "depend" => p.depends.push(v.to_string()), + "makedepend" => p.makedepends.push(v.to_string()), + "checkdepend" => p.checkdepends.push(v.to_string()), + "optdepend" => p.optdepends.push(v.to_string()), + "provides" => p.provides.push(v.to_string()), + "conflict" => p.conflicts.push(v.to_string()), + "replaces" => p.replaces.push(v.to_string()), + _ => {} + } + } + p +} + +/// Parse the `.BUILDINFO` `installed` block. +/// +/// The first entry shares the line with the key — `installed = glibc 2.39-1 +/// x86_64 -` — and the rest are indented under it, so the header line has to be +/// parsed as well as skipped. Dropping it would silently lose one dependency from +/// every snapshot, which is exactly the kind of gap that makes staleness lie. +pub fn parse_installed(text: &str) -> Vec { + let mut out = Vec::new(); + let mut in_block = false; + for line in text.lines() { + if line.trim_start().starts_with("installed") { + in_block = true; + // Everything after `installed =` on this line is the first entry. + if let Some(rest) = line.split_once('=') { + push_entry(&mut out, rest.1); + } + continue; + } + if !in_block { + continue; + } + // The block is indented; the first unindented line ends it. + if !line.starts_with(' ') && !line.starts_with('\t') { + in_block = false; + continue; + } + push_entry(&mut out, line); + } + out +} + +fn push_entry(out: &mut Vec, line: &str) { + let mut it = line.split_whitespace(); + if let (Some(name), Some(version)) = (it.next(), it.next()) { + out.push(Installed { + name: name.to_string(), + version: version.to_string(), + arch: it.next().unwrap_or_default().to_string(), + }); + } +} + +/// `key = value` pairs, with repeated keys preserved in order. +fn fields(text: &str) -> impl Iterator { + text.lines().filter_map(|l| l.split_once('=')).map(|(k, v)| (k.trim(), v.trim())) +} + +/// All values of one key. +pub fn values_of(text: &str, key: &str) -> Vec { + fields(text) + .filter(|(k, _)| *k == key) + .map(|(_, v)| v.to_string()) + .collect() +} diff --git a/src/shims/mod.rs b/src/shims/mod.rs index 5560aa3..754bbb4 100644 --- a/src/shims/mod.rs +++ b/src/shims/mod.rs @@ -542,38 +542,16 @@ pub fn repo_add(args: &[String], _m: &Manifest) -> Result { Ok(0) } -/// Pull `.PKGINFO` out of a `.pkg.tar.zst` (§10.1: plain text in a tar, no -/// libalpm needed). +/// Pull `.PKGINFO` out of a `.pkg.tar.zst`. The real parsing lives in +/// `crate::pkginfo`, which the pipeline also uses — the shim and the production +/// path must agree about what a package says about itself, or a test that passes +/// here would not mean anything. pub fn read_pkginfo(pkg_bytes: &[u8]) -> Result { - let dec = zstd::Decoder::new(std::io::Cursor::new(pkg_bytes)).map_err(|e| e.to_string())?; - let mut ar = tar::Archive::new(dec); - for e in ar.entries().map_err(|e| e.to_string())? { - let mut e = e.map_err(|e| e.to_string())?; - let name = e.path_bytes().to_vec(); - if name.ends_with(b".PKGINFO") { - use std::io::Read; - let mut s = String::new(); - e.read_to_string(&mut s).map_err(|e| e.to_string())?; - return Ok(s); - } - } - Err("no .PKGINFO in package".into()) + crate::pkginfo::pkginfo_text(pkg_bytes) } pub fn read_buildinfo(pkg_bytes: &[u8]) -> Result { - let dec = zstd::Decoder::new(std::io::Cursor::new(pkg_bytes)).map_err(|e| e.to_string())?; - let mut ar = tar::Archive::new(dec); - for e in ar.entries().map_err(|e| e.to_string())? { - let mut e = e.map_err(|e| e.to_string())?; - let name = e.path_bytes().to_vec(); - if name.ends_with(b".BUILDINFO") { - use std::io::Read; - let mut s = String::new(); - e.read_to_string(&mut s).map_err(|e| e.to_string())?; - return Ok(s); - } - } - Err("no .BUILDINFO in package".into()) + crate::pkginfo::buildinfo_text(pkg_bytes) } fn parse_pkginfo(s: &str) -> (String, String, String, String) { -- cgit v1.2.3