Skip to main content

gix_pack/data/
mod.rs

1//! a pack data file
2use crate::MMap;
3use std::path::Path;
4
5/// The offset to an entry into the pack data file, relative to its beginning.
6pub type Offset = u64;
7
8/// An identifier to uniquely identify all packs loaded within a known context or namespace.
9pub type Id = u32;
10
11/// An representing an full- or delta-object within a pack
12#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct Entry {
15    /// The entry's header
16    pub header: entry::Header,
17    /// The decompressed size of the entry in bytes.
18    ///
19    /// Note that for non-delta entries this will be the size of the object itself.
20    pub decompressed_size: u64,
21    /// absolute offset to compressed object data in the pack, just behind the entry's header
22    pub data_offset: Offset,
23    /// The amount of bytes used to encode the entry header in the pack.
24    ///
25    /// Git accepts non-canonical size encodings in pack entry headers. These are encodings that
26    /// continue the variable-length size field even though the remaining size bits are zero, for
27    /// example `b3 00` for a blob of size `3` instead of the canonical single-byte `33`.
28    ///
29    /// Git does not usually write such headers itself, but they can appear when pack data is reused
30    /// verbatim, for example when a server constructs fetch or clone responses from already stored
31    /// pack entries. Readers have to preserve the byte length that was actually consumed: recomputing
32    /// the canonical length from the decoded size would shift [`Entry::pack_offset()`] backwards and
33    /// make ofs-delta base-offset calculations refer to the wrong entry.
34    ///
35    /// A value of `0` means the actual length is unknown, causing [`Entry::header_size()`] to
36    /// recompute the canonical length from the decoded size. This is useful for deserializing data
37    /// produced by older versions.
38    #[cfg_attr(feature = "serde", serde(default))]
39    pub encoded_header_size: u16,
40}
41
42mod file;
43pub use file::{Header, decode, verify};
44///
45pub mod header;
46
47///
48pub mod entry;
49
50///
51#[cfg(feature = "streaming-input")]
52pub mod input;
53
54/// Utilities to encode pack data entries and write them to a `Write` implementation to resemble a pack data file.
55#[cfg(feature = "generate")]
56pub mod output;
57
58/// A slice into a pack file denoting a pack entry.
59///
60/// An entry can be decoded into an object.
61pub type EntryRange = std::ops::Range<Offset>;
62
63/// Supported versions of a pack data file
64#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
65#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66pub enum Version {
67    /// The default pack data version.
68    ///
69    /// This is the version generated by Git and by `gix-pack` writers.
70    #[default]
71    V2,
72    /// A pack data version accepted by Git and recognized by `gix-pack` readers.
73    ///
74    /// Git does not generate this version.
75    /// Entries are decoded with the same layout as [`V2`](Version::V2); the difference
76    /// visible to this crate is the version number stored in the pack header.
77    V3,
78}
79
80/// A pack data file
81pub struct File<T = MMap> {
82    data: T,
83    path: std::path::PathBuf,
84    /// A value to represent this pack uniquely when used with cache lookup, or a way to identify this pack by its location on disk.
85    /// The same location on disk should yield the same id.
86    ///
87    /// These must be unique per pack and must be stable, that is they don't change if the pack doesn't change.
88    /// If the same id is assigned (or reassigned) to different packs, pack creation or cache access will fail in hard-to-debug ways.
89    ///
90    /// This value is controlled by the owning object store, which can use it in whichever way it wants as long as the above constraints are met.
91    pub id: Id,
92    version: Version,
93    num_objects: u32,
94    /// The kind of hash contained within. This is entirely determined by the caller, and repositories have to know which hash to use
95    /// based on their configuration.
96    object_hash: gix_hash::Kind,
97    /// The maximum size of a single allocation caused by user-controlled on-disk pack data.
98    ///
99    /// If `None`, no additional limit is enforced.
100    pub alloc_limit_bytes: Option<usize>,
101}
102
103/// Information about the pack data file itself
104impl<T> File<T>
105where
106    T: crate::FileData,
107{
108    /// The pack data version of this file
109    pub fn version(&self) -> Version {
110        self.version
111    }
112    /// The number of objects stored in this pack data file
113    pub fn num_objects(&self) -> u32 {
114        self.num_objects
115    }
116    /// The length of all mapped data, including the pack header and the pack trailer
117    pub fn data_len(&self) -> usize {
118        self.data.len()
119    }
120    /// The kind of hash we use internally.
121    pub fn object_hash(&self) -> gix_hash::Kind {
122        self.object_hash
123    }
124    /// The position of the byte one past the last pack entry, or in other terms, the first byte of the trailing hash.
125    pub fn pack_end(&self) -> usize {
126        self.data.len() - self.object_hash.len_in_bytes()
127    }
128
129    /// The path to the pack data file on disk
130    pub fn path(&self) -> &Path {
131        &self.path
132    }
133
134    /// Returns the pack data at the given slice if its range is contained in the mapped pack data
135    pub fn entry_slice(&self, slice: EntryRange) -> Option<&[u8]> {
136        let entry_end: usize = slice.end.try_into().expect("end of pack fits into usize");
137        let entry_start = slice.start as usize;
138        self.data.get(entry_start..entry_end)
139    }
140
141    /// Returns the CRC32 of the pack data indicated by `pack_offset` and the `size` of the mapped data.
142    ///
143    /// _Note:_ finding the right size is only possible by decompressing
144    /// the pack entry beforehand, or by using the (to be sorted) offsets stored in an index file.
145    ///
146    /// # Panics
147    ///
148    /// If `pack_offset` or `size` are pointing to a range outside of the mapped pack data.
149    pub fn entry_crc32(&self, pack_offset: Offset, size: usize) -> u32 {
150        let pack_offset: usize = pack_offset.try_into().expect("pack_size fits into usize");
151        crc32fast::hash(&self.data[pack_offset..pack_offset + size])
152    }
153}
154
155///
156pub mod delta;