]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-xfer/main.go
Raise copyright years
[nncp.git] / src / cmd / nncp-xfer / main.go
index df7acd3b9cc3f0b2b881f4f85073ff51ec40e2b7..50e9bf416fb63ce80a8debf0460f85d83d166982 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 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
@@ -128,7 +128,7 @@ func main() {
                goto Tx
        }
        fis, err = dir.Readdir(0)
-       dir.Close()
+       dir.Close() // #nosec G104
        if err != nil {
                ctx.LogE("nncp-xfer", sds, err, "read")
                isBad = true
@@ -159,7 +159,7 @@ func main() {
                        continue
                }
                fisInt, err := dir.Readdir(0)
-               dir.Close()
+               dir.Close() // #nosec G104
                if err != nil {
                        ctx.LogE("nncp-xfer", sds, err, "read")
                        isBad = true
@@ -186,21 +186,23 @@ func main() {
                        _, err = xdr.Unmarshal(fd, &pktEnc)
                        if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
                                ctx.LogD("nncp-xfer", sds, "is not a packet")
-                               fd.Close()
+                               fd.Close() // #nosec G104
                                continue
                        }
                        if pktEnc.Nice > nice {
                                ctx.LogD("nncp-xfer", sds, "too nice")
-                               fd.Close()
+                               fd.Close() // #nosec G104
                                continue
                        }
                        sds["size"] = fiInt.Size()
                        if !ctx.IsEnoughSpace(fiInt.Size()) {
                                ctx.LogE("nncp-xfer", sds, errors.New("is not enough space"), "")
-                               fd.Close()
+                               fd.Close() // #nosec G104
                                continue
                        }
-                       fd.Seek(0, 0)
+                       if _, err = fd.Seek(0, 0); err != nil {
+                               log.Fatalln(err)
+                       }
                        tmp, err := ctx.NewTmpFileWHash()
                        if err != nil {
                                log.Fatalln(err)
@@ -209,20 +211,25 @@ func main() {
                        go func() {
                                _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
                                if err == nil {
-                                       w.Close()
-                                       return
+                                       err = w.Close()
+                               }
+                               if err != nil {
+                                       ctx.LogE("nncp-xfer", sds, err, "copy")
+                                       w.CloseWithError(err) // #nosec G104
                                }
-                               ctx.LogE("nncp-xfer", sds, err, "copy")
-                               w.CloseWithError(err)
                        }()
-                       if _, err = nncp.CopyProgressed(tmp.W, r, nncp.SdsAdd(sds, nncp.SDS{
-                               "pkt":      filename,
-                               "fullsize": sds["size"],
-                       }), ctx.ShowPrgrs); err != nil {
+                       if _, err = nncp.CopyProgressed(
+                               tmp.W, r, "Rx",
+                               nncp.SdsAdd(sds, nncp.SDS{
+                                       "pkt":      filename,
+                                       "fullsize": sds["size"],
+                               }),
+                               ctx.ShowPrgrs,
+                       ); err != nil {
                                ctx.LogE("nncp-xfer", sds, err, "copy")
                                isBad = true
                        }
-                       fd.Close()
+                       fd.Close() // #nosec G104
                        if isBad {
                                tmp.Cancel()
                                continue
@@ -258,7 +265,7 @@ Tx:
                        ctx.LogD("nncp-xfer", sds, "skip")
                        continue
                }
-               dirLock, err := ctx.LockDir(&nodeId, nncp.TTx)
+               dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
                if err != nil {
                        continue
                }
@@ -309,23 +316,23 @@ Tx:
                        sds["pkt"] = pktName
                        if job.PktEnc.Nice > nice {
                                ctx.LogD("nncp-xfer", sds, "too nice")
-                               job.Fd.Close()
+                               job.Fd.Close() // #nosec G104
                                continue
                        }
                        if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
                                ctx.LogD("nncp-xfer", sds, "already exists")
-                               job.Fd.Close()
+                               job.Fd.Close() // #nosec G104
                                continue
                        }
                        if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
                                ctx.LogD("nncp-xfer", sds, "already exists")
-                               job.Fd.Close()
+                               job.Fd.Close() // #nosec G104
                                continue
                        }
                        tmp, err := nncp.TempFile(dstPath, "xfer")
                        if err != nil {
                                ctx.LogE("nncp-xfer", sds, err, "mktemp")
-                               job.Fd.Close()
+                               job.Fd.Close() // #nosec G104
                                isBad = true
                                break
                        }
@@ -333,31 +340,32 @@ Tx:
                        ctx.LogD("nncp-xfer", sds, "created")
                        bufW := bufio.NewWriter(tmp)
                        copied, err := nncp.CopyProgressed(
-                               bufW,
-                               bufio.NewReader(job.Fd),
+                               bufW, bufio.NewReader(job.Fd), "Tx",
                                nncp.SdsAdd(sds, nncp.SDS{"fullsize": job.Size}),
                                ctx.ShowPrgrs,
                        )
-                       job.Fd.Close()
+                       job.Fd.Close() // #nosec G104
                        if err != nil {
                                ctx.LogE("nncp-xfer", sds, err, "copy")
-                               tmp.Close()
+                               tmp.Close() // #nosec G104
                                isBad = true
                                continue
                        }
                        if err = bufW.Flush(); err != nil {
-                               tmp.Close()
+                               tmp.Close() // #nosec G104
                                ctx.LogE("nncp-xfer", sds, err, "flush")
                                isBad = true
                                continue
                        }
                        if err = tmp.Sync(); err != nil {
-                               tmp.Close()
+                               tmp.Close() // #nosec G104
                                ctx.LogE("nncp-xfer", sds, err, "sync")
                                isBad = true
                                continue
                        }
-                       tmp.Close()
+                       if err = tmp.Close(); err != nil {
+                               ctx.LogE("nncp-xfer", sds, err, "sync")
+                       }
                        if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
                                ctx.LogE("nncp-xfer", sds, err, "rename")
                                isBad = true
@@ -368,7 +376,7 @@ Tx:
                                isBad = true
                                continue
                        }
-                       os.Remove(filepath.Join(dstPath, pktName+".part"))
+                       os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
                        delete(sds, "tmp")
                        ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"size": copied}), "")
                        if !*keep {