2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-04 20:37:49 +00:00

Refactor prune and rebuild_index

Factor out and reuse `rebuildIndex()` in cmd_rebuild_index and cmd_prune.

Use contexts.
This commit is contained in:
Pauline Middelink 2017-03-08 20:30:52 +01:00
parent 8a92687d9a
commit 8a05de537f
2 changed files with 19 additions and 41 deletions

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"os"
"restic" "restic"
"restic/debug" "restic/debug"
"restic/errors" "restic/errors"
@ -81,8 +81,8 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
return err return err
} }
done := make(chan struct{}) ctx, cancel := context.WithCancel(gopts.ctx)
defer close(done) defer cancel()
var stats struct { var stats struct {
blobs int blobs int
@ -92,7 +92,7 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
} }
Verbosef("counting files in repo\n") Verbosef("counting files in repo\n")
for _ = range repo.List(restic.DataFile, done) { for _ = range repo.List(restic.DataFile, ctx.Done()) {
stats.packs++ stats.packs++
} }
@ -238,35 +238,10 @@ func pruneRepository(gopts GlobalOptions, repo restic.Repository) error {
bar.Done() bar.Done()
} }
Verbosef("creating new index\n") if err = rebuildIndex(ctx, repo); err != nil {
stats.packs = 0
for _ = range repo.List(restic.DataFile, done) {
stats.packs++
}
bar = newProgressMax(!gopts.Quiet, uint64(stats.packs), "packs")
idx, err = index.New(repo, bar)
if err != nil {
return err return err
} }
var supersedes restic.IDs
for idxID := range repo.List(restic.IndexFile, done) {
h := restic.Handle{Type: restic.IndexFile, Name: idxID.String()}
err := repo.Backend().Remove(h)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to remove index %v: %v\n", idxID.Str(), err)
}
supersedes = append(supersedes, idxID)
}
id, err := idx.Save(repo, supersedes)
if err != nil {
return err
}
Verbosef("saved new index as %v\n", id.Str())
Verbosef("done\n") Verbosef("done\n")
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"restic" "restic"
"restic/index" "restic/index"
@ -35,25 +36,29 @@ func runRebuildIndex(gopts GlobalOptions) error {
return err return err
} }
done := make(chan struct{}) ctx, cancel := context.WithCancel(gopts.ctx)
defer close(done) defer cancel()
return rebuildIndex(ctx, repo)
}
func rebuildIndex(ctx context.Context, repo restic.Repository) error {
Verbosef("counting files in repo\n") Verbosef("counting files in repo\n")
var packs uint64 var packs uint64
for _ = range repo.List(restic.DataFile, done) { for _ = range repo.List(restic.DataFile, ctx.Done()) {
packs++ packs++
} }
bar := newProgressMax(!gopts.Quiet, packs, "packs") bar := newProgressMax(!globalOptions.Quiet, packs, "packs")
idx, err := index.New(repo, bar) idx, err := index.New(repo, bar)
if err != nil { if err != nil {
return err return err
} }
Verbosef("listing old index files\n") Verbosef("finding old index files\n")
var supersedes restic.IDs var supersedes restic.IDs
for id := range repo.List(restic.IndexFile, done) { for id := range repo.List(restic.IndexFile, ctx.Done()) {
supersedes = append(supersedes, id) supersedes = append(supersedes, id)
} }
@ -67,13 +72,11 @@ func runRebuildIndex(gopts GlobalOptions) error {
Verbosef("remove %d old index files\n", len(supersedes)) Verbosef("remove %d old index files\n", len(supersedes))
for _, id := range supersedes { for _, id := range supersedes {
err := repo.Backend().Remove(restic.Handle{ if err := repo.Backend().Remove(restic.Handle{
Type: restic.IndexFile, Type: restic.IndexFile,
Name: id.String(), Name: id.String(),
}) }); err != nil {
Warnf("error removing old index %v: %v\n", id.Str(), err)
if err != nil {
Warnf("error deleting old index %v: %v\n", id.Str(), err)
} }
} }