diff options
Diffstat (limited to 'src/recipe.rs')
| -rw-r--r-- | src/recipe.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/recipe.rs b/src/recipe.rs index a43059d..6c6666c 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -354,6 +354,43 @@ pub fn is_diffable(path: &str) -> bool { ) } +/// `sha256sums=('SKIP')`: the recipe declining to verify a source (§5.3). +pub const SKIP: &str = "SKIP"; + +/// The checksum a recipe declares for source entry `index`, `sha256sums` first +/// and `b2sums` second; `None` when the recipe declares nothing for it. +pub fn declared_sums(recipe: &Recipe, index: usize) -> Option<String> { + recipe + .sha256sums + .get(index) + .or_else(|| recipe.b2sums.get(index)) + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty()) +} + +/// True when a declared checksum is an actual hex digest of the length we +/// expect, rather than `SKIP` or a typo a maintainer left behind. +pub fn is_hex_digest(s: &str) -> bool { + (s.len() == 64 || s.len() == 128) && s.chars().all(|c| c.is_ascii_hexdigit()) +} + +/// The non-VCS sources the recipe refuses to verify (§5.3): `SKIP`, a malformed +/// digest, or no digest at all. A question about the *recipe*, so it lives with +/// the recipe — the watcher and the risk scorer both ask it, and neither should +/// own the answer. +pub fn skip_on_non_vcs(recipe: &Recipe) -> Vec<usize> { + recipe + .source_entries + .iter() + .filter(|e| !e.is_vcs()) + .filter(|e| match declared_sums(recipe, e.index) { + Some(d) => d == SKIP || !is_hex_digest(&d), + None => true, + }) + .map(|e| e.index) + .collect() +} + #[cfg(test)] mod tests { use super::*; |
