Skip to content

Commit 2fada37

Browse files
shuo-wuinnobead
authored andcommitted
Improve checksum calculation function
There is no need to use direct IO during the calculation. Signed-off-by: Shuo Wu <shuo.wu@suse.com>
1 parent ad727d3 commit 2fada37

File tree

1 file changed

+3
-15
lines changed

1 file changed

+3
-15
lines changed

pkg/util/util.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"github.com/sirupsen/logrus"
2121
"google.golang.org/grpc"
2222
"google.golang.org/grpc/connectivity"
23-
24-
"github.com/longhorn/sparse-tools/sparse"
2523
)
2624

2725
const (
@@ -39,25 +37,15 @@ func PrintJSON(obj interface{}) error {
3937
}
4038

4139
func GetFileChecksum(filePath string) (string, error) {
42-
f, err := sparse.NewDirectFileIoProcessor(filePath, os.O_RDONLY, 0)
40+
f, err := os.Open(filePath)
4341
if err != nil {
4442
return "", err
4543
}
4644
defer f.Close()
4745

48-
// 4MB
49-
buf := make([]byte, 1<<22)
5046
h := sha512.New()
51-
52-
for {
53-
nr, err := f.Read(buf)
54-
if err != nil {
55-
if err != io.EOF {
56-
return "", err
57-
}
58-
break
59-
}
60-
h.Write(buf[:nr])
47+
if _, err := io.Copy(h, f); err != nil {
48+
return "", err
6149
}
6250

6351
return hex.EncodeToString(h.Sum(nil)), nil

0 commit comments

Comments
 (0)