]> Cypherpunks.ru repositories - nncp.git/blob - src/check.go
Fix constant name typo
[nncp.git] / src / check.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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(
41                 hsh,
42                 bufio.NewReaderSize(src, MTHBlockSize),
43                 "check", les, showPrgrs,
44         ); err != nil {
45                 return false, err
46         }
47         return bytes.Compare(hsh.Sum(nil), checksum) == 0, nil
48 }
49
50 func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
51         isBad := false
52         for job := range ctx.Jobs(nodeId, xx) {
53                 pktName := Base32Codec.EncodeToString(job.HshValue[:])
54                 les := LEs{
55                         {"XX", string(xx)},
56                         {"Node", nodeId},
57                         {"Pkt", pktName},
58                         {"FullSize", job.Size},
59                 }
60                 logMsg := func(les LEs) string {
61                         return fmt.Sprintf("Checking: %s/%s/%s", nodeId, string(xx), pktName)
62                 }
63                 fd, err := os.Open(job.Path)
64                 if err != nil {
65                         ctx.LogE("checking", les, err, logMsg)
66                         return true
67                 }
68                 gut, err := Check(fd, job.Size, job.HshValue[:], les, ctx.ShowPrgrs)
69                 fd.Close()
70                 if err != nil {
71                         ctx.LogE("checking", les, err, logMsg)
72                         return true
73                 }
74                 if !gut {
75                         isBad = true
76                         ctx.LogE("checking", les, errors.New("bad"), logMsg)
77                 }
78         }
79         return isBad
80 }
81
82 func (ctx *Ctx) Check(nodeId *NodeId) bool {
83         return !(ctx.checkXxIsBad(nodeId, TRx) || ctx.checkXxIsBad(nodeId, TTx))
84 }
85
86 func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[MTHSize]byte, mth MTH) (int64, error) {
87         dirToSync := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
88         pktName := Base32Codec.EncodeToString(hshValue[:])
89         pktPath := filepath.Join(dirToSync, pktName)
90         fd, err := os.Open(pktPath + NoCKSuffix)
91         if err != nil {
92                 return 0, err
93         }
94         defer fd.Close()
95         fi, err := fd.Stat()
96         if err != nil {
97                 return 0, err
98         }
99         size := fi.Size()
100         les := LEs{
101                 {"XX", string(TRx)},
102                 {"Node", nodeId},
103                 {"Pkt", pktName},
104                 {"FullSize", size},
105         }
106         var gut bool
107         if mth == nil {
108                 gut, err = Check(fd, size, hshValue[:], les, ctx.ShowPrgrs)
109         } else {
110                 if _, err = mth.PreaddFrom(
111                         bufio.NewReaderSize(fd, MTHBlockSize),
112                         pktName, ctx.ShowPrgrs,
113                 ); err != nil {
114                         return 0, err
115                 }
116                 if bytes.Compare(mth.Sum(nil), hshValue[:]) == 0 {
117                         gut = true
118                 }
119         }
120         if err != nil || !gut {
121                 return 0, errors.New("checksum mismatch")
122         }
123         if err = os.Rename(pktPath+NoCKSuffix, pktPath); err != nil {
124                 return 0, err
125         }
126         if err = DirSync(dirToSync); err != nil {
127                 return size, err
128         }
129         if ctx.HdrUsage {
130                 if _, err = fd.Seek(0, io.SeekStart); err != nil {
131                         return size, err
132                 }
133                 _, pktEncRaw, err := ctx.HdrRead(fd)
134                 if err != nil {
135                         return size, err
136                 }
137                 ctx.HdrWrite(pktEncRaw, pktPath)
138         }
139         return size, err
140 }