2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
30 const NoCKSuffix = ".nock"
39 hsh := MTHNew(size, 0)
40 if _, err := CopyProgressed(
42 bufio.NewReaderSize(src, MTHBlockSize),
43 "check", les, showPrgrs,
47 return bytes.Equal(hsh.Sum(nil), checksum), nil
50 func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
52 for job := range ctx.Jobs(nodeId, xx) {
53 pktName := Base32Codec.EncodeToString(job.HshValue[:])
58 {"FullSize", job.Size},
60 logMsg := func(les LEs) string {
61 return fmt.Sprintf("Checking: %s/%s/%s", nodeId, string(xx), pktName)
63 fd, err := os.Open(job.Path)
65 ctx.LogE("checking", les, err, logMsg)
68 gut, err := Check(fd, job.Size, job.HshValue[:], les, ctx.ShowPrgrs)
71 ctx.LogE("checking", les, err, logMsg)
76 ctx.LogE("checking", les, errors.New("bad"), logMsg)
82 func (ctx *Ctx) Check(nodeId *NodeId) bool {
83 return !(ctx.checkXxIsBad(nodeId, TRx) || ctx.checkXxIsBad(nodeId, TTx))
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)
108 gut, err = Check(fd, size, hshValue[:], les, ctx.ShowPrgrs)
110 if _, err = mth.PreaddFrom(
111 bufio.NewReaderSize(fd, MTHBlockSize),
112 pktName, ctx.ShowPrgrs,
116 if bytes.Equal(mth.Sum(nil), hshValue[:]) {
120 if err != nil || !gut {
121 return 0, errors.New("checksum mismatch")
123 if err = os.Rename(pktPath+NoCKSuffix, pktPath); err != nil {
126 if err = DirSync(dirToSync); err != nil {
130 if _, err = fd.Seek(0, io.SeekStart); err != nil {
133 _, pktEncRaw, err := ctx.HdrRead(fd)
137 ctx.HdrWrite(pktEncRaw, pktPath)