]> Cypherpunks.ru repositories - nncp.git/commitdiff
Simplify code, no performance penalty
authorSergey Matveev <stargrave@stargrave.org>
Wed, 22 Jan 2020 15:41:10 +0000 (18:41 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 23 Jan 2020 10:57:30 +0000 (13:57 +0300)
src/cmd/nncp-bundle/main.go

index 859fe6f8e5e38f05512d85f9127557e3c76bb690..c3613bd7d9d32a1fafe5782c602c9846c17f5afc 100644 (file)
@@ -182,22 +182,14 @@ func main() {
                }
        } else {
                bufStdin := bufio.NewReaderSize(os.Stdin, CopyBufSize*2)
-               var peeked []byte
-               var prefixIdx int
-               var tarR *tar.Reader
-               var entry *tar.Header
-               var exists bool
                pktEncBuf := make([]byte, nncp.PktEncOverhead)
                var pktEnc *nncp.PktEnc
-               var pktName string
-               var selfPath string
-               var dstPath string
                for {
-                       peeked, err = bufStdin.Peek(CopyBufSize)
+                       peeked, err := bufStdin.Peek(CopyBufSize)
                        if err != nil && err != io.EOF {
                                log.Fatalln("Error during reading:", err)
                        }
-                       prefixIdx = bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix))
+                       prefixIdx := bytes.Index(peeked, []byte(nncp.NNCPBundlePrefix))
                        if prefixIdx == -1 {
                                if err == io.EOF {
                                        break
@@ -208,7 +200,7 @@ func main() {
                        bufStdin.Discard(prefixIdx)
                        tarR = tar.NewReader(bufStdin)
                        sds["xx"] = string(nncp.TRx)
-                       entry, err = tarR.Next()
+                       entry, err := tarR.Next()
                        if err != nil {
                                if err != io.EOF {
                                        ctx.LogD(
@@ -243,7 +235,7 @@ func main() {
                                ctx.LogE("nncp-bundle", sds, errors.New("not enough spool space"), "")
                                continue
                        }
-                       pktName = filepath.Base(entry.Name)
+                       pktName := filepath.Base(entry.Name)
                        if _, err = nncp.Base32Codec.DecodeString(pktName); err != nil {
                                ctx.LogD("nncp-bundle", nncp.SdsAdd(sds, nncp.SDS{"err": "bad packet name"}), "")
                                continue
@@ -266,7 +258,7 @@ func main() {
                        }
                        if *pktEnc.Sender == *ctx.SelfId && *doDelete {
                                if len(nodeIds) > 0 {
-                                       if _, exists = nodeIds[*pktEnc.Recipient]; !exists {
+                                       if _, exists := nodeIds[*pktEnc.Recipient]; !exists {
                                                ctx.LogD("nncp-bundle", sds, "Recipient is not requested")
                                                continue
                                        }
@@ -275,7 +267,7 @@ func main() {
                                sds["xx"] = string(nncp.TTx)
                                sds["node"] = nodeId32
                                sds["pkt"] = pktName
-                               dstPath = filepath.Join(
+                               dstPath := filepath.Join(
                                        ctx.Spool,
                                        nodeId32,
                                        string(nncp.TTx),
@@ -314,7 +306,7 @@ func main() {
                                continue
                        }
                        if len(nodeIds) > 0 {
-                               if _, exists = nodeIds[*pktEnc.Sender]; !exists {
+                               if _, exists := nodeIds[*pktEnc.Sender]; !exists {
                                        ctx.LogD("nncp-bundle", sds, "Sender is not requested")
                                        continue
                                }
@@ -322,8 +314,8 @@ func main() {
                        sds["node"] = nncp.Base32Codec.EncodeToString(pktEnc.Recipient[:])
                        sds["pkt"] = pktName
                        sds["fullsize"] = entry.Size
-                       selfPath = filepath.Join(ctx.Spool, ctx.SelfId.String(), string(nncp.TRx))
-                       dstPath = filepath.Join(selfPath, pktName)
+                       selfPath := filepath.Join(ctx.Spool, ctx.SelfId.String(), string(nncp.TRx))
+                       dstPath := filepath.Join(selfPath, pktName)
                        if _, err = os.Stat(dstPath); err == nil || !os.IsNotExist(err) {
                                ctx.LogD("nncp-bundle", sds, "Packet already exists")
                                continue