"bufio"
"bytes"
"errors"
+ "fmt"
"io"
- "log"
"os"
"path/filepath"
-
- "golang.org/x/crypto/blake2b"
)
const NoCKSuffix = ".nock"
-func Check(src io.Reader, checksum []byte, les LEs, showPrgrs bool) (bool, error) {
- hsh, err := blake2b.New256(nil)
- if err != nil {
- log.Fatalln(err)
- }
- if _, err = CopyProgressed(hsh, bufio.NewReader(src), "check", les, showPrgrs); err != nil {
+func Check(
+ src io.Reader,
+ size int64,
+ checksum []byte,
+ les LEs,
+ showPrgrs bool,
+) (bool, error) {
+ hsh := MTHNew(size, 0)
+ if _, err := CopyProgressed(hsh, bufio.NewReaderSize(src, MTHSize), "check", les, showPrgrs); err != nil {
return false, err
}
return bytes.Compare(hsh.Sum(nil), checksum) == 0, nil
func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
isBad := false
for job := range ctx.Jobs(nodeId, xx) {
+ pktName := Base32Codec.EncodeToString(job.HshValue[:])
les := LEs{
{"XX", string(xx)},
{"Node", nodeId},
- {"Pkt", Base32Codec.EncodeToString(job.HshValue[:])},
+ {"Pkt", pktName},
{"FullSize", job.Size},
}
+ logMsg := func(les LEs) string {
+ return fmt.Sprintf("Checking: %s/%s/%s", nodeId, string(xx), pktName)
+ }
fd, err := os.Open(job.Path)
if err != nil {
- ctx.LogE("check", les, err, "")
+ ctx.LogE("checking", les, err, logMsg)
return true
}
- gut, err := Check(fd, job.HshValue[:], les, ctx.ShowPrgrs)
+ gut, err := Check(fd, job.Size, job.HshValue[:], les, ctx.ShowPrgrs)
fd.Close() // #nosec G104
if err != nil {
- ctx.LogE("check", les, err, "")
+ ctx.LogE("checking", les, err, logMsg)
return true
}
if !gut {
isBad = true
- ctx.LogE("check", les, errors.New("bad"), "")
+ ctx.LogE("checking", les, errors.New("bad"), logMsg)
}
}
return isBad
return !(ctx.checkXxIsBad(nodeId, TRx) || ctx.checkXxIsBad(nodeId, TTx))
}
-func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[32]byte) (int64, error) {
+func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[MTHSize]byte, mth *MTH) (int64, error) {
dirToSync := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
pktName := Base32Codec.EncodeToString(hshValue[:])
pktPath := filepath.Join(dirToSync, pktName)
if err != nil {
return 0, err
}
- defer fd.Close()
size := fi.Size()
les := LEs{
{"XX", string(TRx)},
{"Pkt", pktName},
{"FullSize", size},
}
- gut, err := Check(fd, hshValue[:], les, ctx.ShowPrgrs)
+ var gut bool
+ if mth == nil {
+ gut, err = Check(fd, size, hshValue[:], les, ctx.ShowPrgrs)
+ } else {
+ mth.PktName = pktName
+ if _, err = mth.PrependFrom(bufio.NewReaderSize(fd, MTHSize)); err != nil {
+ return 0, err
+ }
+ if bytes.Compare(mth.Sum(nil), hshValue[:]) == 0 {
+ gut = true
+ }
+ }
if err != nil || !gut {
return 0, errors.New("checksum mismatch")
}