summaryrefslogtreecommitdiffstats
path: root/src/fetch.rs
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-17 21:19:35 +0000
committeraur-build <aur-build@localhost>2026-11-17 21:19:35 +0000
commitf586d11f6a1711145ba4804787217a1bab58751d (patch)
tree3a8cd7d4a3d4097e315907c63e687bd243e8e686 /src/fetch.rs
parentfd3ecf91686b11feea078531baeddb60e6941116 (diff)
downloadaur-build-f586d11f6a1711145ba4804787217a1bab58751d.tar.gz
aur-build-f586d11f6a1711145ba4804787217a1bab58751d.zip
feat: AUR mirror watcher, recipe parsing, candidate creation
The detection path (§5.1, §5.1a, §5.3, §7.1): * git helpers, all subprocess git in one auditable place. Mirrors are cloned with --mirror and polled with git ls-remote, fetching only when the tip actually moved; --no-prune keeps history we may need forever. * .SRCINFO parsing (authoritative) with a PKGBUILD fallback, and source=() parsing that keeps VCS kind, #commit/#tag/#branch refs and the filename:: form. * recipe_hash over the whole snapshot tree, so a shipped patch is part of the recipe and cannot change silently. * source digest resolution: declared checksums are trusted-as-declared (makepkg enforces them, no bandwidth), local files are hashed from the mirror, and SKIP sources are downloaded and stream-hashed into the content-addressed source cache. Detection always re-fetches SKIP sources — answering from our own cache would defeat drift detection. * candidate creation with base = current_accepted_release, collapse in place while unviewed, supersede only once a human has looked, and a previously rejected identity is not re-raised. * non-publishable change handling (I11): unchanged version string means no review, but never silence — recipe drift and pin drift become notices, and a changed digest on a remote source with an unchanged version becomes a high-severity integrity_alert. Also vercmp + dep-string parsing with the ALPM test vectors, and the approval record with optimistic concurrency (§7.1a) that the CLI and web layers will both call. 16 detection tests against real file:// git fixtures, incl. I2 across three unattended detections and I11 for all three drift kinds. Part of P0 (DESIGN.md §17).
Diffstat (limited to 'src/fetch.rs')
-rw-r--r--src/fetch.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/fetch.rs b/src/fetch.rs
new file mode 100644
index 0000000..c1c4575
--- /dev/null
+++ b/src/fetch.rs
@@ -0,0 +1,65 @@
+//! Outbound HTTP. `ureq` is blocking, which is the whole point of this stack
+//! (DESIGN.md §4: "`reqwest` pulls in tokio even in blocking mode — avoid").
+//!
+//! `file://` is supported as a first-class scheme so the test corpus can serve
+//! sources from disk with no network (§16a).
+
+use std::io::{Read, Write};
+use std::path::PathBuf;
+
+use crate::error::{Error, Result};
+
+pub enum Body {
+ File(std::fs::File),
+ Http(Box<dyn Read + Send>),
+}
+
+impl Read for Body {
+ fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+ match self {
+ Body::File(f) => f.read(buf),
+ Body::Http(r) => r.read(buf),
+ }
+ }
+}
+
+/// Open a URL for streaming. Never buffers the whole body: sources are hashed
+/// while they are written into the cache (§5.3).
+pub fn open(url: &str) -> Result<(Body, Option<u64>)> {
+ if let Some(path) = url.strip_prefix("file://") {
+ let p = path.trim_start_matches('/');
+ let p = PathBuf::from(format!("/{p}"));
+ let f = std::fs::File::open(&p).map_err(|e| Error::Http(format!("{url}: {e}")))?;
+ let len = f.metadata().map(|m| m.len()).ok();
+ return Ok((Body::File(f), len));
+ }
+ if url.starts_with("http://") || url.starts_with("https://") {
+ let resp = ureq::get(url)
+ .timeout(std::time::Duration::from_secs(300))
+ .call()
+ .map_err(|e| Error::Http(format!("{url}: {e}")))?;
+ let len = resp
+ .header("content-length")
+ .and_then(|v| v.parse::<u64>().ok());
+ return Ok((Body::Http(resp.into_reader()), len));
+ }
+ Err(Error::invalid(format!("unsupported url scheme: {url}")))
+}
+
+/// Fetch `url` into `dest`, returning `(sha256, size)`.
+pub fn fetch_to(url: &str, dest: &std::path::Path) -> Result<(String, u64)> {
+ if let Some(parent) = dest.parent() {
+ std::fs::create_dir_all(parent)?;
+ }
+ let (body, _len) = open(url)?;
+ let mut tmp = dest.as_os_str().to_os_string();
+ tmp.push(".part");
+ let tmp = PathBuf::from(tmp);
+ let mut file = std::io::BufWriter::new(std::fs::File::create(&tmp)?);
+ let (digest, size) = crate::hash::digest_reader(std::io::BufReader::new(body), Some(&mut file))
+ .map_err(|e| Error::Http(format!("{url}: {e}")))?;
+ file.flush()?;
+ // Rename into place: a half-written cache entry must never be visible.
+ std::fs::rename(&tmp, dest)?;
+ Ok((digest, size))
+}