]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-xfer/main.go
Use explicitly larger bufio's buffer
[nncp.git] / src / cmd / nncp-xfer / main.go
index a349ea27d910143e11e255d03a43e1a73888fac9..dc522a85dbfc0818e07b79d405a3898d34646c11 100644 (file)
@@ -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 {
@@ -275,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()
                                }
@@ -284,7 +304,7 @@ func main() {
                                        w.CloseWithError(err)
                                }
                        }()
-                       if _, err = nncp.CopyProgressed(
+                       _, err = nncp.CopyProgressed(
                                tmp.W, r, "Rx",
                                append(
                                        les,
@@ -292,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(
@@ -425,14 +456,6 @@ Tx:
                                })
                                continue
                        }
-                       if _, err = os.Stat(filepath.Join(
-                               dstPath, nncp.SeenDir, pktName,
-                       )); 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 {
@@ -456,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,
                        )