Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add warning about unsorted files and sort where neccessary #205

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions crates/core/src/backend/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub trait DecryptReadBackend: ReadBackend + Clone + 'static {

/// Streams all files.
///
/// Note: The result comes in arbitrary order!
///
/// # Arguments
///
/// * `p` - The progress bar.
Expand All @@ -145,11 +147,13 @@ pub trait DecryptReadBackend: ReadBackend + Clone + 'static {
p: &impl Progress,
) -> RusticResult<Receiver<RusticResult<(Id, F)>>> {
let list = self.list(F::TYPE).map_err(RusticErrorKind::Backend)?;
self.stream_list(list, p)
self.stream_list(&list, p)
}

/// Streams a list of files.
///
/// Note: The result comes in arbitrary order!
///
/// # Arguments
///
/// * `list` - The list of files to stream.
Expand All @@ -160,15 +164,15 @@ pub trait DecryptReadBackend: ReadBackend + Clone + 'static {
/// If the files could not be read.
fn stream_list<F: RepoFile>(
&self,
list: Vec<Id>,
list: &[Id],
p: &impl Progress,
) -> RusticResult<Receiver<RusticResult<(Id, F)>>> {
p.set_length(list.len() as u64);
let (tx, rx) = unbounded();

list.into_par_iter()
.for_each_with((self, p, tx), |(be, p, tx), id| {
let file = be.get_file::<F>(&id).map(|file| (id, file));
let file = be.get_file::<F>(id).map(|file| (*id, file));
p.inc(1);
tx.send(file).unwrap();
});
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/commands/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,14 +1460,14 @@ fn find_used_blobs(
let ignore_snaps: BTreeSet<_> = ignore_snaps.iter().collect();

let p = pb.progress_counter("reading snapshots...");
let list = be
let list: Vec<_> = be
.list(FileType::Snapshot)
.map_err(RusticErrorKind::Backend)?
.into_iter()
.filter(|id| !ignore_snaps.contains(id))
.collect();
let snap_trees: Vec<_> = be
.stream_list::<SnapshotFile>(list, &p)?
.stream_list::<SnapshotFile>(&list, &p)?
.into_iter()
.map_ok(|(_, snap)| snap.tree)
.try_collect()?;
Expand Down
11 changes: 8 additions & 3 deletions crates/core/src/repofile/snapshotfile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
cmp::Ordering,
collections::BTreeMap,
fmt::{self, Display},
path::{Path, PathBuf},
str::FromStr,
Expand Down Expand Up @@ -550,10 +551,14 @@
p: &impl Progress,
) -> RusticResult<Vec<Self>> {
let ids = be.find_ids(FileType::Snapshot, ids)?;
be.stream_list::<Self>(ids, p)?
let mut list: BTreeMap<_, _> =
be.stream_list::<Self>(&ids, p)?.into_iter().try_collect()?;

Check warning on line 555 in crates/core/src/repofile/snapshotfile.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repofile/snapshotfile.rs#L555

Added line #L555 was not covered by tests
// sort back to original order
Ok(ids
.into_iter()
.map_ok(Self::set_id)
.try_collect()
.filter_map(|id| list.remove_entry(&id))
.map(Self::set_id)
.collect())

Check warning on line 561 in crates/core/src/repofile/snapshotfile.rs

View check run for this annotation

Codecov / codecov/patch

crates/core/src/repofile/snapshotfile.rs#L559-L561

Added lines #L559 - L561 were not covered by tests
}

/// Compare two [`SnapshotFile`]s by criteria from [`SnapshotGroupCriterion`].
Expand Down
6 changes: 6 additions & 0 deletions crates/core/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ impl<P: ProgressBars, S: Open> Repository<P, S> {
///
/// # Errors
///
/// # Note
/// The result is not sorted and may come in random order!
///
// TODO: Document errors
pub fn get_matching_snapshots(
&self,
Expand Down Expand Up @@ -1223,6 +1226,9 @@ impl<P: ProgressBars, S: Open> Repository<P, S> {
///
/// # Returns
///
/// # Note
/// The result is not sorted and may come in random order!
///
/// An iterator over all files of the given type
pub fn stream_files<F: RepoFile>(
&self,
Expand Down
12 changes: 12 additions & 0 deletions crates/core/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ fn test_backup_with_tar_gz_passes(
// pack files should be unchanged
let packs2: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
assert_eq!(packs1, packs2);

// Check if snapshots can be retrieved
let mut ids: Vec<_> = all_snapshots.iter().map(|sn| sn.id.to_string()).collect();
let snaps = repo.get_snapshots(&ids)?;
assert_eq!(snaps, all_snapshots);

// reverse order
all_snapshots.reverse();
ids.reverse();
let snaps = repo.get_snapshots(&ids)?;
assert_eq!(snaps, all_snapshots);

Ok(())
}

Expand Down
Loading