-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.go
More file actions
312 lines (273 loc) · 8.19 KB
/
scan.go
File metadata and controls
312 lines (273 loc) · 8.19 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package cacheevict
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
func applyScanDefaults(cfg *Config) {
if cfg.PrefixCount <= 0 {
cfg.PrefixCount = DefaultPrefixCount
}
}
// OpenIfExists opens the eviction handler if the persistent tracking file
// (.cache-sizes) already exists, or returns (nil, nil) if it does not.
// This is useful for cooperatively updating counters when the mmap tracking
// is active, without creating the file on caches that don't use it.
func OpenIfExists(cfg Config) (*Handler, error) {
path := filepath.Join(cfg.BaseDir, trackingFileName)
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, nil
}
return Open(cfg)
}
// Stats scans the cache directory and returns the total size and file count.
// This works without the mmap handler and is suitable for network caches.
//
// Required Config fields: BaseDir, SubdirPath, IsCachedFile.
// PrefixCount defaults to DefaultPrefixCount (65536) if not set.
// All other fields are ignored.
func Stats(cfg Config) (totalSize int64, totalFiles int64, err error) {
applyScanDefaults(&cfg)
if cfg.BaseDir == "" || cfg.SubdirPath == nil || cfg.IsCachedFile == nil {
return 0, 0, fmt.Errorf("cacheevict: Stats requires BaseDir, SubdirPath, and IsCachedFile")
}
err = walkCache(cfg, func(path string, size int64, mtime time.Time) {
totalSize += size
totalFiles++
})
return
}
// Clear removes all cached files from the cache directory. If the mmap
// tracking file exists, counters are updated via BeforeRemove. Empty
// leaf directories are removed (errors ignored for concurrent safety).
//
// Required Config fields: BaseDir, PrefixCount, SubdirPath, IsCachedFile.
// Optional: IsTempFile (to also clean stale temp files).
func Clear(cfg Config) error {
applyScanDefaults(&cfg)
if cfg.BaseDir == "" || cfg.SubdirPath == nil || cfg.IsCachedFile == nil {
return fmt.Errorf("cacheevict: Clear requires BaseDir, SubdirPath, and IsCachedFile")
}
h, err := OpenIfExists(cfg)
if err != nil {
return fmt.Errorf("cacheevict: open tracking file: %w", err)
}
if h != nil {
defer h.Close()
}
var dirs []string
err = walkCacheAll(cfg, func(path string, size int64, mtime time.Time, isCached bool) {
if h != nil && isCached {
h.BeforeRemove(path)
}
os.Remove(path)
}, func(dir string) {
dirs = append(dirs, dir)
})
// Remove empty directories bottom-up.
removeEmptyDirs(dirs)
return err
}
// Trim removes the oldest cached files until the cache is within the given
// limits. At least one of maxSize, maxFiles, or maxAge must be non-zero.
// If the mmap tracking file exists, counters are updated via BeforeRemove.
// Empty leaf directories are removed (errors ignored for concurrent safety).
//
// Required Config fields: BaseDir, PrefixCount, SubdirPath, IsCachedFile.
func Trim(cfg Config, maxSize, maxFiles int64, maxAge time.Duration) error {
applyScanDefaults(&cfg)
if cfg.BaseDir == "" || cfg.SubdirPath == nil || cfg.IsCachedFile == nil {
return fmt.Errorf("cacheevict: Trim requires BaseDir, SubdirPath, and IsCachedFile")
}
if maxSize <= 0 && maxFiles <= 0 && maxAge <= 0 {
return fmt.Errorf("cacheevict: Trim requires at least one of maxSize, maxFiles, or maxAge")
}
// Collect all cached files with metadata.
var files []fileEntry
var totalSize int64
if err := walkCache(cfg, func(path string, size int64, mtime time.Time) {
files = append(files, fileEntry{path: path, size: size, mtime: mtime})
totalSize += size
}); err != nil {
return err
}
// Sort by mtime ascending (oldest first).
sort.Slice(files, func(i, j int) bool {
return files[i].mtime.Before(files[j].mtime)
})
// Determine which files to delete.
cutoff := time.Now().Add(-maxAge)
totalFiles := int64(len(files))
var toDelete []string
for _, f := range files {
del := false
// Phase 1: age-based eviction.
if maxAge > 0 && f.mtime.Before(cutoff) {
del = true
}
// Phase 2: file-count-based eviction.
if !del && maxFiles > 0 && totalFiles > maxFiles {
del = true
}
// Phase 3: size-based eviction.
if !del && maxSize > 0 && totalSize > maxSize {
del = true
}
if del {
toDelete = append(toDelete, f.path)
totalSize -= f.size
totalFiles--
}
}
if len(toDelete) == 0 {
return nil
}
// Open tracking file if it exists.
h, err := OpenIfExists(cfg)
if err != nil {
return fmt.Errorf("cacheevict: open tracking file: %w", err)
}
if h != nil {
defer h.Close()
}
dirs := make(map[string]struct{})
for _, path := range toDelete {
if h != nil {
h.BeforeRemove(path)
}
os.Remove(path)
dirs[filepath.Dir(path)] = struct{}{}
}
// Remove empty directories.
dirList := make([]string, 0, len(dirs))
for d := range dirs {
dirList = append(dirList, d)
}
removeEmptyDirs(dirList)
return nil
}
// walkCache iterates all cached files in the cache directory, calling fn for
// each. It uses Readdirnames for intermediate directory levels for performance,
// and ReadDir + Info() only at the leaf level where file metadata is needed.
func walkCache(cfg Config, fn func(path string, size int64, mtime time.Time)) error {
return walkCacheAll(cfg, func(path string, size int64, mtime time.Time, isCached bool) {
if isCached {
fn(path, size, mtime)
}
}, nil)
}
// walkCacheAll iterates all files (cached and temp) in the cache directory.
// For each file, fn is called with isCached=true for cached files and false
// for temp files. If dirFn is non-nil, it is called for each leaf directory.
func walkCacheAll(cfg Config, fn func(path string, size int64, mtime time.Time, isCached bool), dirFn func(dir string)) error {
// Cache Readdirnames results for intermediate directories to avoid
// redundant syscalls. Key is the absolute directory path.
dirCache := make(map[string]map[string]struct{})
readdirnamesSet := func(dir string) map[string]struct{} {
if cached, ok := dirCache[dir]; ok {
return cached
}
f, err := os.Open(dir)
if err != nil {
dirCache[dir] = nil
return nil
}
names, err := f.Readdirnames(-1)
f.Close()
if err != nil {
dirCache[dir] = nil
return nil
}
set := make(map[string]struct{}, len(names))
for _, n := range names {
set[n] = struct{}{}
}
dirCache[dir] = set
return set
}
for idx := 0; idx < cfg.PrefixCount; idx++ {
relPath := cfg.SubdirPath(idx)
parts := strings.Split(relPath, string(filepath.Separator))
// Check intermediate levels using cached Readdirnames.
skip := false
dir := cfg.BaseDir
for _, part := range parts[:len(parts)-1] {
entries := readdirnamesSet(dir)
if entries == nil {
skip = true
break
}
if _, ok := entries[part]; !ok {
skip = true
break
}
dir = filepath.Join(dir, part)
}
if skip {
continue
}
// Check the last intermediate level for the leaf directory.
leafName := parts[len(parts)-1]
parentEntries := readdirnamesSet(dir)
if parentEntries == nil {
continue
}
if _, ok := parentEntries[leafName]; !ok {
continue
}
leafDir := filepath.Join(dir, leafName)
if dirFn != nil {
dirFn(leafDir)
}
entries, err := os.ReadDir(leafDir)
if err != nil {
continue
}
for _, e := range entries {
if e.IsDir() {
continue
}
name := e.Name()
isCached := cfg.IsCachedFile(name)
isTemp := cfg.IsTempFile != nil && cfg.IsTempFile(name)
if !isCached && !isTemp {
continue
}
info, err := e.Info()
if err != nil {
continue
}
fn(filepath.Join(leafDir, name), info.Size(), info.ModTime(), isCached)
}
}
return nil
}
// removeEmptyDirs attempts to remove the given directories and their parent
// directories if they become empty. Errors are silently ignored since
// another process may be adding files concurrently.
func removeEmptyDirs(dirs []string) {
// Sort longest first so we remove deepest dirs before their parents.
sort.Slice(dirs, func(i, j int) bool {
return len(dirs[i]) > len(dirs[j])
})
removed := make(map[string]struct{})
for _, d := range dirs {
if _, ok := removed[d]; ok {
continue
}
// Try to remove the directory (only succeeds if empty).
if err := os.Remove(d); err == nil {
removed[d] = struct{}{}
// Try parent too.
parent := filepath.Dir(d)
if _, ok := removed[parent]; !ok {
if os.Remove(parent) == nil {
removed[parent] = struct{}{}
}
}
}
}
}