From e2b007244138140ce8cdeaf22740fc134d8ddeed Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 6 Dec 2020 00:07:45 +0100 Subject: [PATCH] check: add progress bar to the tree structure check --- cmd/restic/cmd_check.go | 6 +++++- internal/checker/checker.go | 5 +++-- internal/checker/checker_test.go | 4 +++- internal/checker/testing.go | 2 +- internal/repository/master_index_test.go | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/restic/cmd_check.go b/cmd/restic/cmd_check.go index 774879490..c1a6e5464 100644 --- a/cmd/restic/cmd_check.go +++ b/cmd/restic/cmd_check.go @@ -240,7 +240,11 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error { Verbosef("check snapshots, trees and blobs\n") errChan = make(chan error) - go chkr.Structure(gopts.ctx, errChan) + go func() { + bar := newProgressMax(!gopts.Quiet, 0, "snapshots") + defer bar.Done() + chkr.Structure(gopts.ctx, bar, errChan) + }() for err := range errChan { errorsFound = true diff --git a/internal/checker/checker.go b/internal/checker/checker.go index e41c1f1b5..1ed470e99 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -354,8 +354,9 @@ func loadSnapshotTreeIDs(ctx context.Context, repo restic.Repository) (ids resti // Structure checks that for all snapshots all referenced data blobs and // subtrees are available in the index. errChan is closed after all trees have // been traversed. -func (c *Checker) Structure(ctx context.Context, errChan chan<- error) { +func (c *Checker) Structure(ctx context.Context, p *progress.Counter, errChan chan<- error) { trees, errs := loadSnapshotTreeIDs(ctx, c.repo) + p.SetMax(uint64(len(trees))) debug.Log("need to check %d trees from snapshots, %d errs returned", len(trees), len(errs)) for _, err := range errs { @@ -376,7 +377,7 @@ func (c *Checker) Structure(ctx context.Context, errChan chan<- error) { c.blobRefs.M.Insert(h) c.blobRefs.Unlock() return blobReferenced - }, nil) + }, p) defer close(errChan) for i := 0; i < defaultParallelism; i++ { diff --git a/internal/checker/checker_test.go b/internal/checker/checker_test.go index f8efd05e8..ad1b15f1a 100644 --- a/internal/checker/checker_test.go +++ b/internal/checker/checker_test.go @@ -43,7 +43,9 @@ func checkPacks(chkr *checker.Checker) []error { } func checkStruct(chkr *checker.Checker) []error { - return collectErrors(context.TODO(), chkr.Structure) + return collectErrors(context.TODO(), func(ctx context.Context, errChan chan<- error) { + chkr.Structure(ctx, nil, errChan) + }) } func checkData(chkr *checker.Checker) []error { diff --git a/internal/checker/testing.go b/internal/checker/testing.go index 6c5be84e2..d672911b1 100644 --- a/internal/checker/testing.go +++ b/internal/checker/testing.go @@ -30,7 +30,7 @@ func TestCheckRepo(t testing.TB, repo restic.Repository) { // structure errChan = make(chan error) - go chkr.Structure(context.TODO(), errChan) + go chkr.Structure(context.TODO(), nil, errChan) for err := range errChan { t.Error(err) diff --git a/internal/repository/master_index_test.go b/internal/repository/master_index_test.go index 9ccf0e59e..3c279696e 100644 --- a/internal/repository/master_index_test.go +++ b/internal/repository/master_index_test.go @@ -368,7 +368,7 @@ func TestIndexSave(t *testing.T) { defer cancel() errCh := make(chan error) - go checker.Structure(ctx, errCh) + go checker.Structure(ctx, nil, errCh) i := 0 for err := range errCh { t.Errorf("checker returned error: %v", err)