restore: remove unused parameter from SelectFilter

This commit is contained in:
Michael Eischer 2024-06-29 19:23:09 +02:00
parent ac44bdf6dd
commit 013a6156bd
3 changed files with 21 additions and 27 deletions

View file

@ -164,7 +164,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, isDir bool) (selectedForRestore bool, childMayBeSelected bool) { selectExcludeFilter := func(item 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)
@ -186,7 +186,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
return selectedForRestore, childMayBeSelected return selectedForRestore, childMayBeSelected
} }
selectIncludeFilter := func(item string, _ string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) { selectIncludeFilter := func(item string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
selectedForRestore = false selectedForRestore = false
childMayBeSelected = false childMayBeSelected = false
for _, includeFn := range includePatternFns { for _, includeFn := range includePatternFns {

View file

@ -29,7 +29,7 @@ type Restorer struct {
Warn func(message string) Warn func(message string)
// SelectFilter determines whether the item is selectedForRestore or whether a childMayBeSelected. // SelectFilter determines whether the item is selectedForRestore or whether a childMayBeSelected.
// selectedForRestore must not depend on isDir as `removeUnexpectedFiles` always passes false to isDir. // selectedForRestore must not depend on isDir as `removeUnexpectedFiles` always passes false to isDir.
SelectFilter func(item string, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) SelectFilter func(item 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 }
@ -100,7 +100,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, bool) (bool, bool) { return true, true }, SelectFilter: func(string, bool) (bool, bool) { return true, true },
sn: sn, sn: sn,
} }
@ -200,7 +200,7 @@ func (res *Restorer) traverseTreeInner(ctx context.Context, target, location str
continue continue
} }
selectedForRestore, childMayBeSelected := res.SelectFilter(nodeLocation, nodeTarget, node.Type == "dir") selectedForRestore, childMayBeSelected := res.SelectFilter(nodeLocation, 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 {
@ -496,7 +496,7 @@ func (res *Restorer) removeUnexpectedFiles(target, location string, expectedFile
} }
// TODO pass a proper value to the isDir parameter once this becomes relevant for the filters // TODO pass a proper value to the isDir parameter once this becomes relevant for the filters
selectedForRestore, _ := res.SelectFilter(nodeLocation, nodeTarget, false) selectedForRestore, _ := res.SelectFilter(nodeLocation, false)
// only delete files that were selected for restore // only delete files that were selected for restore
if selectedForRestore { if selectedForRestore {
if !res.opts.DryRun { if !res.opts.DryRun {

View file

@ -193,7 +193,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, isDir bool) (selectForRestore bool, childMayBeSelected bool) Select func(item string, isDir bool) (selectForRestore bool, childMayBeSelected bool)
}{ }{
// valid test cases // valid test cases
{ {
@ -285,7 +285,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, isDir bool) (selectedForRestore bool, childMayBeSelected bool) { Select: func(item string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
switch item { switch item {
case filepath.FromSlash("/dir"): case filepath.FromSlash("/dir"):
childMayBeSelected = true childMayBeSelected = true
@ -371,16 +371,10 @@ 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, isDir bool) (selectedForRestore bool, childMayBeSelected bool) { res.SelectFilter = func(item string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
t.Logf("restore %v to %v", item, dstpath) t.Logf("restore %v", item)
if !fs.HasPathPrefix(tempdir, dstpath) {
t.Errorf("would restore %v to %v, which is not within the target dir %v",
item, dstpath, tempdir)
return false, false
}
if test.Select != nil { if test.Select != nil {
return test.Select(item, dstpath, isDir) return test.Select(item, isDir)
} }
return true, true return true, true
@ -582,7 +576,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, isDir bool) (selectForRestore bool, childMayBeSelected bool) Select func(item string, isDir bool) (selectForRestore bool, childMayBeSelected bool)
Visitor TraverseTreeCheck Visitor TraverseTreeCheck
}{ }{
{ {
@ -598,7 +592,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"foo": File{Data: "content: foo\n"}, "foo": File{Data: "content: foo\n"},
}, },
}, },
Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
return true, true return true, true
}, },
Visitor: checkVisitOrder([]TreeVisit{ Visitor: checkVisitOrder([]TreeVisit{
@ -627,7 +621,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"foo": File{Data: "content: foo\n"}, "foo": File{Data: "content: foo\n"},
}, },
}, },
Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
if item == "/foo" { if item == "/foo" {
return true, false return true, false
} }
@ -651,7 +645,7 @@ func TestRestorerTraverseTree(t *testing.T) {
}}, }},
}, },
}, },
Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
if item == "/aaa" { if item == "/aaa" {
return true, false return true, false
} }
@ -677,7 +671,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"foo": File{Data: "content: foo\n"}, "foo": File{Data: "content: foo\n"},
}, },
}, },
Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
if strings.HasPrefix(item, "/dir") { if strings.HasPrefix(item, "/dir") {
return true, true return true, true
} }
@ -708,7 +702,7 @@ func TestRestorerTraverseTree(t *testing.T) {
"foo": File{Data: "content: foo\n"}, "foo": File{Data: "content: foo\n"},
}, },
}, },
Select: func(item string, dstpath string, isDir bool) (selectForRestore bool, childMayBeSelected bool) { Select: func(item string, isDir bool) (selectForRestore bool, childMayBeSelected bool) {
switch item { switch item {
case "/dir": case "/dir":
return false, true return false, true
@ -811,7 +805,7 @@ func TestRestorerConsistentTimestampsAndPermissions(t *testing.T) {
res := NewRestorer(repo, sn, Options{}) res := NewRestorer(repo, sn, Options{})
res.SelectFilter = func(item string, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) { res.SelectFilter = func(item string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
switch filepath.ToSlash(item) { switch filepath.ToSlash(item) {
case "/dir": case "/dir":
childMayBeSelected = true childMayBeSelected = true
@ -1256,7 +1250,7 @@ func TestRestoreDelete(t *testing.T) {
}, noopGetGenericAttributes) }, noopGetGenericAttributes)
tests := []struct { tests := []struct {
selectFilter func(item string, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) selectFilter func(item string, isDir bool) (selectedForRestore bool, childMayBeSelected bool)
fileState map[string]bool fileState map[string]bool
}{ }{
{ {
@ -1271,7 +1265,7 @@ func TestRestoreDelete(t *testing.T) {
}, },
}, },
{ {
selectFilter: func(item, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) { selectFilter: func(item string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
return false, false return false, false
}, },
fileState: map[string]bool{ fileState: map[string]bool{
@ -1284,7 +1278,7 @@ func TestRestoreDelete(t *testing.T) {
}, },
}, },
{ {
selectFilter: func(item, dstpath string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) { selectFilter: func(item string, isDir bool) (selectedForRestore bool, childMayBeSelected bool) {
switch item { switch item {
case filepath.FromSlash("/dir"): case filepath.FromSlash("/dir"):
selectedForRestore = true selectedForRestore = true