Files
kopia/cli/command_blob_gc.go
T
John Costa b16302dca9 fix(cli): reject negative values for --parallel flags (#5484)
The --parallel flag was declared with kingpin .IntVar on 12 commands,
which accepted negative values and led to a panic at runtime (e.g.
`snapshot migrate --parallel=-1`).

Declare the --parallel family as uint so kingpin rejects negative input
at parse time.
The uint->int conversion the callee APIs need is done through a small
helper .

Add CLI test that asserts the rejection message (a bare failure check passed
even without the fix since the commands also fail with no repository connected).
Parse via ParseFloat like kingpin's built-in int flag so the only behavior
change is rejecting negatives.
2026-09-09 20:07:17 -07:00

53 lines
1.3 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/repo/maintenance"
)
type commandBlobGC struct {
delete string
parallel uint
prefix string
safety maintenance.SafetyParameters
svc appServices
}
func (c *commandBlobGC) setup(svc appServices, parent commandParent) {
cmd := parent.Command("gc", "Garbage-collect unused blobs").Hidden()
cmd.Flag("delete", "Whether to delete unused blobs").StringVar(&c.delete)
cmd.Flag("parallel", "Number of parallel blob scans").Default("16").UintVar(&c.parallel)
cmd.Flag("prefix", "Only GC blobs with given prefix").StringVar(&c.prefix)
safetyFlagVar(cmd, &c.safety)
cmd.Action(svc.directRepositoryWriteAction(c.run))
c.svc = svc
}
func (c *commandBlobGC) run(ctx context.Context, rep repo.DirectRepositoryWriter) error {
c.svc.dangerousCommand()
opts := maintenance.DeleteUnreferencedPacksOptions{
DryRun: c.delete != "yes",
Parallel: parallelismAsInt(c.parallel),
Prefix: blob.ID(c.prefix),
}
stats, err := maintenance.DeleteUnreferencedPacks(ctx, rep, opts, c.safety)
if err != nil {
return errors.Wrap(err, "error deleting unreferenced blobs")
}
if opts.DryRun && stats.UnreferencedPackCount > 0 {
log(ctx).Info("Pass --delete=yes to delete.")
}
return nil
}