2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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(hsh, bufio.NewReaderSize(src, MTHSize), "check", les, showPrgrs); err != nil {
43 return bytes.Compare(hsh.Sum(nil), checksum) == 0, nil
46 func (ctx *Ctx) checkXxIsBad(nodeId *NodeId, xx TRxTx) bool {
48 for job := range ctx.Jobs(nodeId, xx) {
49 pktName := Base32Codec.EncodeToString(job.HshValue[:])
54 {"FullSize", job.Size},
56 logMsg := func(les LEs) string {
57 return fmt.Sprintf("Checking: %s/%s/%s", nodeId, string(xx), pktName)
59 fd, err := os.Open(job.Path)
61 ctx.LogE("checking", les, err, logMsg)
64 gut, err := Check(fd, job.Size, job.HshValue[:], les, ctx.ShowPrgrs)
67 ctx.LogE("checking", les, err, logMsg)
72 ctx.LogE("checking", les, errors.New("bad"), logMsg)
78 func (ctx *Ctx) Check(nodeId *NodeId) bool {
79 return !(ctx.checkXxIsBad(nodeId, TRx) || ctx.checkXxIsBad(nodeId, TTx))
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)
104 gut, err = Check(fd, size, hshValue[:], les, ctx.ShowPrgrs)
106 if _, err = mth.PreaddFrom(
107 bufio.NewReaderSize(fd, MTHSize),
108 pktName, ctx.ShowPrgrs,
112 if bytes.Compare(mth.Sum(nil), hshValue[:]) == 0 {
116 if err != nil || !gut {
117 return 0, errors.New("checksum mismatch")
119 if err = os.Rename(pktPath+NoCKSuffix, pktPath); err != nil {
122 if err = DirSync(dirToSync); err != nil {
126 if _, err = fd.Seek(0, io.SeekStart); err != nil {
129 _, pktEncRaw, err := ctx.HdrRead(fd)
133 ctx.HdrWrite(pktEncRaw, pktPath)