-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
194 lines (171 loc) · 4.1 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
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
)
type DuplicateFiles struct {
size int64
num int
files []string
}
func main() {
var fileDir string
if len(os.Args) > 1 {
fileDir = os.Args[1]
}
files, filesNum, err := FindFilesInSameSize(fileDir)
if err != nil {
fmt.Printf("FindFilesInSameSize failed, err: %s\n", err.Error())
return
}
allDuplicateFiles, err := FindDuplicateFiles(files)
if err != nil {
fmt.Printf("FindDuplicateFiles failed, err: %s\n", err.Error())
return
}
err = WriteResultFile(fileDir, allDuplicateFiles)
if err != nil {
fmt.Printf("WriteResultFile failed, err: %s\n", err.Error())
return
}
fmt.Printf("Get duplicate files success.\nTotal number of detected files: %d\nTotal number of duplicate files groups: %d\n", filesNum, len(allDuplicateFiles))
}
func FindFilesInSameSize(fileDir string) (files map[int64][]string, filesNum int, err error) {
files = make(map[int64][]string)
if fileDir == "" {
fileDir, _ = os.Getwd()
}
err = filepath.Walk(fileDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
if strings.Contains(err.Error(), "Access is denied") {
return nil
}
return err
}
if !info.IsDir() {
filesNum++
s := info.Size()
if _, ok := files[s]; !ok {
files[s] = []string{path}
} else {
files[s] = append(files[s], path)
}
}
return nil
})
for size, sameSizeFiles := range files {
if len(sameSizeFiles) < 2 {
delete(files, size)
}
}
return
}
func FindDuplicateFiles(files map[int64][]string) (allDuplicateFiles []DuplicateFiles, err error) {
var maxWorkerNum int
if runtime.NumCPU() > 2 {
maxWorkerNum = runtime.NumCPU() - 2
} else {
maxWorkerNum = runtime.NumCPU()
}
var wg sync.WaitGroup
n := len(files)
ch1 := make(chan DuplicateFiles, maxWorkerNum)
ch2 := make(chan DuplicateFiles, len(files))
doneCh := make(chan bool, maxWorkerNum)
go func() {
for size, sameSizeFiles := range files {
ch1 <- DuplicateFiles{size: size, files: sameSizeFiles}
}
for i := 0; i < maxWorkerNum; i++ {
doneCh <- true
}
}()
wg.Add(1)
go func(allDuplicateFiles *[]DuplicateFiles) {
for {
select {
case duplicateFiles := <-ch2:
if duplicateFiles.num > 1 {
*allDuplicateFiles = append(*allDuplicateFiles, duplicateFiles)
}
default:
if n == 0 {
wg.Done()
return
}
}
}
}(&allDuplicateFiles)
for i := 0; i < maxWorkerNum; i++ {
wg.Add(1)
go worker(&n, ch1, ch2, doneCh, &wg)
}
wg.Wait()
return
}
func worker(n *int, ch1, ch2 chan DuplicateFiles, doneCh chan bool, wg *sync.WaitGroup) {
for {
select {
case sSizeFiles := <-ch1:
filesMap := make(map[string][]string)
for _, file := range sSizeFiles.files {
m, err := GetFileMd5(file)
if err != nil {
continue
}
if _, ok := filesMap[m]; !ok {
filesMap[m] = []string{file}
} else {
filesMap[m] = append(filesMap[m], file)
}
}
for _, files := range filesMap {
if len(files) > 1 {
ch2 <- DuplicateFiles{
size: sSizeFiles.size,
num: len(files),
files: files,
}
}
}
*n--
case <-doneCh:
wg.Done()
}
}
}
func GetFileMd5(path string) (string, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
return "", err
}
defer f.Close()
h := md5.New()
_, err = io.Copy(h, f)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func WriteResultFile(fileDir string, allDuplicateFiles []DuplicateFiles) error {
if fileDir == "" {
fileDir, _ = os.Getwd()
}
sort.Slice(allDuplicateFiles, func(i, j int) bool {
return allDuplicateFiles[i].size > allDuplicateFiles[j].size
})
result := fmt.Sprintf("Get duplicate files in %s.\nTotal number of duplicate files groups: %d.\n", fileDir, len(allDuplicateFiles))
for _, allDuplicateFile := range allDuplicateFiles {
result += fmt.Sprintf("\nsize: %d\nnum: %d\nfiles: %s\n", allDuplicateFile.size, allDuplicateFile.num, strings.Join(allDuplicateFile.files, ","))
}
return ioutil.WriteFile("result.txt", []byte(result), 0644)
}