-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.go
178 lines (132 loc) · 3.19 KB
/
scanner.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
package main
import (
"log"
"time"
)
type DupeScanner struct {
files []fileInfo
ticker *time.Ticker
workerCount int
startTime *time.Time
}
func New(ticker *time.Ticker, startTime *time.Time, workerCount int) DupeScanner {
d := DupeScanner{
files: []fileInfo{},
ticker: ticker,
workerCount: workerCount,
startTime: startTime,
}
return d
}
func (ds *DupeScanner) GetFileCount() (l int, ts uint64) {
for _, f := range ds.files {
ts += uint64(f.Size)
}
return len(ds.files), ts
}
// RemoveFileOrphans removes all files that do not share file sizes
func (ds *DupeScanner) RemoveFileOrphans() {
fileSizeCounts := map[uint64]uint64{}
for _, f := range ds.files {
fileSizeCounts[f.Size]++
}
fileSizeHasDupes := map[uint64]bool{}
for fileSize, fileCount := range fileSizeCounts {
if fileCount > 1 {
fileSizeHasDupes[fileSize] = true
}
}
fileSizeCounts = nil
var newFiles []fileInfo
for _, f := range ds.files {
_, ok := fileSizeHasDupes[f.Size]
if !ok {
continue
}
newFiles = append(newFiles, f)
}
fileSizeHasDupes = nil
ds.files = newFiles
}
func (ds *DupeScanner) startWorker(readSize int64, rt ReadOperationType) hasherWorker {
worker := NewBytesWorker(ds.ticker, ds.startTime, ds.workerCount, readSize, rt)
worker.Wg.Add(1)
fileCount, _ := ds.GetFileCount()
go func(w *hasherWorker) {
for _, file := range ds.files {
fileCount--
w.Wg.Add(1)
w.Jobs <- file
}
w.Wg.Done()
}(&worker) // /func
go func(w *hasherWorker) {
for e := range w.Errors {
log.Printf(`error: %v`, e)
}
}(&worker)
go func(w *hasherWorker) {
// Wait jobs to finish
w.Wg.Wait()
for {
if len(w.Results) == 0 && len(w.Jobs) == 0 {
break
}
time.Sleep(time.Millisecond * 10)
}
close(w.Jobs)
close(w.Results)
close(w.Errors)
}(&worker) // /func
return worker
}
func (ds *DupeScanner) RemoveBasedOnBytes(readSize int64, rt ReadOperationType) {
fbw := ds.startWorker(readSize, rt)
hashMap := map[string][]uint64{}
for res := range fbw.Results {
hashMap[res.Hash] = append(hashMap[res.Hash], res.Info.INode)
}
keepInodes := make(map[uint64]bool)
for _, inodes := range hashMap {
if len(inodes) > 1 {
for _, inode := range inodes {
keepInodes[inode] = true
}
}
}
hashMap = nil
var newFiles []fileInfo
for _, file := range ds.files {
_, ok := keepInodes[file.INode]
if !ok {
continue
}
newFiles = append(newFiles, file)
}
keepInodes = nil
ds.files = newFiles
ds.RemoveFileOrphans()
}
// Hash whole files
func (ds *DupeScanner) HashDuplicates(readSize int64) (m map[string]map[uint64][]fileInfo) {
m = make(map[string]map[uint64][]fileInfo)
fbw := ds.startWorker(readSize, READ_WHOLE)
for res := range fbw.Results {
if m[res.Hash] == nil {
m[res.Hash] = make(map[uint64][]fileInfo)
}
m[res.Hash][res.Info.Size] = append(m[res.Hash][res.Info.Size], res.Info)
}
return m
}
func (ds *DupeScanner) Reset() {
ds.files = nil
}
func (ds *DupeScanner) ReportStats() {
fileCount, totalSize := ds.GetFileCount()
log.Printf(`%v files %v`, fileCount, bytesToHuman(totalSize))
}
func (ds *DupeScanner) AddFile(info fileInfo) (err error) {
ds.files = append(ds.files, info)
return nil
}