-
Notifications
You must be signed in to change notification settings - Fork 340
/
command_block_gc.go
50 lines (39 loc) · 1.3 KB
/
command_block_gc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cli
import (
"context"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
)
var (
blockGarbageCollectCommand = blockCommands.Command("gc", "Garbage-collect unused storage blocks")
blockGarbageCollectCommandDelete = blockGarbageCollectCommand.Flag("delete", "Whether to delete unused block").String()
)
func runBlockGarbageCollectAction(ctx context.Context, rep *repo.Repository) error {
unused, err := rep.Blocks.FindUnreferencedBlobs(ctx)
if err != nil {
return errors.Wrap(err, "error looking for unreferenced blobs")
}
if len(unused) == 0 {
printStderr("No unused blocks found.\n")
return nil
}
if *blockGarbageCollectCommandDelete != "yes" {
var totalBytes int64
for _, u := range unused {
printStderr("unused %v (%v bytes)\n", u.BlobID, u.Length)
totalBytes += u.Length
}
printStderr("Would delete %v unused blocks (%v bytes), pass '--delete=yes' to actually delete.\n", len(unused), totalBytes)
return nil
}
for _, u := range unused {
printStderr("Deleting unused block %q (%v bytes)...\n", u.BlobID, u.Length)
if err := rep.Blobs.DeleteBlob(ctx, u.BlobID); err != nil {
return errors.Wrapf(err, "unable to delete block %q", u.BlobID)
}
}
return nil
}
func init() {
blockGarbageCollectCommand.Action(repositoryAction(runBlockGarbageCollectAction))
}