make filter Match on info instead of 9001 args

This commit is contained in:
Lilian Jónsdóttir 2024-07-15 20:15:20 -07:00
parent 20f6dc92bc
commit 15bd040deb
4 changed files with 20 additions and 8 deletions

View file

@ -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,

View file

@ -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,

View file

@ -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() {

View file

@ -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)
}
})