gix-pack 0.76.0

Implements git packs and related data structures
Documentation
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use std::collections::BTreeMap;

use gix_error::{Message, OptionExt, ResourceExhaustionKind, Result, ResultExt, bail};

use super::Tree;

/// Maps each referenced base object ID to indices in `Tree::child_items` of ref-deltas waiting for it.
pub(super) type RefDeltaChildren = BTreeMap<gix_hash::ObjectId, Vec<u32>>;

/// An item stored within the [`Tree`] whose data is stored in a pack file, identified by
/// the offset of its first (`offset`) and last (`next_offset`) bytes.
///
/// It represents either a root entry, or one that relies on a base to be resolvable,
/// alongside associated `data` `T`.
pub struct Item<T> {
    /// The offset into the pack file at which the pack entry's data is located.
    pub offset: crate::data::Offset,
    /// The offset of the next item in the pack file.
    pub next_offset: crate::data::Offset,
    /// Data to store with each Item, effectively data associated with each entry in a pack.
    pub data: T,
    /// Indices into our Tree's `items`, one for each pack entry that depends on us.
    ///
    /// Limited to u32 as that's the maximum amount of objects in a pack.
    // SAFETY INVARIANT:
    //    - only one Item in a tree may have any given child index. `future_child_offsets`
    //      and `ref_child_indices` should also not contain any indices found in `children`.
    //    - These indices should be in bounds for tree.child_items
    children: Vec<u32>,
}

impl<T> Item<T> {
    /// Get the children
    // (we don't want to expose mutable access)
    pub(super) fn children(&self) -> &[u32] {
        &self.children
    }

    pub(super) fn extend_children(&mut self, children: impl IntoIterator<Item = u32>) {
        self.children.extend(children);
    }
}

/// Identify what kind of node we have last seen
pub(super) enum NodeKind {
    Root,
    Child,
}

fn allocation_error(kind: ResourceExhaustionKind) -> Message {
    gix_error::resource_exhaustion(kind, "The pack delta tree is too large to fit in memory")
}

impl<T> Tree<T> {
    /// Instantiate a empty tree capable of storing `num_objects` amounts of items.
    pub(crate) fn with_capacity(num_objects: usize, alloc_limit_bytes: Option<usize>) -> Result<Self> {
        let capacity = num_objects / 2;
        let allocation_bytes = capacity
            .checked_mul(std::mem::size_of::<Item<T>>())
            .ok_or_raise(|| allocation_error(ResourceExhaustionKind::AllocationFailure))?;
        if alloc_limit_bytes.is_some_and(|limit| allocation_bytes > limit) {
            bail!(allocation_error(ResourceExhaustionKind::AllocationLimit));
        }

        let mut root_items = Vec::new();
        root_items
            .try_reserve_exact(capacity)
            .or_raise(|| allocation_error(ResourceExhaustionKind::AllocationFailure))?;
        let mut child_items = Vec::new();
        child_items
            .try_reserve_exact(capacity)
            .or_raise(|| allocation_error(ResourceExhaustionKind::AllocationFailure))?;
        Ok(Tree {
            root_items,
            child_items,
            last_seen: None,
            future_child_offsets: Vec::new(),
            ref_child_indices: BTreeMap::new(),
        })
    }

    pub(super) fn num_items(&self) -> usize {
        self.root_items.len() + self.child_items.len()
    }

    /// Returns self's root and child items.
    ///
    /// You can rely on them following the same `children` invariants as they did in the tree
    pub(super) fn take_root_child_and_refs(self) -> (Vec<Item<T>>, Vec<Item<T>>, RefDeltaChildren) {
        (self.root_items, self.child_items, self.ref_child_indices)
    }

    pub(super) fn assert_is_incrementing_and_update_next_offset(&mut self, offset: crate::data::Offset) -> Result {
        let items = match &self.last_seen {
            Some(NodeKind::Root) => &mut self.root_items,
            Some(NodeKind::Child) => &mut self.child_items,
            None => return Ok(()),
        };
        let item = &mut items.last_mut().expect("last seen won't lie");
        if offset <= item.offset {
            bail!(gix_error::corruption(format!(
                "Pack offsets must only increment. The previous pack offset was {}, the current one is {offset}",
                item.offset
            )));
        }
        item.next_offset = offset;
        Ok(())
    }

    pub(super) fn set_pack_entries_end_and_resolve_ref_offsets(
        &mut self,
        pack_entries_end: crate::data::Offset,
    ) -> Result {
        if !self.future_child_offsets.is_empty() {
            for (parent_offset, child_index) in self.future_child_offsets.drain(..) {
                // SAFETY invariants upheld:
                //  - We are draining from future_child_offsets and adding to children, keeping things the same.
                //  - We can rely on the `future_child_offsets` invariant to be sure that `children` is
                //    not getting any indices that are already in use in `children` elsewhere
                //  - The indices are in bounds for child_items since they were in bounds for future_child_offsets,
                //    we can carry over the invariant.
                if let Ok(i) = self.child_items.binary_search_by_key(&parent_offset, |i| i.offset) {
                    self.child_items[i].children.push(child_index as u32);
                } else if let Ok(i) = self.root_items.binary_search_by_key(&parent_offset, |i| i.offset) {
                    self.root_items[i].children.push(child_index as u32);
                } else {
                    bail!(
                        "The base at {parent_offset} was referred to by a ref-delta, but it was never added to the tree as if the pack was still thin.".corrupted()
                    );
                }
            }
        }

        self.assert_is_incrementing_and_update_next_offset(pack_entries_end)
    }

    /// Add a new root node, one that only has children but is not a child itself, at the given pack `offset` and associate
    /// custom `data` with it.
    pub(crate) fn add_root(&mut self, offset: crate::data::Offset, data: T) -> Result {
        self.assert_is_incrementing_and_update_next_offset(offset)?;
        self.last_seen = NodeKind::Root.into();
        self.root_items.push(Item {
            offset,
            next_offset: 0,
            data,
            // SAFETY INVARIANT upheld: there are no children
            children: Default::default(),
        });
        Ok(())
    }

    /// Add a child of the item at `base_offset` which itself resides at pack `offset` and associate custom `data` with it.
    /// If the base has not been added yet, defer attachment until the tree is finalized before traversal.
    pub(crate) fn add_child(
        &mut self,
        base_offset: crate::data::Offset,
        offset: crate::data::Offset,
        data: T,
    ) -> Result {
        self.assert_is_incrementing_and_update_next_offset(offset)?;

        let next_child_index = self.child_items.len();
        // SAFETY INVARIANT upheld:
        // - This is one of two methods that modifies `children` and future_child_offsets. Out
        //   of the two, it is the only one that produces new indices in the system.
        // - This always pushes next_child_index to *either* `children` or `future_child_offsets`,
        //   maintaining the cross-field invariant there.
        // - This method will always push to child_items (at the end), incrementing
        //   future values of next_child_index. This means next_child_index is always
        //   unique for this method call.
        // - As the only method producing new indices, this is the only time
        //   next_child_index will be added to children/future_child_offsets, upholding the invariant.
        // - Since next_child_index will always be a valid index by the end of this method,
        //   this always produces valid in-bounds indices, upholding the bounds invariant.

        if let Ok(i) = self.child_items.binary_search_by_key(&base_offset, |i| i.offset) {
            self.child_items[i].children.push(next_child_index as u32);
        } else if let Ok(i) = self.root_items.binary_search_by_key(&base_offset, |i| i.offset) {
            self.root_items[i].children.push(next_child_index as u32);
        } else {
            self.future_child_offsets.push((base_offset, next_child_index));
        }

        self.last_seen = NodeKind::Child.into();
        self.child_items.push(Item {
            offset,
            next_offset: 0,
            data,
            // SAFETY INVARIANT upheld: there are no children
            children: Default::default(),
        });
        Ok(())
    }

    /// Add a child whose base is identified by object id and may occur anywhere in the pack.
    /// During traversal, each fully resolved object is hashed and any children waiting for its ID are attached to it.
    #[cfg(feature = "streaming-input")]
    pub(crate) fn add_child_by_id(
        &mut self,
        base_id: gix_hash::ObjectId,
        offset: crate::data::Offset,
        data: T,
    ) -> Result {
        self.assert_is_incrementing_and_update_next_offset(offset)?;

        let child_index = self.child_items.len() as u32;
        self.ref_child_indices.entry(base_id).or_default().push(child_index);
        self.last_seen = NodeKind::Child.into();
        self.child_items.push(Item {
            offset,
            next_offset: 0,
            data,
            children: Default::default(),
        });
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn unresolved_ref_delta_parent_is_corruption() -> gix_testtools::TestResult {
        let mut tree = super::Tree::with_capacity(1, None)?;
        tree.add_child(24, 12, ())?;
        let err = tree
            .set_pack_entries_end_and_resolve_ref_offsets(32)
            .expect_err("the referenced parent offset is absent from the completed pack");
        assert!(
            err.is_corrupted(),
            "the pack's delta references are internally inconsistent"
        );

        assert_eq!(
            err.to_string(),
            "The base at 24 was referred to by a ref-delta, but it was never added to the tree as if the pack was still thin.",
            "classification preserves the diagnostic"
        );
        Ok(())
    }

    #[test]
    fn forward_ref_delta_parent_is_allowed() -> gix_testtools::TestResult {
        let mut tree = super::Tree::with_capacity(2, None)?;
        tree.add_child(24, 12, ())?;
        tree.add_root(24, ())?;
        tree.set_pack_entries_end_and_resolve_ref_offsets(32)?;
        assert_eq!(
            tree.root_items[0].children(),
            &[0],
            "a later parent resolves the forward reference"
        );
        Ok(())
    }

    #[test]
    fn pack_end_must_follow_the_last_entry() -> gix_testtools::TestResult {
        let mut tree = super::Tree::with_capacity(1, None)?;
        tree.add_root(12, ())?;
        let err = tree
            .set_pack_entries_end_and_resolve_ref_offsets(12)
            .expect_err("an entry at or past the pack end is corrupt input");
        assert!(err.is_corrupted(), "invalid end offsets must return a corruption error");
        Ok(())
    }

    #[test]
    fn allocation_failure_is_reported() {
        let err = super::Tree::<()>::with_capacity(usize::MAX, None)
            .err()
            .expect("an impossible attacker-controlled capacity must return an allocation error");
        insta::assert_debug_snapshot!(err, "allocation failure is reported", @"The pack delta tree is too large to fit in memory");
        assert_eq!(
            err.classify().find_map(|classification| match classification.class() {
                gix_error::Class::ResourceExhaustion(kind) => Some(kind),
                _ => None,
            }),
            Some(gix_error::ResourceExhaustionKind::AllocationFailure)
        );

        let err = super::Tree::<()>::with_capacity(2, Some(0))
            .err()
            .expect("the configured allocation limit must apply to delta-tree storage");
        insta::assert_debug_snapshot!(err, "allocation failure is reported", @"The pack delta tree is too large to fit in memory");
        assert_eq!(
            err.classify().find_map(|classification| match classification.class() {
                gix_error::Class::ResourceExhaustion(kind) => Some(kind),
                _ => None,
            }),
            Some(gix_error::ResourceExhaustionKind::AllocationLimit)
        );
    }

    mod from_offsets_in_pack {
        use std::sync::atomic::AtomicBool;

        use gix_error::{ErrorExt, TestResult, message};

        use crate as pack;

        const SMALL_PACK_INDEX: &str = "objects/pack/pack-a2bf8e71d8c18879e499335762dd95119d93d9f1.idx";
        const SMALL_PACK: &str = "objects/pack/pack-a2bf8e71d8c18879e499335762dd95119d93d9f1.pack";

        const INDEX_V1: &str = "objects/pack/pack-c0438c19fb16422b6bbcce24387b3264416d485b.idx";
        const PACK_FOR_INDEX_V1: &str = "objects/pack/pack-c0438c19fb16422b6bbcce24387b3264416d485b.pack";

        use gix_testtools::fixture_path;

        #[test]
        fn v1() -> gix_testtools::TestResult {
            Ok(tree(INDEX_V1, PACK_FOR_INDEX_V1)?)
        }

        #[test]
        fn v2() -> gix_testtools::TestResult {
            Ok(tree(SMALL_PACK_INDEX, SMALL_PACK)?)
        }

        #[test]
        fn invalid_ofs_delta_base_distance_is_reported() -> gix_testtools::TestResult {
            let first_entry_offset = pack::data::header::SIZE as pack::data::Offset;
            let pack_file = gix_testtools::tempfile::NamedTempFile::new()?;
            let mut pack_data = pack::data::header::encode(pack::data::Version::V2, 1).to_vec();
            pack::data::entry::Header::OfsDelta {
                base_distance: first_entry_offset + 1,
            }
            .write_to(0, &mut pack_data)?;
            std::fs::write(pack_file.path(), pack_data)?;

            let result = crate::cache::delta::Tree::from_offsets_in_pack(
                pack_file.path(),
                std::iter::once(()),
                &|_| first_entry_offset,
                &|_| Ok(None),
                &mut gix_utils::progress::Discard,
                &AtomicBool::new(false),
                gix_hash::Kind::Sha1,
            );

            assert!(result.is_err(), "an out-of-bounds delta base is corrupt pack data");
            Ok(())
        }

        #[test]
        fn duplicate_and_overlapping_offsets_are_rejected() -> gix_testtools::TestResult {
            let first = pack::data::header::SIZE as pack::data::Offset;
            let pack_file = gix_testtools::tempfile::NamedTempFile::new()?;
            let mut data = pack::data::header::encode(pack::data::Version::V2, 2).to_vec();
            // A two-byte header lets the second indexed offset overlap its predecessor.
            pack::data::entry::Header::Blob.write_to(16, &mut data)?;
            std::fs::write(pack_file.path(), data)?;
            for second in [first, first + 1] {
                let err = crate::cache::delta::Tree::from_offsets_in_pack(
                    pack_file.path(),
                    [first, second].into_iter(),
                    &|offset| *offset,
                    &|_| Ok(None),
                    &mut gix_utils::progress::Discard,
                    &AtomicBool::new(false),
                    gix_hash::Kind::Sha1,
                )
                .err()
                .expect("index offsets cannot duplicate or overlap pack entry headers");
                assert!(
                    err.is_corrupted(),
                    "invalid index offsets must return a corruption error"
                );
            }
            Ok(())
        }

        #[test]
        fn ref_delta_resolver_error_is_propagated_unchanged() -> TestResult {
            let (pack_file, offsets) = ref_delta_pack()?;
            let callback_error = std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                message("base index is inaccessible").with("path", "base-index"),
            )
            .and_raise(message("could not resolve in-pack base").with("operation", "base lookup"));
            let original_sources: Vec<_> = callback_error
                .iter_errors_with_locations()
                .map(|source| (source.error().to_string(), source.location()))
                .collect();
            let original_classes: Vec<_> = callback_error
                .classify()
                .map(|classification| classification.class())
                .collect();
            let callback_error = std::cell::RefCell::new(Some(callback_error));
            let err = crate::cache::delta::Tree::from_offsets_in_pack(
                pack_file.path(),
                offsets.into_iter(),
                &|offset| *offset,
                &|base_id| {
                    assert_eq!(
                        base_id,
                        gix_hash::Kind::Sha1.null().as_ref(),
                        "the resolver receives the ref-delta's base ID"
                    );
                    Err(callback_error
                        .borrow_mut()
                        .take()
                        .expect("the failing resolver is called only once"))
                },
                &mut gix_utils::progress::Discard,
                &AtomicBool::new(false),
                gix_hash::Kind::Sha1,
            )
            .err()
            .expect("a resolver error must abort tree construction");

            assert_eq!(
                err.iter_errors_with_locations()
                    .map(|source| (source.error().to_string(), source.location()))
                    .collect::<Vec<_>>(),
                original_sources,
                "the resolver's sources and locations survive without added tree context"
            );
            assert_eq!(
                err.classify()
                    .map(|classification| classification.class())
                    .collect::<Vec<_>>(),
                original_classes,
                "tree construction preserves all resolver error classifications"
            );
            assert_eq!(
                err.downcast_any_ref::<std::io::Error>()
                    .expect("the resolver's concrete I/O error remains available")
                    .kind(),
                std::io::ErrorKind::PermissionDenied,
                "the native failure remains available for recovery"
            );
            assert!(!err.is_not_found(), "a resolver failure is not a missing base");
            Ok(())
        }

        #[test]
        fn ref_delta_resolver_none_is_a_missing_base() -> TestResult {
            let (pack_file, offsets) = ref_delta_pack()?;
            let resolutions = std::cell::Cell::new(0);
            let err = crate::cache::delta::Tree::from_offsets_in_pack(
                pack_file.path(),
                offsets.into_iter(),
                &|offset| *offset,
                &|_| {
                    resolutions.set(resolutions.get() + 1);
                    Ok(None)
                },
                &mut gix_utils::progress::Discard,
                &AtomicBool::new(false),
                gix_hash::Kind::Sha1,
            )
            .err()
            .expect("Ok(None) means the ref-delta base is absent from the pack");

            assert_eq!(resolutions.get(), 1, "a missing base is looked up only once");
            assert!(
                err.is_not_found(),
                "an absent base retains its not-found classification"
            );
            assert_eq!(
                err.to_string(),
                format!(
                    "Base object {} was not found in this pack; offset-based tree construction requires in-pack bases",
                    gix_hash::Kind::Sha1.null()
                ),
                "a missing base identifies the ID and the offset-based constructor's in-pack requirement"
            );
            Ok(())
        }

        #[test]
        fn ref_delta_resolver_some_attaches_child_to_base() -> TestResult {
            let (pack_file, offsets) = ref_delta_pack()?;
            let resolutions = std::cell::Cell::new(0);
            let tree = crate::cache::delta::Tree::from_offsets_in_pack(
                pack_file.path(),
                offsets.into_iter(),
                &|offset| *offset,
                &|base_id| {
                    resolutions.set(resolutions.get() + 1);
                    assert_eq!(
                        base_id,
                        gix_hash::Kind::Sha1.null().as_ref(),
                        "the resolver receives the ref-delta's base ID"
                    );
                    Ok(Some(offsets[0]))
                },
                &mut gix_utils::progress::Discard,
                &AtomicBool::new(false),
                gix_hash::Kind::Sha1,
            )?;

            assert_eq!(resolutions.get(), 1, "only the ref-delta requires a base lookup");
            assert_eq!(tree.root_items.len(), 1, "the blob remains the sole root");
            assert_eq!(tree.child_items.len(), 1, "the ref-delta becomes the sole child");
            assert_eq!(tree.root_items[0].offset, offsets[0], "the base keeps its pack offset");
            assert_eq!(
                tree.root_items[0].children(),
                &[0],
                "the resolved base owns the ref-delta child"
            );
            assert_eq!(
                tree.child_items[0].data, offsets[1],
                "the child retains its caller-provided data"
            );
            Ok(())
        }

        fn ref_delta_pack() -> TestResult<(gix_testtools::tempfile::NamedTempFile, [pack::data::Offset; 2])> {
            let pack_file = gix_testtools::tempfile::NamedTempFile::new()?;
            let mut pack_data = pack::data::header::encode(pack::data::Version::V2, 2).to_vec();
            // Tree construction only reads entry headers, so compressed payloads and a trailer are unnecessary.
            let base_offset = pack_data.len() as pack::data::Offset;
            pack::data::entry::Header::Blob.write_to(0, &mut pack_data)?;
            let delta_offset = pack_data.len() as pack::data::Offset;
            pack::data::entry::Header::RefDelta {
                base_id: gix_hash::Kind::Sha1.null(),
            }
            .write_to(0, &mut pack_data)?;
            std::fs::write(pack_file.path(), pack_data)?;
            Ok((pack_file, [base_offset, delta_offset]))
        }

        fn tree(index_path: &str, pack_path: &str) -> gix_testtools::Result {
            let idx = pack::index::File::at(fixture_path(index_path), gix_hash::Kind::Sha1)?;
            crate::cache::delta::Tree::from_offsets_in_pack(
                &fixture_path(pack_path),
                idx.sorted_offsets().into_iter(),
                &|ofs| *ofs,
                &|id| Ok(idx.lookup(id).map(|index| idx.pack_offset_at_index(index))),
                &mut gix_utils::progress::Discard,
                &AtomicBool::new(false),
                gix_hash::Kind::Sha1,
            )?;
            Ok(())
        }
    }

    mod size {
        use gix_testtools::size_ok;

        use super::super::Item;

        #[test]
        fn size_of_pack_tree_item() {
            let actual = std::mem::size_of::<[Item<()>; 7_500_000]>();
            let expected = 300_000_000;
            assert!(
                size_ok(actual, expected),
                "we don't want these to grow unnoticed: {actual} <~ {expected}"
            );
        }

        #[test]
        fn size_of_pack_verify_data_structure() {
            pub struct EntryWithDefault {
                _index_entry: crate::index::Entry,
                _kind: gix_object::Kind,
                _object_size: u64,
                _decompressed_size: u64,
                _compressed_size: u64,
                _header_size: u16,
                _level: u16,
            }

            let actual = std::mem::size_of::<[Item<EntryWithDefault>; 7_500_000]>();
            let sha1 = 840_000_000;
            let sha256_extra = 120_000_000;
            let expected = sha1 + sha256_extra;
            assert!(
                size_ok(actual, expected),
                "we don't want these to grow unnoticed: {actual} <~ {expected}"
            );
        }
    }
}