gix_index/file/
verify.rs

1use std::sync::atomic::AtomicBool;
2
3use crate::File;
4
5mod error {
6    /// The error returned by [File::verify_integrity()][super::File::verify_integrity()].
7    #[derive(Debug, thiserror::Error)]
8    #[allow(missing_docs)]
9    pub enum Error {
10        #[error("Could not read index file to generate hash")]
11        Io(#[from] gix_hash::io::Error),
12        #[error("Index checksum mismatch")]
13        Verify(#[from] gix_hash::verify::Error),
14    }
15}
16pub use error::Error;
17
18impl File {
19    /// Verify the integrity of the index to assure its consistency.
20    pub fn verify_integrity(&self) -> Result<(), Error> {
21        let _span = gix_features::trace::coarse!("gix_index::File::verify_integrity()");
22        if let Some(checksum) = self.checksum {
23            let num_bytes_to_hash =
24                self.path.metadata().map_err(gix_hash::io::Error::from)?.len() - checksum.as_bytes().len() as u64;
25            let should_interrupt = AtomicBool::new(false);
26            gix_hash::bytes_of_file(
27                &self.path,
28                num_bytes_to_hash,
29                checksum.kind(),
30                &mut gix_features::progress::Discard,
31                &should_interrupt,
32            )?
33            .verify(&checksum)?;
34        }
35        Ok(())
36    }
37}