2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-30 00:33:57 +00:00

Add ForAllSnapshots

This commit is contained in:
Alexander Weiss 2020-11-28 08:59:12 +01:00
parent 058b102db0
commit 5b9ee56335
3 changed files with 18 additions and 12 deletions

View File

@ -55,8 +55,7 @@ func prettyPrintJSON(wr io.Writer, item interface{}) error {
} }
func debugPrintSnapshots(ctx context.Context, repo *repository.Repository, wr io.Writer) 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 { return restic.ForAllSnapshots(ctx, repo, nil, func(id restic.ID, snapshot *restic.Snapshot, err error) error {
snapshot, err := restic.LoadSnapshot(ctx, repo, id)
if err != nil { if err != nil {
return err return err
} }

View File

@ -66,14 +66,23 @@ func LoadSnapshot(ctx context.Context, repo Repository, id ID) (*Snapshot, error
return sn, nil return sn, nil
} }
// LoadAllSnapshots returns a list of all snapshots in the repo. // ForAllSnapshots reads all snapshots and calls the given function.
// If a snapshot ID is in excludeIDs, it will not be included in the result. // If a snapshot ID is in excludeIDs, it will be ignored.
func LoadAllSnapshots(ctx context.Context, repo Repository, excludeIDs IDSet) (snapshots []*Snapshot, err error) { func ForAllSnapshots(ctx context.Context, repo Repository, excludeIDs IDSet, fn func(ID, *Snapshot, error) error) error {
err = repo.List(ctx, SnapshotFile, func(id ID, size int64) error { return repo.List(ctx, SnapshotFile, func(id ID, size int64) error {
if excludeIDs.Has(id) { if excludeIDs.Has(id) {
return nil return nil
} }
sn, err := LoadSnapshot(ctx, repo, id) 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 { if err != nil {
return err return err
} }

View File

@ -33,10 +33,9 @@ func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string,
found bool found bool
) )
err = repo.List(ctx, SnapshotFile, func(snapshotID ID, size int64) error { err = ForAllSnapshots(ctx, repo, nil, func(id ID, snapshot *Snapshot, err error) error {
snapshot, err := LoadSnapshot(ctx, repo, snapshotID)
if err != nil { 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) { if snapshot.Time.Before(latest) {
@ -56,7 +55,7 @@ func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string,
} }
latest = snapshot.Time latest = snapshot.Time
latestID = snapshotID latestID = id
found = true found = true
return nil 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) { func FindFilteredSnapshots(ctx context.Context, repo Repository, hosts []string, tags []TagList, paths []string) (Snapshots, error) {
results := make(Snapshots, 0, 20) results := make(Snapshots, 0, 20)
err := repo.List(ctx, SnapshotFile, func(id ID, size int64) error { err := ForAllSnapshots(ctx, repo, nil, func(id ID, sn *Snapshot, err error) error {
sn, err := LoadSnapshot(ctx, repo, id)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not load snapshot %v: %v\n", id.Str(), err) fmt.Fprintf(os.Stderr, "could not load snapshot %v: %v\n", id.Str(), err)
return nil return nil