]> Cypherpunks.ru repositories - nncp.git/blob - src/check.go
MTH
[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         "os"
27         "path/filepath"
28 )
29
30 const NoCKSuffix = ".nock"
31
32 func Check(
33         src io.Reader,
34         size int64,
35         checksum []byte,
36         les LEs,
37         showPrgrs bool,
38 ) (bool, error) {
39         hsh := MTHNew(size, 0)
40         if _, err := CopyProgressed(hsh, bufio.NewReaderSize(src, MTHSize), "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.Size, 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 *[MTHSize]byte, mth *MTH) (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         size := fi.Size()
96         les := LEs{
97                 {"XX", string(TRx)},
98                 {"Node", nodeId},
99                 {"Pkt", pktName},
100                 {"FullSize", size},
101         }
102         var gut bool
103         if mth == nil {
104                 gut, err = Check(fd, size, hshValue[:], les, ctx.ShowPrgrs)
105         } else {
106                 mth.PktName = pktName
107                 if _, err = mth.PrependFrom(bufio.NewReaderSize(fd, MTHSize)); err != nil {
108                         return 0, err
109                 }
110                 if bytes.Compare(mth.Sum(nil), hshValue[:]) == 0 {
111                         gut = true
112                 }
113         }
114         if err != nil || !gut {
115                 return 0, errors.New("checksum mismatch")
116         }
117         if err = os.Rename(pktPath+NoCKSuffix, pktPath); err != nil {
118                 return 0, err
119         }
120         if err = DirSync(dirToSync); err != nil {
121                 return size, err
122         }
123         if ctx.HdrUsage {
124                 if _, err = fd.Seek(0, io.SeekStart); err != nil {
125                         return size, err
126                 }
127                 _, pktEncRaw, err := ctx.HdrRead(fd)
128                 if err != nil {
129                         return size, err
130                 }
131                 ctx.HdrWrite(pktEncRaw, pktPath)
132         }
133         return size, err
134 }