Files
kopia/cli/command_content_verify.go
Jarek Kowalski c8fcae93aa logging: refactored logging
This is mostly mechanical and changes how loggers are instantiated.

Logger is now associated with a context, passed around all methods,
(most methods had ctx, but had to add it in a few missing places).

By default Kopia does not produce any logs, but it can be overridden,
either locally for a nested context, by calling

ctx = logging.WithLogger(ctx, newLoggerFunc)

To override logs globally, call logging.SetDefaultLogger(newLoggerFunc)

This refactoring allowed removing dependency from Kopia repo
and go-logging library (the CLI still uses it, though).

It is now also possible to have all test methods emit logs using
t.Logf() so that they show up in failure reports, which should make
debugging of test failures suck less.
2020-02-25 17:24:44 -08:00

71 lines
1.6 KiB
Go

package cli
import (
"context"
"sync/atomic"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
)
var (
contentVerifyCommand = contentCommands.Command("verify", "Verify contents")
contentVerifyIDs = contentVerifyCommand.Arg("id", "IDs of blocks to show (or 'all')").Required().Strings()
contentVerifyParallel = contentVerifyCommand.Flag("parallel", "Parallelism").Int()
)
func runContentVerifyCommand(ctx context.Context, rep *repo.Repository) error {
for _, contentID := range toContentIDs(*contentVerifyIDs) {
if contentID == "all" {
return verifyAllContents(ctx, rep)
}
if err := contentVerify(ctx, rep, contentID); err != nil {
return err
}
}
return nil
}
func verifyAllContents(ctx context.Context, rep *repo.Repository) error {
var errorCount int32
err := rep.Content.IterateContents(ctx, content.IterateOptions{
Parallel: *contentVerifyParallel,
}, func(ci content.Info) error {
if err := contentVerify(ctx, rep, ci.ID); err != nil {
atomic.AddInt32(&errorCount, 1)
}
return nil
})
if err != nil {
return errors.Wrap(err, "iterate contents")
}
if errorCount == 0 {
return nil
}
return errors.Errorf("encountered %v errors", errorCount)
}
func contentVerify(ctx context.Context, r *repo.Repository, contentID content.ID) error {
if _, err := r.Content.GetContent(ctx, contentID); err != nil {
log(ctx).Warningf("content %v is invalid: %v", contentID, err)
return err
}
log(ctx).Infof("content %v is ok", contentID)
return nil
}
func init() {
contentVerifyCommand.Action(repositoryAction(runContentVerifyCommand))
}