-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_dupes.go
194 lines (174 loc) · 4.13 KB
/
find_dupes.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
package main
import (
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"sync"
)
var DefaultNumWorkers int = 2
var Excludes = map[string]bool{
".": true,
"..": true,
".DS_Store": true,
}
type FileDesc struct {
path string
mtime int64
size int64
}
func generateFileDesc(id int, path string) (FileDesc, error) {
var mTime, size int64
fileInfo, err := os.Stat(path)
if err != nil {
fmt.Printf("worker %d: error os.Stat(%s): %s\n", id, path, err)
return FileDesc{}, err
} else {
mTime = fileInfo.ModTime().Unix()
size = fileInfo.Size()
}
return FileDesc{path, mTime, size}, nil
}
func getMD5(path string) (string, error) {
h := md5.New()
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
buf := make([]byte, 4096)
for {
count, err := file.Read(buf)
if err != nil {
if err == io.EOF {
break
} else {
return "", err
}
}
h.Write(buf[0:count])
}
// XXX can call h.checkSum?
hash := fmt.Sprintf("%x", h.Sum(nil))
return hash, nil
}
func processFiles(id int, taskQueue <-chan string, doneQueue chan<- map[int64][]FileDesc) {
filesBySize := make(map[int64][]FileDesc)
fmt.Printf("worker %d: starting up!\n", id)
for filename := range taskQueue {
fileDesc, err := generateFileDesc(id, filename)
if err != nil {
fmt.Printf("worker %d: error generating entry for %s: %s\n", id, filename, err)
continue
}
filesBySize[fileDesc.size] = append(filesBySize[fileDesc.size], fileDesc)
}
fmt.Printf("worker %d: found sizes for %d files\n", id, len(filesBySize))
doneQueue <- filesBySize
}
func findDupes(dirname string, numWorkers int) map[string][]FileDesc {
taskQueue := make(chan string, 100)
doneQueue := make(chan map[int64][]FileDesc)
for i := 0; i < numWorkers; i++ {
go processFiles(i, taskQueue, doneQueue)
}
err := filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("Error accessing %s: %s\n", path, err)
return nil
}
if _, ok := Excludes[info.Name()]; ok {
return nil
}
if info.Mode().IsRegular() {
taskQueue <- path
}
return nil
})
if err != nil {
fmt.Printf("Error crawling directory tree: %s\n", err)
}
close(taskQueue)
filesBySize := make(map[int64][]FileDesc)
for i := 0; i < numWorkers; i++ {
workerFilesBySize := <-doneQueue
for k, v := range workerFilesBySize {
filesBySize[k] = append(filesBySize[k], v...)
}
}
// walk dupes, get md5s
mutex := sync.Mutex{}
filesByHash := make(map[string][]FileDesc)
c := make(chan bool)
for _, fileDescs := range filesBySize {
if len(fileDescs) < 2 {
continue
}
for _, file := range fileDescs {
go func(f FileDesc) {
hash, err := getMD5(f.path)
if err != nil {
fmt.Printf("error getMD5(%s): %s\n", f.path, err)
} else {
mutex.Lock()
filesByHash[hash] = append(filesByHash[hash], f)
mutex.Unlock()
}
c <- true
}(file)
}
for range fileDescs {
<-c
}
}
return filesByHash
}
func printDupes(dirname string, numWorkers int) {
filesByHash := findDupes(dirname, numWorkers)
if len(filesByHash) == 0 {
fmt.Printf("\nNo dupes found.\n")
return
}
fmt.Printf("\nDuplicates:\n")
for hash, fileDescs := range filesByHash {
if len(fileDescs) < 2 {
continue
}
fmt.Printf("\n%s (size %d)\n", hash, fileDescs[0].size)
for _, file := range fileDescs {
fmt.Printf("\t%s (mtime %d)\n", file.path, file.mtime)
}
}
}
// adapted from http://stackoverflow.com/a/25567952
func IsDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir(), err
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: find_dupes <PATH> [ <NUM_WORKERS> ]\n")
os.Exit(1)
}
srcDir := os.Args[1]
if isDir, err := IsDirectory(srcDir); err != nil || !isDir {
fmt.Printf("%s is not a directory\n", srcDir)
os.Exit(1)
}
numWorkers := DefaultNumWorkers
if len(os.Args) > 2 {
n, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
numWorkers = n
}
fmt.Printf("Searching %s with %d workers\n", srcDir, numWorkers)
printDupes(srcDir, numWorkers)
}