restore: simplfy selectFilter arguments

This commit is contained in:
Michael Eischer 2024-06-29 17:53:47 +02:00
parent a9a60f77ce
commit d762f4ee64
4 changed files with 18 additions and 26 deletions

View file

@ -161,7 +161,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
msg.E("Warning: %s\n", message) msg.E("Warning: %s\n", message)
} }
selectExcludeFilter := func(item string, _ string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { selectExcludeFilter := func(item string, _ string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
matched := false matched := false
for _, rejectFn := range excludePatternFns { for _, rejectFn := range excludePatternFns {
matched = matched || rejectFn(item) matched = matched || rejectFn(item)
@ -178,12 +178,12 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
// therefore childMayMatch does not matter, but we should not go down // therefore childMayMatch does not matter, but we should not go down
// unless the dir is selected for restore // unless the dir is selected for restore
selectedForRestore = !matched selectedForRestore = !matched
childMayBeSelected = selectedForRestore && node.Type == "dir" childMayBeSelected = selectedForRestore && isDir
return selectedForRestore, childMayBeSelected return selectedForRestore, childMayBeSelected
} }
selectIncludeFilter := func(item string, _ string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { selectIncludeFilter := func(item string, _ string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
selectedForRestore = false selectedForRestore = false
childMayBeSelected = false childMayBeSelected = false
for _, includeFn := range includePatternFns { for _, includeFn := range includePatternFns {
@ -195,7 +195,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
break break
} }
} }
childMayBeSelected = childMayBeSelected && node.Type == "dir" childMayBeSelected = childMayBeSelected && isDir
return selectedForRestore, childMayBeSelected return selectedForRestore, childMayBeSelected
} }

View file

@ -27,7 +27,7 @@ type Restorer struct {
Error func(location string, err error) error Error func(location string, err error) error
Warn func(message string) Warn func(message string)
SelectFilter func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) SelectFilter func(item string, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool)
} }
var restorerAbortOnAllErrors = func(_ string, err error) error { return err } var restorerAbortOnAllErrors = func(_ string, err error) error { return err }
@ -97,7 +97,7 @@ func NewRestorer(repo restic.Repository, sn *restic.Snapshot, opts Options) *Res
opts: opts, opts: opts,
fileList: make(map[string]bool), fileList: make(map[string]bool),
Error: restorerAbortOnAllErrors, Error: restorerAbortOnAllErrors,
SelectFilter: func(string, string, *restic.Node) (bool, bool) { return true, true }, SelectFilter: func(string, string, bool) (bool, bool) { return true, true },
sn: sn, sn: sn,
} }
@ -154,7 +154,7 @@ func (res *Restorer) traverseTree(ctx context.Context, target, location string,
continue continue
} }
selectedForRestore, childMayBeSelected := res.SelectFilter(nodeLocation, nodeTarget, node) selectedForRestore, childMayBeSelected := res.SelectFilter(nodeLocation, nodeTarget, node.Type == "dir")
debug.Log("SelectFilter returned %v %v for %q", selectedForRestore, childMayBeSelected, nodeLocation) debug.Log("SelectFilter returned %v %v for %q", selectedForRestore, childMayBeSelected, nodeLocation)
if selectedForRestore { if selectedForRestore {

View file

@ -192,7 +192,7 @@ func TestRestorer(t *testing.T) {
Files map[string]string Files map[string]string
ErrorsMust map[string]map[string]struct{} ErrorsMust map[string]map[string]struct{}
ErrorsMay map[string]map[string]struct{} ErrorsMay map[string]map[string]struct{}
Select func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) Select func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool)
}{ }{
// valid test cases // valid test cases
{ {
@ -284,7 +284,7 @@ func TestRestorer(t *testing.T) {
Files: map[string]string{ Files: map[string]string{
"dir/file": "content: file\n", "dir/file": "content: file\n",
}, },
Select: func(item, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { Select: func(item, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
switch item { switch item {
case filepath.FromSlash("/dir"): case filepath.FromSlash("/dir"):
childMayBeSelected = true childMayBeSelected = true
@ -370,7 +370,7 @@ func TestRestorer(t *testing.T) {
// make sure we're creating a new subdir of the tempdir // make sure we're creating a new subdir of the tempdir
tempdir = filepath.Join(tempdir, "target") tempdir = filepath.Join(tempdir, "target")
res.SelectFilter = func(item, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { res.SelectFilter = func(item, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
t.Logf("restore %v to %v", item, dstpath) t.Logf("restore %v to %v", item, dstpath)
if !fs.HasPathPrefix(tempdir, dstpath) { if !fs.HasPathPrefix(tempdir, dstpath) {
t.Errorf("would restore %v to %v, which is not within the target dir %v", t.Errorf("would restore %v to %v, which is not within the target dir %v",
@ -379,7 +379,7 @@ func TestRestorer(t *testing.T) {
} }
if test.Select != nil { if test.Select != nil {
return test.Select(item, dstpath, node) return test.Select(item, dstpath, isDir)
} }
return true, true return true, true
@ -570,7 +570,7 @@ func checkVisitOrder(list []TreeVisit) TraverseTreeCheck {
func TestRestorerTraverseTree(t *testing.T) { func TestRestorerTraverseTree(t *testing.T) {
var tests = []struct { var tests = []struct {
Snapshot Snapshot
Select func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) Select func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool)
Visitor TraverseTreeCheck Visitor TraverseTreeCheck
}{ }{
{ {
@ -586,7 +586,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"foo": File{Data: "content: foo\n"}, "foo": File{Data: "content: foo\n"},
}, },
}, },
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
return true, true return true, true
}, },
Visitor: checkVisitOrder([]TreeVisit{ Visitor: checkVisitOrder([]TreeVisit{
@ -613,7 +613,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"foo": File{Data: "content: foo\n"}, "foo": File{Data: "content: foo\n"},
}, },
}, },
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
if item == "/foo" { if item == "/foo" {
return true, false return true, false
} }
@ -635,7 +635,7 @@ func TestRestorerTraverseTree(t *testing.T) {
}}, }},
}, },
}, },
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
if item == "/aaa" { if item == "/aaa" {
return true, false return true, false
} }
@ -659,7 +659,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"foo": File{Data: "content: foo\n"}, "foo": File{Data: "content: foo\n"},
}, },
}, },
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
if strings.HasPrefix(item, "/dir") { if strings.HasPrefix(item, "/dir") {
return true, true return true, true
} }
@ -688,7 +688,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"foo": File{Data: "content: foo\n"}, "foo": File{Data: "content: foo\n"},
}, },
}, },
Select: func(item string, dstpath string, node *restic.Node) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
switch item { switch item {
case "/dir": case "/dir":
return false, true return false, true
@ -788,7 +788,7 @@ func TestRestorerConsistentTimestampsAndPermissions(t *testing.T) {
res := NewRestorer(repo, sn, Options{}) res := NewRestorer(repo, sn, Options{})
res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { res.SelectFilter = func(item string, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
switch filepath.ToSlash(item) { switch filepath.ToSlash(item) {
case "/dir": case "/dir":
childMayBeSelected = true childMayBeSelected = true

View file

@ -13,7 +13,6 @@ import (
"time" "time"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
restoreui "github.com/restic/restic/internal/ui/restore" restoreui "github.com/restic/restic/internal/ui/restore"
) )
@ -34,10 +33,6 @@ func TestRestorerRestoreEmptyHardlinkedFields(t *testing.T) {
res := NewRestorer(repo, sn, Options{}) res := NewRestorer(repo, sn, Options{})
res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
return true, true
}
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -108,9 +103,6 @@ func testRestorerProgressBar(t *testing.T, dryRun bool) {
mock := &printerMock{} mock := &printerMock{}
progress := restoreui.NewProgress(mock, 0) progress := restoreui.NewProgress(mock, 0)
res := NewRestorer(repo, sn, Options{Progress: progress, DryRun: dryRun}) res := NewRestorer(repo, sn, Options{Progress: progress, DryRun: dryRun})
res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
return true, true
}
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())