1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
use std::sync::atomic::AtomicBool;
use gix_features::{parallel, progress::Progress, zlib};
use crate::index;
mod reduce;
///
#[allow(clippy::empty_docs)]
pub mod with_index;
///
#[allow(clippy::empty_docs)]
pub mod with_lookup;
use reduce::Reducer;
mod error;
pub use error::Error;
use gix_features::progress::DynNestedProgress;
mod types;
pub use types::{Algorithm, ProgressId, SafetyCheck, Statistics};
/// Traversal options for [`index::File::traverse()`].
#[derive(Debug, Clone)]
pub struct Options<F> {
/// The algorithm to employ.
pub traversal: Algorithm,
/// If `Some`, only use the given amount of threads. Otherwise, the amount of threads to use will be selected based on
/// the amount of available logical cores.
pub thread_limit: Option<usize>,
/// The kinds of safety checks to perform.
pub check: SafetyCheck,
/// A function to create a pack cache
pub make_pack_lookup_cache: F,
}
impl Default for Options<fn() -> crate::cache::Never> {
fn default() -> Self {
Options {
check: Default::default(),
traversal: Default::default(),
thread_limit: None,
make_pack_lookup_cache: || crate::cache::Never,
}
}
}
/// The outcome of the [`traverse()`][index::File::traverse()] method.
pub struct Outcome {
/// The checksum obtained when hashing the file, which matched the checksum contained within the file.
pub actual_index_checksum: gix_hash::ObjectId,
/// The statistics obtained during traversal.
pub statistics: Statistics,
}
/// Traversal of pack data files using an index file
impl index::File {
/// Iterate through all _decoded objects_ in the given `pack` and handle them with a `Processor`.
/// The return value is (pack-checksum, [`Outcome`], `progress`), thus the pack traversal will always verify
/// the whole packs checksum to assure it was correct. In case of bit-rod, the operation will abort early without
/// verifying all objects using the [interrupt mechanism][gix_features::interrupt] mechanism.
///
/// # Algorithms
///
/// Using the [`Options::traversal`] field one can chose between two algorithms providing different tradeoffs. Both invoke
/// `new_processor()` to create functions receiving decoded objects, their object kind, index entry and a progress instance to provide
/// progress information.
///
/// * [`Algorithm::DeltaTreeLookup`] builds an index to avoid any unnecessary computation while resolving objects, avoiding
/// the need for a cache entirely, rendering `new_cache()` unused.
/// One could also call [`traverse_with_index()`][index::File::traverse_with_index()] directly.
/// * [`Algorithm::Lookup`] uses a cache created by `new_cache()` to avoid having to re-compute all bases of a delta-chain while
/// decoding objects.
/// One could also call [`traverse_with_lookup()`][index::File::traverse_with_lookup()] directly.
///
/// Use [`thread_limit`][Options::thread_limit] to further control parallelism and [`check`][SafetyCheck] to define how much the passed
/// objects shall be verified beforehand.
pub fn traverse<C, Processor, E, F>(
&self,
pack: &crate::data::File,
progress: &mut dyn DynNestedProgress,
should_interrupt: &AtomicBool,
processor: Processor,
Options {
traversal,
thread_limit,
check,
make_pack_lookup_cache,
}: Options<F>,
) -> Result<Outcome, Error<E>>
where
C: crate::cache::DecodeEntry,
E: std::error::Error + Send + Sync + 'static,
Processor: FnMut(gix_object::Kind, &[u8], &index::Entry, &dyn Progress) -> Result<(), E> + Send + Clone,
F: Fn() -> C + Send + Clone,
{
match traversal {
Algorithm::Lookup => self.traverse_with_lookup(
processor,
pack,
progress,
should_interrupt,
with_lookup::Options {
thread_limit,
check,
make_pack_lookup_cache,
},
),
Algorithm::DeltaTreeLookup => self.traverse_with_index(
pack,
processor,
progress,
should_interrupt,
with_index::Options { check, thread_limit },
),
}
}
fn possibly_verify<E>(
&self,
pack: &crate::data::File,
check: SafetyCheck,
pack_progress: &mut dyn Progress,
index_progress: &mut dyn Progress,
should_interrupt: &AtomicBool,
) -> Result<gix_hash::ObjectId, Error<E>>
where
E: std::error::Error + Send + Sync + 'static,
{
Ok(if check.file_checksum() {
if self.pack_checksum() != pack.checksum() {
return Err(Error::PackMismatch {
actual: pack.checksum(),
expected: self.pack_checksum(),
});
}
let (pack_res, id) = parallel::join(
move || pack.verify_checksum(pack_progress, should_interrupt),
move || self.verify_checksum(index_progress, should_interrupt),
);
pack_res?;
id?
} else {
self.index_checksum()
})
}
#[allow(clippy::too_many_arguments)]
fn decode_and_process_entry<C, E>(
&self,
check: SafetyCheck,
pack: &crate::data::File,
cache: &mut C,
buf: &mut Vec<u8>,
inflate: &mut zlib::Inflate,
progress: &mut dyn Progress,
index_entry: &index::Entry,
processor: &mut impl FnMut(gix_object::Kind, &[u8], &index::Entry, &dyn Progress) -> Result<(), E>,
) -> Result<crate::data::decode::entry::Outcome, Error<E>>
where
C: crate::cache::DecodeEntry,
E: std::error::Error + Send + Sync + 'static,
{
let pack_entry = pack.entry(index_entry.pack_offset)?;
let pack_entry_data_offset = pack_entry.data_offset;
let entry_stats = pack
.decode_entry(
pack_entry,
buf,
inflate,
&|id, _| {
let index = self.lookup(id)?;
pack.entry(self.pack_offset_at_index(index))
.ok()
.map(crate::data::decode::entry::ResolvedBase::InPack)
},
cache,
)
.map_err(|e| Error::PackDecode {
source: e,
id: index_entry.oid,
offset: index_entry.pack_offset,
})?;
let object_kind = entry_stats.kind;
let header_size = (pack_entry_data_offset - index_entry.pack_offset) as usize;
let entry_len = header_size + entry_stats.compressed_size;
process_entry(
check,
object_kind,
buf,
index_entry,
|| pack.entry_crc32(index_entry.pack_offset, entry_len),
progress,
processor,
)?;
Ok(entry_stats)
}
}
#[allow(clippy::too_many_arguments)]
fn process_entry<E>(
check: SafetyCheck,
object_kind: gix_object::Kind,
decompressed: &[u8],
index_entry: &index::Entry,
pack_entry_crc32: impl FnOnce() -> u32,
progress: &dyn Progress,
processor: &mut impl FnMut(gix_object::Kind, &[u8], &index::Entry, &dyn Progress) -> Result<(), E>,
) -> Result<(), Error<E>>
where
E: std::error::Error + Send + Sync + 'static,
{
if check.object_checksum() {
let actual_oid = gix_object::compute_hash(index_entry.oid.kind(), object_kind, decompressed);
if actual_oid != index_entry.oid {
return Err(Error::PackObjectMismatch {
actual: actual_oid,
expected: index_entry.oid,
offset: index_entry.pack_offset,
kind: object_kind,
});
}
if let Some(desired_crc32) = index_entry.crc32 {
let actual_crc32 = pack_entry_crc32();
if actual_crc32 != desired_crc32 {
return Err(Error::Crc32Mismatch {
actual: actual_crc32,
expected: desired_crc32,
offset: index_entry.pack_offset,
kind: object_kind,
});
}
}
}
processor(object_kind, decompressed, index_entry, progress).map_err(Error::Processor)
}