1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 const NoCKSuffix = ".nock"
37 hsh := MTHNew(size, 0)
38 if _, err := CopyProgressed(
40 bufio.NewReaderSize(src, MTHBlockSize),
41 "check", les, showPrgrs,
45 return bytes.Equal(hsh.Sum(nil), checksum), nil
48 func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
50 for job := range ctx.Jobs(nodeId, xx) {
51 pktName := Base32Codec.EncodeToString(job.HshValue[:])
56 {"FullSize", job.Size},
58 logMsg := func(les LEs) string {
59 return fmt.Sprintf("Checking: %s/%s/%s", nodeId, string(xx), pktName)
61 fd, err := os.Open(job.Path)
63 ctx.LogE("checking", les, err, logMsg)
66 gut, err := Check(fd, job.Size, job.HshValue[:], les, ctx.ShowPrgrs)
69 ctx.LogE("checking", les, err, logMsg)
74 ctx.LogE("checking", les, errors.New("bad"), logMsg)
80 func (ctx *Ctx) Check(nodeId *NodeId) bool {
81 return !(ctx.checkXxIsBad(nodeId, TRx) || ctx.checkXxIsBad(nodeId, TTx))
84 func (ctx *Ctx) CheckNoCK(nodeId *NodeId, hshValue *[MTHSize]byte, mth MTH) (int64, error) {
85 dirToSync := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
86 pktName := Base32Codec.EncodeToString(hshValue[:])
87 pktPath := filepath.Join(dirToSync, pktName)
88 fd, err := os.Open(pktPath + NoCKSuffix)
106 gut, err = Check(fd, size, hshValue[:], les, ctx.ShowPrgrs)
108 if _, err = mth.PreaddFrom(
109 bufio.NewReaderSize(fd, MTHBlockSize),
110 pktName, ctx.ShowPrgrs,
114 if bytes.Equal(mth.Sum(nil), hshValue[:]) {
118 if err != nil || !gut {
119 return 0, errors.New("checksum mismatch")
121 if err = os.Rename(pktPath+NoCKSuffix, pktPath); err != nil {
124 if err = DirSync(dirToSync); err != nil {
128 if _, err = fd.Seek(0, io.SeekStart); err != nil {
131 _, pktEncRaw, err := ctx.HdrRead(fd)
135 ctx.HdrWrite(pktEncRaw, pktPath)