diff --git a/src/backend/choose.rs b/src/backend/choose.rs index d0548aa..43abb82 100644 --- a/src/backend/choose.rs +++ b/src/backend/choose.rs @@ -53,12 +53,13 @@ impl ReadBackend for ChooseBackend { &self, tpe: FileType, id: &Id, + cacheable: bool, offset: u32, length: u32, ) -> Result> { match self { - Local(local) => local.read_partial(tpe, id, offset, length).await, - Rest(rest) => rest.read_partial(tpe, id, offset, length).await, + Local(local) => local.read_partial(tpe, id, cacheable, offset, length).await, + Rest(rest) => rest.read_partial(tpe, id, cacheable, offset, length).await, } } } @@ -72,10 +73,10 @@ impl WriteBackend for ChooseBackend { } } - async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()> { + async fn write_file(&self, tpe: FileType, id: &Id, cacheable: bool, f: File) -> Result<()> { match self { - Local(local) => local.write_file(tpe, id, f).await, - Rest(rest) => rest.write_file(tpe, id, f).await, + Local(local) => local.write_file(tpe, id, cacheable, f).await, + Rest(rest) => rest.write_file(tpe, id, cacheable, f).await, } } diff --git a/src/backend/decrypt.rs b/src/backend/decrypt.rs index db7ce67..b402b5c 100644 --- a/src/backend/decrypt.rs +++ b/src/backend/decrypt.rs @@ -20,6 +20,7 @@ pub trait DecryptReadBackend: ReadBackend { &self, tpe: FileType, id: &Id, + cacheable: bool, offset: u32, length: u32, ) -> Result>; @@ -169,12 +170,16 @@ impl DecryptReadBackend for DecryptBackend { &self, tpe: FileType, id: &Id, + cacheable: bool, offset: u32, length: u32, ) -> Result> { - Ok(self - .key - .decrypt_data(&self.backend.read_partial(tpe, id, offset, length).await?)?) + Ok(self.key.decrypt_data( + &self + .backend + .read_partial(tpe, id, cacheable, offset, length) + .await?, + )?) } } @@ -200,10 +205,13 @@ impl ReadBackend for DecryptBackend { &self, tpe: FileType, id: &Id, + cacheable: bool, offset: u32, length: u32, ) -> Result> { - self.backend.read_partial(tpe, id, offset, length).await + self.backend + .read_partial(tpe, id, cacheable, offset, length) + .await } } @@ -213,8 +221,8 @@ impl WriteBackend for DecryptBackend { self.backend.create().await } - async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()> { - self.backend.write_file(tpe, id, f).await + async fn write_file(&self, tpe: FileType, id: &Id, cacheable: bool, f: File) -> Result<()> { + self.backend.write_file(tpe, id, cacheable, f).await } async fn write_bytes(&self, tpe: FileType, id: &Id, buf: Vec) -> Result<()> { diff --git a/src/backend/dry_run.rs b/src/backend/dry_run.rs index 023aa1e..0a63f9c 100644 --- a/src/backend/dry_run.rs +++ b/src/backend/dry_run.rs @@ -29,11 +29,12 @@ impl DecryptReadBackend for DryRunBackend { &self, tpe: FileType, id: &Id, + cacheable: bool, offset: u32, length: u32, ) -> Result> { self.be - .read_encrypted_partial(tpe, id, offset, length) + .read_encrypted_partial(tpe, id, cacheable, offset, length) .await } } @@ -56,10 +57,13 @@ impl ReadBackend for DryRunBackend { &self, tpe: FileType, id: &Id, + cacheable: bool, offset: u32, length: u32, ) -> Result> { - self.be.read_partial(tpe, id, offset, length).await + self.be + .read_partial(tpe, id, cacheable, offset, length) + .await } } @@ -95,10 +99,10 @@ impl WriteBackend for DryRunBackend { } } - async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()> { + async fn write_file(&self, tpe: FileType, id: &Id, cacheable: bool, f: File) -> Result<()> { match self.dry_run { true => Ok(()), - false => self.be.write_file(tpe, id, f).await, + false => self.be.write_file(tpe, id, cacheable, f).await, } } diff --git a/src/backend/local.rs b/src/backend/local.rs index c7dce91..56954e6 100644 --- a/src/backend/local.rs +++ b/src/backend/local.rs @@ -101,6 +101,7 @@ impl ReadBackend for LocalBackend { &self, tpe: FileType, id: &Id, + _cacheable: bool, offset: u32, length: u32, ) -> Result> { @@ -124,7 +125,13 @@ impl WriteBackend for LocalBackend { Ok(()) } - async fn write_file(&self, tpe: FileType, id: &Id, mut f: File) -> Result<()> { + async fn write_file( + &self, + tpe: FileType, + id: &Id, + _cacheable: bool, + mut f: File, + ) -> Result<()> { v3!("writing tpe: {:?}, id: {}", &tpe, &id); let filename = self.path(tpe, id); let mut file = fs::OpenOptions::new() diff --git a/src/backend/mod.rs b/src/backend/mod.rs index d10fa85..3cad358 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -76,6 +76,7 @@ pub trait ReadBackend: Clone + Send + Sync + 'static { &self, tpe: FileType, id: &Id, + cacheable: bool, offset: u32, length: u32, ) -> Result>; @@ -133,7 +134,7 @@ pub trait ReadBackend: Clone + Send + Sync + 'static { #[async_trait] pub trait WriteBackend: ReadBackend { async fn create(&self) -> Result<()>; - async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()>; + async fn write_file(&self, tpe: FileType, id: &Id, cacheable: bool, f: File) -> Result<()>; async fn write_bytes(&self, tpe: FileType, id: &Id, buf: Vec) -> Result<()>; async fn remove(&self, tpe: FileType, id: &Id) -> Result<()>; } diff --git a/src/backend/rest.rs b/src/backend/rest.rs index 01c1575..f2d3268 100644 --- a/src/backend/rest.rs +++ b/src/backend/rest.rs @@ -107,6 +107,7 @@ impl ReadBackend for RestBackend { &self, tpe: FileType, id: &Id, + _cacheable: bool, offset: u32, length: u32, ) -> Result> { @@ -135,7 +136,7 @@ impl WriteBackend for RestBackend { Ok(()) } - async fn write_file(&self, tpe: FileType, id: &Id, f: File) -> Result<()> { + async fn write_file(&self, tpe: FileType, id: &Id, _cacheable: bool, f: File) -> Result<()> { v3!("writing tpe: {:?}, id: {}", &tpe, &id); self.client .post(self.url(tpe, id)) diff --git a/src/blob/packer.rs b/src/blob/packer.rs index d7e0454..90850a6 100644 --- a/src/blob/packer.rs +++ b/src/blob/packer.rs @@ -48,6 +48,7 @@ impl Packer { future: None, be: be.clone(), indexer: indexer.clone(), + cacheable: blob_type.is_cacheable(), }; Ok(Self { be, @@ -234,6 +235,7 @@ struct FileWriter { future: Option>>, be: BE, indexer: SharedIndexer, + cacheable: bool, } impl FileWriter { @@ -244,8 +246,9 @@ impl FileWriter { let be = self.be.clone(); let indexer = self.indexer.clone(); + let cacheable = self.cacheable; self.future = Some(spawn(async move { - be.write_file(FileType::Pack, &id, file).await?; + be.write_file(FileType::Pack, &id, cacheable, file).await?; index.time = Some(Local::now()); indexer.write().await.add(index).await?; Ok(()) diff --git a/src/commands/prune.rs b/src/commands/prune.rs index 381f661..8b6f6f1 100644 --- a/src/commands/prune.rs +++ b/src/commands/prune.rs @@ -730,7 +730,13 @@ impl Pruner { continue; } let data = be - .read_partial(FileType::Pack, &pack.id, blob.offset, blob.length) + .read_partial( + FileType::Pack, + &pack.id, + blob.tpe.is_cacheable(), + blob.offset, + blob.length, + ) .await?; match blob.tpe { BlobType::Data => &mut data_packer, diff --git a/src/commands/restore.rs b/src/commands/restore.rs index f712a38..ea3da4e 100644 --- a/src/commands/restore.rs +++ b/src/commands/restore.rs @@ -114,7 +114,7 @@ async fn restore_contents( stream.push(spawn(async move { // read pack at blob_offset with length blob_length let data = be - .read_encrypted_partial(FileType::Pack, &pack, bl.offset, bl.length) + .read_encrypted_partial(FileType::Pack, &pack, false, bl.offset, bl.length) .await .unwrap(); diff --git a/src/index/binarysorted.rs b/src/index/binarysorted.rs index f331b74..92f9958 100644 --- a/src/index/binarysorted.rs +++ b/src/index/binarysorted.rs @@ -114,6 +114,7 @@ impl ReadIndex for Index { vec.binary_search_by_key(id, |e| e.id).ok().map(|index| { let be = &vec[index]; IndexEntry::new( + *tpe, self.packs[be.pack_idx], be.offset, be.length, diff --git a/src/index/mod.rs b/src/index/mod.rs index a8bfd09..d6c5069 100644 --- a/src/index/mod.rs +++ b/src/index/mod.rs @@ -24,6 +24,7 @@ pub use indexer::*; #[derive(Debug, Clone, Constructor, Getters)] pub struct IndexEntry { + blob_type: BlobType, pack: Id, offset: u32, length: u32, @@ -33,6 +34,7 @@ pub struct IndexEntry { impl IndexEntry { pub fn from_index_blob(blob: &IndexBlob, pack: Id) -> Self { Self { + blob_type: blob.tpe, pack, offset: blob.offset, length: blob.length, @@ -43,7 +45,13 @@ impl IndexEntry { /// Get a blob described by IndexEntry from the backend pub async fn read_data(&self, be: &B) -> Result> { let data = be - .read_encrypted_partial(FileType::Pack, &self.pack, self.offset, self.length) + .read_encrypted_partial( + FileType::Pack, + &self.pack, + self.blob_type.is_cacheable(), + self.offset, + self.length, + ) .await?; Ok(match self.uncompressed_length { None => data,