]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/check.go
Logging refactoring, no centralized humanizer
[nncp.git] / src / check.go
index 824f0c706558a5c4f0f8c81c2c63205a1a7a73d9..a8a32e2c7899bcb68c4c0f6b23394f72312c2aac 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2020 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -21,18 +21,23 @@ import (
        "bufio"
        "bytes"
        "errors"
+       "fmt"
        "io"
        "log"
+       "os"
+       "path/filepath"
 
        "golang.org/x/crypto/blake2b"
 )
 
-func Check(src io.Reader, checksum []byte, sds SDS, showPrgrs bool) (bool, error) {
+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", sds, showPrgrs); err != nil {
+       if _, err = CopyProgressed(hsh, bufio.NewReader(src), "check", les, showPrgrs); err != nil {
                return false, err
        }
        return bytes.Compare(hsh.Sum(nil), checksum) == 0, nil
@@ -41,21 +46,30 @@ func Check(src io.Reader, checksum []byte, sds SDS, showPrgrs bool) (bool, error
 func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
        isBad := false
        for job := range ctx.Jobs(nodeId, xx) {
-               sds := SDS{
-                       "xx":       string(xx),
-                       "node":     nodeId,
-                       "pkt":      Base32Codec.EncodeToString(job.HshValue[:]),
-                       "fullsize": job.Size,
+               pktName := Base32Codec.EncodeToString(job.HshValue[:])
+               les := LEs{
+                       {"XX", string(xx)},
+                       {"Node", nodeId},
+                       {"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("checking", les, err, logMsg)
+                       return true
                }
-               gut, err := Check(job.Fd, job.HshValue[:], sds, ctx.ShowPrgrs)
-               job.Fd.Close() // #nosec G104
+               gut, err := Check(fd, job.HshValue[:], les, ctx.ShowPrgrs)
+               fd.Close() // #nosec G104
                if err != nil {
-                       ctx.LogE("check", sds, err, "")
+                       ctx.LogE("checking", les, err, logMsg)
                        return true
                }
                if !gut {
                        isBad = true
-                       ctx.LogE("check", sds, errors.New("bad"), "")
+                       ctx.LogE("checking", les, errors.New("bad"), logMsg)
                }
        }
        return isBad
@@ -64,3 +78,47 @@ func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
 func (ctx *Ctx) Check(nodeId *NodeId) bool {
        return !(ctx.checkXxIsBad(nodeId, TRx) || ctx.checkXxIsBad(nodeId, TTx))
 }
+
+func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[32]byte) (int64, error) {
+       dirToSync := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
+       pktName := Base32Codec.EncodeToString(hshValue[:])
+       pktPath := filepath.Join(dirToSync, pktName)
+       fd, err := os.Open(pktPath + NoCKSuffix)
+       if err != nil {
+               return 0, err
+       }
+       defer fd.Close()
+       fi, err := fd.Stat()
+       if err != nil {
+               return 0, err
+       }
+       defer fd.Close()
+       size := fi.Size()
+       les := LEs{
+               {"XX", string(TRx)},
+               {"Node", nodeId},
+               {"Pkt", pktName},
+               {"FullSize", size},
+       }
+       gut, err := Check(fd, hshValue[:], les, ctx.ShowPrgrs)
+       if err != nil || !gut {
+               return 0, errors.New("checksum mismatch")
+       }
+       if err = os.Rename(pktPath+NoCKSuffix, pktPath); err != nil {
+               return 0, err
+       }
+       if err = DirSync(dirToSync); err != nil {
+               return size, err
+       }
+       if ctx.HdrUsage {
+               if _, err = fd.Seek(0, io.SeekStart); err != nil {
+                       return size, err
+               }
+               _, pktEncRaw, err := ctx.HdrRead(fd)
+               if err != nil {
+                       return size, err
+               }
+               ctx.HdrWrite(pktEncRaw, pktPath)
+       }
+       return size, err
+}