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)
}
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
for _, rejectFn := range excludePatternFns {
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
// unless the dir is selected for restore
selectedForRestore = !matched
childMayBeSelected = selectedForRestore && node.Type == "dir"
childMayBeSelected = selectedForRestore && isDir
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
childMayBeSelected = false
for _, includeFn := range includePatternFns {
@ -195,7 +195,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
break
}
}
childMayBeSelected = childMayBeSelected && node.Type == "dir"
childMayBeSelected = childMayBeSelected && isDir
return selectedForRestore, childMayBeSelected
}

View file

@ -27,7 +27,7 @@ type Restorer struct {
Error func(location string, err error) error
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 }
@ -97,7 +97,7 @@ func NewRestorer(repo restic.Repository, sn *restic.Snapshot, opts Options) *Res
opts: opts,
fileList: make(map[string]bool),
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,
}
@ -154,7 +154,7 @@ func (res *Restorer) traverseTree(ctx context.Context, target, location string,
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)
if selectedForRestore {

View file

@ -192,7 +192,7 @@ func TestRestorer(t *testing.T) {
Files map[string]string
ErrorsMust 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
{
@ -284,7 +284,7 @@ func TestRestorer(t *testing.T) {
Files: map[string]string{
"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 {
case filepath.FromSlash("/dir"):
childMayBeSelected = true
@ -370,7 +370,7 @@ func TestRestorer(t *testing.T) {
// make sure we're creating a new subdir of the tempdir
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)
if !fs.HasPathPrefix(tempdir, dstpath) {
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 {
return test.Select(item, dstpath, node)
return test.Select(item, dstpath, isDir)
}
return true, true
@ -570,7 +570,7 @@ func checkVisitOrder(list []TreeVisit) TraverseTreeCheck {
func TestRestorerTraverseTree(t *testing.T) {
var tests = []struct {
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
}{
{
@ -586,7 +586,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"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
},
Visitor: checkVisitOrder([]TreeVisit{
@ -613,7 +613,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"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" {
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" {
return true, false
}
@ -659,7 +659,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"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") {
return true, true
}
@ -688,7 +688,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"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 {
case "/dir":
return false, true
@ -788,7 +788,7 @@ func TestRestorerConsistentTimestampsAndPermissions(t *testing.T) {
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) {
case "/dir":
childMayBeSelected = true

View file

@ -13,7 +13,6 @@ import (
"time"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
restoreui "github.com/restic/restic/internal/ui/restore"
)
@ -34,10 +33,6 @@ func TestRestorerRestoreEmptyHardlinkedFields(t *testing.T) {
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)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@ -108,9 +103,6 @@ func testRestorerProgressBar(t *testing.T, dryRun bool) {
mock := &printerMock{}
progress := restoreui.NewProgress(mock, 0)
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)
ctx, cancel := context.WithCancel(context.Background())