-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfiledupe.go
More file actions
294 lines (258 loc) · 7.95 KB
/
filedupe.go
File metadata and controls
294 lines (258 loc) · 7.95 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
package main
import (
"bufio"
"crypto/md5"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"sync"
"syscall"
)
type fileInfo struct {
size int64
path string
inode uint64
partialMD5 string
fullMD5 string
}
type FileDupes struct {
rootPath string
minSize int64
excludeList []string
crossMountPoints bool
outputFilename string
fileList []fileInfo
dupeCandidates []fileInfo
dupes []fileInfo
dirCount int
}
func newFileDupes(path string, size int64, outfilename string) *FileDupes {
return &FileDupes{
rootPath: path,
minSize: size,
excludeList: []string{"Backups.backupdb"},
crossMountPoints: false,
outputFilename: outfilename,
}
}
func (fd *FileDupes) run() {
// TODO: Add a -v / --verbose flag that prints progress counts after each stage
// (files found, candidates, confirmed dupes) so large scans give feedback.
fd.walkTree()
fmt.Printf("Found %d files to be processed\n", len(fd.fileList))
fd.findPotentialDupes()
fd.confirmDupes()
fd.writeDupeFile()
fmt.Printf("Found %d files that appear to be duplicates\n", len(fd.dupes))
}
func (fd *FileDupes) walkTree() {
// TODO: The entire file list is accumulated in memory before hashing begins.
// On filesystems with millions of files this can exhaust memory; consider
// a streaming approach that feeds files into hashing as they are discovered.
err := filepath.Walk(fd.rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
// Handle permission errors or other issues by logging and skipping
fmt.Fprintf(os.Stderr, "Error accessing path %q: %v\n", path, err)
return filepath.SkipDir
}
if info.IsDir() {
fd.dirCount++ // TODO: dirCount is tracked but never printed or returned; use it in a summary or remove it.
for _, exclude := range fd.excludeList {
if info.Name() == exclude {
return filepath.SkipDir
}
}
if !fd.crossMountPoints && isMount(path) {
return filepath.SkipDir
}
} else if info.Size() > fd.minSize {
fd.fileList = append(fd.fileList, fileInfo{
size: info.Size(),
path: path,
inode: getInode(info),
})
}
return nil
})
if err != nil {
fmt.Printf("Error walking the path %v: %v\n", fd.rootPath, err)
}
}
func (fd *FileDupes) findPotentialDupes() {
sizeMap := make(map[int64][]fileInfo)
for _, file := range fd.fileList {
sizeMap[file.size] = append(sizeMap[file.size], file)
}
var wg sync.WaitGroup
// TODO: The concurrency limit of 8 is hardcoded. On spinning disks 8 concurrent
// readers cause seek thrashing; on SSDs more parallelism helps. Expose this
// as a -j / --jobs flag so callers can tune it.
// TODO: There is no context/cancellation support. A long scan cannot be interrupted
// cleanly on Ctrl-C. Thread a context.Context through and respect cancellation.
semaphore := make(chan struct{}, 8)
var mu sync.Mutex
for _, files := range sizeMap {
if len(files) > 1 {
for i := range files {
wg.Add(1)
semaphore <- struct{}{}
go func(f *fileInfo) {
defer wg.Done()
defer func() { <-semaphore }()
// TODO: The 10-chunk (80KB) partial hash assumes differences appear early.
// Files with identical headers (ISO images, video containers) will always
// pass this check and trigger expensive full reads. Consider sampling
// from multiple offsets or increasing coverage.
pMD5 := calculateMD5(f.path, 10)
if pMD5 != "" {
f.partialMD5 = pMD5
mu.Lock()
fd.dupeCandidates = append(fd.dupeCandidates, *f)
mu.Unlock()
}
}(&files[i])
}
}
}
wg.Wait()
fmt.Printf("Processed %d files\n", len(fd.fileList))
}
// Redefining confirmDupes for the actual implementation
// Redefining confirmDupes for the actual implementation
func (fd *FileDupes) confirmDupes() {
partialMD5Map := make(map[string][]fileInfo)
for _, file := range fd.dupeCandidates {
partialMD5Map[file.partialMD5] = append(partialMD5Map[file.partialMD5], file)
}
var wg sync.WaitGroup
semaphore := make(chan struct{}, 8)
var mu sync.Mutex
var fullyHashedFiles []fileInfo
for _, files := range partialMD5Map {
if len(files) > 1 {
for _, f := range files {
wg.Add(1)
semaphore <- struct{}{}
go func(info fileInfo) {
defer wg.Done()
defer func() { <-semaphore }()
info.fullMD5 = calculateMD5(info.path, 0)
if info.fullMD5 != "" {
mu.Lock()
fullyHashedFiles = append(fullyHashedFiles, info)
mu.Unlock()
}
}(f)
}
}
}
wg.Wait()
// Now group by full MD5
fullMD5Map := make(map[string][]fileInfo)
for _, file := range fullyHashedFiles {
fullMD5Map[file.fullMD5] = append(fullMD5Map[file.fullMD5], file)
}
for _, files := range fullMD5Map {
if len(files) > 1 {
// TODO: Hard links (files sharing the same inode) are reported as duplicates even
// though they occupy no extra disk space. Filter groups where all entries share
// the same inode before appending to fd.dupes.
fd.dupes = append(fd.dupes, files...)
}
}
}
func (fd *FileDupes) writeDupeFile() {
file, err := os.Create(fd.outputFilename)
if err != nil {
fmt.Printf("Error creating output file: %v\n", err)
return
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
sort.Slice(fd.dupes, func(i, j int) bool {
return fd.dupes[i].size < fd.dupes[j].size
})
for _, file := range fd.dupes {
_, err := fmt.Fprintf(writer, "%d %s %d %s\n", file.size, file.fullMD5, file.inode, file.path)
if err != nil {
fmt.Printf("Error writing to output file: %v\n", err)
return
}
}
}
func calculateMD5(filename string, numChunks int) string {
// TODO: Switch from MD5 to SHA-256 (crypto/sha256). MD5 is cryptographically broken;
// while accidental collisions are rare, using SHA-256 costs little and removes ambiguity.
file, err := os.Open(filename)
if err != nil {
// TODO: Returning "" on error causes unreadable files to match each other as
// "duplicates". Return a sentinel (e.g. a distinct error value) and skip those entries.
return ""
}
defer file.Close()
hash := md5.New()
buf := make([]byte, 8192)
// If numChunks is set, read that many chunks.
// If numChunks is 0 (or negative), read until EOF.
for i := 0; numChunks <= 0 || i < numChunks; i++ {
n, err := file.Read(buf)
if err != nil {
if err == io.EOF {
break
}
return ""
}
hash.Write(buf[:n])
}
return fmt.Sprintf("%x", hash.Sum(nil))
}
func isMount(path string) bool {
// This is a simplified check and may not work for all cases
parent := filepath.Dir(path)
pathStat, err := os.Stat(path)
if err != nil {
return false
}
parentStat, err := os.Stat(parent)
if err != nil {
return false
}
pathSys := pathStat.Sys().(*syscall.Stat_t)
parentSys := parentStat.Sys().(*syscall.Stat_t)
return pathSys.Dev != parentSys.Dev
}
func getInode(info os.FileInfo) uint64 {
return info.Sys().(*syscall.Stat_t).Ino
}
func main() {
filename := flag.String("f", "dupes.out", "save dupe list to FILE")
dir := flag.String("d", ".", "directory to use")
size := flag.Int64("s", 250000, "min size of file to check")
// Handling exclude list is a bit tricky with standard flag,
// usually requires a custom Value type for repeated flags.
// For simplicity, let's stick to the default exclude list or add a simple comma-separated string if needed.
// The Python script used action="append".
// We can implement a string slice flag.
var excludes stringSlice
flag.Var(&excludes, "e", "directories to exclude (can be repeated)")
flag.Parse()
fmt.Printf("file: %s dir: %s size: %d\n", *filename, *dir, *size)
fd := newFileDupes(*dir, *size, *filename)
if len(excludes) > 0 {
fd.excludeList = excludes
}
fd.run()
}
// stringSlice handles repeated string flags
type stringSlice []string
func (s *stringSlice) String() string {
return fmt.Sprintf("%v", *s)
}
func (s *stringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}