rewrite: use SelectByName like in the backup command

This commit is contained in:
Michael Eischer 2022-09-09 22:33:12 +02:00
parent 7ebaf6e899
commit ad14d6e4ac
2 changed files with 9 additions and 7 deletions

View file

@ -81,14 +81,14 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
return false, err return false, err
} }
checkExclude := func(nodepath string) bool { selectByName := func(nodepath string) bool {
for _, reject := range rejectByNameFuncs { for _, reject := range rejectByNameFuncs {
if reject(nodepath) { if reject(nodepath) {
return true
}
}
return false return false
} }
}
return true
}
wg, wgCtx := errgroup.WithContext(ctx) wg, wgCtx := errgroup.WithContext(ctx)
repo.StartPackUploader(wgCtx, wg) repo.StartPackUploader(wgCtx, wg)
@ -96,7 +96,7 @@ func rewriteSnapshot(ctx context.Context, repo *repository.Repository, sn *resti
var filteredTree restic.ID var filteredTree restic.ID
wg.Go(func() error { wg.Go(func() error {
filteredTree, err = walker.FilterTree(wgCtx, repo, "/", *sn.Tree, &walker.TreeFilterVisitor{ filteredTree, err = walker.FilterTree(wgCtx, repo, "/", *sn.Tree, &walker.TreeFilterVisitor{
CheckExclude: checkExclude, SelectByName: selectByName,
PrintExclude: func(path string) { Verbosef(fmt.Sprintf("excluding %s\n", path)) }, PrintExclude: func(path string) { Verbosef(fmt.Sprintf("excluding %s\n", path)) },
}) })
if err != nil { if err != nil {

View file

@ -8,10 +8,12 @@ import (
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
) )
type RejectByNameFunc func(path string) bool // SelectByNameFunc returns true for all items that should be included (files and
// dirs). If false is returned, files are ignored and dirs are not even walked.
type SelectByNameFunc func(item string) bool
type TreeFilterVisitor struct { type TreeFilterVisitor struct {
CheckExclude RejectByNameFunc SelectByName SelectByNameFunc
PrintExclude func(string) PrintExclude func(string)
} }
@ -27,7 +29,7 @@ func FilterTree(ctx context.Context, repo restic.Repository, nodepath string, no
tb := restic.NewTreeJSONBuilder() tb := restic.NewTreeJSONBuilder()
for _, node := range curTree.Nodes { for _, node := range curTree.Nodes {
path := path.Join(nodepath, node.Name) path := path.Join(nodepath, node.Name)
if visitor.CheckExclude(path) { if !visitor.SelectByName(path) {
if visitor.PrintExclude != nil { if visitor.PrintExclude != nil {
visitor.PrintExclude(path) visitor.PrintExclude(path)
} }