-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiletable.go
348 lines (289 loc) · 9.04 KB
/
filetable.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/josephvusich/go-matchers"
)
type recordSet map[*fileRecord]struct{}
type fileTable struct {
// The directory passed to scanner.Scan
scanDir string
// scanDir relative to the startup working directory
relDir string
db *db
// 0 == quiet, -1 == error/not a terminal
termWidth int
options *options
totals *totals
pairs [][]string
namePairs [][]string
}
func newFileTable(o *options, t *totals) *fileTable {
return &fileTable{
db: newDB(),
options: o,
totals: t,
}
}
type checksum struct {
size int64
hash [ChecksumBlockSize]byte
}
type fileRecord struct {
// Absolute file path.
FilePath string
// File path relative to startup working directory.
RelPath string
// File path relative to the respective dir passed to scanner.Scan
PathSuffix string
// Lowercased filename for case-insensitive matching.
FoldedName string
// Lowercased parent directory basename.
FoldedParent string
os.FileInfo
HasChecksum bool
FailedChecksum error
Checksum checksum
// true/false indicates whether this file is protected from destructive operations.
// nil if protection status has not yet been determined.
protect *bool
satisfiesKept *bool
everMatchedContent bool
}
func foldName(filePath string) string {
return strings.ToLower(filepath.Base(filePath))
}
func (r *fileRecord) SatisfiesKept(k *matchers.RuleSet) bool {
if r.satisfiesKept == nil {
ok := k.Includes(r.FilePath)
r.satisfiesKept = &ok
}
return *r.satisfiesKept
}
// Note that `p` is ignored if there is already a cached result
func (r *fileRecord) Protect(p *matchers.RuleSet) bool {
if r.protect == nil {
ok := p.Includes(r.FilePath)
r.protect = &ok
}
return *r.protect
}
func (r *fileRecord) String() string {
return fmt.Sprintf("%s: %t %X", r.FilePath, r.HasChecksum, r.Checksum)
}
func newFileRecord(path string, info os.FileInfo, relPath string, pathSuffix string) *fileRecord {
return &fileRecord{
FilePath: path,
RelPath: relPath,
PathSuffix: pathSuffix,
FoldedName: foldName(path),
FoldedParent: foldName(filepath.Base(filepath.Dir(path))),
FileInfo: info,
}
}
// Rel returns absPath relative to the startup working directory,
// or absPath if filepath.Rel fails.
func (t *fileTable) Rel(absPath string) (rel string) {
rel, err := filepath.Rel(t.scanDir, absPath)
if err != nil {
return absPath
}
return filepath.Join(t.relDir, rel)
}
const truncFill = " ... "
func (t *fileTable) progress(s string, makeRelPath bool) {
if t.termWidth <= 0 {
return
}
if makeRelPath {
s = t.Rel(s)
}
if t.termWidth > len(truncFill)+2 && len(s) >= t.termWidth {
chunkSize := (t.termWidth - len(truncFill) - 1) >> 1
s = s[:chunkSize] + truncFill + s[len(s)-chunkSize:]
}
fmt.Printf("\033[2K%s\r", s)
}
type matchFlag uint
const (
matchNothing matchFlag = 0b0000000000000000 // default value, usually replaced with matchContent
matchName matchFlag = 0b0000000000000001 // case-insensitive
matchSize matchFlag = 0b0000000000000010 // implied by matchContent and matchHardlink
matchContent = 0b0000000000000100 | matchSize // implied by matchHardlink
matchHardlink = 0b0000000000001000 | matchContent // used by --copy and for categorization
matchCopyName = 0b0000000000010000 // one filename must contain the other, e.g., "foo" and "foo copy (1)"
matchParent = 0b0000000000100000 // parent directory name (folded)
matchPathSuffix = 0b0000000001000000 | matchParent // path relative to the directory passed to scanner.Scan
matchNameSuffix = 0b0000000010000000 // one filename must end with the other, e.g., "foo-fizz-buzz" and "fizz-buzz"
matchNamePrefix = 0b0000000100000000 // one filename must begin with the other, e.g., "foo-fizz-buzz" and "foo-fizz"
fileIsUnique matchFlag = 0b0010000000000000 // no match found
fileIsSkipped matchFlag = 0b0100000000000000 // file was excluded e.g., due to size requirements
fileIsIgnored matchFlag = 0b1000000000000000 // status returned for directories
)
func (m matchFlag) has(flag matchFlag) bool {
return m&flag == flag
}
func (m matchFlag) Error() string {
return fmt.Sprintf("MatchType<0b%b>", m)
}
func (t *fileTable) find(f, pathSuffix string) (match *fileRecord, current *fileRecord, err error) {
if t.options.Exclude.Includes(f) {
return nil, nil, fileIsIgnored
}
st, err := os.Stat(f)
if err != nil {
return nil, nil, err
}
match, current, err = t.findStat(f, st, pathSuffix)
if matchFlag, ok := err.(matchFlag); ok {
if matchFlag.has(matchContent) {
t.pairs = append(t.pairs, []string{
current.FilePath,
match.FilePath,
})
}
if matchFlag.has(matchName) {
t.namePairs = append(t.namePairs, []string{
current.FilePath,
match.FilePath,
})
}
}
return match, current, err
}
func (t *fileTable) findStat(f string, st os.FileInfo, pathSuffix string) (match *fileRecord, current *fileRecord, err error) {
if st.IsDir() {
return nil, nil, fileIsIgnored
}
if st.Size() < t.options.MinSize() {
return nil, nil, fileIsSkipped
}
current = newFileRecord(f, st, t.Rel(f), pathSuffix)
q := &query{}
if t.options.MatchMode.has(matchName) {
current.byName(q)
}
if t.options.MatchMode.has(matchParent) {
current.byParent(q)
}
if t.options.MatchMode.has(matchPathSuffix) {
current.byPathSuffix(q)
}
if t.options.MatchMode.has(matchSize) {
current.bySize(q)
}
// Ignore checksums for now, as hardlinks can match content without the overhead of comparison
// Query for any known files that match all desired fields (except content/checksum)
candidates := t.db.query(q)
// If the current file is protected, filter for unprotected candidates
if current.Protect(&t.options.Protect) {
filtered := recordSet{}
for other := range candidates {
if !other.Protect(&t.options.Protect) {
filtered[other] = struct{}{}
}
}
candidates = filtered
}
// If copyname mode is active, filter down the candidate list
if t.options.MatchMode.has(matchCopyName) {
filtered := recordSet{}
for other := range candidates {
if isCopyName(current.FoldedName, other.FoldedName) {
filtered[other] = struct{}{}
}
}
candidates = filtered
}
if t.options.MatchMode.has(matchNameSuffix) {
filtered := recordSet{}
for other := range candidates {
if isNameSuffix(current.FoldedName, other.FoldedName) {
filtered[other] = struct{}{}
}
}
candidates = filtered
}
if t.options.MatchMode.has(matchNamePrefix) {
filtered := recordSet{}
for other := range candidates {
if isNamePrefix(current.FoldedName, other.FoldedName) {
filtered[other] = struct{}{}
}
}
candidates = filtered
}
if len(t.options.Comparers) != 0 {
filtered := recordSet{}
for other := range candidates {
allMatch := true
for _, c := range t.options.Comparers {
if !c.AreEqual(current, other) {
allMatch = false
break
}
}
if allMatch {
filtered[other] = struct{}{}
}
}
candidates = filtered
}
// If there is a matching hardlink, skip further checking
// Name is the only non-hardlink-included field
for other := range candidates {
if areHardlinked(current, other) {
return other, current, t.options.MatchMode | matchHardlink
}
}
// --copy is not interested in non-hardlinks
if t.options.MatchMode.has(matchHardlink) {
t.db.insert(current)
return current, current, fileIsUnique
}
// If matching content is not important, return random valid match, if any
if !t.options.MatchMode.has(matchContent) {
for other := range candidates {
return other, current, t.options.MatchMode
}
}
// If we get here, we're matching content and no hardlink was found
// First we check any existing checksum matches for full equality
if current.HasChecksum {
current.byChecksum(q)
existingChecksums := t.db.query(q)
for other := range existingChecksums {
if equalFiles(current, other, t.options) {
return other, current, t.options.MatchMode
}
}
}
if other := t.checkCandidates(current, candidates); other != nil {
return other, current, t.options.MatchMode
}
t.db.insert(current)
return current, current, fileIsUnique
}
func (t *fileTable) checkCandidates(current *fileRecord, candidates recordSet) (other *fileRecord) {
// If there were no checksum matches, we need to look at any otherwise-matching files with no checksum yet
if len(candidates) == 0 {
return nil
}
if err := t.Checksum(current, false); err != nil {
// We might still find a hardlink match later, even without deep comparison
return nil
}
// Already-checksummed files will have been found and eliminated via the index already
// We only want to consider files that have not yet been checksummed
for other := range candidates {
if err := t.Checksum(other, true); err != nil {
continue
}
if other.Checksum == current.Checksum && equalFiles(current, other, t.options) {
return other
}
}
return nil
}