Skip to content

Commit

Permalink
put ProgressBars into Repository struct
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Jun 20, 2023
1 parent 0317ed5 commit 2342c5b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 48 deletions.
5 changes: 2 additions & 3 deletions crates/rustic_core/examples/check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! `check` example
use rustic_core::{CheckOpts, NoProgressBars, Repository, RepositoryOptions};
use rustic_core::{CheckOpts, Repository, RepositoryOptions};
use simplelog::{Config, LevelFilter, SimpleLogger};

fn main() {
Expand All @@ -14,6 +14,5 @@ fn main() {

// Check respository with standard options
let opts = CheckOpts::default();
let progress = NoProgressBars {};
repo.check(opts, &progress).unwrap()
repo.check(opts).unwrap()
}
11 changes: 5 additions & 6 deletions crates/rustic_core/src/commands/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,30 @@ use crate::{
Tree,
};

pub fn cat_file(repo: &OpenRepository, tpe: FileType, id: &str) -> RusticResult<Bytes> {
pub fn cat_file<P>(repo: &OpenRepository<P>, tpe: FileType, id: &str) -> RusticResult<Bytes> {
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<Bytes> {
pub fn cat_blob<P>(repo: &IndexedRepository<P>, tpe: BlobType, id: &str) -> RusticResult<Bytes> {
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<P: ProgressBars>(
repo: &IndexedRepository<P>,
snap: &str,
sn_filter: impl FnMut(&SnapshotFile) -> bool + Send + Sync,
pb: &impl ProgressBars,
) -> RusticResult<Bytes> {
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
Expand Down
3 changes: 2 additions & 1 deletion crates/rustic_core/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ pub struct CheckOpts {
}

impl CheckOpts {
pub fn run(self, repo: &OpenRepository, pb: &impl ProgressBars) -> RusticResult<()> {
pub fn run<P: ProgressBars>(self, repo: &OpenRepository<P>) -> 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] {
Expand Down
39 changes: 24 additions & 15 deletions crates/rustic_core/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -196,15 +196,22 @@ pub(crate) fn read_password_from_reader(file: &mut impl BufRead) -> RusticResult
}

#[derive(Debug)]
pub struct Repository {
pub struct Repository<P> {
name: String,
pub be: HotColdBackend<ChooseBackend>,
pub be_hot: Option<ChooseBackend>,
opts: RepositoryOptions,
pb: P,
}

impl Repository {
impl Repository<NoProgressBars> {
pub fn new(opts: &RepositoryOptions) -> RusticResult<Self> {
Self::new_with_progress(opts, NoProgressBars {})
}
}

impl<P> Repository<P> {
pub fn new_with_progress(opts: &RepositoryOptions, pb: P) -> RusticResult<Self> {
let be = match &opts.repository {
Some(repo) => ChooseBackend::from_url(repo)?,
None => return Err(RepositoryErrorKind::NoRepositoryGiven.into()),
Expand Down Expand Up @@ -238,6 +245,7 @@ impl Repository {
be,
be_hot,
opts: opts.clone(),
pb,
})
}

Expand Down Expand Up @@ -276,7 +284,7 @@ impl Repository {
}
}

pub fn open(self) -> RusticResult<OpenRepository> {
pub fn open(self) -> RusticResult<OpenRepository<P>> {
let config_ids = match self.be.list(FileType::Config) {
Ok(val) => val,
Err(_e) => return Err(RepositoryErrorKind::ListingRepositoryConfigFileFailed.into()),
Expand Down Expand Up @@ -329,6 +337,7 @@ impl Repository {
be_hot: self.be_hot,
config,
opts: self.opts,
pb: self.pb,
})
}
}
Expand Down Expand Up @@ -357,7 +366,7 @@ pub(crate) fn get_key(be: &impl ReadBackend, password: Option<String>) -> Rustic
}

#[derive(Debug)]
pub struct OpenRepository {
pub struct OpenRepository<P> {
pub name: String,
pub be: HotColdBackend<ChooseBackend>,
pub be_hot: Option<ChooseBackend>,
Expand All @@ -366,40 +375,40 @@ pub struct OpenRepository {
pub dbe: DecryptBackend<CachedBackend<HotColdBackend<ChooseBackend>>, Key>,
pub config: ConfigFile,
pub opts: RepositoryOptions,
pub(crate) pb: P,
}

impl OpenRepository {
impl<P: ProgressBars> OpenRepository<P> {
pub fn cat_file(&self, tpe: FileType, id: &str) -> RusticResult<Bytes> {
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<IndexedRepository> {
let index = IndexBackend::new(&self.dbe, &pb.progress_counter(""))?;
pub fn to_indexed(self) -> RusticResult<IndexedRepository<P>> {
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<P> {
pub(crate) repo: OpenRepository<P>,
pub(crate) index:
IndexBackend<DecryptBackend<CachedBackend<HotColdBackend<ChooseBackend>>, Key>>,
}

impl IndexedRepository {
impl<P: ProgressBars> IndexedRepository<P> {
pub fn cat_blob(&self, tpe: BlobType, id: &str) -> RusticResult<Bytes> {
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<Bytes> {
commands::cat::cat_tree(self, snap, sn_filter, pb)
commands::cat::cat_tree(self, snap, sn_filter)
}
}
9 changes: 5 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -167,7 +167,7 @@ impl Configurable<RusticConfig> for EntryPoint {
}
}

fn open_repository(repo: Repository) -> OpenRepository {
fn open_repository<P>(repo: Repository<P>) -> OpenRepository<P> {
match repo.open() {
Ok(it) => it,
Err(err) => {
Expand All @@ -177,8 +177,9 @@ fn open_repository(repo: Repository) -> OpenRepository {
}
}

fn get_repository(config: &Arc<RusticConfig>) -> Repository {
match Repository::new(&config.repository) {
fn get_repository(config: &Arc<RusticConfig>) -> Repository<ProgressOptions> {
let po = config.global.progress_options;
match Repository::new_with_progress(&config.repository, po) {
Ok(it) => it,
Err(err) => {
status_err!("{}", err);
Expand Down
14 changes: 5 additions & 9 deletions src/commands/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,19 @@ 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 {
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) => 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())?);

Expand Down
3 changes: 1 addition & 2 deletions src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
4 changes: 2 additions & 2 deletions src/commands/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,9 +849,9 @@ impl Pruner {
}

#[allow(clippy::significant_drop_tightening)]
fn do_prune(
fn do_prune<P>(
self,
repo: OpenRepository,
repo: OpenRepository<P>,
opts: &PruneCmd,
progress_options: &ProgressOptions,
) -> Result<()> {
Expand Down
12 changes: 6 additions & 6 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P>(
repo: &OpenRepository<P>,
packs: impl ExactSizeIterator<Item = Id>,
wait: bool,
progress_options: &ProgressOptions,
Expand Down Expand Up @@ -97,10 +97,10 @@ pub(crate) fn warm_up(
Ok(())
}

pub(crate) fn copy(
pub(crate) fn copy<P>(
snapshots: &[SnapshotFile],
index: &impl IndexedBackend,
repo_dest: &OpenRepository,
repo_dest: &OpenRepository<P>,
) -> Result<()> {
let config = RUSTIC_APP.config();
let be_dest = &repo_dest.dbe;
Expand Down Expand Up @@ -201,9 +201,9 @@ pub(crate) fn copy(
Ok(())
}

pub(crate) fn relevant_snapshots<F>(
pub(crate) fn relevant_snapshots<F, P>(
snaps: &[SnapshotFile],
dest_repo: &OpenRepository,
dest_repo: &OpenRepository<P>,
filter: F,
p: &impl Progress,
) -> Result<Vec<SnapshotFile>>
Expand Down

0 comments on commit 2342c5b

Please sign in to comment.