]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-xfer/main.go
Remove huge usage headers, -warranty exists anyway
[nncp.git] / src / cmd / nncp-xfer / main.go
index 0acba4b8edcb50804e8be030e66e9b5c8185383a..d92e8b9dd8c807868c449bbd3b785de30f5bcf0c 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
 
 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
@@ -24,17 +24,17 @@ import (
        "flag"
        "fmt"
        "io"
+       "io/fs"
        "log"
        "os"
        "path/filepath"
 
        "github.com/dustin/go-humanize"
-       "go.cypherpunks.ru/nncp/v7"
+       "go.cypherpunks.ru/nncp/v8"
 )
 
 func usage() {
-       fmt.Fprintf(os.Stderr, nncp.UsageHeader())
-       fmt.Fprintf(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
+       fmt.Fprint(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
        fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
        flag.PrintDefaults()
 }
@@ -105,7 +105,7 @@ func main() {
        selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
        isBad := false
        var dir *os.File
-       var fis []os.FileInfo
+       var entries []os.DirEntry
        var les nncp.LEs
        var logMsg func(les nncp.LEs) string
        if *txOnly {
@@ -120,7 +120,7 @@ func main() {
        }
        ctx.LogD("xfer-self", les, logMsg)
        if _, err = os.Stat(selfPath); err != nil {
-               if os.IsNotExist(err) {
+               if errors.Is(err, fs.ErrNotExist) {
                        ctx.LogD("xfer-self-no-dir", les, func(les nncp.LEs) string {
                                return logMsg(les) + ": no directory"
                        })
@@ -140,8 +140,8 @@ func main() {
                isBad = true
                goto Tx
        }
-       fis, err = dir.Readdir(0)
-       dir.Close() // #nosec G104
+       entries, err = dir.ReadDir(0)
+       dir.Close()
        if err != nil {
                ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
                        return logMsg(les) + ": reading"
@@ -149,12 +149,12 @@ func main() {
                isBad = true
                goto Tx
        }
-       for _, fi := range fis {
-               if !fi.IsDir() {
+       for _, entry := range entries {
+               if !entry.IsDir() {
                        continue
                }
-               nodeId, err := nncp.NodeIdFromString(fi.Name())
-               les := append(les, nncp.LE{K: "Node", V: fi.Name()})
+               nodeId, err := nncp.NodeIdFromString(entry.Name())
+               les := append(les, nncp.LE{K: "Node", V: entry.Name()})
                logMsg := func(les nncp.LEs) string {
                        return "Packet transfer, received from " + ctx.NodeName(nodeId)
                }
@@ -176,7 +176,7 @@ func main() {
                        })
                        continue
                }
-               dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
+               dir, err = os.Open(filepath.Join(selfPath, entry.Name()))
                if err != nil {
                        ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
                                return logMsg(les) + ": opening"
@@ -185,7 +185,7 @@ func main() {
                        continue
                }
                fisInt, err := dir.Readdir(0)
-               dir.Close() // #nosec G104
+               dir.Close()
                if err != nil {
                        ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
                                return logMsg(les) + ": reading"
@@ -194,7 +194,7 @@ func main() {
                        continue
                }
                for _, fiInt := range fisInt {
-                       if !fi.IsDir() {
+                       if !fiInt.IsDir() {
                                continue
                        }
                        // Check that it is valid Base32 encoding
@@ -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 || !errors.Is(err, fs.ErrNotExist) {
+                               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")
                                }
@@ -241,14 +261,14 @@ func main() {
                                                return logMsg(les) + ": not valid packet: " + err.Error()
                                        },
                                )
-                               fd.Close() // #nosec G104
+                               fd.Close()
                                continue
                        }
                        if pktEnc.Nice > nice {
                                ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
                                        return logMsg(les) + ": too nice"
                                })
-                               fd.Close() // #nosec G104
+                               fd.Close()
                                continue
                        }
                        les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
@@ -261,10 +281,10 @@ func main() {
                        }
                        if !ctx.IsEnoughSpace(fiInt.Size()) {
                                ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
-                               fd.Close() // #nosec G104
+                               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,16 +293,18 @@ 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()
                                }
                                if err != nil {
                                        ctx.LogE("xfer-rx", les, err, logMsg)
-                                       w.CloseWithError(err) // #nosec G104
+                                       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() // #nosec G104
-                       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(
@@ -356,7 +389,7 @@ Tx:
                }
                _, err = os.Stat(nodePath)
                if err != nil {
-                       if os.IsNotExist(err) {
+                       if errors.Is(err, fs.ErrNotExist) {
                                ctx.LogD("xfer-tx-not-exist", les, func(les nncp.LEs) string {
                                        return logMsg(les) + ": does not exist"
                                })
@@ -387,7 +420,7 @@ Tx:
                }
                _, err = os.Stat(dstPath)
                if err != nil {
-                       if os.IsNotExist(err) {
+                       if errors.Is(err, fs.ErrNotExist) {
                                if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
                                        ctx.UnlockDir(dirLock)
                                        ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
@@ -417,18 +450,12 @@ Tx:
                                })
                                continue
                        }
-                       if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
+                       if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !errors.Is(err, fs.ErrNotExist) {
                                ctx.LogD("xfer-tx-exists", les, func(les nncp.LEs) string {
                                        return logMsg(les) + ": already exists"
                                })
                                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 {
@@ -446,40 +473,42 @@ Tx:
                                ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
                                        return logMsg(les) + ": opening"
                                })
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                isBad = true
                                continue
                        }
                        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,
                        )
-                       fd.Close() // #nosec G104
+                       fd.Close()
                        if err != nil {
                                ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
                                        return logMsg(les) + ": copying"
                                })
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                isBad = true
                                continue
                        }
                        if err = bufW.Flush(); err != nil {
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
                                        return logMsg(les) + ": flushing"
                                })
                                isBad = true
                                continue
                        }
-                       if err = tmp.Sync(); err != nil {
-                               tmp.Close() // #nosec G104
-                               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 {
@@ -500,7 +529,7 @@ Tx:
                                isBad = true
                                continue
                        }
-                       os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
+                       os.Remove(filepath.Join(dstPath, pktName+".part"))
                        les = les[:len(les)-1]
                        ctx.LogI(
                                "xfer-tx",
@@ -518,7 +547,7 @@ Tx:
                                        })
                                        isBad = true
                                } else if ctx.HdrUsage {
-                                       os.Remove(job.Path + nncp.HdrSuffix)
+                                       os.Remove(nncp.JobPath2Hdr(job.Path))
                                }
                        }
                }