From 1ba2df586bff1579499d2b3e5818ad56c147a1bc Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Tue, 20 Jun 2023 21:36:14 +0200 Subject: [PATCH] put ProgressBars into Repository struct --- crates/rustic_core/examples/check.rs | 5 ++- crates/rustic_core/src/commands/cat.rs | 11 +++---- crates/rustic_core/src/commands/check.rs | 3 +- crates/rustic_core/src/repository.rs | 39 +++++++++++++++--------- src/commands.rs | 9 +++--- src/commands/cat.rs | 14 +++------ src/commands/check.rs | 3 +- src/commands/prune.rs | 4 +-- src/helpers.rs | 12 ++++---- 9 files changed, 52 insertions(+), 48 deletions(-) diff --git a/crates/rustic_core/examples/check.rs b/crates/rustic_core/examples/check.rs index de1c8e525..8dc39deb7 100644 --- a/crates/rustic_core/examples/check.rs +++ b/crates/rustic_core/examples/check.rs @@ -1,5 +1,5 @@ //! `check` example -use rustic_core::{CheckOpts, NoProgressBars, Repository, RepositoryOptions}; +use rustic_core::{CheckOpts, Repository, RepositoryOptions}; fn main() { let mut repo_opts = RepositoryOptions::default(); @@ -8,6 +8,5 @@ fn main() { let repo = Repository::new(&repo_opts).unwrap().open().unwrap(); let opts = CheckOpts::default(); - let progress = NoProgressBars {}; - repo.check(opts, &progress).unwrap() + repo.check(opts).unwrap() } diff --git a/crates/rustic_core/src/commands/cat.rs b/crates/rustic_core/src/commands/cat.rs index f558379b1..644afc175 100644 --- a/crates/rustic_core/src/commands/cat.rs +++ b/crates/rustic_core/src/commands/cat.rs @@ -8,31 +8,30 @@ use crate::{ Tree, }; -pub fn cat_file(repo: &OpenRepository, tpe: FileType, id: &str) -> RusticResult { +pub fn cat_file

(repo: &OpenRepository

, tpe: FileType, id: &str) -> RusticResult { let id = repo.dbe.find_id(tpe, id)?; let data = repo.dbe.read_encrypted_full(tpe, &id)?; Ok(data) } -pub fn cat_blob(repo: &IndexedRepository, tpe: BlobType, id: &str) -> RusticResult { +pub fn cat_blob

(repo: &IndexedRepository

, tpe: BlobType, id: &str) -> RusticResult { let id = Id::from_hex(id)?; let data = repo.index.blob_from_backend(tpe, &id)?; Ok(data) } -pub fn cat_tree( - repo: &IndexedRepository, +pub fn cat_tree( + 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.repo.dbe, id, sn_filter, - &pb.progress_counter("getting snapshot..."), + &repo.repo.pb.progress_counter("getting snapshot..."), )?; let node = Tree::node_from_path(&repo.index, snap.tree, Path::new(path))?; let id = node diff --git a/crates/rustic_core/src/commands/check.rs b/crates/rustic_core/src/commands/check.rs index 0832bbd8e..d354218e7 100644 --- a/crates/rustic_core/src/commands/check.rs +++ b/crates/rustic_core/src/commands/check.rs @@ -28,11 +28,12 @@ pub struct CheckOpts { } impl CheckOpts { - pub fn run(self, repo: &OpenRepository, pb: &impl ProgressBars) -> RusticResult<()> { + pub fn run(self, repo: &OpenRepository

) -> RusticResult<()> { let be = &repo.dbe; let cache = &repo.cache; let hot_be = &repo.be_hot; let raw_be = &repo.be; + let pb = &repo.pb; if !self.trust_cache { if let Some(cache) = &cache { for file_type in [FileType::Snapshot, FileType::Index] { diff --git a/crates/rustic_core/src/repository.rs b/crates/rustic_core/src/repository.rs index d5f3380da..973b18fbf 100644 --- a/crates/rustic_core/src/repository.rs +++ b/crates/rustic_core/src/repository.rs @@ -34,7 +34,7 @@ use crate::{ error::RepositoryErrorKind, index::IndexEntry, repofile::{configfile::ConfigFile, indexfile::IndexPack, keyfile::find_key_in_backend}, - BlobType, IndexBackend, ProgressBars, RusticResult, SnapshotFile, + BlobType, IndexBackend, NoProgressBars, ProgressBars, RusticResult, SnapshotFile, }; pub(super) mod constants { @@ -196,15 +196,22 @@ pub(crate) fn read_password_from_reader(file: &mut impl BufRead) -> RusticResult } #[derive(Debug)] -pub struct Repository { +pub struct Repository

{ name: String, pub be: HotColdBackend, pub be_hot: Option, opts: RepositoryOptions, + pb: P, } -impl Repository { +impl Repository { pub fn new(opts: &RepositoryOptions) -> RusticResult { + Self::new_with_progress(opts, NoProgressBars {}) + } +} + +impl

Repository

{ + pub fn new_with_progress(opts: &RepositoryOptions, pb: P) -> RusticResult { let be = match &opts.repository { Some(repo) => ChooseBackend::from_url(repo)?, None => return Err(RepositoryErrorKind::NoRepositoryGiven.into()), @@ -238,6 +245,7 @@ impl Repository { be, be_hot, opts: opts.clone(), + pb, }) } @@ -276,7 +284,7 @@ impl Repository { } } - pub fn open(self) -> RusticResult { + pub fn open(self) -> RusticResult> { let config_ids = match self.be.list(FileType::Config) { Ok(val) => val, Err(_e) => return Err(RepositoryErrorKind::ListingRepositoryConfigFileFailed.into()), @@ -329,6 +337,7 @@ impl Repository { be_hot: self.be_hot, config, opts: self.opts, + pb: self.pb, }) } } @@ -357,7 +366,7 @@ pub(crate) fn get_key(be: &impl ReadBackend, password: Option) -> Rustic } #[derive(Debug)] -pub struct OpenRepository { +pub struct OpenRepository

{ pub name: String, pub be: HotColdBackend, pub be_hot: Option, @@ -366,31 +375,32 @@ pub struct OpenRepository { pub dbe: DecryptBackend>, Key>, pub config: ConfigFile, pub opts: RepositoryOptions, + pub(crate) pb: P, } -impl OpenRepository { +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 check(&self, opts: CheckOpts) -> RusticResult<()> { + opts.run(self) } - pub fn to_indexed(self, pb: &impl ProgressBars) -> RusticResult { - let index = IndexBackend::new(&self.dbe, &pb.progress_counter(""))?; + pub fn to_indexed(self) -> RusticResult> { + let index = IndexBackend::new(&self.dbe, &self.pb.progress_counter(""))?; Ok(IndexedRepository { repo: self, index }) } } #[derive(Debug)] -pub struct IndexedRepository { - pub(crate) repo: OpenRepository, +pub struct IndexedRepository

{ + pub(crate) repo: OpenRepository

, pub(crate) index: IndexBackend>, Key>>, } -impl IndexedRepository { +impl IndexedRepository

{ pub fn cat_blob(&self, tpe: BlobType, id: &str) -> RusticResult { commands::cat::cat_blob(self, tpe, id) } @@ -398,8 +408,7 @@ impl IndexedRepository { &self, snap: &str, sn_filter: impl FnMut(&SnapshotFile) -> bool + Send + Sync, - pb: &impl ProgressBars, ) -> RusticResult { - commands::cat::cat_tree(self, snap, sn_filter, pb) + commands::cat::cat_tree(self, snap, sn_filter) } } diff --git a/src/commands.rs b/src/commands.rs index 56a1690b7..731284c52 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -34,7 +34,7 @@ use crate::{ repair::RepairCmd, repoinfo::RepoInfoCmd, restore::RestoreCmd, self_update::SelfUpdateCmd, show_config::ShowConfigCmd, snapshots::SnapshotCmd, tag::TagCmd, }, - config::RusticConfig, + config::{progress_options::ProgressOptions, RusticConfig}, {Application, RUSTIC_APP}, }; @@ -167,7 +167,7 @@ impl Configurable for EntryPoint { } } -fn open_repository(repo: Repository) -> OpenRepository { +fn open_repository

(repo: Repository

) -> OpenRepository

{ match repo.open() { Ok(it) => it, Err(err) => { @@ -177,8 +177,9 @@ fn open_repository(repo: Repository) -> OpenRepository { } } -fn get_repository(config: &Arc) -> Repository { - match Repository::new(&config.repository) { +fn get_repository(config: &Arc) -> Repository { + let po = config.global.progress_options; + match Repository::new_with_progress(&config.repository, po) { Ok(it) => it, Err(err) => { status_err!("{}", err); diff --git a/src/commands/cat.rs b/src/commands/cat.rs index 3628b1137..d590df30f 100644 --- a/src/commands/cat.rs +++ b/src/commands/cat.rs @@ -61,8 +61,6 @@ impl Runnable for CatCmd { impl CatCmd { fn inner_run(&self) -> Result<()> { let config = RUSTIC_APP.config(); - let po = config.global.progress_options; - let repo = open_repository(get_repository(&config)); let data = match &self.cmd { @@ -70,14 +68,12 @@ impl CatCmd { 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) => repo.to_indexed(&po)?.cat_blob(BlobType::Tree, &opt.id)?, - CatSubCmd::DataBlob(opt) => repo.to_indexed(&po)?.cat_blob(BlobType::Data, &opt.id)?, + CatSubCmd::TreeBlob(opt) => repo.to_indexed()?.cat_blob(BlobType::Tree, &opt.id)?, + CatSubCmd::DataBlob(opt) => repo.to_indexed()?.cat_blob(BlobType::Data, &opt.id)?, // special treatment for cating a tree within a snapshot - CatSubCmd::Tree(opt) => repo.to_indexed(&po)?.cat_tree( - &opt.snap, - |sn| config.snapshot_filter.matches(sn), - &po, - )?, + CatSubCmd::Tree(opt) => repo + .to_indexed()? + .cat_tree(&opt.snap, |sn| config.snapshot_filter.matches(sn))?, }; println!("{}", String::from_utf8(data.to_vec())?); diff --git a/src/commands/check.rs b/src/commands/check.rs index 4bf8faadf..5cdd90c9e 100644 --- a/src/commands/check.rs +++ b/src/commands/check.rs @@ -21,10 +21,9 @@ pub(crate) struct CheckCmd { impl Runnable for CheckCmd { fn run(&self) { let config = RUSTIC_APP.config(); - let progress_options = config.global.progress_options; let repo = open_repository(get_repository(&config)); - if let Err(err) = repo.check(self.opts, &progress_options) { + if let Err(err) = repo.check(self.opts) { status_err!("{}", err); RUSTIC_APP.shutdown(Shutdown::Crash); }; diff --git a/src/commands/prune.rs b/src/commands/prune.rs index 607c2bb7b..f8feae42c 100644 --- a/src/commands/prune.rs +++ b/src/commands/prune.rs @@ -849,9 +849,9 @@ impl Pruner { } #[allow(clippy::significant_drop_tightening)] - fn do_prune( + fn do_prune

( self, - repo: OpenRepository, + repo: OpenRepository

, opts: &PruneCmd, progress_options: &ProgressOptions, ) -> Result<()> { diff --git a/src/helpers.rs b/src/helpers.rs index e50347389..8101a2ce4 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -25,8 +25,8 @@ pub(super) mod constants { pub(super) const MAX_READER_THREADS_NUM: usize = 20; } -pub(crate) fn warm_up_wait( - repo: &OpenRepository, +pub(crate) fn warm_up_wait

( + repo: &OpenRepository

, packs: impl ExactSizeIterator, wait: bool, progress_options: &ProgressOptions, @@ -97,10 +97,10 @@ pub(crate) fn warm_up( Ok(()) } -pub(crate) fn copy( +pub(crate) fn copy

( snapshots: &[SnapshotFile], index: &impl IndexedBackend, - repo_dest: &OpenRepository, + repo_dest: &OpenRepository

, ) -> Result<()> { let config = RUSTIC_APP.config(); let be_dest = &repo_dest.dbe; @@ -201,9 +201,9 @@ pub(crate) fn copy( Ok(()) } -pub(crate) fn relevant_snapshots( +pub(crate) fn relevant_snapshots( snaps: &[SnapshotFile], - dest_repo: &OpenRepository, + dest_repo: &OpenRepository

, filter: F, p: &impl Progress, ) -> Result>