summaryrefslogtreecommitdiffstats
path: root/src/watch/sources.rs
blob: a437a2b87005690b92551cac5e64f820a0760e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! The source interface the build pipeline asks watch for: what a tree hashes
//! to, and a guarantee that a cached source is the bytes its name claims.

use std::path::Path;

use crate::config::Config;
use crate::db::Db;
use crate::error::{Error, Guard, Result};
use crate::watch::digests::{cached_path, download_and_cache};
use crate::watch::mirror::read_tree;

/// The recipe hash of a mirror at a commit — the same value the reviewer saw,
/// so a build can refuse a tree that moved underneath the approval.
pub fn recipe_hash(mirror: &Path, commit: &str) -> Result<String> {
    Ok(read_tree(mirror, commit)?.recipe_hash())
}

/// Ensure `url` is cached and that what is cached hashes to `digest`, failing
/// closed on a mismatch.
pub fn verify_cached(cfg: &Config, db: &Db, url: &str, digest: &str) -> Result<()> {
    let found = match cached_path(db, url, digest) {
        Some(p) => crate::hash::digest_file(&p)?.0,
        None => download_and_cache(cfg, db, url)?.0,
    };
    if found != digest {
        return Err(Error::fail_closed(Guard::DigestMismatch {
            url: url.to_string(),
            expected: digest.to_string(),
            found,
        }));
    }
    Ok(())
}