-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_identical_files.go
73 lines (67 loc) · 1.66 KB
/
list_identical_files.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
// -----------------------------------------------------------------------------
// CMDX Utilities Suite cmdx/[list_identical_files.go]
// (c) balarabe@protonmail.com License: GPLv3
// -----------------------------------------------------------------------------
package main
import (
"bytes"
)
// listIdenticalFiles _ _
func listIdenticalFiles(cmd Command, args []string) {
var filter string
args, filter = splitArgsFilter(args)
if len(args) < 1 {
env.Println("'duplicates' requires:" +
" <source dir>, or <source dir> and <target dir>")
return
}
var toFilesMap FilesMap
if len(args) == 1 {
toFilesMap = getFilesMap(args[0], filter)
} else {
toFilesMap = getFilesMap(args[1], filter)
}
duplicates := make(map[string]bool, 1)
for size, fromFiles := range getFilesMap(args[0], filter) {
toFiles := toFilesMap[size]
if len(toFiles) == 0 {
continue
}
for _, from := range fromFiles {
if duplicates[from.Path] {
continue
}
var fromData []byte
first := true
for _, to := range toFiles {
if from.Path == to.Path {
continue
}
if len(fromData) == 0 {
data, done := env.ReadFile(from.Path)
if !done {
continue
}
fromData = data
}
toData, done := env.ReadFile(to.Path)
if !done {
continue
}
if !bytes.Equal(fromData, toData) {
continue
}
if first {
first = false
env.Println()
env.Println(from.Path)
}
env.Println(to.Path)
duplicates[to.Path] = true
}
}
}
}
// TODO: add a function to call when each duplicate is found.
// TODO: can be moved to 'fs' with some changes
// end