]> Cypherpunks.ru repositories - nncp.git/blob - src/check.go
a8a32e2c7899bcb68c4c0f6b23394f72312c2aac
[nncp.git] / src / check.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package nncp
19
20 import (
21         "bufio"
22         "bytes"
23         "errors"
24         "fmt"
25         "io"
26         "log"
27         "os"
28         "path/filepath"
29
30         "golang.org/x/crypto/blake2b"
31 )
32
33 const NoCKSuffix = ".nock"
34
35 func Check(src io.Reader, checksum []byte, les LEs, showPrgrs bool) (bool, error) {
36         hsh, err := blake2b.New256(nil)
37         if err != nil {
38                 log.Fatalln(err)
39         }
40         if _, err = CopyProgressed(hsh, bufio.NewReader(src), "check", les, showPrgrs); err != nil {
41                 return false, err
42         }
43         return bytes.Compare(hsh.Sum(nil), checksum) == 0, nil
44 }
45
46 func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
47         isBad := false
48         for job := range ctx.Jobs(nodeId, xx) {
49                 pktName := Base32Codec.EncodeToString(job.HshValue[:])
50                 les := LEs{
51                         {"XX", string(xx)},
52                         {"Node", nodeId},
53                         {"Pkt", pktName},
54                         {"FullSize", job.Size},
55                 }
56                 logMsg := func(les LEs) string {
57                         return fmt.Sprintf("Checking: %s/%s/%s", nodeId, string(xx), pktName)
58                 }
59                 fd, err := os.Open(job.Path)
60                 if err != nil {
61                         ctx.LogE("checking", les, err, logMsg)
62                         return true
63                 }
64                 gut, err := Check(fd, job.HshValue[:], les, ctx.ShowPrgrs)
65                 fd.Close() // #nosec G104
66                 if err != nil {
67                         ctx.LogE("checking", les, err, logMsg)
68                         return true
69                 }
70                 if !gut {
71                         isBad = true
72                         ctx.LogE("checking", les, errors.New("bad"), logMsg)
73                 }
74         }
75         return isBad
76 }
77
78 func (ctx *Ctx) Check(nodeId *NodeId) bool {
79         return !(ctx.checkXxIsBad(nodeId, TRx) || ctx.checkXxIsBad(nodeId, TTx))
80 }
81
82 func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[32]byte) (int64, error) {
83         dirToSync := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
84         pktName := Base32Codec.EncodeToString(hshValue[:])
85         pktPath := filepath.Join(dirToSync, pktName)
86         fd, err := os.Open(pktPath + NoCKSuffix)
87         if err != nil {
88                 return 0, err
89         }
90         defer fd.Close()
91         fi, err := fd.Stat()
92         if err != nil {
93                 return 0, err
94         }
95         defer fd.Close()
96         size := fi.Size()
97         les := LEs{
98                 {"XX", string(TRx)},
99                 {"Node", nodeId},
100                 {"Pkt", pktName},
101                 {"FullSize", size},
102         }
103         gut, err := Check(fd, hshValue[:], les, ctx.ShowPrgrs)
104         if err != nil || !gut {
105                 return 0, errors.New("checksum mismatch")
106         }
107         if err = os.Rename(pktPath+NoCKSuffix, pktPath); err != nil {
108                 return 0, err
109         }
110         if err = DirSync(dirToSync); err != nil {
111                 return size, err
112         }
113         if ctx.HdrUsage {
114                 if _, err = fd.Seek(0, io.SeekStart); err != nil {
115                         return size, err
116                 }
117                 _, pktEncRaw, err := ctx.HdrRead(fd)
118                 if err != nil {
119                         return size, err
120                 }
121                 ctx.HdrWrite(pktEncRaw, pktPath)
122         }
123         return size, err
124 }