Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions hashfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

const BLOCKSIZE = 2 << 10 << 7 // kb

func Hashfile(path string, hashAlgo string, perfMonBytes func(int64)) (string, error) {
func Hashfile(path string, hashAlgo string, perfMonBytes func(int64)) (string, int64, int64, error) {
var h hash.Hash
switch hashAlgo {
case "md5":
Expand All @@ -24,20 +24,28 @@ func Hashfile(path string, hashAlgo string, perfMonBytes func(int64)) (string, e
case "blake3":
h = blake3.New(32, nil)
default:
return "", errors.New("algo '" + hashAlgo + "' is unknown.")
return "", 0, 0, errors.New("algo '" + hashAlgo + "' is unknown.")
}

file, err := os.Open(path)
if err != nil {
return "", err
return "", 0, 0, err
}
defer file.Close()

// Get file info AFTER opening to avoid race condition
var info os.FileInfo
if info, err = file.Stat(); err != nil {
return "", 0, 0, err
}
mtime := int64(info.ModTime().UnixNano() / 1e6) // convert to ms
size := info.Size()

buf := make([]byte, BLOCKSIZE)
for {
bytesRead, err := file.Read(buf)
if err != nil && err != io.EOF {
return "", err
return "", 0, 0, err
}
if bytesRead == 0 {
break
Expand All @@ -47,7 +55,7 @@ func Hashfile(path string, hashAlgo string, perfMonBytes func(int64)) (string, e
perfMonBytes(int64(bytesRead))
}
}
return hex.EncodeToString(h.Sum(nil)), nil
return hex.EncodeToString(h.Sum(nil)), mtime, size, nil
}

func hashMd5(data []byte) string {
Expand Down
7 changes: 2 additions & 5 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,8 @@ func (i *Index) mtimeChanged(name string, ii idxInfo) bool {

func (i *Index) calcFile(name string, algo string) (*idxInfo, error) {
path := slpath.Join(i.path, name)
mtime, size, err := getMtS(path)
if err != nil {
return nil, err
}
hash, err := Hashfile(path, algo, i.context.perfMonBytes)

hash, mtime, size, err := Hashfile(path, algo, i.context.perfMonBytes)
if err != nil {
return nil, err
}
Expand Down
Loading