diff --git a/Cargo.toml b/Cargo.toml index 3be22ff..e3d9556 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] anyhow = "1.0" thiserror = "1.0" +derive-new = "0.5" aes256ctr_poly1305aes = { path = "../aes256ctr_poly1305aes" } base64 = "0.13" scrypt = { version = "0.8", default-features = false } @@ -21,3 +22,4 @@ boomphf = "0.5" walkdir = "2" # commands clap = { version = "3", features = ["derive"] } + diff --git a/src/blob/mod.rs b/src/blob/mod.rs index d88ac35..cf6d99e 100644 --- a/src/blob/mod.rs +++ b/src/blob/mod.rs @@ -1,5 +1,8 @@ +use anyhow::Result; +use derive_new::new; use serde::{Deserialize, Serialize}; +use crate::backend::{FileType, ReadBackend}; use crate::id::Id; #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] @@ -10,34 +13,52 @@ pub enum BlobType { Tree, } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, new)] pub struct Blob { - pub tpe: BlobType, - pub id: Id, + tpe: BlobType, + id: Id, } -#[derive(Debug)] +#[derive(Debug, new)] pub struct BlobInformation { - pub blob: Blob, - pub offset: u32, - pub length: u32, + blob: Blob, + offset: u32, + length: u32, } -#[derive(Debug)] +#[derive(Debug, new)] pub struct IndexEntry { - pub pack: Id, - pub bi: BlobInformation, + pack: Id, + bi: BlobInformation, } impl IndexEntry { + /// Get a blob described by IndexEntry from the backend + pub fn read_data(&self, be: &B) -> Result> { + Ok(be.read_partial(FileType::Pack, self.pack, self.offset(), self.length())?) + } + + #[inline] pub fn id(&self) -> &Id { &self.bi.blob.id } + #[inline] pub fn tpe(&self) -> &BlobType { &self.bi.blob.tpe } + #[inline] + pub fn offset(&self) -> u32 { + self.bi.offset + } + + #[inline] + pub fn length(&self) -> u32 { + self.bi.length + } + + #[inline] pub fn blob(&self) -> &Blob { &self.bi.blob } diff --git a/src/commands/cat.rs b/src/commands/cat.rs index 162e767..b188f51 100644 --- a/src/commands/cat.rs +++ b/src/commands/cat.rs @@ -20,10 +20,10 @@ pub(super) fn execute(be: &impl ReadBackend, dbe: &impl ReadBackend, opts: Opts) let tpe = match opts.tpe.as_str() { // special treatment for catingg blobs: read the index and use it to locate the blob "blob" => { - let blob = AllIndexFiles::new(be) - .get_id(id) - .ok_or(anyhow!("blob not found in index"))?; - let dec = be.read_partial(FileType::Pack, blob.pack, blob.bi.offset, blob.bi.length)?; + let dec = AllIndexFiles::new(be) + .get_id(&id) + .ok_or(anyhow!("blob not found in index"))? + .read_data(be)?; print!("{}", String::from_utf8_lossy(&dec)); return Ok(()); } diff --git a/src/index/mod.rs b/src/index/mod.rs index bed6ce0..679e578 100644 --- a/src/index/mod.rs +++ b/src/index/mod.rs @@ -6,11 +6,11 @@ use crate::id::Id; pub trait ReadIndex { fn iter(&self) -> Box + '_>; - fn get_id(&self, id: Id) -> Option { - self.iter().find(|e| e.bi.blob.id == id) + fn get_id(&self, id: &Id) -> Option { + self.iter().find(|e| e.id() == id) } - fn get_blob(&self, blob: Blob) -> Option { - self.iter().find(|e| e.bi.blob == blob) + fn get_blob(&self, blob: &Blob) -> Option { + self.iter().find(|e| e.blob() == blob) } } diff --git a/src/repo/index.rs b/src/repo/index.rs index 693e9d7..0f8f5b1 100644 --- a/src/repo/index.rs +++ b/src/repo/index.rs @@ -28,10 +28,7 @@ impl IntoIterator for IndexFile { fn into_iter(self) -> Self::IntoIter { Box::new(self.packs.into_iter().flat_map(|p| { - p.blobs.into_iter().map(move |b| IndexEntry { - pack: p.id, - bi: b.to_bi(), - }) + p.blobs.into_iter().map(move |b| IndexEntry::new(p.id,b.to_bi())) })) } } @@ -39,10 +36,7 @@ impl IntoIterator for IndexFile { impl ReadIndex for IndexFile { fn iter(&self) -> Box + '_> { Box::new(self.packs.iter().flat_map(|p| { - p.blobs.iter().map(|b| IndexEntry { - pack: p.id, - bi: b.to_bi(), - }) + p.blobs.iter().map(|b| IndexEntry::new(p.id,b.to_bi())) })) } } @@ -64,13 +58,6 @@ struct BlobIndex { impl BlobIndex { pub fn to_bi(&self) -> BlobInformation { - BlobInformation { - blob: Blob { - id: self.id, - tpe: self.tpe, - }, - offset: self.offset, - length: self.length, - } + BlobInformation::new(Blob::new(self.tpe, self.id), self.offset, self.length) } }