-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
298 lines (232 loc) · 6.91 KB
/
main.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
package main
import (
"os"
"log"
"runtime"
"github.com/raspi/dirscanner"
"time"
"math"
"flag"
"path/filepath"
"fmt"
)
var VERSION = `0.0.0`
var BUILD = `dev`
const (
AUTHOR = `Pekka Järvinen`
YEAR = 2018
HOMEPAGE = `https://github.com/raspi/duplikaatti`
)
const (
MEBIBYTE = 1048576
GIBIBYTE = 1073741824
)
func getFilterFunc() dirscanner.FileValidatorFunction {
return func(info dirscanner.FileInformation) bool {
if info.Size == 0 {
return false
}
if info.Mode&os.ModeType != 0 {
return false
}
return true
}
}
type KeepFile struct {
Priority uint8
INode uint64
}
func main() {
readSize := int64(MEBIBYTE)
actuallyRemove := false
flag.BoolVar(&actuallyRemove, `remove`, false, `Actually remove files.`)
flag.Usage = func() {
f := filepath.Base(os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "Duplicate file remover (version %v build %v)\n", VERSION, BUILD)
fmt.Fprintf(flag.CommandLine.Output(), "Removes duplicate files. Algorithm idea from rdfind.\n")
fmt.Fprintf(flag.CommandLine.Output(), "\n")
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [options] <directories>:\n", f)
fmt.Fprintf(flag.CommandLine.Output(), "\nParameters:\n")
flag.PrintDefaults()
fmt.Fprintf(flag.CommandLine.Output(), "\n")
fmt.Fprintf(flag.CommandLine.Output(), "Examples:\n")
fmt.Fprintf(flag.CommandLine.Output(), " Test what would be removed:\n")
fmt.Fprintf(flag.CommandLine.Output(), " %v /home/raspi/storage /mnt/storage\n", f)
fmt.Fprintf(flag.CommandLine.Output(), " Remove files:\n")
fmt.Fprintf(flag.CommandLine.Output(), " %v -remove /home/raspi/storage /mnt/storage\n", f)
fmt.Fprintf(flag.CommandLine.Output(), "\n")
ai := 1
fmt.Fprintf(flag.CommandLine.Output(), "Algorithm:\n")
fmt.Fprintf(flag.CommandLine.Output(), " %v. Get file list from given directory list.\n", ai)
ai++
fmt.Fprintf(flag.CommandLine.Output(), " %v. Remove all orphans (only one file with same size).\n", ai)
ai++
fmt.Fprintf(flag.CommandLine.Output(), " %v. Read first %v bytes (%v) of files.\n", ai, readSize, bytesToHuman(uint64(readSize)))
ai++
fmt.Fprintf(flag.CommandLine.Output(), " %v. Remove all orphans.\n", ai)
ai++
fmt.Fprintf(flag.CommandLine.Output(), " %v. Read last %v bytes (%v) of files.\n", ai, readSize, bytesToHuman(uint64(readSize)))
ai++
fmt.Fprintf(flag.CommandLine.Output(), " %v. Remove all orphans.\n", ai)
ai++
fmt.Fprintf(flag.CommandLine.Output(), " %v. Hash whole files.\n", ai)
ai++
fmt.Fprintf(flag.CommandLine.Output(), " %v. Select first file with checksum X not to be removed and add rest of the files to a duplicates list.\n", ai)
ai++
fmt.Fprintf(flag.CommandLine.Output(), " %v. Remove duplicates.\n", ai)
ai++
fmt.Fprintf(flag.CommandLine.Output(), "\n")
fmt.Fprintf(flag.CommandLine.Output(), "(c) %v %v- / %v\n", AUTHOR, YEAR, HOMEPAGE)
fmt.Fprintf(flag.CommandLine.Output(), "\n")
}
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
if !isPowerOfTwo(uint64(readSize)) {
fmt.Printf(`readSize (%v) is not power of two`, readSize)
os.Exit(1)
}
dirs := flag.Args()
// Check that all given arguments are directories
for _, dir := range dirs {
_, err := isDirectory(dir)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if actuallyRemove {
log.Printf("ACTUALLY DELETING FILES! PRESS CTRL+C TO ABORT!")
} else {
log.Printf("Note: Not actually deleting files (dry run)")
}
// Ticker for stats
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
now := time.Now()
workerCount := runtime.NumCPU()
dupes := New(ticker, &now, workerCount)
filterFunc := getFilterFunc()
// look-up table for inodes
seenInodes := map[uint64]bool{}
log.Printf(`Generating file list..`)
// First get a recursive file listing
for _, dir := range dirs {
scanner := dirscanner.New()
err := scanner.Init(workerCount*2, filterFunc)
if err != nil {
panic(err)
}
err = scanner.ScanDirectory(dir)
if err != nil {
panic(err)
}
prio := uint8(math.MaxUint8)
lastDir := ``
lastFile := ``
fileCount := 0
scanloop:
for {
select {
case <-scanner.Finished: // Finished getting file list
log.Printf(`got all files`)
break scanloop
case e, ok := <-scanner.Errors: // Error happened, handle, discard or abort
if ok {
log.Printf(`got error: %v`, e)
//s.Aborted <- true // Abort
}
case info, ok := <-scanner.Information: // Got information where worker is currently
if ok {
lastDir = info.Directory
}
case <-ticker.C: // Display some progress stats
log.Printf(`%v Files scanned: %v Last file: %#v Dir: %#v`, time.Since(now).Truncate(time.Second), fileCount, lastFile, lastDir)
case res, ok := <-scanner.Results:
if ok {
fileCount++
lastFile = res.Path
_, iok := seenInodes[res.Identifier]
if !iok {
seenInodes[res.Identifier] = true
dupes.AddFile(newFileInfo(prio, res))
}
}
}
}
scanner.Close()
prio--
} // End of recursive scan
// Now we have list of files
log.Printf(`File list generated..`)
dupes.ReportStats()
log.Printf(`Removing orphans..`)
dupes.RemoveFileOrphans()
log.Printf(`Getting file information..`)
dupes.ReportStats()
log.Printf(`Reading first bytes..`)
dupes.RemoveBasedOnBytes(readSize, READ_FIRST)
dupes.ReportStats()
log.Printf(`Reading last bytes..`)
dupes.RemoveBasedOnBytes(readSize, READ_LAST)
dupes.ReportStats()
deletedCount := uint64(0)
deletedSize := uint64(0)
log.Printf(`Hashing files..`)
hashed := dupes.HashDuplicates(readSize)
dupes.Reset()
for _, v := range GetDuplicateList(hashed) {
for idx, f := range v {
if idx == 0 {
log.Printf(`Keeping %v`, f.Path)
continue
}
deletedSize += uint64(f.Size)
deletedCount++
log.Printf(`Deleting %v`, f.Path)
if actuallyRemove {
err := os.Remove(f.Path)
if err != nil {
log.Printf(`%v`, err)
}
}
}
}
log.Printf(`Deleted %v files, %v`, deletedCount, bytesToHuman(deletedSize))
log.Printf(`Took %v`, time.Since(now).Truncate(time.Second))
log.Printf(`Done.`)
}
func GetDuplicateList(m map[string]map[uint64][]fileInfo) (dupes [][]fileInfo) {
for _, sizeKey := range m {
for _, files := range sizeKey {
var selected []fileInfo
// Find best candidate
bestCandidateFile := KeepFile{
Priority: 0,
INode: math.MaxUint64,
}
for _, file := range files {
if file.INode < bestCandidateFile.INode && file.Priority >= bestCandidateFile.Priority {
bestCandidateFile.INode = file.INode
bestCandidateFile.Priority = file.Priority
}
}
// List what to keep and discard
var keep fileInfo
var discard []fileInfo
for _, file := range files {
if file.INode == bestCandidateFile.INode {
keep = file
} else {
discard = append(discard, file)
}
}
selected = append(selected, keep)
selected = append(selected, discard...)
dupes = append(dupes, selected)
}
}
return dupes
}