diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-14 14:08:36 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-14 14:08:36 +0200 |
| commit | 1d3f2d1e4c59867024470d3444e12698b7ebb22e (patch) | |
| tree | 4fa14f455c72f9b206a680dcc1b0f4c1f3708158 /internal/scan | |
| parent | 07c24054cab965800983ef40f53a05c2db131ede (diff) | |
| download | krino-0.0.4.tar.gz krino-0.0.4.zip | |
0.0.4: max-size, exclude forms, --min-agev0.0.4
Diffstat (limited to 'internal/scan')
| -rw-r--r-- | internal/scan/scan.go | 8 | ||||
| -rw-r--r-- | internal/scan/scan_test.go | 32 |
2 files changed, 40 insertions, 0 deletions
diff --git a/internal/scan/scan.go b/internal/scan/scan.go index e87b741..7fe9e92 100644 --- a/internal/scan/scan.go +++ b/internal/scan/scan.go @@ -36,6 +36,7 @@ const ( Symlink // a symbolic link (never followed) NotRegular // fifo, socket, device Unreadable // a directory that could not be read + TooBig // larger than MaxSize ) // String names a Reason the way it should read in a report. @@ -53,6 +54,8 @@ func (r Reason) String() string { return "not a regular file" case Unreadable: return "unreadable" + case TooBig: + return "too big" default: return fmt.Sprintf("Reason(%d)", int(r)) } @@ -72,6 +75,7 @@ type Options struct { Exclude []string // absolute directories never entered Busy []string // suffixes, e.g. ".part" MinAge time.Duration + MaxSize int64 // bytes; 0: no limit Now time.Time } @@ -209,6 +213,10 @@ func (w *walker) walk(dir, relDir string, depth int, entries []os.DirEntry) erro w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: TooNew}) continue } + if w.opt.MaxSize > 0 && info.Size() > w.opt.MaxSize { + w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: TooBig}) + continue + } w.result.Files = append(w.result.Files, File{ Path: path, Rel: rel, diff --git a/internal/scan/scan_test.go b/internal/scan/scan_test.go index 94f922b..a245893 100644 --- a/internal/scan/scan_test.go +++ b/internal/scan/scan_test.go @@ -261,3 +261,35 @@ func TestIgnoredDirectoryReportedOnce(t *testing.T) { t.Fatalf("skipped = %v, want %v (the directory once, not its three files)", skipped(r), want) } } + +// TestTooBig: with MaxSize set, a file larger than it is skipped as too +// big; a file of exactly MaxSize is kept; MaxSize 0 means no limit. +func TestTooBig(t *testing.T) { + root := tree(t) + for name, size := range map[string]int{"small.bin": 10, "exact.bin": 100, "large.bin": 101} { + p := filepath.Join(root, name) + if err := os.WriteFile(p, make([]byte, size), 0o644); err != nil { + t.Fatal(err) + } + old := now.Add(-time.Hour) + if err := os.Chtimes(p, old, old); err != nil { + t.Fatal(err) + } + } + r, err := Walk(root, Options{MaxSize: 100, Now: now}) + if err != nil { + t.Fatal(err) + } + if want := []string{"exact.bin", "small.bin"}; !reflect.DeepEqual(rels(r), want) { + t.Fatalf("files = %v, want %v", rels(r), want) + } + if got := skipped(r); !reflect.DeepEqual(got, map[string]Reason{"large.bin": TooBig}) { + t.Fatalf("skipped = %v", got) + } + if TooBig.String() != "too big" { + t.Errorf("TooBig reads %q", TooBig.String()) + } + if r, _ := Walk(root, Options{Now: now}); len(rels(r)) != 3 { + t.Errorf("MaxSize 0 skipped files: %v", skipped(r)) + } +} |
