]> 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 9c29b0284b1975b4da44fcd927a3cb5841901a32..d92e8b9dd8c807868c449bbd3b785de30f5bcf0c 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2022 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,6 +24,7 @@ import (
        "flag"
        "fmt"
        "io"
+       "io/fs"
        "log"
        "os"
        "path/filepath"
@@ -33,8 +34,7 @@ import (
 )
 
 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,7 +140,7 @@ func main() {
                isBad = true
                goto Tx
        }
-       fis, err = dir.Readdir(0)
+       entries, err = dir.ReadDir(0)
        dir.Close()
        if err != nil {
                ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
@@ -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"
@@ -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
@@ -215,7 +215,7 @@ func main() {
                                string(nncp.TRx),
                                nncp.SeenDir,
                                fiInt.Name(),
-                       )); err == nil || !os.IsNotExist(err) {
+                       )); err == nil || !errors.Is(err, fs.ErrNotExist) {
                                ctx.LogI("xfer-rx-seen", les, func(les nncp.LEs) string {
                                        return logMsg(les) + ": packet already seen"
                                })
@@ -293,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()
                                }
@@ -387,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"
                                })
@@ -418,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)
@@ -448,7 +450,7 @@ 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"
                                })
@@ -477,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,
                        )