diff --git a/crates/rustic_core/src/commands/cat.rs b/crates/rustic_core/src/commands/cat.rs index e85217f..f558379 100644 --- a/crates/rustic_core/src/commands/cat.rs +++ b/crates/rustic_core/src/commands/cat.rs @@ -3,8 +3,9 @@ use std::path::Path; use bytes::Bytes; use crate::{ - error::CommandErrorKind, BlobType, DecryptReadBackend, FileType, Id, IndexBackend, - IndexedBackend, OpenRepository, ProgressBars, ReadBackend, RusticResult, SnapshotFile, Tree, + error::CommandErrorKind, repository::IndexedRepository, BlobType, DecryptReadBackend, FileType, + Id, IndexedBackend, OpenRepository, ProgressBars, ReadBackend, RusticResult, SnapshotFile, + Tree, }; pub fn cat_file(repo: &OpenRepository, tpe: FileType, id: &str) -> RusticResult { @@ -13,31 +14,30 @@ pub fn cat_file(repo: &OpenRepository, tpe: FileType, id: &str) -> RusticResult< Ok(data) } -pub fn cat_blob( - repo: &OpenRepository, - tpe: BlobType, - id: &str, - pb: &impl ProgressBars, -) -> RusticResult { +pub fn cat_blob(repo: &IndexedRepository, tpe: BlobType, id: &str) -> RusticResult { let id = Id::from_hex(id)?; - let data = IndexBackend::new(&repo.dbe, &pb.progress_hidden())?.blob_from_backend(tpe, &id)?; + let data = repo.index.blob_from_backend(tpe, &id)?; Ok(data) } pub fn cat_tree( - repo: &OpenRepository, + repo: &IndexedRepository, snap: &str, sn_filter: impl FnMut(&SnapshotFile) -> bool + Send + Sync, pb: &impl ProgressBars, ) -> RusticResult { let (id, path) = snap.split_once(':').unwrap_or((snap, "")); - let snap = SnapshotFile::from_str(&repo.dbe, id, sn_filter, &pb.progress_counter(""))?; - let index = IndexBackend::new(&repo.dbe, &pb.progress_counter(""))?; - let node = Tree::node_from_path(&index, snap.tree, Path::new(path))?; + let snap = SnapshotFile::from_str( + &repo.repo.dbe, + id, + sn_filter, + &pb.progress_counter("getting snapshot..."), + )?; + let node = Tree::node_from_path(&repo.index, snap.tree, Path::new(path))?; let id = node .subtree .ok_or_else(|| CommandErrorKind::PathIsNoDir(path.to_string()))?; - let data = index.blob_from_backend(BlobType::Tree, &id)?; + let data = repo.index.blob_from_backend(BlobType::Tree, &id)?; Ok(data) } diff --git a/crates/rustic_core/src/commands/check.rs b/crates/rustic_core/src/commands/check.rs index 2769b1f..1f9d6b7 100644 --- a/crates/rustic_core/src/commands/check.rs +++ b/crates/rustic_core/src/commands/check.rs @@ -28,7 +28,7 @@ pub struct CheckOpts { } impl CheckOpts { - pub fn run(&self, repo: &OpenRepository, pb: &impl ProgressBars) -> RusticResult<()> { + pub fn run(self, repo: &OpenRepository, pb: &impl ProgressBars) -> RusticResult<()> { let be = &repo.dbe; let cache = &repo.cache; let hot_be = &repo.be_hot; diff --git a/crates/rustic_core/src/lib.rs b/crates/rustic_core/src/lib.rs index 6ae6e09..b7d5389 100644 --- a/crates/rustic_core/src/lib.rs +++ b/crates/rustic_core/src/lib.rs @@ -120,10 +120,7 @@ pub use crate::{ BlobLocation, BlobType, BlobTypeMap, Initialize, Sum, }, chunker::random_poly, - commands::{ - cat::{cat_blob, cat_file, cat_tree}, - check::CheckOpts, - }, + commands::check::CheckOpts, crypto::{aespoly1305::Key, hasher::hash}, error::{RusticError, RusticResult}, file::{AddFileResult, FileInfos, RestoreStats}, diff --git a/crates/rustic_core/src/repository.rs b/crates/rustic_core/src/repository.rs index cf4927f..11d44ff 100644 --- a/crates/rustic_core/src/repository.rs +++ b/crates/rustic_core/src/repository.rs @@ -6,6 +6,7 @@ use std::{ process::Command, }; +use bytes::Bytes; use derive_more::Add; use log::{debug, info}; @@ -28,11 +29,12 @@ use crate::{ decrypt::DecryptReadBackend, decrypt::DecryptWriteBackend, hotcold::HotColdBackend, FileType, ReadBackend, }, + commands::{self, check::CheckOpts}, crypto::aespoly1305::Key, error::RepositoryErrorKind, index::IndexEntry, repofile::{configfile::ConfigFile, indexfile::IndexPack, keyfile::find_key_in_backend}, - RusticResult, + BlobType, IndexBackend, ProgressBars, RusticResult, SnapshotFile, }; pub(super) mod constants { @@ -331,18 +333,6 @@ impl Repository { } } -#[derive(Debug)] -pub struct OpenRepository { - pub name: String, - pub be: HotColdBackend, - pub be_hot: Option, - pub key: Key, - pub cache: Option, - pub dbe: DecryptBackend>, Key>, - pub config: ConfigFile, - pub opts: RepositoryOptions, -} - pub(crate) fn get_key(be: &impl ReadBackend, password: Option) -> RusticResult { for _ in 0..constants::MAX_PASSWORD_RETRIES { match password { @@ -365,3 +355,51 @@ pub(crate) fn get_key(be: &impl ReadBackend, password: Option) -> Rustic } Err(RepositoryErrorKind::IncorrectPassword.into()) } + +#[derive(Debug)] +pub struct OpenRepository { + pub name: String, + pub be: HotColdBackend, + pub be_hot: Option, + pub key: Key, + pub cache: Option, + pub dbe: DecryptBackend>, Key>, + pub config: ConfigFile, + pub opts: RepositoryOptions, +} + +impl OpenRepository { + pub fn cat_file(&self, tpe: FileType, id: &str) -> RusticResult { + commands::cat::cat_file(self, tpe, id) + } + + pub fn check(&self, opts: CheckOpts, pb: &impl ProgressBars) -> RusticResult<()> { + opts.run(self, pb) + } + + pub fn to_indexed(self, pb: &impl ProgressBars) -> RusticResult { + let index = IndexBackend::new(&self.dbe, &pb.progress_counter(""))?; + Ok(IndexedRepository { repo: self, index }) + } +} + +#[derive(Debug)] +pub struct IndexedRepository { + pub(crate) repo: OpenRepository, + pub(crate) index: + IndexBackend>, Key>>, +} + +impl IndexedRepository { + pub fn cat_blob(&self, tpe: BlobType, id: &str) -> RusticResult { + commands::cat::cat_blob(self, tpe, id) + } + pub fn cat_tree( + &self, + snap: &str, + sn_filter: impl FnMut(&SnapshotFile) -> bool + Send + Sync, + pb: &impl ProgressBars, + ) -> RusticResult { + commands::cat::cat_tree(self, snap, sn_filter, pb) + } +} diff --git a/src/commands/cat.rs b/src/commands/cat.rs index 17a342f..3628b11 100644 --- a/src/commands/cat.rs +++ b/src/commands/cat.rs @@ -11,7 +11,7 @@ use abscissa_core::{Command, Runnable, Shutdown}; use anyhow::Result; -use rustic_core::{cat_blob, cat_file, cat_tree, BlobType, FileType}; +use rustic_core::{BlobType, FileType}; /// `cat` subcommand #[derive(clap::Parser, Command, Debug)] @@ -66,15 +66,14 @@ impl CatCmd { let repo = open_repository(get_repository(&config)); let data = match &self.cmd { - CatSubCmd::Config => cat_file(&repo, FileType::Config, "")?, - CatSubCmd::Index(opt) => cat_file(&repo, FileType::Index, &opt.id)?, - CatSubCmd::Snapshot(opt) => cat_file(&repo, FileType::Snapshot, &opt.id)?, + CatSubCmd::Config => repo.cat_file(FileType::Config, "")?, + CatSubCmd::Index(opt) => repo.cat_file(FileType::Index, &opt.id)?, + CatSubCmd::Snapshot(opt) => repo.cat_file(FileType::Snapshot, &opt.id)?, // special treatment for cating blobs: read the index and use it to locate the blob - CatSubCmd::TreeBlob(opt) => cat_blob(&repo, BlobType::Tree, &opt.id, &po)?, - CatSubCmd::DataBlob(opt) => cat_blob(&repo, BlobType::Data, &opt.id, &po)?, + CatSubCmd::TreeBlob(opt) => repo.to_indexed(&po)?.cat_blob(BlobType::Tree, &opt.id)?, + CatSubCmd::DataBlob(opt) => repo.to_indexed(&po)?.cat_blob(BlobType::Data, &opt.id)?, // special treatment for cating a tree within a snapshot - CatSubCmd::Tree(opt) => cat_tree( - &repo, + CatSubCmd::Tree(opt) => repo.to_indexed(&po)?.cat_tree( &opt.snap, |sn| config.snapshot_filter.matches(sn), &po, diff --git a/src/commands/check.rs b/src/commands/check.rs index b0efc2b..4bf8faa 100644 --- a/src/commands/check.rs +++ b/src/commands/check.rs @@ -24,7 +24,7 @@ impl Runnable for CheckCmd { let progress_options = config.global.progress_options; let repo = open_repository(get_repository(&config)); - if let Err(err) = self.opts.run(&repo, &progress_options) { + if let Err(err) = repo.check(self.opts, &progress_options) { status_err!("{}", err); RUSTIC_APP.shutdown(Shutdown::Crash); };