diff options
Diffstat (limited to 'internal/scan')
| -rw-r--r-- | internal/scan/scan.go | 12 | ||||
| -rw-r--r-- | internal/scan/scan_test.go | 21 |
2 files changed, 32 insertions, 1 deletions
diff --git a/internal/scan/scan.go b/internal/scan/scan.go index 77cca66..bd717e0 100644 --- a/internal/scan/scan.go +++ b/internal/scan/scan.go @@ -229,7 +229,7 @@ func (w *walker) walk(dir, relDir string, depth int, entries []os.DirEntry) erro w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: Busy}) continue } - if w.opt.Now.Sub(info.ModTime()) < w.opt.MinAge { + if Age(w.opt.Now, info.ModTime()) < w.opt.MinAge { w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: TooNew}) continue } @@ -252,3 +252,13 @@ func (w *walker) isBusy(name string, names map[string]bool) bool { } return false } + +// Age is how long ago mtime was, at now. A modification time ahead of the +// clock (a skewed server, an archive's timestamps) counts as brand new: age +// 0, so min-age 0 still considers the file (review cli F5). +func Age(now, mtime time.Time) time.Duration { + if a := now.Sub(mtime); a > 0 { + return a + } + return 0 +} diff --git a/internal/scan/scan_test.go b/internal/scan/scan_test.go index e9aeb73..35e7a0d 100644 --- a/internal/scan/scan_test.go +++ b/internal/scan/scan_test.go @@ -319,3 +319,24 @@ func TestFilesCarryInode(t *testing.T) { t.Errorf("rename changed the identity: %+v then %+v", first.Files[0], second.Files) } } + +// TestFutureFileIsNotTooNewAtMinAgeZero: a file whose modification time is +// ahead of the clock counts as brand new - skipped while min-age is above +// zero, considered at min-age 0 (review cli F5). +func TestFutureFileIsNotTooNewAtMinAgeZero(t *testing.T) { + root := tree(t) + p := filepath.Join(root, "future.txt") + if err := os.WriteFile(p, []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + ahead := now.Add(time.Hour) + if err := os.Chtimes(p, ahead, ahead); err != nil { + t.Fatal(err) + } + if r, _ := Walk(root, Options{Now: now}); len(rels(r)) != 1 { + t.Errorf("min-age 0: files %v, skipped %v; want future.txt considered", rels(r), skipped(r)) + } + if r, _ := Walk(root, Options{Now: now, MinAge: time.Minute}); skipped(r)["future.txt"] != TooNew { + t.Errorf("min-age 1m: skipped %v; want future.txt too new", skipped(r)) + } +} |
