Skip to main content

gix_pack/data/entry/
mod.rs

1use crate::data::Entry;
2
3const _TYPE_EXT1: u8 = 0;
4const COMMIT: u8 = 1;
5const TREE: u8 = 2;
6const BLOB: u8 = 3;
7const TAG: u8 = 4;
8const _TYPE_EXT2: u8 = 5;
9const OFS_DELTA: u8 = 6;
10const REF_DELTA: u8 = 7;
11
12/// A way to uniquely identify the location of an entry within a pack bundle
13#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct Location {
16    /// The id of the pack containing the object. It's unique within its frame of reference which is the owning object database.
17    pub pack_id: u32,
18    /// The size of the entry of disk so that the range of bytes of the entry is `pack_offset..pack_offset + entry_size`.
19    pub entry_size: usize,
20    /// The start of the entry in the pack identified by `pack_id`.
21    pub pack_offset: data::Offset,
22}
23
24impl Location {
25    /// Compute a range suitable for lookup in pack data using the [`entry_slice()`][crate::data::File::entry_slice()] method.
26    pub fn entry_range(&self, pack_offset: data::Offset) -> crate::data::EntryRange {
27        pack_offset..pack_offset + self.entry_size as u64
28    }
29}
30
31/// Access
32impl Entry {
33    /// Compute the pack offset to the base entry of the object represented by this entry, or
34    /// return `None` if the distance would underflow or is invalid.
35    pub fn checked_base_pack_offset(&self, distance: u64) -> Option<data::Offset> {
36        let pack_offset = self.data_offset - self.header_size() as u64;
37        Header::verified_base_pack_offset(pack_offset, distance)
38    }
39
40    /// Compute the pack offset to the base entry of the object represented by this entry.
41    ///
42    /// # Panics
43    ///
44    /// Panics if the `distance` will cause an underflow or is invalid.
45    pub fn base_pack_offset(&self, distance: u64) -> data::Offset {
46        self.checked_base_pack_offset(distance)
47            .expect("in-bound distance of deltas")
48    }
49    /// The pack offset at which this entry starts
50    pub fn pack_offset(&self) -> data::Offset {
51        self.data_offset - self.header_size() as u64
52    }
53    /// The amount of bytes used to describe this entry in the pack. The header starts at [`Self::pack_offset()`]
54    pub fn header_size(&self) -> usize {
55        self.header.size(self.decompressed_size)
56    }
57}
58
59///
60pub mod decode;
61
62mod header;
63pub use header::Header;
64
65use crate::data;