Refactor sync file reading

This commit is contained in:
nemunaire 2022-11-21 14:38:16 +01:00
commit 6ca71230c1
4 changed files with 74 additions and 62 deletions

View file

@ -1,7 +1,6 @@
package fic
import (
"bufio"
"crypto"
_ "crypto/sha1"
"encoding/hex"
@ -169,13 +168,15 @@ func minifyHash(hash string) string {
}
// CheckBufferHash checks if the bufio has the given digest.
func CreateHashBuffers() (io.Writer, *hash.Hash, *hash.Hash) {
func CreateHashBuffers(rd io.Reader) (*hash.Hash, *hash.Hash) {
hash160 := crypto.SHA1.New()
hash512 := crypto.BLAKE2b_512.New()
w := io.MultiWriter(hash160, hash512)
return w, &hash160, &hash512
io.Copy(w, rd)
return &hash160, &hash512
}
// CheckBufferHash checks if the bufio has the given digest.
@ -219,11 +220,7 @@ func checkFileHash(filePath string, digest []byte) (dgst []byte, size int64, err
defer fd.Close()
size = fi.Size()
w, hash160, hash512 := CreateHashBuffers()
if _, err = io.Copy(w, bufio.NewReader(fd)); err != nil {
return
}
hash160, hash512 := CreateHashBuffers(fd)
dgst, err = CheckBufferHash(hash160, hash512, digest)