From 15bd040debc5dd2e81990a8682620da77e9eb777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Mon, 15 Jul 2024 20:15:20 -0700 Subject: [PATCH] make filter Match on info instead of 9001 args --- internal/files/disk.go | 4 ++-- internal/files/trash.go | 2 +- internal/filter/filter.go | 9 ++++++--- internal/filter/filter_test.go | 13 +++++++++++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/files/disk.go b/internal/files/disk.go index c771008..3ea6eb4 100644 --- a/internal/files/disk.go +++ b/internal/files/disk.go @@ -125,7 +125,7 @@ func walk_dir(dir string, f *filter.Filter) (files Files) { name := d.Name() info, _ := d.Info() - if f.Match(name, info.ModTime(), info.Size(), info.IsDir()) { + if f.Match(info) { log.Debugf("found matching file: %s %s", name, info.ModTime()) i, err := os.Stat(p) if err != nil { @@ -170,7 +170,7 @@ func read_dir(dir string, f *filter.Filter) (files Files) { path := filepath.Dir(filepath.Join(dir, name)) - if f.Match(name, info.ModTime(), info.Size(), info.IsDir()) { + if f.Match(info) { log.Debugf("found matching file: %s %s", name, info.ModTime()) files = append(files, DiskFile{ name: name, diff --git a/internal/files/trash.go b/internal/files/trash.go index a72fdb6..b3aea3a 100644 --- a/internal/files/trash.go +++ b/internal/files/trash.go @@ -95,7 +95,7 @@ func FindTrash(trashdir, ogdir string, f *filter.Filter) (files Files, outerr er return nil } - if f.Match(filename, date, info.Size(), info.IsDir()) { + if f.Match(info) { log.Debugf("%s: deleted on %s", filename, date.Format(trash_info_date_fmt)) files = append(files, TrashInfo{ name: filename, diff --git a/internal/filter/filter.go b/internal/filter/filter.go index ecf50bc..8d7540c 100644 --- a/internal/filter/filter.go +++ b/internal/filter/filter.go @@ -3,6 +3,7 @@ package filter import ( "fmt" + "io/fs" "path/filepath" "regexp" "slices" @@ -49,9 +50,11 @@ func (f *Filter) AddFileNames(filenames ...string) { } } -func (f *Filter) Match(filename string, modified time.Time, size int64, isdir bool) bool { - // this might be unnessary but w/e - filename = filepath.Clean(filename) +func (f *Filter) Match(info fs.FileInfo) bool { + filename := info.Name() + modified := info.ModTime() + isdir := info.IsDir() + size := info.Size() // on or before/after, not both if !f.on.IsZero() { diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go index d2bfd56..38a85a8 100644 --- a/internal/filter/filter_test.go +++ b/internal/filter/filter_test.go @@ -2,6 +2,7 @@ package filter import ( "fmt" + "io/fs" "math" "testing" "time" @@ -47,8 +48,16 @@ type singletest struct { isdir bool modified time.Time size int64 + mode fs.FileMode } +func (s singletest) Name() string { return s.filename } +func (s singletest) Size() int64 { return s.size } +func (s singletest) Mode() fs.FileMode { return s.mode } +func (s singletest) ModTime() time.Time { return s.modified } +func (s singletest) IsDir() bool { return s.isdir } +func (s singletest) Sys() any { return nil } + func (s singletest) String() string { return fmt.Sprintf("filename:'%s' modified:'%s' size:'%d' isdir:'%t'", s.filename, s.modified, s.size, s.isdir) } @@ -72,7 +81,7 @@ func testmatch(t *testing.T, testers []testholder) { for _, tst := range tester.good { t.Run(fmt.Sprintf(testnamefmt+"_good", tst.filename, tst.modified), func(t *testing.T) { - if !f.Match(tst.filename, tst.modified, tst.size, tst.isdir) { + if !f.Match(tst) { t.Fatalf("(%s) didn't match (%s) but should have", tst, tester) } }) @@ -80,7 +89,7 @@ func testmatch(t *testing.T, testers []testholder) { for _, tst := range tester.bad { t.Run(fmt.Sprintf(testnamefmt+"_bad", tst.filename, tst.modified), func(t *testing.T) { - if f.Match(tst.filename, tst.modified, tst.size, tst.isdir) { + if f.Match(tst) { t.Fatalf("(%s) matched (%s) but shouldn't have", tst, tester) } })