From 7b9ae91e04f7d715a016df397f69c6d4444131f8 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 7 Nov 2021 22:33:44 +0100 Subject: [PATCH] copy: Load snapshots before indexes --- cmd/restic/cmd_check.go | 4 ++++ internal/checker/checker.go | 14 +++++++++++--- internal/checker/checker_test.go | 4 ++++ internal/checker/testing.go | 5 +++++ internal/repository/master_index_test.go | 5 +++++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index fa71bdba8..e7edff39e 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -211,6 +211,10 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { } chkr := checker.New(repo, opts.CheckUnused) + err = chkr.LoadSnapshots(gopts.ctx) + if err != nil { + return err + } Verbosef("load indexes\n") hints, errs := chkr.LoadIndex(gopts.ctx) diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 93e58cedc..8e49209f5 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -11,6 +11,7 @@ import ( "sync" "github.com/minio/sha256-simd" + "github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/hashing" @@ -35,6 +36,7 @@ type Checker struct { trackUnused bool masterIndex *repository.MasterIndex + snapshots restic.Lister repo restic.Repository } @@ -75,6 +77,12 @@ func (err ErrOldIndexFormat) Error() string { return fmt.Sprintf("index %v has old format", err.ID.Str()) } +func (c *Checker) LoadSnapshots(ctx context.Context) error { + var err error + c.snapshots, err = backend.MemorizeList(ctx, c.repo.Backend(), restic.SnapshotFile) + return err +} + // LoadIndex loads all index files. func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) { debug.Log("Start") @@ -278,8 +286,8 @@ func (c *Checker) checkTreeWorker(ctx context.Context, trees <-chan restic.TreeI } } -func loadSnapshotTreeIDs(ctx context.Context, repo restic.Repository) (ids restic.IDs, errs []error) { - err := restic.ForAllSnapshots(ctx, repo.Backend(), repo, nil, func(id restic.ID, sn *restic.Snapshot, err error) error { +func loadSnapshotTreeIDs(ctx context.Context, lister restic.Lister, repo restic.Repository) (ids restic.IDs, errs []error) { + err := restic.ForAllSnapshots(ctx, lister, repo, nil, func(id restic.ID, sn *restic.Snapshot, err error) error { if err != nil { errs = append(errs, err) return nil @@ -300,7 +308,7 @@ func loadSnapshotTreeIDs(ctx context.Context, repo restic.Repository) (ids resti // subtrees are available in the index. errChan is closed after all trees have // been traversed. func (c *Checker) Structure(ctx context.Context, p *progress.Counter, errChan chan<- error) { - trees, errs := loadSnapshotTreeIDs(ctx, c.repo) + trees, errs := loadSnapshotTreeIDs(ctx, c.snapshots, c.repo) p.SetMax(uint64(len(trees))) debug.Log("need to check %d trees from snapshots, %d errs returned", len(trees), len(errs)) diff --git a/internal/checker/checker_test.go b/internal/checker/checker_test.go index 948d5a480..1330211eb 100644 --- a/internal/checker/checker_test.go +++ b/internal/checker/checker_test.go @@ -44,6 +44,10 @@ func checkPacks(chkr *checker.Checker) []error { } func checkStruct(chkr *checker.Checker) []error { + err := chkr.LoadSnapshots(context.TODO()) + if err != nil { + return []error{err} + } return collectErrors(context.TODO(), func(ctx context.Context, errChan chan<- error) { chkr.Structure(ctx, nil, errChan) }) diff --git a/internal/checker/testing.go b/internal/checker/testing.go index d672911b1..0668406d8 100644 --- a/internal/checker/testing.go +++ b/internal/checker/testing.go @@ -20,6 +20,11 @@ func TestCheckRepo(t testing.TB, repo restic.Repository) { t.Fatalf("errors loading index: %v", hints) } + err := chkr.LoadSnapshots(context.TODO()) + if err != nil { + t.Error(err) + } + // packs errChan := make(chan error) go chkr.Packs(context.TODO(), errChan) diff --git a/internal/repository/master_index_test.go b/internal/repository/master_index_test.go index f1fe9af7e..2470dadfc 100644 --- a/internal/repository/master_index_test.go +++ b/internal/repository/master_index_test.go @@ -369,6 +369,11 @@ func TestIndexSave(t *testing.T) { } checker := checker.New(repo, false) + err = checker.LoadSnapshots(context.TODO()) + if err != nil { + t.Error(err) + } + hints, errs := checker.LoadIndex(context.TODO()) for _, h := range hints { t.Logf("hint: %v\n", h)