Function git_pack::multi_index::chunk::fanout::from_bytes
source · Expand description
Decode the fanout table contained in chunk
, or return None
if it didn’t have the expected size.
Examples found in repository?
src/multi_index/init.rs (line 102)
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
fn try_from(path: &Path) -> Result<Self, Self::Error> {
let data = crate::mmap::read_only(path).map_err(|source| Error::Io {
source,
path: path.to_owned(),
})?;
const TRAILER_LEN: usize = git_hash::Kind::shortest().len_in_bytes(); /* trailing hash */
if data.len()
< Self::HEADER_LEN
+ git_chunk::file::Index::size_for_entries(4 /*index names, fan, offsets, oids*/)
+ chunk::fanout::SIZE
+ TRAILER_LEN
{
return Err(Error::Corrupt {
message: "multi-index file is truncated and too short",
});
}
let (version, object_hash, num_chunks, num_indices) = {
let (signature, data) = data.split_at(4);
if signature != Self::SIGNATURE {
return Err(Error::Corrupt {
message: "Invalid signature",
});
}
let (version, data) = data.split_at(1);
let version = match version[0] {
1 => Version::V1,
version => return Err(Error::UnsupportedVersion { version }),
};
let (object_hash, data) = data.split_at(1);
let object_hash = git_hash::Kind::try_from(object_hash[0])
.map_err(|unknown| Error::UnsupportedObjectHash { kind: unknown })?;
let (num_chunks, data) = data.split_at(1);
let num_chunks = num_chunks[0];
let (_num_base_files, data) = data.split_at(1); // TODO: handle base files once it's clear what this does
let (num_indices, _) = data.split_at(4);
let num_indices = crate::read_u32(num_indices);
(version, object_hash, num_chunks, num_indices)
};
let chunks = git_chunk::file::Index::from_bytes(&data, Self::HEADER_LEN, num_chunks as u32)?;
let index_names = chunks.data_by_id(&data, chunk::index_names::ID)?;
let index_names = chunk::index_names::from_bytes(index_names, num_indices)?;
let fan = chunks.data_by_id(&data, chunk::fanout::ID)?;
let fan = chunk::fanout::from_bytes(fan).ok_or(Error::MultiPackFanSize)?;
let num_objects = fan[255];
let lookup = chunks.validated_usize_offset_by_id(chunk::lookup::ID, |offset| {
chunk::lookup::is_valid(&offset, object_hash, num_objects)
.then(|| offset)
.ok_or(Error::InvalidChunkSize {
id: chunk::lookup::ID,
message: "The chunk with alphabetically ordered object ids doesn't have the correct size",
})
})??;
let offsets = chunks.validated_usize_offset_by_id(chunk::offsets::ID, |offset| {
chunk::offsets::is_valid(&offset, num_objects)
.then(|| offset)
.ok_or(Error::InvalidChunkSize {
id: chunk::offsets::ID,
message: "The chunk with offsets into the pack doesn't have the correct size",
})
})??;
let large_offsets = chunks
.validated_usize_offset_by_id(chunk::large_offsets::ID, |offset| {
chunk::large_offsets::is_valid(&offset)
.then(|| offset)
.ok_or(Error::InvalidChunkSize {
id: chunk::large_offsets::ID,
message: "The chunk with large offsets into the pack doesn't have the correct size",
})
})
.ok()
.transpose()?;
let checksum_offset = chunks.highest_offset() as usize;
let trailer = &data[checksum_offset..];
if trailer.len() != object_hash.len_in_bytes() {
return Err(Error::Corrupt {
message:
"Trailing checksum didn't have the expected size or there were unknown bytes after the checksum.",
});
}
Ok(File {
data,
path: path.to_owned(),
version,
hash_len: object_hash.len_in_bytes(),
object_hash,
fan,
index_names,
lookup_ofs: lookup.start,
offsets_ofs: offsets.start,
large_offsets_ofs: large_offsets.map(|r| r.start),
num_objects,
num_indices,
})
}