mirror of
https://github.com/kopia/kopia.git
synced 2026-07-11 22:26:11 -04:00
* fix(cli): only run automatic maintenance after data-modifying operations Automatic maintenance previously ran after any successful repository-writer command, including control-only operations such as `policy set`, `acl ...` and `user ...`. Make repositoryWriterAction skip maintenance by default and add repositoryWriterActionWithMaintenance, used only by data-modifying commands (snapshot create/delete/expire/migrate/fix-*/copy-move-history/pin, manifest delete). Fixes #3174 * fix(cli): treat snapshot pin and manifest delete as control-only Per review, move both to repositoryWriterAction so they no longer trigger opportunistic automatic maintenance. pin orphans no content and manifest delete is a low-level operation not meant for direct use; any content it orphans is reclaimed at the next scheduled maintenance. --------- Co-authored-by: max <max@example.com>
67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/blob"
|
|
"github.com/kopia/kopia/snapshot"
|
|
"github.com/kopia/kopia/snapshot/snapshotfs"
|
|
)
|
|
|
|
type commandSnapshotFixInvalidFiles struct {
|
|
common commonRewriteSnapshots
|
|
|
|
verifyFilesPercent float64
|
|
verifier *snapshotfs.Verifier
|
|
|
|
invalidFileHandling string
|
|
|
|
failedFileCallback snapshotfs.RewriteFailedEntryCallback
|
|
}
|
|
|
|
func (c *commandSnapshotFixInvalidFiles) setup(svc appServices, parent commandParent) {
|
|
cmd := parent.Command("invalid-files", "Remove references to any invalid (unreadable) files from snapshots.")
|
|
c.common.setup(svc, cmd)
|
|
|
|
cmd.Flag("invalid-file-handling", "How to handle invalid files").Default(invalidEntryStub).EnumVar(&c.invalidFileHandling, invalidEntryFail, invalidEntryStub, invalidEntryKeep, invalidEntryRemove)
|
|
cmd.Flag("verify-files-percent", "Verify a percentage of files by fully downloading them [0.0 .. 100.0]").Default("0").Float64Var(&c.verifyFilesPercent)
|
|
|
|
cmd.Action(svc.repositoryWriterActionWithMaintenance(c.run))
|
|
}
|
|
|
|
func (c *commandSnapshotFixInvalidFiles) rewriteEntry(ctx context.Context, pathFromRoot string, ent *snapshot.DirEntry) (*snapshot.DirEntry, error) {
|
|
if ent.Type != snapshot.EntryTypeDirectory {
|
|
if err := c.verifier.VerifyFile(ctx, ent.ObjectID, pathFromRoot, ent.FileSize); err != nil {
|
|
log(ctx).Warnf("removing invalid file %v due to: %v", pathFromRoot, err)
|
|
|
|
return c.failedFileCallback(ctx, pathFromRoot, ent, err)
|
|
}
|
|
}
|
|
|
|
return ent, nil
|
|
}
|
|
|
|
func (c *commandSnapshotFixInvalidFiles) run(ctx context.Context, rep repo.RepositoryWriter) error {
|
|
c.failedFileCallback = failedEntryCallback(rep, c.invalidFileHandling)
|
|
|
|
opts := snapshotfs.VerifierOptions{
|
|
VerifyFilesPercent: c.verifyFilesPercent,
|
|
}
|
|
|
|
if dr, ok := rep.(repo.DirectRepository); ok {
|
|
blobMap, err := blob.ReadBlobMap(ctx, dr.BlobReader())
|
|
if err != nil {
|
|
return errors.Wrap(err, "unable to read blob map")
|
|
}
|
|
|
|
opts.BlobMap = blobMap
|
|
}
|
|
|
|
c.verifier = snapshotfs.NewVerifier(ctx, rep, opts)
|
|
|
|
return c.common.rewriteMatchingSnapshots(ctx, rep, c.rewriteEntry)
|
|
}
|