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}
24
25mod file;
26pub use file::{decode, verify, Header};
27///
28pub mod header;
29
30///
31pub mod init {
32 pub use super::header::decode::Error;
33}
34
35///
36pub mod entry;
37
38///
39#[cfg(feature = "streaming-input")]
40pub mod input;
41
42/// Utilities to encode pack data entries and write them to a `Write` implementation to resemble a pack data file.
43#[cfg(feature = "generate")]
44pub mod output;
45
46/// A slice into a pack file denoting a pack entry.
47///
48/// An entry can be decoded into an object.
49pub type EntryRange = std::ops::Range<Offset>;
50
51/// Supported versions of a pack data file
52#[derive(Default, PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
53#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
54#[allow(missing_docs)]
55pub enum Version {
56 #[default]
57 V2,
58 V3,
59}
60
61/// A pack data file
62pub struct File<T = MMap> {
63 data: T,
64 path: std::path::PathBuf,
65 /// A value to represent this pack uniquely when used with cache lookup, or a way to identify this pack by its location on disk.
66 /// The same location on disk should yield the same id.
67 ///
68 /// These must be unique per pack and must be stable, that is they don't change if the pack doesn't change.
69 /// If the same id is assigned (or reassigned) to different packs, pack creation or cache access will fail in hard-to-debug ways.
70 ///
71 /// 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.
72 pub id: Id,
73 version: Version,
74 num_objects: u32,
75 /// The size of the hash contained within. This is entirely determined by the caller, and repositories have to know which hash to use
76 /// based on their configuration.
77 hash_len: usize,
78 object_hash: gix_hash::Kind,
79 /// The maximum size of a single allocation caused by user-controlled on-disk pack data.
80 ///
81 /// If `None`, no additional limit is enforced.
82 alloc_limit_bytes: Option<usize>,
83}
84
85/// Information about the pack data file itself
86impl<T> File<T>
87where
88 T: crate::FileData,
89{
90 /// The pack data version of this file
91 pub fn version(&self) -> Version {
92 self.version
93 }
94 /// The number of objects stored in this pack data file
95 pub fn num_objects(&self) -> u32 {
96 self.num_objects
97 }
98 /// The length of all mapped data, including the pack header and the pack trailer
99 pub fn data_len(&self) -> usize {
100 self.data.len()
101 }
102 /// The kind of hash we use internally.
103 pub fn object_hash(&self) -> gix_hash::Kind {
104 self.object_hash
105 }
106 /// The maximum size of a single allocation caused by user-controlled on-disk pack data.
107 ///
108 /// A value of `None` means no additional limit is enforced.
109 pub fn alloc_limit_bytes(&self) -> Option<usize> {
110 self.alloc_limit_bytes
111 }
112 /// The position of the byte one past the last pack entry, or in other terms, the first byte of the trailing hash.
113 pub fn pack_end(&self) -> usize {
114 self.data.len() - self.hash_len
115 }
116
117 /// The path to the pack data file on disk
118 pub fn path(&self) -> &Path {
119 &self.path
120 }
121
122 /// Returns the pack data at the given slice if its range is contained in the mapped pack data
123 pub fn entry_slice(&self, slice: EntryRange) -> Option<&[u8]> {
124 let entry_end: usize = slice.end.try_into().expect("end of pack fits into usize");
125 let entry_start = slice.start as usize;
126 self.data.get(entry_start..entry_end)
127 }
128
129 /// Returns the CRC32 of the pack data indicated by `pack_offset` and the `size` of the mapped data.
130 ///
131 /// _Note:_ finding the right size is only possible by decompressing
132 /// the pack entry beforehand, or by using the (to be sorted) offsets stored in an index file.
133 ///
134 /// # Panics
135 ///
136 /// If `pack_offset` or `size` are pointing to a range outside of the mapped pack data.
137 pub fn entry_crc32(&self, pack_offset: Offset, size: usize) -> u32 {
138 let pack_offset: usize = pack_offset.try_into().expect("pack_size fits into usize");
139 gix_features::hash::crc32(&self.data[pack_offset..pack_offset + size])
140 }
141}
142
143///
144pub mod delta;