Skip to content

Commit

Permalink
Improve checksum calculation function
Browse files Browse the repository at this point in the history
There is no need to use direct IO during the calculation.

Signed-off-by: Shuo Wu <shuo.wu@suse.com>
  • Loading branch information
shuo-wu authored and innobead committed May 31, 2022
1 parent ad727d3 commit 2fada37
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"

"github.com/longhorn/sparse-tools/sparse"
)

const (
Expand All @@ -39,25 +37,15 @@ func PrintJSON(obj interface{}) error {
}

func GetFileChecksum(filePath string) (string, error) {
f, err := sparse.NewDirectFileIoProcessor(filePath, os.O_RDONLY, 0)
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()

// 4MB
buf := make([]byte, 1<<22)
h := sha512.New()

for {
nr, err := f.Read(buf)
if err != nil {
if err != io.EOF {
return "", err
}
break
}
h.Write(buf[:nr])
if _, err := io.Copy(h, f); err != nil {
return "", err
}

return hex.EncodeToString(h.Sum(nil)), nil
Expand Down

0 comments on commit 2fada37

Please sign in to comment.