-
Notifications
You must be signed in to change notification settings - Fork 0
/
dedup.go
423 lines (410 loc) · 14.7 KB
/
dedup.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
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
package main
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"hash"
"io"
"os"
"sync"
"syscall"
"github.com/hekmon/cunits/v2"
"github.com/hekmon/liveprogress"
"golang.org/x/sync/semaphore"
)
type dedupedReport struct {
Reference *FileInfos
HardLinked FileInfosList
}
func dedup(pathATree, pathBTree *FileInfos, totalAFiles int64, tokenPool *semaphore.Weighted) (errorCount int) {
// Setup global progress bar
// liveprogress.RefreshInterval = time.Second
barConfig := liveprogress.DefaultConfig
barConfig.Width = 30
barConfig.Empty = ' '
globalProgress := liveprogress.SetMainLineAsBar(uint64(totalAFiles), barConfig,
liveprogress.AppendPercent(),
liveprogress.AppendDecorator(func(b *liveprogress.Bar) string {
return fmt.Sprintf(" Global progress: %d/%d files processed", b.Current(), totalAFiles)
}),
)
var (
liveprogressErr error
stdout, stderr io.Writer
)
if liveprogressErr = liveprogress.Start(); liveprogressErr != nil {
fmt.Fprintf(os.Stderr, "failed to start liveprogress: %s\n", liveprogressErr)
stdout = os.Stdout
stderr = os.Stderr
} else {
stdout = liveprogress.Bypass()
stderr = stdout
}
// error logger
errChan := make(chan error)
errorsDone := make(chan any)
go func() {
for err := range errChan {
fmt.Fprintf(stderr, "ERROR: %s\n", err)
errorCount++
}
close(errorsDone)
}()
// report logger
dedupedChan := make(chan dedupedReport)
dedupedDone := make(chan any)
dedupedList := make(map[*FileInfos]FileInfosList)
go func() {
for deduped := range dedupedChan {
dedupedList[deduped.Reference] = deduped.HardLinked
}
close(dedupedDone)
}()
// start nodes processing from the top of the tree
var workers sync.WaitGroup
processNode(
pathATree,
pathBTree,
concurrentToolBox{
tokenPool: tokenPool,
waitGroup: &workers,
stdout: stdout,
barConfig: barConfig,
processedReporting: globalProgress.CurrentIncrement,
dedupedChan: dedupedChan,
errChan: errChan,
},
)
workers.Wait()
// Stop utilities goroutines
close(dedupedChan)
close(errChan)
<-dedupedDone
<-errorsDone
// Print results log
if liveprogressErr == nil {
liveprogress.Stop(true)
}
var (
totalSaved cunits.Bits
totalFiles int
)
if len(dedupedList) > 0 {
fmt.Println()
fmt.Println("Dedup listing:")
for refFile, matchsDeduped := range dedupedList {
fileSize := cunits.ImportInByte(float64(refFile.Infos.Size()))
saved := fileSize * cunits.Bits(len(matchsDeduped))
if apply {
fmt.Printf("File '%s' (%s) has %d match(s) (saved %s):\n",
refFile.FullPath, fileSize, len(matchsDeduped), saved)
} else {
fmt.Printf("File '%s' (%s) has %d match(s) (potential saving of %s):\n",
refFile.FullPath, fileSize, len(matchsDeduped), saved)
}
for _, matchDeduped := range matchsDeduped {
if apply {
fmt.Printf("\tMatch '%s' has been replaced by a hardlink\n", matchDeduped.FullPath)
} else {
fmt.Printf("\tMatch '%s' could have been replaced by a hardlink\n", matchDeduped.FullPath)
}
}
totalSaved += saved
totalFiles += len(matchsDeduped)
}
}
fmt.Println()
if apply {
fmt.Printf("%d file(s) deduped with hard linking saving a total of %s\n", totalFiles, totalSaved)
} else {
fmt.Printf("%d file(s) could be deduped with hard linking to save a total of %s\n", totalFiles, totalSaved)
}
return
}
type concurrentToolBox struct {
tokenPool *semaphore.Weighted
waitGroup *sync.WaitGroup
stdout io.Writer
barConfig liveprogress.BarConfig
processedReporting func()
dedupedChan chan<- dedupedReport
errChan chan<- error
}
func processNode(refFile, pathBTree *FileInfos, concurrent concurrentToolBox) {
if refFile.Infos.Mode().IsDir() {
for _, child := range refFile.Children {
processNode(child, pathBTree, concurrent)
}
} else {
processFileFindCandidates(refFile, pathBTree, concurrent)
}
}
func processFileFindCandidates(refFile, pathBTree *FileInfos, concurrent concurrentToolBox) {
// if all IO workers are busy, slow down a little bit
_ = concurrent.tokenPool.Acquire(context.Background(), 1)
concurrent.tokenPool.Release(1)
// If we do not make it to candidates evaluation, report the file as processed
processed := true
defer func() {
if processed {
concurrent.processedReporting()
}
}()
// do not process empty files
if refFile.Infos.Size() == 0 {
return
}
// if min size set, enforce it
if minSize != 0 {
fileSize := cunits.ImportInByte(float64(refFile.Infos.Size()))
if fileSize < minSize {
if debug {
fmt.Fprintf(concurrent.stdout, "Reference file '%s' size (%s) is lower than minSize (%s): skipping\n",
refFile.FullPath, fileSize, minSize)
}
return
}
}
// try to find candidates by size
sizeCandidates := findFileWithSize(refFile.Infos.Size(), pathBTree)
if len(sizeCandidates) == 0 {
return
}
if debug {
fmt.Fprintf(concurrent.stdout, "Reference file '%s' has %d size candidate(s): %s\n",
refFile.FullPath, len(sizeCandidates), sizeCandidates)
}
// start several inodes checks to discard unfitted candidates
refFileSystem, ok := refFile.Infos.Sys().(*syscall.Stat_t)
if !ok {
concurrent.errChan <- fmt.Errorf("can not check for hardlinks for '%s': backing storage device can not be checked", refFile.FullPath)
return
}
finalCandidates := make(FileInfosList, 0, len(sizeCandidates))
for _, candidate := range sizeCandidates {
candidateSystem, ok := candidate.Infos.Sys().(*syscall.Stat_t)
if !ok {
concurrent.errChan <- fmt.Errorf("can not check for hardlinks for '%s': backing storage device can not be checked", candidate.FullPath)
continue
}
// in case refFile already has hardlinks, check if candidate inode is the same as reffile inode
if refFileSystem.Nlink > 1 && candidateSystem.Ino == refFileSystem.Ino {
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' is already a hardlink of '%s': skipping\n",
candidate.FullPath, refFile.FullPath)
}
continue
}
// check if inode metadata is identical if necessary
if candidateSystem.Uid != refFileSystem.Uid {
if !force {
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' UID (%d) is not the same as ref file '%s' UID (%d): skipping (activate force mode to retain it)\n",
candidate.FullPath, candidateSystem.Uid, refFile.FullPath, refFileSystem.Uid)
}
continue
}
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' UID (%d) is not the same as ref file '%s' UID (%d): keeping it anyway (force mode is on)\n",
candidate.FullPath, candidateSystem.Uid, refFile.FullPath, refFileSystem.Uid)
}
}
if candidateSystem.Gid != refFileSystem.Gid {
if !force {
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' GID (%d) is not the same as ref file '%s' GID (%d): skipping (activate force mode to retain it)\n",
candidate.FullPath, candidateSystem.Uid, refFile.FullPath, refFileSystem.Uid)
}
continue
}
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' GID (%d) is not the same as ref file '%s' GID (%d): keeping it anyway (force mode is on)\n",
candidate.FullPath, candidateSystem.Gid, refFile.FullPath, refFileSystem.Gid)
}
}
if candidateSystem.Mode != refFileSystem.Mode {
if !force {
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' mode (%o) is not the same as ref file '%s' mode (%o): skipping (activate force mode to retain it)\n",
candidate.FullPath, candidateSystem.Mode, refFile.FullPath, refFileSystem.Mode)
}
continue
}
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' mode (%o) is not the same as ref file '%s' mode (%o): keeping it anyway (force mode is on)\n",
candidate.FullPath, candidateSystem.Mode, refFile.FullPath, refFileSystem.Mode)
}
}
// candidate survived all the tests, keeping it to the finals
finalCandidates = append(finalCandidates, candidate)
}
if len(finalCandidates) == 0 {
return
}
// do not report this file as processed (yet) when exiting this fx as candidates must be evaluated:
// processFileEvaluateCandidates() will mark this file as processed when done
processed = false
// final candidates ready
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' has %d final candidate(s) for dedup/hardlinking: %s\n",
refFile.FullPath, len(finalCandidates), finalCandidates)
}
// Start a goroutine as handler for this file (no token used as this goroutine will not produce IO itself but will launch others goroutines that will)
concurrent.waitGroup.Add(1)
go func() {
processFileEvaluateCandidates(refFile, finalCandidates, concurrent)
concurrent.waitGroup.Done()
}()
// return to let the caller goroutine continue to walk the mem tree
}
func findFileWithSize(refSize int64, node *FileInfos) (candidates FileInfosList) {
if node.Infos.Mode().IsDir() {
for _, child := range node.Children {
candidates = append(candidates, findFileWithSize(refSize, child)...)
}
} else {
if node.Infos.Size() == refSize {
candidates = append(candidates, node)
}
}
return
}
func processFileEvaluateCandidates(refFile *FileInfos, candidates FileInfosList, concurrent concurrentToolBox) {
// Mark the file as processed when done
defer concurrent.processedReporting()
// Do not create the progress bar (yet) if there is not at least one worker available for the original hash
_ = concurrent.tokenPool.Acquire(context.Background(), 1) // no err check as error can only come from expired context
// create the progress bar for this particular file
totalSize := cunits.ImportInByte((float64(refFile.Infos.Size() * int64(len(candidates)+1))))
fileBar := liveprogress.AddBar(uint64(totalSize.Byte()), concurrent.barConfig,
liveprogress.AppendPercent(),
liveprogress.AppendDecorator(func(b *liveprogress.Bar) string {
return fmt.Sprintf(" %s + %d candidate(s) (total hashing: %s/%s)",
refFile.Infos.Name(), len(candidates), cunits.ImportInByte(float64(b.Current())), totalSize)
}),
liveprogress.AppendDecorator(func(b *liveprogress.Bar) string {
return " Remaining:"
}),
liveprogress.AppendTimeRemaining(),
)
// prepare to compute checksums for reFile and its candidates
var (
originalHash []byte
candidatesHashes [][]byte
fileProcessingWaitGroup sync.WaitGroup
err error
)
// launch reffile hash in a weighted goroutine (token already took before creating the progress bar)
fileProcessingWaitGroup.Add(1)
go func() {
defer concurrent.tokenPool.Release(1)
defer fileProcessingWaitGroup.Done()
if originalHash, err = computeHash(refFile.FullPath, fileBar.CurrentAdd); err != nil {
concurrent.errChan <- fmt.Errorf("failed to compute hash of ref File %s: %w", refFile.FullPath, err)
return
}
if debug {
fmt.Fprintf(concurrent.stdout, "SHA256 computed for '%s': %x\n", refFile.FullPath, originalHash)
}
}()
// compute checksums of the candidates in weighted goroutines too
candidatesHashes = make([][]byte, len(candidates))
for candidateIndex, candidate := range candidates {
_ = concurrent.tokenPool.Acquire(context.Background(), 1) // no err check as error can only come from expired context
fileProcessingWaitGroup.Add(1)
go func(localCandidate *FileInfos, target *[]byte) {
defer concurrent.tokenPool.Release(1)
defer fileProcessingWaitGroup.Done()
if *target, err = computeHash(localCandidate.FullPath, fileBar.CurrentAdd); err != nil {
concurrent.errChan <- fmt.Errorf("failed to compute hash of candidate File %s: %w", localCandidate.FullPath, err)
return
}
if debug {
fmt.Fprintf(concurrent.stdout, "SHA256 computed for '%s': %x\n", localCandidate.FullPath, *target)
}
}(candidate, &(candidatesHashes[candidateIndex]))
}
// wait for hashing goroutines to finish
fileProcessingWaitGroup.Wait()
// start dedup using the computed hashes
deduped := processFileDedupCandidates(refFile, originalHash, candidates, candidatesHashes, concurrent)
// Done, show end message
if apply {
fmt.Fprintf(concurrent.stdout, "%s: %d/%d candidates hardlinked (saved %s)\n",
refFile.Infos.Name(), len(deduped), len(candidates), cunits.ImportInByte(float64(refFile.Infos.Size()*int64(len(deduped)))))
} else {
fmt.Fprintf(concurrent.stdout, "%s: %d/%d candidates could be hardlinked (potential saving of %s)\n",
refFile.Infos.Name(), len(deduped), len(candidates), cunits.ImportInByte(float64(refFile.Infos.Size()*int64(len(deduped)))))
}
liveprogress.RemoveBar(fileBar)
// send dedup report if any files deduped
if len(deduped) > 0 {
concurrent.dedupedChan <- dedupedReport{
Reference: refFile,
HardLinked: deduped,
}
}
}
func computeHash(path string, reportWritten func(add uint64)) (hash []byte, err error) {
// Prepare the hasher
hashReporter := hasherProgress{
hasher: sha256.New(),
report: reportWritten,
}
// Open file for reading
fd, err := os.Open(path)
if err != nil {
return
}
defer fd.Close()
// Feed it to the hasher
if _, err = io.Copy(hashReporter, fd); err != nil {
return
}
hash = hashReporter.hasher.Sum(nil)
return
}
type hasherProgress struct {
hasher hash.Hash
report func(add uint64)
}
func (wp hasherProgress) Write(p []byte) (n int, err error) {
n, err = wp.hasher.Write(p)
if err == nil || errors.Is(err, io.EOF) {
wp.report(uint64(n))
}
return
}
func processFileDedupCandidates(refFile *FileInfos, originalHash []byte, candidates FileInfosList, candidatesHashes [][]byte, concurrent concurrentToolBox) (deduped FileInfosList) {
deduped = make(FileInfosList, 0, len(candidates))
for candidateIndex, candidateHash := range candidatesHashes {
if !bytes.Equal(originalHash, candidateHash) {
if debug {
fmt.Fprintf(concurrent.stdout, "File '%s' checksum does not match ref file '%s' checksum: skipping\n",
candidates[candidateIndex].FullPath, refFile.FullPath)
}
continue
}
// Same file found !
if apply {
fmt.Fprintf(concurrent.stdout, "Match found for '%s': '%s' has the same checksum: replacing by a hard link\n",
refFile.FullPath, candidates[candidateIndex].FullPath)
if err := os.Remove(candidates[candidateIndex].FullPath); err != nil {
concurrent.errChan <- fmt.Errorf("can not make hardlink against '%s': failed to remove '%s': %s", refFile.FullPath, candidates[candidateIndex].FullPath, err)
continue
}
if err := os.Link(refFile.FullPath, candidates[candidateIndex].FullPath); err != nil {
concurrent.errChan <- fmt.Errorf("can not make hardlink between '%s' <> '%s' (file already removed!): %s", refFile.FullPath, candidates[candidateIndex].FullPath, err)
continue
}
deduped = append(deduped, candidates[candidateIndex])
} else {
fmt.Fprintf(concurrent.stdout, "Match found for '%s': '%s' has the same checksum: it could be replaced by a hard link\n",
refFile.FullPath, candidates[candidateIndex].FullPath)
deduped = append(deduped, candidates[candidateIndex])
}
}
return
}