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