90 lines · 2.7 KB · Rust
RawDownload
use crate::pack::index::{self, Kind, FAN_LEN, V2_SIGNATURE};use byteorder::{BigEndian, ByteOrder};use filebuffer::FileBuffer;use git_object::SHA1_SIZE;use quick_error::quick_error;use std::{convert::TryFrom, mem::size_of, path::Path}; quick_error! {    #[derive(Debug)]    pub enum Error {        Io(err: std::io::Error, path: std::path::PathBuf) {            display("Could not open pack index file at '{}'", path.display())            source(err)        }        Corrupt(msg: String) {            display("{}", msg)        }        UnsupportedVersion(version: u32) {            display("Unsupported index version: {}", version)        }    }} const N32_SIZE: usize = size_of::<u32>();const FOOTER_SIZE: usize = SHA1_SIZE * 2; /// Instantiationimpl index::File {    pub fn at(path: impl AsRef<Path>) -> Result<index::File, Error> {        Self::try_from(path.as_ref())    }} impl TryFrom<&Path> for index::File {    type Error = Error;     fn try_from(path: &Path) -> Result<Self, Self::Error> {        let data = FileBuffer::open(&path).map_err(|e| Error::Io(e, path.to_owned()))?;        let idx_len = data.len();        if idx_len < FAN_LEN * N32_SIZE + FOOTER_SIZE {            return Err(Error::Corrupt(format!(                "Pack index of size {} is too small for even an empty index",                idx_len            )));        }        let (kind, version, fan, num_objects) = {            let (kind, d) = {                let (sig, d) = data.split_at(V2_SIGNATURE.len());                if sig == V2_SIGNATURE {                    (Kind::V2, d)                } else {                    (Kind::V1, &data[..])                }            };            let (version, d) = {                let (mut v, mut d) = (1, d);                if let Kind::V2 = kind {                    let (vd, dr) = d.split_at(N32_SIZE);                    d = dr;                    v = BigEndian::read_u32(vd);                    if v != Kind::V2 as u32 {                        return Err(Error::UnsupportedVersion(v));                    }                }                (v, d)            };            let (fan, bytes_read) = read_fan(d);            let (_, _d) = d.split_at(bytes_read);            let num_objects = fan[FAN_LEN - 1];             (kind, version, fan, num_objects)        };        Ok(index::File {            data,            path: path.to_owned(),            kind,            num_objects,            version,            fan,        })    }} fn read_fan(d: &[u8]) -> ([u32; FAN_LEN], usize) {    let mut fan = [0; FAN_LEN];    for (c, f) in d.chunks(N32_SIZE).zip(fan.iter_mut()) {        *f = BigEndian::read_u32(c);    }    (fan, FAN_LEN * N32_SIZE)}