Skip to content

Commit

Permalink
use Repository states
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Jun 29, 2023
1 parent fc90098 commit d3f2e73
Show file tree
Hide file tree
Showing 27 changed files with 299 additions and 188 deletions.
8 changes: 5 additions & 3 deletions crates/rustic_core/examples/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ fn main() {
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let mut repo_opts = RepositoryOptions::default();
repo_opts.repository = Some("/tmp/repo".to_string());
repo_opts.password = Some("test".to_string());
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let repo = Repository::new(&repo_opts).unwrap().open().unwrap();

// Check respository with standard options
Expand Down
8 changes: 5 additions & 3 deletions crates/rustic_core/examples/forget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ fn main() {
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let mut repo_opts = RepositoryOptions::default();
repo_opts.repository = Some("/tmp/repo".to_string());
repo_opts.password = Some("test".to_string());
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let repo = Repository::new(&repo_opts).unwrap().open().unwrap();

// Check respository with standard options
Expand Down
8 changes: 5 additions & 3 deletions crates/rustic_core/examples/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ fn main() {
let _ = SimpleLogger::init(LevelFilter::Info, Config::default());

// Open repository
let mut repo_opts = RepositoryOptions::default();
repo_opts.repository = Some("/tmp/repo".to_string());
repo_opts.password = Some("test".to_string());
let repo_opts = RepositoryOptions {
repository: Some("/tmp/repo".to_string()),
password: Some("test".to_string()),
..Default::default()
};
let repo = Repository::new(&repo_opts).unwrap().open().unwrap();

let prune_opts = PruneOpts::default();
Expand Down
37 changes: 23 additions & 14 deletions crates/rustic_core/src/commands/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,49 @@ use std::path::Path;
use bytes::Bytes;

use crate::{
error::CommandErrorKind, repository::IndexedRepository, BlobType, DecryptReadBackend, FileType,
Id, IndexedBackend, OpenRepository, ProgressBars, ReadBackend, RusticResult, SnapshotFile,
Tree,
error::CommandErrorKind,
repository::{Indexed, Open, Repository},
BlobType, DecryptReadBackend, FileType, Id, IndexedBackend, ProgressBars, ReadBackend,
RusticResult, SnapshotFile, Tree,
};

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)?;
pub(crate) fn cat_file<P, S: Open>(
repo: &Repository<P, S>,
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<P>(repo: &IndexedRepository<P>, tpe: BlobType, id: &str) -> RusticResult<Bytes> {
pub(crate) fn cat_blob<P, S: Indexed>(
repo: &Repository<P, S>,
tpe: BlobType,
id: &str,
) -> RusticResult<Bytes> {
let id = Id::from_hex(id)?;
let data = repo.index.blob_from_backend(tpe, &id)?;
let data = repo.index().blob_from_backend(tpe, &id)?;

Ok(data)
}

pub fn cat_tree<P: ProgressBars>(
repo: &IndexedRepository<P>,
pub(crate) fn cat_tree<P: ProgressBars, S: Indexed>(
repo: &Repository<P, S>,
snap: &str,
sn_filter: impl FnMut(&SnapshotFile) -> bool + Send + Sync,
) -> RusticResult<Bytes> {
let (id, path) = snap.split_once(':').unwrap_or((snap, ""));
let snap = SnapshotFile::from_str(
&repo.repo.dbe,
repo.dbe(),
id,
sn_filter,
&repo.repo.pb.progress_counter("getting snapshot..."),
&repo.pb.progress_counter("getting snapshot..."),
)?;
let node = Tree::node_from_path(&repo.index, snap.tree, Path::new(path))?;
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 = repo.index.blob_from_backend(BlobType::Tree, &id)?;
let data = repo.index().blob_from_backend(BlobType::Tree, &id)?;
Ok(data)
}
16 changes: 9 additions & 7 deletions crates/rustic_core/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use rayon::prelude::{IntoParallelIterator, ParallelBridge, ParallelIterator};
use zstd::stream::decode_all;

use crate::{
hash, progress::ProgressBars, BlobType, Cache, DecryptReadBackend, FileType, Id, IndexBackend,
IndexCollector, IndexFile, IndexPack, IndexType, IndexedBackend, NodeType, OpenRepository,
PackHeader, PackHeaderLength, PackHeaderRef, Progress, ReadBackend, RusticResult, SnapshotFile,
TreeStreamerOnce,
hash,
progress::ProgressBars,
repository::{Open, Repository},
BlobType, Cache, DecryptReadBackend, FileType, Id, IndexBackend, IndexCollector, IndexFile,
IndexPack, IndexType, IndexedBackend, NodeType, PackHeader, PackHeaderLength, PackHeaderRef,
Progress, ReadBackend, RusticResult, SnapshotFile, TreeStreamerOnce,
};

/// `check` subcommand
Expand All @@ -28,9 +30,9 @@ pub struct CheckOpts {
}

impl CheckOpts {
pub fn run<P: ProgressBars>(self, repo: &OpenRepository<P>) -> RusticResult<()> {
let be = &repo.dbe;
let cache = &repo.cache;
pub(crate) fn run<P: ProgressBars, S: Open>(self, repo: &Repository<P, S>) -> RusticResult<()> {
let be = repo.dbe();
let cache = repo.cache();
let hot_be = &repo.be_hot;
let raw_be = &repo.be;
let pb = &repo.pb;
Expand Down
11 changes: 6 additions & 5 deletions crates/rustic_core/src/commands/dump.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::io::Write;

use crate::{
error::CommandErrorKind, repository::IndexedRepository, BlobType, IndexedBackend, Node,
NodeType, RusticResult,
error::CommandErrorKind,
repository::{Indexed, Repository},
BlobType, IndexedBackend, Node, NodeType, RusticResult,
};

pub(crate) fn dump<P>(
repo: &IndexedRepository<P>,
pub(crate) fn dump<P, S: Indexed>(
repo: &Repository<P, S>,
node: &Node,
w: &mut impl Write,
) -> RusticResult<()> {
Expand All @@ -16,7 +17,7 @@ pub(crate) fn dump<P>(

for id in node.content.as_ref().unwrap() {
// TODO: cache blobs which are needed later
let data = repo.index.blob_from_backend(BlobType::Data, id)?;
let data = repo.index().blob_from_backend(BlobType::Data, id)?;
w.write_all(&data)?;
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/rustic_core/src/commands/forget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;
use serde_with::{serde_as, DisplayFromStr};

use crate::{
Id, OpenRepository, ProgressBars, RusticResult, SnapshotFile, SnapshotGroup,
repository::Open, Id, ProgressBars, Repository, RusticResult, SnapshotFile, SnapshotGroup,
SnapshotGroupCriterion, StringList,
};

Expand Down Expand Up @@ -41,8 +41,8 @@ impl ForgetGroups {
}
}

pub(crate) fn get_forget_snapshots<P: ProgressBars>(
repo: &OpenRepository<P>,
pub(crate) fn get_forget_snapshots<P: ProgressBars, S: Open>(
repo: &Repository<P, S>,
keep: &KeepOptions,
group_by: SnapshotGroupCriterion,
filter: impl FnMut(&SnapshotFile) -> bool,
Expand Down
35 changes: 19 additions & 16 deletions crates/rustic_core/src/commands/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use itertools::Itertools;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};

use crate::{
error::CommandErrorKind, BlobType, BlobTypeMap, DecryptReadBackend, DecryptWriteBackend,
FileType, HeaderEntry, Id, IndexBackend, IndexBlob, IndexCollector, IndexFile, IndexPack,
IndexType, IndexedBackend, Indexer, Initialize, NodeType, OpenRepository, PackSizer, Progress,
ProgressBars, ReadBackend, ReadIndex, Repacker, RusticResult, SnapshotFile, Sum,
TreeStreamerOnce,
error::CommandErrorKind, repository::Open, BlobType, BlobTypeMap, DecryptReadBackend,
DecryptWriteBackend, FileType, HeaderEntry, Id, IndexBackend, IndexBlob, IndexCollector,
IndexFile, IndexPack, IndexType, IndexedBackend, Indexer, Initialize, NodeType, PackSizer,
Progress, ProgressBars, ReadBackend, ReadIndex, Repacker, Repository, RusticResult,
SnapshotFile, Sum, TreeStreamerOnce,
};

pub(super) mod constants {
Expand Down Expand Up @@ -115,11 +115,14 @@ impl Default for PruneOpts {
}

impl PruneOpts {
pub fn get_plan<P: ProgressBars>(&self, repo: &OpenRepository<P>) -> RusticResult<PrunePlan> {
pub fn get_plan<P: ProgressBars, S: Open>(
&self,
repo: &Repository<P, S>,
) -> RusticResult<PrunePlan> {
let pb = &repo.pb;
let be = &repo.dbe;
let be = repo.dbe();

if repo.config.version < 2 && self.repack_uncompressed {
if repo.config().version < 2 && self.repack_uncompressed {
return Err(CommandErrorKind::RepackUncompressedRepoV1.into());
}

Expand Down Expand Up @@ -158,9 +161,9 @@ impl PruneOpts {
pruner.check()?;
let repack_cacheable_only = self
.repack_cacheable_only
.unwrap_or_else(|| repo.config.is_hot == Some(true));
.unwrap_or_else(|| repo.config().is_hot == Some(true));
let pack_sizer =
total_size.map(|tpe, size| PackSizer::from_config(&repo.config, tpe, size));
total_size.map(|tpe, size| PackSizer::from_config(repo.config(), tpe, size));
pruner.decide_packs(
Duration::from_std(*self.keep_pack).map_err(CommandErrorKind::FromOutOfRangeError)?,
Duration::from_std(*self.keep_delete).map_err(CommandErrorKind::FromOutOfRangeError)?,
Expand Down Expand Up @@ -745,13 +748,13 @@ impl PrunePlan {
}

#[allow(clippy::significant_drop_tightening)]
pub fn do_prune<P: ProgressBars>(
pub fn do_prune<P: ProgressBars, S: Open>(
self,
repo: &OpenRepository<P>,
repo: &Repository<P, S>,
opts: &PruneOpts,
) -> RusticResult<()> {
repo.warm_up_wait(self.repack_packs().into_iter())?;
let be = &repo.dbe;
let be = repo.dbe();
let pb = &repo.pb;

let indexer = Indexer::new_unindexed(be.clone()).into_shared();
Expand All @@ -776,15 +779,15 @@ impl PrunePlan {
be.clone(),
BlobType::Tree,
indexer.clone(),
&repo.config,
repo.config(),
size_after_prune[BlobType::Tree],
)?;

let data_repacker = Repacker::new(
be.clone(),
BlobType::Data,
indexer.clone(),
&repo.config,
repo.config(),
size_after_prune[BlobType::Data],
)?;

Expand Down Expand Up @@ -1050,7 +1053,7 @@ impl PackInfo {

// find used blobs in repo
fn find_used_blobs(
index: &(impl IndexedBackend + Unpin),
index: &impl IndexedBackend,
ignore_snaps: &[Id],
pb: &impl ProgressBars,
) -> RusticResult<HashMap<Id, u8>> {
Expand Down
15 changes: 9 additions & 6 deletions crates/rustic_core/src/commands/repoinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use serde::{Deserialize, Serialize};
use crate::{
index::IndexEntry,
repofile::indexfile::{IndexFile, IndexPack},
BlobType, BlobTypeMap, DecryptReadBackend, FileType, OpenRepository, Progress, ProgressBars,
ReadBackend, Repository, RusticResult, ALL_FILE_TYPES,
repository::Open,
BlobType, BlobTypeMap, DecryptReadBackend, FileType, Progress, ProgressBars, ReadBackend,
Repository, RusticResult, ALL_FILE_TYPES,
};

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -53,8 +54,8 @@ impl PackInfo {
}
}

pub(crate) fn collect_index_infos<P: ProgressBars>(
repo: &OpenRepository<P>,
pub(crate) fn collect_index_infos<P: ProgressBars, S: Open>(
repo: &Repository<P, S>,
) -> RusticResult<IndexInfos> {
let mut blob_info = BlobTypeMap::<()>::default().map(|blob_type, _| BlobInfo {
blob_type,
Expand All @@ -72,7 +73,7 @@ pub(crate) fn collect_index_infos<P: ProgressBars>(
let mut pack_info_delete = pack_info;

let p = repo.pb.progress_counter("scanning index...");
for index in repo.dbe.stream_all::<IndexFile>(&p)? {
for index in repo.dbe().stream_all::<IndexFile>(&p)? {
let index = index?.1;
for pack in &index.packs {
let tpe = pack.blob_type();
Expand Down Expand Up @@ -130,7 +131,9 @@ pub(crate) fn collect_file_info(be: &impl ReadBackend) -> RusticResult<Vec<RepoF
Ok(files)
}

pub fn collect_file_infos<P: ProgressBars>(repo: &Repository<P>) -> RusticResult<RepoFileInfos> {
pub fn collect_file_infos<P: ProgressBars, S>(
repo: &Repository<P, S>,
) -> RusticResult<RepoFileInfos> {
let p = repo.pb.progress_spinner("scanning files...");
let files = collect_file_info(&repo.be)?;
let files_hot = repo.be_hot.as_ref().map(collect_file_info).transpose()?;
Expand Down
34 changes: 17 additions & 17 deletions crates/rustic_core/src/commands/snapshots.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
//! `smapshot` subcommand

use crate::{
OpenRepository, ProgressBars, RusticResult, SnapshotFile, SnapshotGroup, SnapshotGroupCriterion,
repository::Open, ProgressBars, Repository, RusticResult, SnapshotFile, SnapshotGroup,
SnapshotGroupCriterion,
};

pub(crate) fn get_snapshot_group<P: ProgressBars>(
repo: &OpenRepository<P>,
pub(crate) fn get_snapshot_group<P: ProgressBars, S: Open>(
repo: &Repository<P, S>,
ids: &[String],
group_by: SnapshotGroupCriterion,
filter: impl FnMut(&SnapshotFile) -> bool,
) -> RusticResult<Vec<(SnapshotGroup, Vec<SnapshotFile>)>> {
let pb = &repo.pb;
let dbe = repo.dbe();
let p = pb.progress_counter("getting snapshots...");
let groups = match ids {
[] => SnapshotFile::group_from_backend(&repo.dbe, filter, group_by, &p)?,
[id] if id == "latest" => {
SnapshotFile::group_from_backend(&repo.dbe, filter, group_by, &p)?
.into_iter()
.map(|(group, mut snaps)| {
snaps.sort_unstable();
let last_idx = snaps.len() - 1;
snaps.swap(0, last_idx);
snaps.truncate(1);
(group, snaps)
})
.collect::<Vec<_>>()
}
[] => SnapshotFile::group_from_backend(dbe, filter, group_by, &p)?,
[id] if id == "latest" => SnapshotFile::group_from_backend(dbe, filter, group_by, &p)?
.into_iter()
.map(|(group, mut snaps)| {
snaps.sort_unstable();
let last_idx = snaps.len() - 1;
snaps.swap(0, last_idx);
snaps.truncate(1);
(group, snaps)
})
.collect::<Vec<_>>(),
_ => {
let item = (
SnapshotGroup::default(),
SnapshotFile::from_ids(&repo.dbe, ids, &p)?,
SnapshotFile::from_ids(dbe, ids, &p)?,
);
vec![item]
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rustic_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ pub use crate::{
},
RepoFile,
},
repository::{read_password_from_reader, OpenRepository, Repository, RepositoryOptions},
repository::{read_password_from_reader, Open, OpenStatus, Repository, RepositoryOptions},
};
Loading

0 comments on commit d3f2e73

Please sign in to comment.