-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.go
454 lines (392 loc) · 10.1 KB
/
scanner.go
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/dustin/go-humanize"
)
type scanner struct {
Mutex struct {
// For removing filesystem files prior to linking/cloning, get read lock
// Termination of process requires write lock
Destructive sync.RWMutex
}
table *fileTable
options options
totals totals
}
func newScanner() *scanner {
s := &scanner{}
s.table = newFileTable(&s.options, &s.totals)
s.options.MustKeep.DefaultInclude = true
return s
}
// Don't display warnings for these dotfiles
var silentSkip = map[string]struct{}{
".DS_Store": {},
".DocumentRevisions-V100": {},
".Spotlight-V100": {},
".TemporaryItems": {},
".Trashes": {},
".fseventsd": {},
}
func (f *scanner) Scan(dirs ...string) (err error) {
if f.options.MatchMode == 0 {
return errors.New("MatchMode not specified in options")
}
if !f.options.Quiet {
f.table.termWidth, _ = terminalWidth()
}
f.totals.Start()
wd, err := os.Getwd()
if err != nil {
return err
}
if len(dirs) == 0 {
dirs = []string{wd}
}
for _, d := range dirs {
suffixes := map[string]string{}
if f.table.scanDir, err = filepath.Abs(d); err != nil {
return fmt.Errorf("unable to resolve \"%s\": %w", d, err)
}
if f.table.relDir, err = filepath.Rel(wd, f.table.scanDir); err != nil || len(f.table.relDir) >= len(f.table.scanDir) {
f.table.relDir = f.table.scanDir
}
if err = filepath.Walk(f.table.scanDir, func(path string, info os.FileInfo, inErr error) error {
pathSuffix := ""
if info != nil && !info.IsDir() {
dir := filepath.Dir(path)
suffix, ok := suffixes[dir]
if !ok {
suffix, err = filepath.Rel(f.table.scanDir, dir)
if err != nil {
suffix = dir
}
suffixes[dir] = suffix
}
pathSuffix = suffix
}
return f.walkFunc(path, pathSuffix, info, inErr)
}); err != nil {
return err
}
}
return nil
}
func (f *scanner) walkFunc(path, pathSuffix string, info os.FileInfo, inErr error) error {
if info == nil {
return fmt.Errorf("unable to stat: %s", path)
}
typ := info.Mode()
base := filepath.Base(path)
if base[0] == '.' || inErr != nil {
_, silent := silentSkip[base]
if !silent {
if inErr != nil {
fmt.Printf("%s: %s\n", path, inErr)
return nil
}
if f.options.Verbose {
fmt.Printf("%s: skipping dot-prefix\n", path)
}
}
if inErr == nil && typ.IsDir() {
return filepath.SkipDir
}
return nil
}
f.table.progress(path, true)
// Avoid hogging too many resources
time.Sleep(0)
if !f.options.Recursive && typ.IsDir() && path != f.table.scanDir {
return filepath.SkipDir
}
if typ&os.ModeSymlink != 0 {
if typ.IsDir() {
return filepath.SkipDir
}
return nil
}
current, err := f.execute(path, pathSuffix)
if err == nil {
fmt.Printf(" success\n")
f.totals.Processed.Add(current)
} else if err == noErrDryRun || err == fileIsSkipped {
if err == noErrDryRun {
fmt.Printf(" skipped\n")
}
f.totals.Skipped.Add(current)
} else if err != fileIsIgnored {
f.totals.Errors.Add(current)
if current != nil {
fmt.Printf(" %s: %s\n", current.RelPath, err)
} else {
fmt.Println(err)
}
}
return nil
}
var (
// Used as a special status for dry-runs
// Files skipped for other reasons should use fileIsSkipped
// Unlike fileIsSkipped, noErrDryRun displays the filepath along with "skipped"
noErrDryRun = errors.New("skipped")
)
// selectAndSwap returns true if current should be kept, replacing match,
// based on protection status and filename preferences. Returns an error
// if both records are protected.
func (f *scanner) selectAndSwap(current, match *fileRecord, m matchFlag) (swapMatch bool, err error) {
currentCanBeKept := current.SatisfiesKept(&f.options.MustKeep)
matchCanBeKept := match.SatisfiesKept(&f.options.MustKeep)
currentProtected := current.Protect(&f.options.Protect)
if currentProtected && f.options.Verbose {
fmt.Printf(" skip( %s ) protected\n", current.RelPath)
}
matchProtected := match.Protect(&f.options.Protect)
if matchProtected && f.options.Verbose {
fmt.Printf(" skip( %s ) protected\n", match.RelPath)
}
canSwap := !matchProtected && currentCanBeKept
canAvoidSwap := !currentProtected && matchCanBeKept
if canSwap != canAvoidSwap {
return canSwap, nil
}
if !canSwap && !canAvoidSwap {
return false, fileIsSkipped
}
if m.has(matchCopyName) && len(current.FoldedName) < len(match.FoldedName) {
if f.options.Verbose {
fmt.Printf(" keep-shortest( %s ) kept\n", current.RelPath)
}
return true, nil
}
if filepath.Dir(current.FilePath) == filepath.Dir(match.FilePath) && current.FoldedName < match.FoldedName {
if f.options.Verbose {
fmt.Printf(" keep-first-in-sort( %s ) kept\n", current.RelPath)
}
return true, nil
}
if f.options.TimestampBehavior != TimestampIgnore && f.options.TimestampBehavior != "" {
currentNewer := current.ModTime().After(match.ModTime())
wantNewer := f.options.TimestampBehavior == TimestampNewer
if currentNewer == wantNewer {
if f.options.Verbose {
fmt.Printf(" %s( %s ) kept\n", f.options.TimestampBehavior, current.RelPath)
}
return true, nil
}
}
return false, nil
}
func (f *scanner) execute(path, pathSuffix string) (current *fileRecord, err error) {
match, current, err := f.table.find(path, pathSuffix)
if current != nil {
f.totals.Files.Add(current)
}
m, ok := err.(matchFlag)
if !ok || m == fileIsIgnored || m == fileIsSkipped {
return current, err
}
if m == fileIsUnique || match == current || match.FilePath == current.FilePath {
f.totals.Unique.Add(current)
return current, fileIsIgnored
}
comparison := "=="
if m.has(matchHardlink) {
f.totals.Links.Add(current)
if f.options.IgnoreExistingLinks {
return current, fileIsIgnored
}
comparison = "<=>"
} else {
f.totals.Dupes.Add(current)
}
if f.options.Verbose || !current.Protect(&f.options.Protect) || !match.Protect(&f.options.Protect) {
fmt.Printf("%s %s %s (%s)\n", match.RelPath, comparison, current.RelPath, humanize.IBytes(uint64(current.Size())))
}
verb := f.options.Verb()
if verb == VerbNone {
return current, fileIsIgnored
}
swapMatch, err := f.selectAndSwap(current, match, m)
if err != nil {
return current, err
}
if swapMatch {
if match.Protect(&f.options.Protect) {
panic("attempted invalid swap")
}
f.table.db.remove(match)
f.table.db.insert(current)
match, current = current, match
}
f.Mutex.Destructive.RLock()
defer f.Mutex.Destructive.RUnlock()
// TODO handle uid and gid and perms
switch verb {
case VerbDelete:
fmt.Printf(" delete( %s )", current.RelPath)
if f.options.DryRun {
return current, noErrDryRun
}
err = os.Remove(current.FilePath)
if err == nil {
if m.has(matchHardlink) {
f.totals.Links.Remove(current)
} else {
f.totals.Dupes.Remove(current)
}
}
return current, err
case VerbClone, VerbMakeLinks, VerbSplitLinks:
x := "clone"
a := cloneFile
if verb == VerbMakeLinks {
if m.has(matchHardlink) {
return current, fileIsIgnored
}
x = "hardlink"
a = os.Link
} else if verb == VerbSplitLinks {
if !m.has(matchHardlink) && !f.options.CopyUnlinked {
return current, fileIsIgnored
}
x = "copy"
a = copyFile
}
fmt.Printf(" %s( %s => %s )", x, match.RelPath, current.RelPath)
if f.options.DryRun {
return current, noErrDryRun
}
for retry := 0; retry < 3; retry++ {
tmp, err := tempName(current)
if err != nil {
return current, err
}
if err = a(match.FilePath, tmp); err != nil {
if errors.Is(err, syscall.EEXIST) {
continue
}
os.Remove(tmp)
return current, fmt.Errorf("%s: %w", f.table.Rel(tmp), err)
}
if err = os.Rename(tmp, current.FilePath); err == nil {
switch verb {
case VerbMakeLinks:
f.totals.Dupes.Remove(current)
f.totals.Links.Add(match)
case VerbSplitLinks:
f.totals.Links.Remove(current)
f.totals.Dupes.Add(current)
case VerbClone:
if m.has(matchHardlink) {
f.totals.Links.Remove(current)
} else {
f.totals.Dupes.Remove(current)
}
f.totals.Cloned.Add(match)
}
}
return current, err
}
}
return current, fileIsIgnored
}
func tempName(r *fileRecord) (string, error) {
dir := filepath.Dir(r.FilePath)
if dir == "" {
dir = "."
}
f, err := ioutil.TempFile(dir, ".fdf-")
if err != nil {
return "", err
}
name := f.Name()
defer os.Remove(name)
defer f.Close()
return name, nil
}
func (f *scanner) Exit(code int) {
f.Mutex.Destructive.Lock()
os.Exit(code)
}
type totals struct {
Started time.Time
Files total
Unique total
Dupes total
Cloned total
Links total
Processed total
Skipped total
Errors total
}
type total struct {
count uint64
size uint64
}
func (t *totals) PrettyFormat(v verb) string {
lines := []string{
fmt.Sprintf("%s elapsed", t.End()),
}
for _, x := range []struct {
total
suffix string
}{
{t.Files, "scanned"},
{t.Unique, "unique"},
{t.Links, "as hardlinks"},
{t.Cloned, "as clones"},
{t.Dupes, "duplicated"},
{},
{t.Processed, fmt.Sprintf("%s successfully", v.PastTense())},
{t.Skipped, "skipped"},
{t.Errors, "had errors"},
} {
if x.count != 0 {
lines = append(lines, fmt.Sprintf("%s %s", x.String(), x.suffix))
} else if x.suffix == "" {
lines = append(lines, "")
}
}
if lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
return strings.Join(lines, "\n")
}
func (t *total) String() string {
count, size := t.Get()
return fmt.Sprintf("%d files (%s)", count, humanize.IBytes(size))
}
func (t *totals) Start() {
t.Started = time.Now()
}
func (t *totals) End() time.Duration {
return time.Since(t.Started)
}
func (t *total) Add(r *fileRecord) {
atomic.AddUint64(&t.count, 1)
if r != nil && r.Size() > 0 {
atomic.AddUint64(&t.size, uint64(r.Size()))
}
}
func (t *total) Remove(r *fileRecord) {
atomic.AddUint64(&t.count, uSubtract(1))
if r != nil && r.Size() > 0 {
atomic.AddUint64(&t.size, uSubtract(uint64(r.Size())))
}
}
func uSubtract(positive uint64) (negative uint64) {
return ^(positive - 1)
}
func (t *total) Get() (count, size uint64) {
return atomic.LoadUint64(&t.count), atomic.LoadUint64(&t.size)
}