summaryrefslogtreecommitdiff
path: root/internal/scan/scan.go
blob: bd717e0c1ed92c696ce8c0e54de6fd1eb587cc2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// SPDX-License-Identifier: GPL-3.0-or-later

// Package scan walks a directory tree and reports the files krino will
// consider sorting, and why the rest were skipped.
package scan

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"sort"
	"time"

	"krino/internal/ignore"
)

// File is a regular file found by Walk.
type File struct {
	Path    string // absolute, cleaned
	Rel     string // slash-separated, relative to the root
	Name    string // base name
	Size    int64
	ModTime time.Time
	Mode    fs.FileMode

	// Dev and Ino identify the file on its filesystem; both are 0 where the
	// platform reports neither.
	Dev, Ino uint64
}

// NewFile builds the File for path, at rel under the root, from its Lstat
// info.
func NewFile(path, rel string, info fs.FileInfo) File {
	dev, ino := fileID(info)
	return File{
		Path:    path,
		Rel:     rel,
		Name:    filepath.Base(path),
		Size:    info.Size(),
		ModTime: info.ModTime(),
		Mode:    info.Mode(),
		Dev:     dev,
		Ino:     ino,
	}
}

// Reason is why an entry was not returned as a File.
type Reason int

const (
	Ignored    Reason = iota // matched an ignore pattern
	Busy                     // a sibling NAME<busy-suffix> exists
	TooNew                   // modified less than MinAge ago
	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.
func (r Reason) String() string {
	switch r {
	case Ignored:
		return "ignored"
	case Busy:
		return "busy"
	case TooNew:
		return "too new"
	case Symlink:
		return "symlink"
	case NotRegular:
		return "not a regular file"
	case Unreadable:
		return "unreadable"
	case TooBig:
		return "too big"
	default:
		return fmt.Sprintf("Reason(%d)", int(r))
	}
}

// Skipped is one entry Walk did not return as a File, and why.
type Skipped struct {
	Rel    string
	Reason Reason
}

// Options controls how Walk traverses a directory.
type Options struct {
	Recursive bool
	MaxDepth  int             // 0: unlimited; 1: the root's own entries only
	Ignore    *ignore.Matcher // nil: nothing ignored
	Exclude   []string        // absolute directories never entered
	Busy      []string        // suffixes, e.g. ".part"
	MinAge    time.Duration
	MaxSize   int64 // bytes; 0: no limit
	Now       time.Time
}

// Result is everything Walk found under a root.
type Result struct {
	Files   []File    // sorted by Rel
	Skipped []Skipped // sorted by Rel
}

// Walk lists the files under root that krino will consider, and reports why
// the rest were skipped. root must exist, be a directory, and be readable;
// an unreadable subdirectory found during the walk is reported as
// Unreadable and does not abort the scan.
func Walk(root string, opt Options) (*Result, error) {
	root, err := filepath.Abs(root)
	if err != nil {
		return nil, err
	}
	info, err := os.Stat(root)
	if err != nil {
		return nil, err
	}
	if !info.IsDir() {
		return nil, fmt.Errorf("%s is not a directory", root)
	}
	entries, err := os.ReadDir(root)
	if err != nil {
		return nil, err
	}

	exclude := make(map[string]bool, len(opt.Exclude))
	for _, e := range opt.Exclude {
		exclude[filepath.Clean(e)] = true
	}

	w := &walker{root: root, opt: opt, exclude: exclude}
	if err := w.walk(root, "", 1, entries); err != nil {
		return nil, err
	}

	sort.Slice(w.result.Files, func(i, j int) bool { return w.result.Files[i].Rel < w.result.Files[j].Rel })
	sort.Slice(w.result.Skipped, func(i, j int) bool { return w.result.Skipped[i].Rel < w.result.Skipped[j].Rel })
	return &w.result, nil
}

// walker accumulates the Result across recursive calls.
type walker struct {
	opt     Options
	root    string
	exclude map[string]bool
	result  Result
}

// walk applies the skip checks to entries, the already-read contents of dir
// (at relDir relative to the root, dir's own entries at depth). A
// subdirectory it recurses into is read here, right before recursing, so a
// ReadDir failure on it can be reported as Unreadable and skipped without
// aborting the rest of the walk; only a failure reading dir itself (passed
// in by the caller) would need to propagate, and only Walk's own read of
// the root works that way.
func (w *walker) walk(dir, relDir string, depth int, entries []os.DirEntry) error {
	// The set of names in this directory, built once, for the busy check.
	names := make(map[string]bool, len(entries))
	for _, e := range entries {
		names[e.Name()] = true
	}

	for _, e := range entries {
		name := e.Name()
		rel := name
		if relDir != "" {
			rel = relDir + "/" + name
		}
		path := filepath.Join(dir, name)

		// A symlink is never followed, whatever it points to; DirEntry's
		// Type is Lstat-like and does not resolve it.
		if e.Type()&fs.ModeSymlink != 0 {
			w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: Symlink})
			continue
		}

		if e.IsDir() {
			// Directories themselves are not reported, with two
			// exceptions: the symlink case above, and an ignored
			// directory (C2) — reported once for the directory itself,
			// not for each file inside it, since pruning it without
			// descending is the whole point; without this, its contents
			// would appear in no count and in no -v listing at all.
			if !w.opt.Recursive {
				continue
			}
			if w.opt.Ignore != nil && w.opt.Ignore.Match(rel, true) {
				w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: Ignored})
				continue
			}
			if w.exclude[filepath.Clean(path)] {
				continue
			}
			if w.opt.MaxDepth > 0 && depth+1 > w.opt.MaxDepth {
				continue
			}
			subEntries, err := os.ReadDir(path)
			if err != nil {
				w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: Unreadable})
				continue
			}
			if err := w.walk(path, rel, depth+1, subEntries); err != nil {
				return err
			}
			continue
		}

		info, err := e.Info()
		if err != nil {
			if errors.Is(err, fs.ErrNotExist) {
				continue // vanished mid-walk (a download finishing, say): silently skipped
			}
			w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: Unreadable})
			continue
		}
		if !info.Mode().IsRegular() {
			w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: NotRegular})
			continue
		}
		if w.opt.Ignore != nil && w.opt.Ignore.Match(rel, false) {
			w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: Ignored})
			continue
		}
		if busy := w.isBusy(name, names); busy {
			w.result.Skipped = append(w.result.Skipped, Skipped{Rel: rel, Reason: Busy})
			continue
		}
		if Age(w.opt.Now, info.ModTime()) < w.opt.MinAge {
			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, NewFile(path, rel, info))
	}
	return nil
}

// isBusy reports whether name+suffix, for any configured Busy suffix, is
// among names — the sibling of an in-progress download.
func (w *walker) isBusy(name string, names map[string]bool) bool {
	for _, suffix := range w.opt.Busy {
		if names[name+suffix] {
			return true
		}
	}
	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
}