/
crc32.go
321 lines (291 loc) · 8.26 KB
/
crc32.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
package checksum
import (
"bufio"
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/liumingmin/goutils/log"
"github.com/liumingmin/goutils/utils"
)
const OS_FILE_R_W = 0666
type ChecksumInfo struct {
FilePath string
Crc32Val string
FileSize string
}
func ChecksumCrc32(fileReader io.Reader) (uint32, error) {
hash := crc32.NewIEEE()
if _, err := io.Copy(hash, fileReader); err != nil {
return 0, err
}
return hash.Sum32(), nil
}
// AddFolderSuffix 为路径添加分隔符后缀
func AddFolderSuffix(folder string) string {
separator := string(os.PathSeparator)
if strings.HasSuffix(folder, separator) {
return folder
}
return folder + separator
}
// CompareChecksumFiles
// srcDir 校验目标文件夹
// checksumPath 校验文件路径
// ignores 忽略的文件
func CompareChecksumFiles(ctx context.Context, root string, checksumPath string) error {
info, err := os.Stat(root)
if err != nil || !info.IsDir() {
return errors.New(fmt.Sprintf("open src dir %s faild", root))
}
checkInfo, err := GetChecksumInfo(ctx, checksumPath)
if err != nil {
return err
}
paths := GetCheckFileList(ctx, checkInfo)
if err != nil {
return err
}
err = ChecksumFilesWithCheckInfo(root, checkInfo, paths)
if err != nil {
return err
}
return nil
}
func IsChecksumFileValid(ctx context.Context, checksumPath, md5Path string) bool {
bContent, err := ioutil.ReadFile(md5Path)
if err != nil {
log.Error(ctx, "open md5Path:%s error:%v", md5Path, err)
return false
}
fd, err := os.Open(checksumPath)
if err != nil {
log.Error(ctx, "open checksumPath:%s error:%v", checksumPath, err)
return false
}
defer fd.Close()
md5Handle := md5.New()
_, err = io.Copy(md5Handle, fd)
if err != nil {
log.Error(ctx, "checksumPath:%s md5 error:%v", checksumPath, err)
return false
}
md5Val := md5Handle.Sum(nil)
hexMd5 := hex.EncodeToString(md5Val)
if bytes.Equal(bContent, []byte(hexMd5)) {
return true
}
log.Error(ctx, "checksumPath:%s md5 is not equal with md5 file:%s", checksumPath, md5Path)
return false
}
func ChecksumFilesWithCheckInfo(root string, checkInfo map[string]*ChecksumInfo, files []string) error {
for _, file := range files {
fileInfo, err := GetFileInfo(root, file)
if err != nil {
return err
}
originInfo, ok := checkInfo[file]
if !ok || originInfo == nil {
return fmt.Errorf("filename:%s origin check info invalid", file)
}
if (fileInfo.Crc32Val != originInfo.Crc32Val) || (fileInfo.FileSize != originInfo.FileSize) {
return fmt.Errorf("filename:%s checksum not equal", file)
}
}
return nil
}
func GetFileInfo(root, fileName string) (*ChecksumInfo, error) {
filePath := filepath.Join(root, fileName)
info, err := os.Stat(filePath)
if err != nil {
return nil, err
}
fd, err := os.Open(filePath) //path是相对路径
if err != nil {
return nil, err
}
defer fd.Close()
value, _ := ChecksumCrc32(fd)
return &ChecksumInfo{
FilePath: fileName,
Crc32Val: strconv.FormatUint(uint64(value), 10),
FileSize: strconv.FormatUint(uint64(info.Size()), 10),
}, nil
}
// GenerateChecksumFile 生成checksum文件
func GenerateChecksumFile(ctx context.Context, folder string, checksumName string) (checkSumPath string, err error) {
return GenerateChecksumFileWithIgnore(ctx, folder, checksumName, []string{})
}
// GenerateChecksumFileWithIgnore 在排除某些文件的基础上,生成checksum
func GenerateChecksumFileWithIgnore(ctx context.Context, folder string, checksumName string, ignores []string) (checkSumPath string, err error) {
if folder == "" || checksumName == "" {
err = errors.New("folder or checksum can`t be empty")
return
}
folder = AddFolderSuffix(folder)
paths, err := PopulateFilePathsRecursively(ctx, folder, ignores)
if err != nil {
log.Error(ctx, "open folder failed. err:%v", err)
return
}
builder := strings.Builder{}
for idx, path := range paths {
err = func() error {
file, err := os.Open(filepath.Join(folder, path))
if err != nil {
log.Error(ctx, "open filepath:%s failed. err:%v", path, err)
return err
}
defer file.Close()
checksumCrc32, err := ChecksumCrc32(file)
if err != nil {
log.Error(ctx, "get filepath:%s crc32 val failed. err:%v", path, err)
return err
}
fileInfo, _ := file.Stat()
builder.WriteString(fmt.Sprintf("%v|%v|%v", strings.ReplaceAll(path, string(os.PathSeparator), "/"), checksumCrc32, fileInfo.Size()))
if idx < len(paths)-1 {
builder.WriteString("\n")
}
return nil
}()
if err != nil {
return "", err
}
}
// 生成文件checksum文件
checkSumPath = filepath.Join(folder, fmt.Sprintf("%s.checksum", checksumName))
err = ioutil.WriteFile(checkSumPath, []byte(builder.String()), OS_FILE_R_W)
return
}
// GenerateChecksumMd5File 生成checksum.md5文件
func GenerateChecksumMd5File(ctx context.Context, checksumPath string) (checksumMd5Path string, err error) {
//打开文件
file, err := os.Open(checksumPath)
if err != nil {
log.Error(ctx, "open checksumPath:%s error:%v", checksumPath, err)
return
}
defer file.Close()
// 生成md5
md5Handle := md5.New()
_, err = io.Copy(md5Handle, file)
if err != nil {
log.Error(ctx, "copy file failed, err:%v", err)
return
}
md5Val := md5Handle.Sum(nil)
hexVal := hex.EncodeToString(md5Val)
log.Info(ctx, "checksumPath:%s generate md5 val: %s", checksumPath, hexVal)
checksumMd5Path = checksumPath + ".md5"
// 写入文件
err = ioutil.WriteFile(checksumMd5Path, []byte(hexVal), OS_FILE_R_W)
return
}
// PopulateFilePathsRecursively 递归获取文件夹内所有文件的路径
func PopulateFilePathsRecursively(ctx context.Context, folder string, ignores []string) ([]string, error) {
paths := make([]string, 0)
filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Error(ctx, "folder [%v] scan meet some err: %v, should skip", folder, err)
return err
}
relPath, _ := filepath.Rel(folder, path)
isContain, _ := utils.StringsInArray(ignores, relPath)
if info.IsDir() && isContain {
return filepath.SkipDir
}
if info.IsDir() || isContain {
return nil
}
paths = append(paths, relPath)
log.Debug(ctx, "folder: %v", info.Name())
return nil
})
return paths, nil
}
func GetChecksumInfo(ctx context.Context, checksumPath string) (checkInfo map[string]*ChecksumInfo, err error) {
checkInfo = make(map[string]*ChecksumInfo)
_, err = os.Stat(checksumPath)
if err != nil {
return
}
content, err := ioutil.ReadFile(checksumPath)
if err != nil {
log.Error(ctx, "read checksum file error:%v", err)
return checkInfo, err
}
reader := bufio.NewReader(bytes.NewReader(content))
for {
line, _, lineErr := reader.ReadLine()
if lineErr == io.EOF {
break
}
spVals := strings.Split(string(line), "|")
if len(spVals) == 3 {
checkInfo[spVals[0]] = &ChecksumInfo{
FilePath: spVals[0],
Crc32Val: spVals[1],
FileSize: spVals[2],
}
} else {
return checkInfo, errors.New("checksum file content invalid")
}
}
return checkInfo, nil
}
func GetCheckFileList(ctx context.Context, checkInfo map[string]*ChecksumInfo) []string {
var files []string
for k, _ := range checkInfo {
files = append(files, k)
}
return files
}
func RelWalkInfo(ctx context.Context, root string, ignores ...string) ([]string, error) {
paths, err := WalkInfo(ctx, root, ignores...)
if err != nil {
return nil, err
}
var relPaths []string
for _, path := range paths {
relPath, _ := filepath.Rel(root, path)
relPaths = append(relPaths, strings.ReplaceAll(relPath, string(os.PathSeparator), "/"))
}
return relPaths, nil
}
func WalkInfo(ctx context.Context, root string, ignores ...string) ([]string, error) {
var err error
var fds []os.FileInfo
var files []string
if _, err = os.Lstat(root); err != nil {
return nil, err
}
if fds, err = ioutil.ReadDir(root); err != nil {
return nil, err
}
for _, fd := range fds {
if ok, _ := utils.StringsInArray(ignores, fd.Name()); ok {
log.Debug(context.Background(), "walk info ignore file path:%v", fd.Name())
continue
}
if fd.IsDir() {
subFiles, err := WalkInfo(ctx, root, ignores...)
if err != nil {
return nil, err
}
files = append(files, subFiles...)
} else {
files = append(files, filepath.Join(root, fd.Name()))
}
}
return files, nil
}