-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
494 lines (458 loc) · 17 KB
/
Copy pathmain.go
File metadata and controls
494 lines (458 loc) · 17 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"time"
"github.com/photosphere/fast-file-diff-go/lib"
"github.com/spf13/cobra"
)
const (
ExitSuccess = 0
ExitUsage = 1
ExitFatal = 2
ExitNonFatal = 3
)
// log is the global logger (set at startup by lib); all code in this package uses it.
var log = lib.Log
// runSummary holds the counts and timings needed to display the run summary.
type runSummary struct {
leftDir string
rightDir string
totalCompared int
leftOnlyCount int
rightOnlyCount int
differentCount int
sameCount int
startTime time.Time
scanDuration time.Duration
compareDuration time.Duration
numWorkers int
workerUtilizationPercent int
}
// displaySummary writes the run summary to the logger (always) and to stderr when printToStderr is true.
func displaySummary(printToStderr bool, s runSummary) {
elapsed := time.Since(s.startTime)
avgPerComparison := time.Duration(0)
if s.totalCompared > 0 {
avgPerComparison = elapsed / time.Duration(s.totalCompared)
}
lines := []string{
"",
"Summary:",
fmt.Sprintf(" Left directory: %s", s.leftDir),
fmt.Sprintf(" Right directory: %s", s.rightDir),
fmt.Sprintf(" Total files compared: %d", s.totalCompared),
fmt.Sprintf(" Files only on left: %d", s.leftOnlyCount),
fmt.Sprintf(" Files only on right: %d", s.rightOnlyCount),
fmt.Sprintf(" Files different: %d", s.differentCount),
fmt.Sprintf(" Files same: %d", s.sameCount),
fmt.Sprintf(" Scanning: %s", s.scanDuration.Round(time.Millisecond)),
fmt.Sprintf(" Comparing: %s", s.compareDuration.Round(time.Millisecond)),
fmt.Sprintf(" Total time: %s", elapsed.Round(time.Millisecond)),
fmt.Sprintf(" Average per comparison: %s", avgPerComparison.Round(time.Microsecond)),
fmt.Sprintf(" Workers: %d", s.numWorkers),
fmt.Sprintf(" Workers utilized: %d%%", s.workerUtilizationPercent),
}
for _, line := range lines {
if line == "" {
log.Write("")
if printToStderr {
fmt.Fprintln(os.Stderr)
}
} else {
log.Write(line)
if printToStderr {
fmt.Fprintln(os.Stderr, line)
}
}
}
log.Write("")
if printToStderr {
fmt.Fprintln(os.Stderr)
}
}
// Runs the CLI; on any error exits with ExitUsage so scripts get a consistent exit code.
func main() {
defer log.Close()
if err := rootCmd.Execute(); err != nil {
os.Exit(ExitUsage)
}
}
// Version is set at build time via -ldflags "-X main.Version=..."; empty means "dev".
var Version string
// Hold flag values so runRoot can read them without passing through cobra.
var dirBatchSize int
var numWorkers int
var hashAlg string
var hashThreshold int
var outputFormat string
var quiet bool
var full bool
var showSame bool
// Single top-level command; requireZeroOrTwoArgs validates args, runRoot does the diff.
var rootCmd = &cobra.Command{
Use: "ffd <left-dir> <right-dir>",
Short: "Fast file diff between two directory trees",
Long: "Compare two directory trees recursively. Left dir and right dir are required positional arguments.",
Args: cobra.MatchAll(cobra.ArbitraryArgs, requireZeroOrTwoArgs),
RunE: runRoot,
}
// Binds flags to the package-level vars; defaults match the spec (e.g. xxhash, 10MiB threshold).
func init() {
if Version == "" {
Version = "dev"
}
rootCmd.Version = Version
rootCmd.SetVersionTemplate("{{.Version}}\n")
rootCmd.Flags().IntVar(&dirBatchSize, "dir-batch-size", 4096, "Batch size for directory reads (entries per syscall)")
rootCmd.Flags().IntVar(&numWorkers, "workers", runtime.NumCPU(), "Number of worker goroutines for comparing file pairs")
rootCmd.Flags().StringVar(&hashAlg, "hash", "xxhash", "Hash algorithm for content comparison: xxhash, sha256, md5")
rootCmd.Flags().IntVar(&hashThreshold, "threshold", 10*1024*1024, "Size threshold in bytes: files smaller are read in full to hash, larger are streamed")
rootCmd.Flags().StringVar(&outputFormat, "format", "text", "Output format: text, table, json, yaml")
rootCmd.Flags().BoolVar(&quiet, "quiet", false, "Suppress progress and final error-log message (for scripting)")
rootCmd.Flags().BoolVar(&full, "full", false, "Always hash file contents for every pair (ignore same size+mtime shortcut)")
rootCmd.Flags().BoolVar(&showSame, "show-same", false, "In the log file, include the full list of identical (same) files")
rootCmd.AddCommand(lsCmd)
rootCmd.AddCommand(versionCmd)
}
// lsCmd lists all files under a directory recursively (one relative path per line). Uses the same walk code as the diff.
var lsCmd = &cobra.Command{
Use: "ls [directory]",
Short: "List files in a directory recursively",
Long: "Walk the given directory and print the relative path of every file (one per line). Uses the same walk implementation as the diff.",
Args: cobra.ExactArgs(1),
RunE: runLs,
}
// versionCmd prints the version number to stdout and exits (script-friendly).
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number",
Long: "Print the version number of this build to stdout. Use --version for the same from the root command.",
Args: cobra.NoArgs,
RunE: runVersion,
}
// runVersion writes the version string to stdout; used by the version command.
func runVersion(cmd *cobra.Command, args []string) error {
fmt.Println(Version)
return nil
}
// Enforces 0 args (for --help) or 2 args (left and right dir); used as cobra's Args so users get a clear error.
func requireZeroOrTwoArgs(cmd *cobra.Command, args []string) error {
if len(args) == 0 || len(args) == 2 {
return nil
}
return fmt.Errorf("requires 0 or 2 arguments, got %d", len(args))
}
// runLs discovers files under the given directory and prints each relative path to stdout (one per line) as they are found.
func runLs(cmd *cobra.Command, args []string) error {
root := args[0]
if err := lib.EnsureDir(root); err != nil {
return fmt.Errorf("not a directory: %w", err)
}
start := time.Now()
fileCh := make(chan lib.DiscoveredFile, 256)
doneCh := make(chan struct{})
var count atomic.Int32
go func() {
for file := range fileCh {
fmt.Fprintln(cmd.OutOrStdout(), filepath.ToSlash(file.Rel))
count.Add(1)
}
close(doneCh)
}()
util := lib.NewWorkerUtilization(numWorkers, 30)
go lib.Discover([]lib.DirJob{{Root: root, RelDir: "", Side: lib.SideLeft}}, fileCh, dirBatchSize, numWorkers, util)
<-doneCh
elapsed := time.Since(start)
fmt.Fprintf(cmd.ErrOrStderr(), "Listed %d files in %v\n", count.Load(), elapsed.Round(time.Millisecond))
return nil
}
// Validates dirs, walks both trees, compares pairs (with progress when not quiet), then writes diffs in the chosen format. Drives lib for walk, discovery, hashing, and output; progress and logging stay here so the CLI controls UX.
func runRoot(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.SetOut(os.Stdout)
return cmd.Usage()
}
left, right := args[0], args[1]
if err := lib.EnsureDir(left); err != nil {
fmt.Fprintf(os.Stderr, "left directory: %v\n", err)
os.Exit(ExitFatal)
}
if err := lib.EnsureDir(right); err != nil {
fmt.Fprintf(os.Stderr, "right directory: %v\n", err)
os.Exit(ExitFatal)
}
if !quiet {
defer log.PrintLogPaths()
}
// Print compared directories at start (logger always; stderr when not quiet).
log.Write("left directory: " + left)
log.Write("right directory: " + right)
if !quiet {
fmt.Fprintln(os.Stderr, "Left directory: ", left)
fmt.Fprintln(os.Stderr, "Right directory: ", right)
}
log.Write("started comparison")
startTime := time.Now()
pool := lib.NewPathPool()
set := lib.NewDiscoveredSet(pool)
compareResultCh := make(chan lib.CompareResult, 256)
progressCounts := &lib.ProgressCounts{}
// Phase 1: discover all file pairs by walking both trees.
walkFileCh := make(chan lib.DiscoveredFile, 256)
walkDoneCh := make(chan struct{})
go func() {
for file := range walkFileCh {
set.Add(file.Rel, file.Side)
}
close(walkDoneCh)
}()
const utilWindowTicks = 30 // ~3 seconds at 100ms tick; longer window so "workers active" is meaningful when work is bursty
discoveryWorkerUtilization := lib.NewWorkerUtilization(numWorkers, utilWindowTicks)
go lib.Discover(
[]lib.DirJob{{Root: left, RelDir: "", Side: lib.SideLeft}, {Root: right, RelDir: "", Side: lib.SideRight}},
walkFileCh, dirBatchSize, numWorkers, discoveryWorkerUtilization)
if !quiet && lib.IsTTY(os.Stderr) {
go discoveryProgressLoop(set, walkDoneCh, numWorkers, discoveryWorkerUtilization)
}
<-walkDoneCh
scanDuration := time.Since(startTime)
pairPaths := set.PairPaths()
totalCompared := len(pairPaths)
var diffs []lib.DiffResult
compareWorkerUtilization := lib.NewWorkerUtilization(numWorkers, utilWindowTicks)
compareStart := time.Now()
go lib.Compare(left, right, pairPaths, numWorkers, hashAlg, hashThreshold, full, compareResultCh, progressCounts, compareWorkerUtilization)
compareDoneCh := make(chan struct{})
if !quiet && lib.IsTTY(os.Stderr) {
go compareProgressLoop(progressCounts, compareDoneCh, numWorkers, compareWorkerUtilization)
}
var compareResults []lib.CompareResult
for result := range compareResultCh {
compareResults = append(compareResults, result)
if result.Diff != nil {
diffs = append(diffs, *result.Diff)
}
}
close(compareDoneCh)
compareDuration := time.Since(compareStart)
differentCount := len(diffs)
sameCount := totalCompared - differentCount
if sameCount < 0 {
sameCount = 0
}
leftOnlyPaths := set.LeftOnlyPaths()
leftOnlyCount := 0
for _, relativePath := range leftOnlyPaths {
path := filepath.Join(left, relativePath)
if info, err := os.Stat(path); err == nil && info.Mode().IsRegular() {
diffs = append(diffs, lib.DiffResult{Rel: relativePath, Reason: "left only", LeftSize: info.Size(), LeftMtime: info.ModTime().Truncate(time.Second), LeftOnly: true})
leftOnlyCount++
}
}
rightOnlyPaths := set.RightOnlyPaths()
rightOnlyCount := 0
for _, relativePath := range rightOnlyPaths {
path := filepath.Join(right, relativePath)
if info, err := os.Stat(path); err == nil && info.Mode().IsRegular() {
diffs = append(diffs, lib.DiffResult{Rel: relativePath, Reason: "right only", RightSize: info.Size(), RightMtime: info.ModTime().Truncate(time.Second)})
rightOnlyCount++
}
}
log.Flush()
switch outputFormat {
case "table":
lib.FormatTable(diffs)
case "json":
lib.FormatJSON(diffs)
case "yaml":
lib.FormatYAML(diffs)
default:
lib.FormatTextTreeWithSections(diffs, differentCount, compareResults, showSame)
}
displaySummary(!quiet, runSummary{
leftDir: left,
rightDir: right,
totalCompared: totalCompared,
leftOnlyCount: leftOnlyCount,
rightOnlyCount: rightOnlyCount,
differentCount: differentCount,
sameCount: sameCount,
startTime: startTime,
scanDuration: scanDuration,
compareDuration: compareDuration,
numWorkers: numWorkers,
workerUtilizationPercent: compareWorkerUtilization.UtilizedPercentWholeRun(),
})
if log.ErrorCount() > 0 {
if !quiet {
fmt.Fprintln(os.Stderr, "Errors occurred; check the error log for details.")
}
os.Exit(ExitNonFatal)
}
return nil
}
// onlySideLogLines returns log lines for "left only" or "right only" paths. When a directory has all its contents in paths, it returns a single "directory X is left/right only and contains N files" line instead of listing each file. baseDir is the tree root (left or right); sideLabel is "left" or "right".
func onlySideLogLines(baseDir string, paths []string, sideLabel string) []string {
if len(paths) == 0 {
return nil
}
pathSet := make(map[string]bool)
pathIsDir := make(map[string]bool)
for _, p := range paths {
pathSet[p] = true
full := filepath.Join(baseDir, p)
if info, err := os.Stat(full); err == nil {
pathIsDir[p] = info.IsDir()
}
}
var collapsibleDirs map[string]int // dir -> number of regular files under it
for _, p := range paths {
if !pathIsDir[p] {
continue
}
fullDir := filepath.Join(baseDir, p)
fileCount := 0
allUnderInSet := true
_ = filepath.Walk(fullDir, func(fullPath string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
rel, err := filepath.Rel(baseDir, fullPath)
if err != nil {
return nil
}
relSlash := filepath.ToSlash(rel)
if relSlash == "." {
return nil
}
if !pathSet[relSlash] {
allUnderInSet = false
return filepath.SkipAll
}
if info.Mode().IsRegular() {
fileCount++
}
return nil
})
if allUnderInSet && fileCount >= 0 {
if collapsibleDirs == nil {
collapsibleDirs = make(map[string]int)
}
collapsibleDirs[p] = fileCount
}
}
// Maximal collapsible: dirs not under any other collapsible dir
maximal := make(map[string]int)
for d, n := range collapsibleDirs {
underAnother := false
for d2 := range collapsibleDirs {
if d2 != d && strings.HasPrefix(d, d2+"/") {
underAnother = true
break
}
}
if !underAnother {
maximal[d] = n
}
}
var lines []string
for _, p := range paths {
if n, ok := maximal[p]; ok {
lines = append(lines, fmt.Sprintf("directory %s is %s only and contains %d file(s)", p, sideLabel, n))
continue
}
underMaximal := false
for d := range maximal {
if p != d && strings.HasPrefix(p, d+"/") {
underMaximal = true
break
}
}
if underMaximal {
continue
}
lines = append(lines, " "+p)
}
return lines
}
// Prints "scanning: N left-only, N right-only, N pairs" to stderr on a ticker until doneCh closes. Appends the percentage of workers utilized (from workerUtilization.Tick()).
func discoveryProgressLoop(set *lib.DiscoveredSet, doneCh <-chan struct{}, numWorkers int, workerUtilization *lib.WorkerUtilization) {
tick := time.NewTicker(100 * time.Millisecond)
defer tick.Stop()
for {
select {
case <-doneCh:
return
case <-tick.C:
leftOnly := set.LeftOnlyCount()
rightOnly := set.RightOnlyCount()
pairs := set.PairsCount()
windowed := workerUtilization.Tick()
total := workerUtilization.UtilizedPercentWholeRun()
workStats := fmt.Sprintf(" [worker utilization 3s: %d%%, total: %d%%]", windowed, total)
writeProgressLine("Scanning: %d left-only, %d right-only, %d pairs (%d workers)%s ", leftOnly, rightOnly, pairs, numWorkers, workStats)
}
}
}
// Extrapolates remaining time from elapsed and (processed, pending) so we can show "~Xs remaining"; returns 0 if processed or pending is non-positive.
func estimateRemainingFromElapsed(elapsed time.Duration, processed, pending int32) time.Duration {
if processed <= 0 || pending <= 0 {
return 0
}
averagePerPair := elapsed / time.Duration(processed)
return averagePerPair * time.Duration(pending)
}
// Uses progress counts and start time (from ProgressCounts) to compute remaining time; used by compareProgressLoop with atomically loaded values.
func estimateRemainingDuration(processed, pending int32, startTimeUnixNano int64) time.Duration {
if startTimeUnixNano == 0 {
return 0
}
elapsed := time.Since(time.Unix(0, startTimeUnixNano))
return estimateRemainingFromElapsed(elapsed, processed, pending)
}
// writeProgressLine overwrites the current stderr line with the formatted message, clearing to end of line first so no leftover text remains.
func writeProgressLine(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "\r\033[K"+format, args...)
}
// Prints "comparing: N of M, ~Xs remaining" to stderr until doneCh closes. If workerUtilization is non-nil, appends the percentage of workers utilized in the last second (from workerUtilization.Tick()).
func compareProgressLoop(progressCounts *lib.ProgressCounts, doneCh <-chan struct{}, numWorkers int, workerUtilization *lib.WorkerUtilization) {
tick := time.NewTicker(100 * time.Millisecond)
defer tick.Stop()
for {
select {
case <-doneCh:
writeProgressLine("")
return
case <-tick.C:
processedCount := atomic.LoadInt32(&progressCounts.Processed)
totalPairs := atomic.LoadInt32(&progressCounts.TotalPairs)
startTimeNano := atomic.LoadInt64(&progressCounts.StartTimeUnixNano)
if processedCount == 0 && totalPairs == 0 {
continue
}
windowed := workerUtilization.Tick()
total := workerUtilization.UtilizedPercentWholeRun()
workStats := fmt.Sprintf(" [worker utilization 3s: %d%%, total: %d%%]", windowed, total)
if totalPairs > 0 {
pending := totalPairs - processedCount
if pending < 0 {
pending = 0
}
remaining := estimateRemainingDuration(processedCount, pending, startTimeNano)
if remaining > 0 {
writeProgressLine("Comparing: %d of %d, ~%s remaining (%d workers)%s ", processedCount, totalPairs, remaining.Round(time.Second), numWorkers, workStats)
} else {
writeProgressLine("Comparing: %d of %d (%d workers)%s ", processedCount, totalPairs, numWorkers, workStats)
}
} else {
enqueuedCount := atomic.LoadInt32(&progressCounts.Enqueued)
writeProgressLine("Processed %d, enqueued %d (%d workers)%s ", processedCount, enqueuedCount, numWorkers, workStats)
}
}
}
}