Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 13, 2020
1 parent 0817b24 commit 2888f1b
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 94 deletions.
4 changes: 2 additions & 2 deletions demos/git-count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn run() -> Result<()> {
}
};
let index = odb::pack::index::File::at(index)?;
let pack = odb::pack::File::at(pack)?;
use odb::pack::decoded::Header::*;
let pack = odb::pack::data::File::at(pack)?;
use odb::pack::data::decoded::Header::*;

writeln!(
stdout(),
Expand Down
6 changes: 3 additions & 3 deletions git-odb/src/loose/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ quick_error! {
display("{}", msg)
}
Io(err: std::io::Error, action: &'static str, path: PathBuf) {
display("Could not {} file at '{}'", action, path.display())
display("Could not {} data at '{}'", action, path.display())
cause(err)
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ impl Db {
object::Kind::Tag | object::Kind::Commit | object::Kind::Tree => {
let mut compressed = SmallVec::from_buf(compressed);
// Read small objects right away and store them in memory while we
// have a file handle available and 'hot'. Note that we don't decompress yet!
// have a data handle available and 'hot'. Note that we don't decompress yet!
let file_size = input_stream
.metadata()
.map_err(|e| Error::Io(e, "read metadata", path.to_owned()))?
Expand All @@ -172,7 +172,7 @@ impl Db {
(compressed, None)
}
}
object::Kind::Blob => (SmallVec::default(), Some(path)), // we will open the file again when needed. Maybe we can load small sized objects anyway
object::Kind::Blob => (SmallVec::default(), Some(path)), // we will open the data again when needed. Maybe we can load small sized objects anyway
}
};

Expand Down
2 changes: 1 addition & 1 deletion git-odb/src/loose/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ quick_error! {
cause(err)
}
Io(err: std::io::Error, action: &'static str, path: PathBuf) {
display("Could not {} file at '{}'", action, path.display())
display("Could not {} data at '{}'", action, path.display())
cause(err)
}
}
Expand Down
16 changes: 8 additions & 8 deletions git-odb/src/pack/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ quick_error! {
#[derive(Debug)]
pub enum Error {
InvalidPath(path: PathBuf) {
display("An 'idx' extension is expected of an index file: '{}'", path.display())
display("An 'idx' extension is expected of an index data: '{}'", path.display())
}
Pack(err: pack::Error) {
Pack(err: pack::data::Error) {
display("Could not instantiate pack")
from()
cause(err)
Expand All @@ -22,20 +22,20 @@ quick_error! {
from()
cause(err)
}
Decode(err: pack::decode::Error) {
Decode(err: pack::data::decode::Error) {
display("Could not decode object")
}
}
}

/// A packfile with an index
pub struct Bundle {
pack: pack::File,
pack: pack::data::File,
index: pack::index::File,
}

impl Bundle {
/// `path` is either a pack file or an index file
/// `path` is either a pack data or an index data
pub fn at(path: impl AsRef<Path>) -> Result<Self, Error> {
Self::try_from(path.as_ref())
}
Expand All @@ -60,7 +60,7 @@ impl Bundle {
out,
|id, _out| {
self.index.lookup_index(id).map(|idx| {
pack::decode::ResolvedBase::InPack(self.pack.entry(self.index.pack_offset_at_index(idx)))
pack::data::decode::ResolvedBase::InPack(self.pack.entry(self.index.pack_offset_at_index(idx)))
})
},
cache,
Expand All @@ -85,10 +85,10 @@ impl TryFrom<&Path> for Bundle {
Ok(match ext {
"idx" => Self {
index: pack::index::File::at(path)?,
pack: pack::File::at(path.with_extension("pack"))?,
pack: pack::data::File::at(path.with_extension("pack"))?,
},
"pack" => Self {
pack: pack::File::at(path)?,
pack: pack::data::File::at(path)?,
index: pack::index::File::at(path.with_extension("idx"))?,
},
_ => return Err(Error::InvalidPath(path.to_owned())),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
pack::cache,
pack::{decoded, File},
pack::data::{decoded, File},
zlib::Inflate,
};
use git_object as object;
Expand Down Expand Up @@ -85,15 +85,15 @@ impl File {
}

/// Currently only done during pack verification - 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.
/// the pack entry beforehand, or by using the (to be sorted) offsets stored in an index 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])
}

fn assure_v2(&self) {
assert!(
if let crate::pack::Kind::V2 = self.kind.clone() {
if let crate::pack::data::Kind::V2 = self.kind.clone() {
true
} else {
false
Expand Down Expand Up @@ -142,7 +142,7 @@ impl File {
resolve: impl Fn(&object::Id, &mut Vec<u8>) -> Option<ResolvedBase>,
cache: &mut impl cache::DecodeEntry,
) -> Result<DecodeEntryOutcome, Error> {
use crate::pack::decoded::Header::*;
use crate::pack::data::decoded::Header::*;
match entry.header {
Tree | Blob | Commit | Tag => {
out.resize(
Expand Down Expand Up @@ -174,7 +174,7 @@ impl File {
out: &mut Vec<u8>,
cache: &mut impl cache::DecodeEntry,
) -> Result<DecodeEntryOutcome, Error> {
use crate::pack::decoded::Header;
use crate::pack::data::decoded::Header;
// all deltas, from the one that produces the desired object (first) to the oldest at the end of the chain
let mut chain = SmallVec::<[Delta; 10]>::default();
let first_entry = last.clone();
Expand Down
File renamed without changes.
55 changes: 12 additions & 43 deletions git-odb/src/pack/file/mod.rs → git-odb/src/pack/data/file.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
use crate::pack::data;
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};

pub mod decode;

pub mod decoded;

pub mod verify;

quick_error! {
#[derive(Debug)]
pub enum Error {
Io(err: std::io::Error, path: std::path::PathBuf) {
display("Could not open pack file at '{}'", path.display())
display("Could not open pack data at '{}'", path.display())
cause(err)
}
Corrupt(msg: String) {
Expand All @@ -28,65 +23,39 @@ quick_error! {

const N32_SIZE: usize = size_of::<u32>();

#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub enum Kind {
V2,
V3,
}

pub struct File {
data: FileBuffer,
path: std::path::PathBuf,
kind: Kind,
num_objects: u32,
}

/// Instantiation and basic file information
impl File {
pub fn kind(&self) -> Kind {
self.kind.clone()
}
pub fn num_objects(&self) -> u32 {
self.num_objects
}
pub fn data_len(&self) -> usize {
self.data.len()
}
pub fn path(&self) -> &Path {
&self.path
}

pub fn at(path: impl AsRef<Path>) -> Result<File, Error> {
File::try_from(path.as_ref())
/// Instantiation
impl data::File {
pub fn at(path: impl AsRef<Path>) -> Result<data::File, Error> {
data::File::try_from(path.as_ref())
}
}

impl TryFrom<&Path> for File {
impl TryFrom<&Path> for data::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 pack_len = data.len();
if pack_len < N32_SIZE * 3 + SHA1_SIZE {
return Err(Error::Corrupt(format!(
"Pack file of size {} is too small for even an empty pack",
"Pack data of size {} is too small for even an empty pack",
pack_len
)));
}
let mut ofs = 0;
if &data[ofs..ofs + b"PACK".len()] != b"PACK" {
return Err(Error::Corrupt("Pack file type not recognized".into()));
return Err(Error::Corrupt("Pack data type not recognized".into()));
}
ofs += N32_SIZE;
let kind = match BigEndian::read_u32(&data[ofs..ofs + N32_SIZE]) {
2 => Kind::V2,
3 => Kind::V3,
2 => data::Kind::V2,
3 => data::Kind::V3,
v => return Err(Error::UnsupportedVersion(v)),
};
ofs += N32_SIZE;
let num_objects = BigEndian::read_u32(&data[ofs..ofs + N32_SIZE]);

Ok(File {
Ok(data::File {
data,
path: path.to_owned(),
kind,
Expand Down
37 changes: 37 additions & 0 deletions git-odb/src/pack/data/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use filebuffer::FileBuffer;
use std::path::Path;

pub mod decode;
pub mod decoded;
pub mod verify;

mod file;
pub use file::*;

#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub enum Kind {
V2,
V3,
}

pub struct File {
data: FileBuffer,
path: std::path::PathBuf,
kind: Kind,
num_objects: u32,
}

impl File {
pub fn kind(&self) -> Kind {
self.kind.clone()
}
pub fn num_objects(&self) -> u32 {
self.num_objects
}
pub fn data_len(&self) -> usize {
self.data.len()
}
pub fn path(&self) -> &Path {
&self.path
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::pack::File;
use crate::pack::data::File;
use git_object::{self as object, SHA1_SIZE};
use quick_error::quick_error;

Expand All @@ -9,7 +9,7 @@ quick_error! {
display("pack checksum mismatch: expected {}, got {}", expected, actual)
}
Io(err: std::io::Error) {
display("could not read pack file")
display("could not read pack data")
from()
cause(err)
}
Expand Down
2 changes: 1 addition & 1 deletion git-odb/src/pack/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ 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())
display("Could not open pack index data at '{}'", path.display())
cause(err)
}
Corrupt(msg: String) {
Expand Down
20 changes: 10 additions & 10 deletions git-odb/src/pack/index/verify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
pack,
pack::index,
pack::{cache, decode::DecodeEntryOutcome},
pack::{cache, data::decode::DecodeEntryOutcome},
};
use git_features::progress::{self, Progress};
use git_object::SHA1_SIZE;
Expand All @@ -15,23 +15,23 @@ quick_error! {
Mismatch { expected: git_object::Id, actual: git_object::Id } {
display("index checksum mismatch: expected {}, got {}", expected, actual)
}
PackChecksum(err: pack::verify::Error) {
display("The pack of this index file failed to verify its checksums")
PackChecksum(err: pack::data::verify::Error) {
display("The pack of this index data failed to verify its checksums")
from()
cause(err)
}
PackDecode(err: pack::decode::Error, id: git_object::Id, offset: u64) {
PackDecode(err: pack::data::decode::Error, id: git_object::Id, offset: u64) {
display("Object {} at offset {} could not be decoded", id, offset)
cause(err)
}
PackMismatch { expected: git_object::Id, actual: git_object::Id } {
display("The packfiles checksum didn't match the index file checksum: expected {}, got {}", expected, actual)
display("The packfiles checksum didn't match the index data checksum: expected {}, got {}", expected, actual)
}
PackObjectMismatch { expected: git_object::Id, actual: git_object::Id, offset: u64, kind: git_object::Kind} {
display("The SHA1 of {} object at offset {} didn't match the checksum in the index file: expected {}, got {}", kind, offset, expected, actual)
display("The SHA1 of {} object at offset {} didn't match the checksum in the index data: expected {}, got {}", kind, offset, expected, actual)
}
Crc32Mismatch { expected: u32, actual: u32, offset: u64, kind: git_object::Kind} {
display("The CRC32 of {} object at offset {} didn't match the checksum in the index file: expected {}, got {}", kind, offset, expected, actual)
display("The CRC32 of {} object at offset {} didn't match the checksum in the index data: expected {}, got {}", kind, offset, expected, actual)
}
}
}
Expand Down Expand Up @@ -76,7 +76,7 @@ pub struct Outcome {
pub pack_size: u64,
}

/// Verify and validate the content of the index file
/// Verify and validate the content of the index data
impl index::File {
pub fn checksum_of_index(&self) -> git_object::Id {
git_object::Id::from_20_bytes(&self.data[self.data.len() - SHA1_SIZE..])
Expand All @@ -92,7 +92,7 @@ impl index::File {
/// is indeed as advertised via its SHA1 as stored in this index, as well as the CRC32 hash.
pub fn verify_checksum_of_index<P, C>(
&self,
pack: Option<&pack::File>,
pack: Option<&pack::data::File>,
thread_limit: Option<usize>,
progress: Option<P>,
make_cache: impl Fn() -> C + Send + Sync,
Expand All @@ -102,7 +102,7 @@ impl index::File {
<P as Progress>::SubProgress: Send,
C: cache::DecodeEntry,
{
use crate::pack::decode::ResolvedBase;
use crate::pack::data::decode::ResolvedBase;
use git_features::parallel::{self, in_parallel_if};

let mut root = progress::DoOrDiscard::from(progress);
Expand Down
3 changes: 1 addition & 2 deletions git-odb/src/pack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ pub mod index;

pub mod cache;

mod file;
pub use self::file::*;
pub mod data;

mod bundle;
pub use bundle::{Bundle, Object};
Loading

0 comments on commit 2888f1b

Please sign in to comment.