1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
//! a pack data file
use std::{convert::TryInto, path::Path};
use filebuffer::FileBuffer;
use git_hash::SIZE_OF_SHA1_DIGEST as SHA1_SIZE;
/// An representing an full- or delta-object within a pack
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Entry {
/// The entry's header
pub header: entry::Header,
/// The decompressed size of the object in bytes
pub decompressed_size: u64,
/// absolute offset to compressed object data in the pack, just behind the entry's header
pub data_offset: u64,
}
/// A borrowed object using a borrowed slice as backing buffer.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct Object<'a> {
/// kind of object
pub kind: git_object::Kind,
/// decoded, decompressed data, owned by a backing store.
pub data: &'a [u8],
/// If `Some`, this object is from a pack whose pack location can be used to look up pack related information
pub pack_location: Option<crate::bundle::Location>,
}
mod file;
pub use file::{decode_entry, verify, ResolvedBase};
///
pub mod header;
///
pub mod entry;
pub mod object;
///
pub mod input;
/// Utilities to encode pack data entries and write them to a `Write` implementation to resemble a pack data file.
pub mod output;
/// A slice into a pack file denoting a pack entry.
///
/// An entry can be decoded into an object.
pub type EntryRange = std::ops::Range<u64>;
/// Supported versions of a pack data file
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum Version {
V2,
V3,
}
impl Default for Version {
fn default() -> Self {
Version::V2
}
}
/// A pack data file
pub struct File {
data: FileBuffer,
path: std::path::PathBuf,
/// A hash to represent the `path` field when used with cache lookup, or a way to identify this pack by its location on disk.
///
/// Note that `path` might not be canonicalized, thus different hashes might actually refer to the same pack on disk. This will
/// only lead to less efficient cache usage.
/// TODO: remove this intrinsic id as it will henceforth be handled separately by the object store
pub id: u32,
version: Version,
num_objects: u32,
}
/// Information about the pack data file itself
impl File {
/// The pack data version of this file
pub fn version(&self) -> Version {
self.version
}
/// The number of objects stored in this pack data file
pub fn num_objects(&self) -> u32 {
self.num_objects
}
/// The length of all mapped data, including the pack header and the pack trailer
pub fn data_len(&self) -> usize {
self.data.len()
}
/// The position of the byte one past the last pack entry, or in other terms, the first byte of the trailing hash.
pub fn pack_end(&self) -> usize {
self.data.len() - SHA1_SIZE
}
/// The path to the pack data file on disk
pub fn path(&self) -> &Path {
&self.path
}
/// Returns the pack data at the given slice if its range is contained in the mapped pack data
pub fn entry_slice(&self, slice: EntryRange) -> Option<&[u8]> {
let entry_end: usize = slice.end.try_into().expect("end of pack fits into usize");
let entry_start = slice.start as usize;
self.data.get(entry_start..entry_end)
}
/// Returns the CRC32 of the pack data indicated by `pack_offset` and the `size` of the mapped data.
///
/// _Note:_ finding the right size is only possible by decompressing
/// the pack entry beforehand, or by using the (to be sorted) offsets stored in an index file.
///
/// # Panics
///
/// If `pack_offset` or `size` are pointing to a range outside of the mapped pack data.
pub fn entry_crc32(&self, pack_offset: u64, size: usize) -> u32 {
let pack_offset: usize = pack_offset.try_into().expect("pack_size fits into usize");
git_features::hash::crc32(&self.data[pack_offset..pack_offset + size])
}
}
pub(crate) mod delta;