-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
238 lines (218 loc) · 5.6 KB
/
util.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
package fs
import (
"crypto/md5"
"encoding/hex"
md52 "github.com/hopeio/cherry/utils/crypto/md5"
"github.com/hopeio/cherry/utils/log"
"github.com/hopeio/cherry/utils/slices"
"io"
"os"
stdpath "path"
"strings"
)
func Exist(filepath string) bool {
_, err := os.Stat(filepath)
return err == nil
}
func NotExist(filepath string) bool {
_, err := os.Stat(filepath)
return os.IsNotExist(err)
}
func Md5(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
file.Close()
return "", err
}
err = file.Close()
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
func Md5Equal(path1, path2 string) (bool, error) {
md51, err := Md5(path1)
if err != nil {
return false, err
}
md52, err := Md5(path2)
if err != nil {
return false, err
}
return md51 == md52, nil
}
func GetMd5Name(name string) string {
ext := stdpath.Ext(name)
fileName := strings.TrimSuffix(name, ext)
fileName = md52.EncodeString(fileName)
return fileName + ext
}
type duplicateFile struct {
path string
md5 string
}
// 去除目录中重复的文件,默认保留参数靠前目录中的文件
func DirsDeDuplicate(dirs ...string) error {
return DirsDuplicateHandle(func(path1, path2 string) error {
log.Debugf("exists: %s,remove:%s", path1, path2)
return os.Remove(path2)
}, dirs...)
}
func DirsDuplicateHandle(callback func(path1, path2 string) error, dirs ...string) error {
fileSizeMap := make(map[int64][]*duplicateFile)
for _, tmpDir := range dirs {
err := RangeFile(tmpDir, func(dir string, entry os.DirEntry) error {
info, _ := entry.Info()
path := dir + PathSeparator + entry.Name()
duplicateFiles, ok := fileSizeMap[info.Size()]
var entryMd5 string
if ok {
var err error
entryMd5, err = Md5(path)
if err != nil {
return err
}
for _, file := range duplicateFiles {
if file.md5 == "" {
file.md5, err = Md5(file.path)
if err != nil {
return err
}
}
if file.md5 == entryMd5 {
return callback(file.path, path)
}
}
}
fileSizeMap[info.Size()] = append(fileSizeMap[info.Size()], &duplicateFile{path: path, md5: entryMd5})
return nil
})
if err != nil {
return err
}
}
return nil
}
// DirsRangeDuplicateHandle
// rangeCallback 返回值为错误和是否继续
func DirsRangeDuplicateHandle(rangeCallback func(dir string, entry os.DirEntry) (error, bool), duplicateCallback func(path1, path2 string) error, dirs ...string) error {
fileSizeMap := make(map[int64][]*duplicateFile)
for _, tmpDir := range dirs {
err := RangeFile(tmpDir, func(dir string, entry os.DirEntry) error {
if err, goon := rangeCallback(dir, entry); !goon {
return err
}
info, _ := entry.Info()
path := dir + PathSeparator + entry.Name()
duplicateFiles, ok := fileSizeMap[info.Size()]
var entryMd5 string
if ok {
var err error
entryMd5, err = Md5(path)
if err != nil {
return err
}
for _, file := range duplicateFiles {
if file.md5 == "" {
file.md5, err = Md5(file.path)
if err != nil {
return err
}
}
if file.md5 == entryMd5 {
return duplicateCallback(file.path, path)
}
}
}
fileSizeMap[info.Size()] = append(fileSizeMap[info.Size()], &duplicateFile{path: path, md5: entryMd5})
return nil
})
if err != nil {
return err
}
}
return nil
}
func TwoDirDuplicateHandle(dir1, dir2 string, callback func(path1, path2 string) error) error {
fileSizeMap := make(map[int64][]*duplicateFile)
err := RangeFile(dir1, func(dir string, entry os.DirEntry) error {
info, _ := entry.Info()
fileSizeMap[info.Size()] = append(fileSizeMap[info.Size()], &duplicateFile{path: dir + PathSeparator + entry.Name()})
return nil
})
if err != nil {
return err
}
return RangeFile(dir2, func(dir string, entry os.DirEntry) error {
info, _ := entry.Info()
if duplicateFiles, ok := fileSizeMap[info.Size()]; ok {
path := dir + PathSeparator + entry.Name()
entryMd5, err := Md5(path)
if err != nil {
return err
}
for _, file := range duplicateFiles {
if file.md5 == "" {
file.md5, err = Md5(file.path)
if err != nil {
return err
}
}
if file.md5 == entryMd5 {
return callback(file.path, path)
}
}
}
return nil
})
}
// 去除两个目录中重复的文件,默认保留第一个目录中的文件
func TwoDirDeDuplicate(dir1, dir2 string) error {
return TwoDirDuplicateHandle(dir1, dir2, func(path1, path2 string) error {
log.Debug("remove:", path2)
return os.Remove(path2)
})
}
// 两个目录同步,第一个参数为主目录,参考目录,第二个参数目录与第一个保持一致
func Sync(mainDir, slaveDir string) error {
mainDirEntries, err := os.ReadDir(mainDir)
if err == nil {
return err
}
if len(mainDirEntries) == 0 {
return nil
}
_, err = os.Stat(slaveDir)
if os.IsNotExist(err) {
return CopyDir(mainDir, slaveDir)
}
slaveDirEntries, err := os.ReadDir(slaveDir)
if err == nil {
return err
}
_, intersection, diff1, diff2 := slices.UnionAndIntersectionAndDifference(mainDirEntries, slaveDirEntries)
for _, entry := range diff2 {
err := os.RemoveAll(slaveDir + PathSeparator + entry.Name())
if err != nil {
return err
}
}
for _, entry := range diff1 {
err = CopyDir(mainDir+PathSeparator+entry.Name(), slaveDir+PathSeparator+entry.Name())
if err != nil {
return err
}
}
for _, entry := range intersection {
err = Sync(mainDir+PathSeparator+entry.Name(), slaveDir+PathSeparator+entry.Name())
if err != nil {
return err
}
}
return nil
}