Skip to content

Commit 1bb249e

Browse files
committed
修复符号文件对比一致性时md5计算失败问题
- 新增计算字符串md5值方法,修复字符串计算md5错用文件md5值方法问题
1 parent 65cbaf2 commit 1bb249e

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

helper/file.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ func Copy(src, dst string) (written int64, err error) {
102102
return written, nil
103103
}
104104

105-
// Md5 计算文件的MD5
106-
func Md5(path string) (string, error) {
105+
// FileMd5 计算文件的MD5
106+
func FileMd5(path string) (string, error) {
107107
// 打开文件
108108
file, err := os.Open(path)
109109
if err != nil {
@@ -126,8 +126,8 @@ func Md5(path string) (string, error) {
126126
return md5Checksum, nil
127127
}
128128

129-
// Sha256 计算文件的SHA256
130-
func Sha256(path string) (string, error) {
129+
// FileSha256 计算文件的SHA256
130+
func FileSha256(path string) (string, error) {
131131
// 打开文件
132132
file, err := os.Open(path)
133133
if err != nil {

helper/string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package helper
22

33
import (
4+
"crypto/md5"
45
"crypto/rand"
6+
"encoding/hex"
57
"math"
68
"math/big"
79
"strings"
@@ -37,3 +39,11 @@ func RandomString(length int) (string, error) {
3739
}
3840
return string(b), nil
3941
}
42+
43+
// StringMd5 计算字符串的MD5值
44+
func StringMd5(str string) string {
45+
hash := md5.New()
46+
hash.Write([]byte(str))
47+
// 计算 MD5 校验和
48+
return hex.EncodeToString(hash.Sum(nil))
49+
}

storage.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (s *Storage) IsSameV2(ctx context.Context, localPath, remotePath string) bo
221221
case enum.SymlinkFile:
222222
if isDir, _ := helper.IsDir(localPath); !isDir {
223223
log.Debugf("SymlinkFile %s", localPath)
224-
localMd5, err = helper.Md5(localPath)
224+
localMd5, err = helper.FileMd5(localPath)
225225
if err != nil {
226226
log.Errorf("MD5 error: %s", err.Error())
227227
return false
@@ -237,11 +237,7 @@ func (s *Storage) IsSameV2(ctx context.Context, localPath, remotePath string) bo
237237
// 获取目标地址
238238
target, _ := helper.GetSymlinkTarget(localPath)
239239
// 计算md5值
240-
localMd5, err = helper.Md5(target)
241-
if err != nil {
242-
log.Errorf("MD5 error: %s", err.Error())
243-
return false
244-
}
240+
localMd5 = helper.StringMd5(target)
245241
default:
246242
return true
247243
}
@@ -285,7 +281,7 @@ func (s *Storage) IsSameV2(ctx context.Context, localPath, remotePath string) bo
285281

286282
// 计算本地文件的md5
287283
if localMd5 == "" {
288-
localMd5, _ = helper.Md5(localPath)
284+
localMd5, _ = helper.FileMd5(localPath)
289285
}
290286
log.Debugf("Compare %s, Local Md5: %s, Remote ETag: %s", localPath, localMd5, objectInfo.ETag)
291287
if strings.ToLower(localMd5) == strings.ToLower(objectInfo.ETag) {
@@ -327,7 +323,7 @@ func (s *Storage) IsSame(ctx context.Context, localPath, localMd5, remotePath st
327323

328324
// 计算本地文件的md5
329325
if localMd5 == "" {
330-
localMd5, _ = helper.Md5(localPath)
326+
localMd5, _ = helper.FileMd5(localPath)
331327
}
332328
log.Debugf("Compare %s, Local Md5: %s, Remote ETag: %s", remotePath, localMd5, objectInfo.ETag)
333329
if strings.ToLower(localMd5) == strings.ToLower(objectInfo.ETag) {

0 commit comments

Comments
 (0)