Skip to main content

gix_pack/data/file/
verify.rs

1use std::sync::atomic::AtomicBool;
2
3use gix_features::progress::Progress;
4
5use crate::data::File;
6
7///
8pub mod checksum {
9    /// Returned by [`data::File::verify_checksum()`][crate::data::File::verify_checksum()].
10    pub type Error = crate::verify::checksum::Error;
11}
12
13/// Checksums and verify checksums
14impl<T> File<T>
15where
16    T: crate::FileData,
17{
18    /// The checksum in the trailer of this pack data file
19    pub fn checksum(&self) -> gix_hash::ObjectId {
20        gix_hash::ObjectId::from_bytes_or_panic(&self.data[self.data.len() - self.hash_len..])
21    }
22
23    /// Verifies that the checksum of the packfile over all bytes preceding it indeed matches the actual checksum,
24    /// returning the actual checksum equivalent to the return value of [`checksum()`][File::checksum()] if there
25    /// is no mismatch.
26    ///
27    /// Note that if no `progress` is desired, one can pass [`gix_features::progress::Discard`].
28    ///
29    /// Have a look at [`index::File::verify_integrity(…)`][crate::index::File::verify_integrity()] for an
30    /// even more thorough integrity check.
31    pub fn verify_checksum(
32        &self,
33        progress: &mut dyn Progress,
34        should_interrupt: &AtomicBool,
35    ) -> Result<gix_hash::ObjectId, checksum::Error> {
36        crate::verify::checksum_on_disk_or_mmap(
37            self.path(),
38            &self.data,
39            self.checksum(),
40            self.object_hash,
41            progress,
42            should_interrupt,
43        )
44    }
45}