make commands methods of repository structs

This commit is contained in:
Alexander Weiss 2023-06-13 08:26:34 +02:00
parent a9f6741284
commit d50b95d88c
6 changed files with 75 additions and 41 deletions

View File

@ -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<Bytes> {
@ -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<Bytes> {
pub fn cat_blob(repo: &IndexedRepository, tpe: BlobType, id: &str) -> RusticResult<Bytes> {
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<Bytes> {
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)
}

View File

@ -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;

View File

@ -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},

View File

@ -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<ChooseBackend>,
pub be_hot: Option<ChooseBackend>,
pub key: Key,
pub cache: Option<Cache>,
pub dbe: DecryptBackend<CachedBackend<HotColdBackend<ChooseBackend>>, Key>,
pub config: ConfigFile,
pub opts: RepositoryOptions,
}
pub(crate) fn get_key(be: &impl ReadBackend, password: Option<String>) -> RusticResult<Key> {
for _ in 0..constants::MAX_PASSWORD_RETRIES {
match password {
@ -365,3 +355,51 @@ pub(crate) fn get_key(be: &impl ReadBackend, password: Option<String>) -> Rustic
}
Err(RepositoryErrorKind::IncorrectPassword.into())
}
#[derive(Debug)]
pub struct OpenRepository {
pub name: String,
pub be: HotColdBackend<ChooseBackend>,
pub be_hot: Option<ChooseBackend>,
pub key: Key,
pub cache: Option<Cache>,
pub dbe: DecryptBackend<CachedBackend<HotColdBackend<ChooseBackend>>, Key>,
pub config: ConfigFile,
pub opts: RepositoryOptions,
}
impl OpenRepository {
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 to_indexed(self, pb: &impl ProgressBars) -> RusticResult<IndexedRepository> {
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<DecryptBackend<CachedBackend<HotColdBackend<ChooseBackend>>, Key>>,
}
impl IndexedRepository {
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)
}
}

View File

@ -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,

View File

@ -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);
};