diff --git a/cmd/restic/cmd_debug.go b/cmd/restic/cmd_debug.go index 68213759e..0eb1c8e87 100644 --- a/cmd/restic/cmd_debug.go +++ b/cmd/restic/cmd_debug.go @@ -55,8 +55,7 @@ func prettyPrintJSON(wr io.Writer, item interface{}) error { } func debugPrintSnapshots(ctx context.Context, repo *repository.Repository, wr io.Writer) error { - return repo.List(ctx, restic.SnapshotFile, func(id restic.ID, size int64) error { - snapshot, err := restic.LoadSnapshot(ctx, repo, id) + return restic.ForAllSnapshots(ctx, repo, nil, func(id restic.ID, snapshot *restic.Snapshot, err error) error { if err != nil { return err } diff --git a/internal/restic/snapshot.go b/internal/restic/snapshot.go index 86e98e234..75d05e7c4 100644 --- a/internal/restic/snapshot.go +++ b/internal/restic/snapshot.go @@ -66,14 +66,23 @@ func LoadSnapshot(ctx context.Context, repo Repository, id ID) (*Snapshot, error return sn, nil } -// LoadAllSnapshots returns a list of all snapshots in the repo. -// If a snapshot ID is in excludeIDs, it will not be included in the result. -func LoadAllSnapshots(ctx context.Context, repo Repository, excludeIDs IDSet) (snapshots []*Snapshot, err error) { - err = repo.List(ctx, SnapshotFile, func(id ID, size int64) error { +// ForAllSnapshots reads all snapshots and calls the given function. +// If a snapshot ID is in excludeIDs, it will be ignored. +func ForAllSnapshots(ctx context.Context, repo Repository, excludeIDs IDSet, fn func(ID, *Snapshot, error) error) error { + return repo.List(ctx, SnapshotFile, func(id ID, size int64) error { if excludeIDs.Has(id) { return nil } sn, err := LoadSnapshot(ctx, repo, id) + return fn(id, sn, err) + }) + +} + +// LoadAllSnapshots returns a list of all snapshots in the repo. +// If a snapshot ID is in excludeIDs, it will not be included in the result. +func LoadAllSnapshots(ctx context.Context, repo Repository, excludeIDs IDSet) (snapshots Snapshots, err error) { + err = ForAllSnapshots(ctx, repo, excludeIDs, func(id ID, sn *Snapshot, err error) error { if err != nil { return err } diff --git a/internal/restic/snapshot_find.go b/internal/restic/snapshot_find.go index 50395c814..fe9b943de 100644 --- a/internal/restic/snapshot_find.go +++ b/internal/restic/snapshot_find.go @@ -33,10 +33,9 @@ func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string, found bool ) - err = repo.List(ctx, SnapshotFile, func(snapshotID ID, size int64) error { - snapshot, err := LoadSnapshot(ctx, repo, snapshotID) + err = ForAllSnapshots(ctx, repo, nil, func(id ID, snapshot *Snapshot, err error) error { if err != nil { - return errors.Errorf("Error loading snapshot %v: %v", snapshotID.Str(), err) + return errors.Errorf("Error loading snapshot %v: %v", id.Str(), err) } if snapshot.Time.Before(latest) { @@ -56,7 +55,7 @@ func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string, } latest = snapshot.Time - latestID = snapshotID + latestID = id found = true return nil }) @@ -90,8 +89,7 @@ func FindSnapshot(ctx context.Context, repo Repository, s string) (ID, error) { func FindFilteredSnapshots(ctx context.Context, repo Repository, hosts []string, tags []TagList, paths []string) (Snapshots, error) { results := make(Snapshots, 0, 20) - err := repo.List(ctx, SnapshotFile, func(id ID, size int64) error { - sn, err := LoadSnapshot(ctx, repo, id) + err := ForAllSnapshots(ctx, repo, nil, func(id ID, sn *Snapshot, err error) error { if err != nil { fmt.Fprintf(os.Stderr, "could not load snapshot %v: %v\n", id.Str(), err) return nil