Files
kopia/cli/command_snapshot_fix_remove_files.go
Jarek Kowalski 9bf9cac7fb refactor(repository): ensure we always parse content.ID and object.ID (#1960)
* refactor(repository): ensure we always parse content.ID and object.ID

This changes the types to be incompatible with string to prevent direct
conversion to and from string.

This has the additional benefit of reducing number of memory allocations
and bytes for all IDs.

content.ID went from 2 allocations to 1:
   typical case 32 characters + 16 bytes per-string overhead
   worst-case 65 characters + 16 bytes per-string overhead
   now: 34 bytes

object.ID went from 2 allocations to 1:
   typical case 32 characters + 16 bytes per-string overhead
   worst-case 65 characters + 16 bytes per-string overhead
   now: 36 bytes

* move index.{ID,IDRange} methods to separate files

* replaced index.IDFromHash with content.IDFromHash externally

* minor tweaks and additional tests

* Update repo/content/index/id_test.go

Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>

* Update repo/content/index/id_test.go

Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>

* pr feedback

* post-merge fixes

* pr feedback

* pr feedback

* fixed subtle regression in sortedContents()

This was actually not producing invalid results because of how base36
works, just not sorting as efficiently as it could.

Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>
2022-05-25 14:15:56 +00:00

62 lines
1.6 KiB
Go

package cli
import (
"context"
"path"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/snapshot"
)
type commandSnapshotFixRemoveFiles struct {
common commonRewriteSnapshots
removeObjectIDs []string
removeFilesByName []string
}
func (c *commandSnapshotFixRemoveFiles) setup(svc appServices, parent commandParent) {
cmd := parent.Command("remove-files", "Remove references to the specified files from snapshots.")
c.common.setup(svc, cmd)
cmd.Flag("object-id", "Remove files by their object ID").StringsVar(&c.removeObjectIDs)
cmd.Flag("filename", "Remove files by filename (wildcards are supported)").StringsVar(&c.removeFilesByName)
cmd.Action(svc.repositoryWriterAction(c.run))
}
func (c *commandSnapshotFixRemoveFiles) rewriteEntry(ctx context.Context, dirRelativePath string, ent *snapshot.DirEntry) (*snapshot.DirEntry, error) {
for _, id := range c.removeObjectIDs {
if ent.ObjectID.String() == id {
log(ctx).Infof("will remove file %v", path.Join(dirRelativePath, ent.Name))
return nil, nil
}
}
for _, n := range c.removeFilesByName {
matched, err := path.Match(n, ent.Name)
if err != nil {
return nil, errors.Wrap(err, "invalid wildcard")
}
if matched {
log(ctx).Infof("will remove file %v", path.Join(dirRelativePath, ent.Name))
return nil, nil
}
}
return ent, nil
}
func (c *commandSnapshotFixRemoveFiles) run(ctx context.Context, rep repo.RepositoryWriter) error {
if len(c.removeObjectIDs)+len(c.removeFilesByName) == 0 {
return errors.Errorf("must specify files to remove")
}
return c.common.rewriteMatchingSnapshots(ctx, rep, c.rewriteEntry)
}