gix_pack/data/file/init.rs
1use gix_error::Result;
2use std::path::{Path, PathBuf};
3
4use gix_error::{ResultExt, bail, message};
5
6use crate::data;
7
8/// Instantiation
9impl data::File<crate::MMap> {
10 /// Try opening a data file at the given `path`.
11 ///
12 /// The `object_hash` is a way to read (and write) the same file format with different hashes, as the hash kind
13 /// isn't stored within the file format itself.
14 ///
15 /// This constructor leaves allocation limiting disabled, allowing allocations of any size dictated by pack data.
16 /// Use [`Self::from_data()`] together with [`File::with_alloc_limit_bytes()`][crate::data::File::with_alloc_limit_bytes()]
17 /// when working with untrusted input.
18 pub fn at(path: impl AsRef<Path>, object_hash: gix_hash::Kind) -> Result<Self> {
19 Self::at_inner(path.as_ref(), object_hash)
20 }
21
22 fn at_inner(path: &Path, object_hash: gix_hash::Kind) -> Result<Self> {
23 let data = crate::mmap::read_only(path)
24 .or_raise(|| message!("Could not open pack data file at \"{}\"", path.display()))?;
25 Self::from_data(data, path.to_owned(), object_hash)
26 }
27}
28
29impl<T> data::File<T>
30where
31 T: crate::FileData,
32{
33 /// Instantiate a data file from `data` as assumed to be read or memory-mapped from `path`.
34 ///
35 /// This constructor leaves allocation limiting disabled, allowing allocations of any size dictated by pack data.
36 /// Call [`File::with_alloc_limit_bytes()`][crate::data::File::with_alloc_limit_bytes()] before decoding entries from untrusted input.
37 pub fn from_data(data: T, path: PathBuf, object_hash: gix_hash::Kind) -> Result<Self> {
38 let hash_len = object_hash.len_in_bytes();
39 let pack_len = data.len();
40 let id = crc32fast::hash(path.as_os_str().to_string_lossy().as_bytes());
41 if pack_len < data::header::SIZE + hash_len {
42 bail!(gix_error::corruption(format!(
43 "Pack data of size {pack_len} is too small for even an empty pack with shortest hash"
44 )));
45 }
46 let (kind, num_objects) = data::header::decode(
47 &data[..data::header::SIZE]
48 .try_into()
49 .expect("enough data after previous check"),
50 )?;
51 Ok(Self {
52 data,
53 path,
54 id,
55 version: kind,
56 num_objects,
57 object_hash,
58 alloc_limit_bytes: None,
59 })
60 }
61
62 /// Configure the maximum size of a single allocation caused by user-controlled on-disk pack data.
63 ///
64 /// Use `None` to disable the limit, which is also the default.
65 ///
66 /// This is currently enforced when decoding pack entries and resolving delta chains.
67 /// The limit applies to the combined size of buffers that hold both work data and delta instructions.
68 /// Buffer growth uses exact reservations when a limit is set, and retains existing capacity.
69 /// Callers that allocate from pack metadata directly should consult [`File::alloc_limit_bytes()`][crate::data::File::alloc_limit_bytes]
70 /// and apply the same limit themselves.
71 pub fn with_alloc_limit_bytes(mut self, alloc_limit_bytes: Option<usize>) -> Self {
72 self.alloc_limit_bytes = alloc_limit_bytes;
73 self
74 }
75}