Skip to main content

gix_pack/data/file/
verify.rs

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