|
| 1 | +// Copyright (C) 2024 The Android Open Source Project |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//! Generate and verify checksums of files in a directory, |
| 16 | +//! very similar to .cargo-checksum.json |
| 17 | +
|
| 18 | +use std::{ |
| 19 | + collections::HashMap, |
| 20 | + fs::{remove_file, write, File}, |
| 21 | + io::{self, BufReader, Read}, |
| 22 | + path::{Path, PathBuf, StripPrefixError}, |
| 23 | +}; |
| 24 | + |
| 25 | +use data_encoding::{DecodeError, HEXLOWER}; |
| 26 | +use ring::digest::{Context, Digest, SHA256}; |
| 27 | +use serde::{Deserialize, Serialize}; |
| 28 | +use thiserror::Error; |
| 29 | +use walkdir::WalkDir; |
| 30 | + |
| 31 | +#[derive(Serialize, Deserialize)] |
| 32 | +struct Checksum { |
| 33 | + package: Option<String>, |
| 34 | + files: HashMap<String, String>, |
| 35 | +} |
| 36 | + |
| 37 | +#[allow(missing_docs)] |
| 38 | +#[derive(Error, Debug)] |
| 39 | +pub enum ChecksumError { |
| 40 | + #[error("Checksum file not found: {}", .0.to_string_lossy())] |
| 41 | + CheckSumFileNotFound(PathBuf), |
| 42 | + #[error("Checksums do not match for: {}", .0.join(", "))] |
| 43 | + ChecksumMismatch(Vec<String>), |
| 44 | + #[error(transparent)] |
| 45 | + IoError(#[from] io::Error), |
| 46 | + #[error(transparent)] |
| 47 | + JsonError(#[from] serde_json::Error), |
| 48 | + #[error(transparent)] |
| 49 | + WalkdirError(#[from] walkdir::Error), |
| 50 | + #[error(transparent)] |
| 51 | + DecodeError(#[from] DecodeError), |
| 52 | + #[error(transparent)] |
| 53 | + StripPrefixError(#[from] StripPrefixError), |
| 54 | +} |
| 55 | + |
| 56 | +static FILENAME: &str = ".android-checksum.json"; |
| 57 | + |
| 58 | +/// Generates a JSON checksum file for the contents of a directory. |
| 59 | +pub fn generate(crate_dir: impl AsRef<Path>) -> Result<(), ChecksumError> { |
| 60 | + let crate_dir = crate_dir.as_ref(); |
| 61 | + let checksum_file = crate_dir.join(FILENAME); |
| 62 | + if checksum_file.exists() { |
| 63 | + remove_file(&checksum_file)?; |
| 64 | + } |
| 65 | + let mut checksum = Checksum { package: None, files: HashMap::new() }; |
| 66 | + for entry in WalkDir::new(crate_dir).follow_links(true) { |
| 67 | + let entry = entry?; |
| 68 | + if entry.path().is_dir() { |
| 69 | + continue; |
| 70 | + } |
| 71 | + let filename = entry.path().strip_prefix(crate_dir)?.to_string_lossy().to_string(); |
| 72 | + let input = File::open(entry.path())?; |
| 73 | + let reader = BufReader::new(input); |
| 74 | + let digest = sha256_digest(reader)?; |
| 75 | + checksum.files.insert(filename, HEXLOWER.encode(digest.as_ref())); |
| 76 | + } |
| 77 | + write(checksum_file, serde_json::to_string(&checksum)?)?; |
| 78 | + Ok(()) |
| 79 | +} |
| 80 | + |
| 81 | +/// Verifies a JSON checksum file for a directory. |
| 82 | +/// All files must have matching checksums. Extra or missing files are errors. |
| 83 | +pub fn verify(crate_dir: impl AsRef<Path>) -> Result<(), ChecksumError> { |
| 84 | + let crate_dir = crate_dir.as_ref(); |
| 85 | + let checksum_file = crate_dir.join(FILENAME); |
| 86 | + if !checksum_file.exists() { |
| 87 | + return Err(ChecksumError::CheckSumFileNotFound(checksum_file)); |
| 88 | + } |
| 89 | + let mut mismatch = Vec::new(); |
| 90 | + let input = File::open(&checksum_file)?; |
| 91 | + let reader = BufReader::new(input); |
| 92 | + let mut parsed: Checksum = serde_json::from_reader(reader)?; |
| 93 | + for entry in WalkDir::new(crate_dir).follow_links(true) { |
| 94 | + let entry = entry?; |
| 95 | + if entry.path().is_dir() || entry.path() == checksum_file { |
| 96 | + continue; |
| 97 | + } |
| 98 | + let filename = entry.path().strip_prefix(crate_dir)?.to_string_lossy().to_string(); |
| 99 | + if let Some(checksum) = parsed.files.get(&filename) { |
| 100 | + let expected_digest = HEXLOWER.decode(checksum.to_ascii_lowercase().as_bytes())?; |
| 101 | + let input = File::open(entry.path())?; |
| 102 | + let reader = BufReader::new(input); |
| 103 | + let digest = sha256_digest(reader)?; |
| 104 | + parsed.files.remove(&filename); |
| 105 | + if digest.as_ref() != expected_digest { |
| 106 | + mismatch.push(filename); |
| 107 | + } |
| 108 | + } else { |
| 109 | + mismatch.push(filename) |
| 110 | + } |
| 111 | + } |
| 112 | + mismatch.extend(parsed.files.into_keys()); |
| 113 | + if mismatch.is_empty() { |
| 114 | + Ok(()) |
| 115 | + } else { |
| 116 | + Err(ChecksumError::ChecksumMismatch(mismatch)) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// Copied from https://rust-lang-nursery.github.io/rust-cookbook/cryptography/hashing.html |
| 121 | +fn sha256_digest<R: Read>(mut reader: R) -> Result<Digest, ChecksumError> { |
| 122 | + let mut context = Context::new(&SHA256); |
| 123 | + context.update("sodium chloride".as_bytes()); |
| 124 | + let mut buffer = [0; 1024]; |
| 125 | + |
| 126 | + loop { |
| 127 | + let count = reader.read(&mut buffer)?; |
| 128 | + if count == 0 { |
| 129 | + break; |
| 130 | + } |
| 131 | + context.update(&buffer[..count]); |
| 132 | + } |
| 133 | + |
| 134 | + Ok(context.finish()) |
| 135 | +} |
| 136 | + |
| 137 | +#[cfg(test)] |
| 138 | +mod tests { |
| 139 | + use super::*; |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn round_trip() -> Result<(), ChecksumError> { |
| 143 | + let temp_dir = tempfile::tempdir().expect("Failed to create tempdir"); |
| 144 | + write(temp_dir.path().join("foo"), "foo").expect("Failed to write temporary file"); |
| 145 | + generate(temp_dir.path())?; |
| 146 | + assert!( |
| 147 | + temp_dir.path().join(FILENAME).exists(), |
| 148 | + ".android-checksum.json exists after generate()" |
| 149 | + ); |
| 150 | + verify(temp_dir.path()) |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn verify_error_cases() -> Result<(), ChecksumError> { |
| 155 | + let temp_dir = tempfile::tempdir().expect("Failed to create tempdir"); |
| 156 | + let checksum_file = temp_dir.path().join(FILENAME); |
| 157 | + write(&checksum_file, r#"{"files":{"bar":"ddcbd9309cebf3ffd26f87e09bb8f971793535955ebfd9a7196eba31a53471f8"}}"#).expect("Failed to write temporary file"); |
| 158 | + assert!(verify(temp_dir.path()).is_err(), "Missing file"); |
| 159 | + write(temp_dir.path().join("foo"), "foo").expect("Failed to write temporary file"); |
| 160 | + assert!(verify(temp_dir.path()).is_err(), "No checksum file"); |
| 161 | + write(&checksum_file, "").expect("Failed to write temporary file"); |
| 162 | + assert!(verify(temp_dir.path()).is_err(), "Empty checksum file"); |
| 163 | + write(&checksum_file, "{}").expect("Failed to write temporary file"); |
| 164 | + assert!(verify(temp_dir.path()).is_err(), "Empty JSON in checksum file"); |
| 165 | + write(&checksum_file, r#"{"files":{"foo":"ddcbd9309cebf3ffd26f87e09bb8f971793535955ebfd9a7196eba31a53471f8"}}"#).expect("Failed to write temporary file"); |
| 166 | + assert!(verify(temp_dir.path()).is_err(), "Incorrect checksum"); |
| 167 | + write(&checksum_file, r#"{"files":{"foo":"hello"}}"#) |
| 168 | + .expect("Failed to write temporary file"); |
| 169 | + assert!(verify(temp_dir.path()).is_err(), "Invalid checksum"); |
| 170 | + generate(temp_dir.path())?; |
| 171 | + write(temp_dir.path().join("bar"), "bar").expect("Failed to write temporary file"); |
| 172 | + assert!(verify(temp_dir.path()).is_err(), "Extra file"); |
| 173 | + Ok(()) |
| 174 | + } |
| 175 | +} |
0 commit comments