X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcmd%2Fnncp-xfer%2Fmain.go;h=dc522a85dbfc0818e07b79d405a3898d34646c11;hb=c2b17fd0fc439e420710f53602604e8823c2fb1d;hp=fb7a0e0807e1aa8edb45af08acce79ac65907527;hpb=54b0f8a0e20847d666dd445bd92c282fd9ab5dec;p=nncp.git diff --git a/src/cmd/nncp-xfer/main.go b/src/cmd/nncp-xfer/main.go index fb7a0e0..dc522a8 100644 --- a/src/cmd/nncp-xfer/main.go +++ b/src/cmd/nncp-xfer/main.go @@ -1,6 +1,6 @@ /* NNCP -- Node to Node copy, utilities for store-and-forward data exchange -Copyright (C) 2016-2021 Sergey Matveev +Copyright (C) 2016-2022 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -29,7 +29,7 @@ import ( "path/filepath" "github.com/dustin/go-humanize" - "go.cypherpunks.ru/nncp/v7" + "go.cypherpunks.ru/nncp/v8" ) func usage() { @@ -209,6 +209,24 @@ func main() { ctx.NodeName(nodeId), filename, ) } + if _, err = os.Stat(filepath.Join( + ctx.Spool, + nodeId.String(), + string(nncp.TRx), + nncp.SeenDir, + fiInt.Name(), + )); err == nil || !os.IsNotExist(err) { + ctx.LogI("xfer-rx-seen", les, func(les nncp.LEs) string { + return logMsg(les) + ": packet already seen" + }) + if !*keep { + if err = os.Remove(filename); err != nil { + ctx.LogE("xfer-rx-remove", les, err, logMsg) + isBad = true + } + } + continue + } fd, err := os.Open(filename) if err != nil { ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string { @@ -218,7 +236,7 @@ func main() { continue } pktEnc, pktEncRaw, err := ctx.HdrRead(fd) - if err != nil { + if err == nil { switch pktEnc.Magic { case nncp.MagicNNCPEv1.B: err = nncp.MagicNNCPEv1.TooOld() @@ -229,6 +247,8 @@ func main() { case nncp.MagicNNCPEv4.B: err = nncp.MagicNNCPEv4.TooOld() case nncp.MagicNNCPEv5.B: + err = nncp.MagicNNCPEv5.TooOld() + case nncp.MagicNNCPEv6.B: default: err = errors.New("is not an encrypted packet") } @@ -264,7 +284,7 @@ func main() { fd.Close() continue } - if _, err = fd.Seek(0, 0); err != nil { + if _, err = fd.Seek(0, io.SeekStart); err != nil { log.Fatalln(err) } tmp, err := ctx.NewTmpFileWHash() @@ -273,7 +293,9 @@ func main() { } r, w := io.Pipe() go func() { - _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size()) + _, err := io.CopyN( + w, bufio.NewReaderSize(fd, nncp.MTHBlockSize), fiInt.Size(), + ) if err == nil { err = w.Close() } @@ -282,7 +304,7 @@ func main() { w.CloseWithError(err) } }() - if _, err = nncp.CopyProgressed( + _, err = nncp.CopyProgressed( tmp.W, r, "Rx", append( les, @@ -290,13 +312,24 @@ func main() { nncp.LE{K: "FullSize", V: fiInt.Size()}, ), ctx.ShowPrgrs, - ); err != nil { + ) + fd.Close() + if err != nil { ctx.LogE("xfer-rx", les, err, logMsg) + tmp.Cancel() isBad = true + continue } - fd.Close() - if isBad { + if err = tmp.W.Flush(); err != nil { + ctx.LogE("xfer-rx", les, err, logMsg) tmp.Cancel() + isBad = true + continue + } + if tmp.Checksum() != fiInt.Name() { + ctx.LogE("xfer-rx", les, errors.New("checksum mismatch"), logMsg) + tmp.Cancel() + isBad = true continue } if err = tmp.Commit(filepath.Join( @@ -423,12 +456,6 @@ Tx: }) continue } - if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) { - ctx.LogD("xfer-tx-seen", les, func(les nncp.LEs) string { - return logMsg(les) + ": already seen" - }) - continue - } tmp, err := nncp.TempFile(dstPath, "xfer") if err != nil { ctx.LogE("xfer-tx-mktemp", les, err, func(les nncp.LEs) string { @@ -452,7 +479,7 @@ Tx: } bufW := bufio.NewWriter(tmp) copied, err := nncp.CopyProgressed( - bufW, bufio.NewReader(fd), "Tx", + bufW, bufio.NewReaderSize(fd, nncp.MTHBlockSize), "Tx", append(les, nncp.LE{K: "FullSize", V: job.Size}), ctx.ShowPrgrs, ) @@ -473,13 +500,15 @@ Tx: isBad = true continue } - if err = tmp.Sync(); err != nil { - tmp.Close() - ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string { - return logMsg(les) + ": syncing" - }) - isBad = true - continue + if !nncp.NoSync { + if err = tmp.Sync(); err != nil { + tmp.Close() + ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string { + return logMsg(les) + ": syncing" + }) + isBad = true + continue + } } if err = tmp.Close(); err != nil { ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string { @@ -518,7 +547,7 @@ Tx: }) isBad = true } else if ctx.HdrUsage { - os.Remove(job.Path + nncp.HdrSuffix) + os.Remove(nncp.JobPath2Hdr(job.Path)) } } }