]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/tx.go
More errors checking
[nncp.git] / src / tx.go
index 7d497769031695cfd058317b3511ca851028592d..4c31d92ad3e127a983dcd7edb4f9f7ac54673c0e 100644 (file)
--- a/src/tx.go
+++ b/src/tx.go
@@ -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-2020 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
@@ -51,6 +51,7 @@ func (ctx *Ctx) Tx(
        nice uint8,
        size, minSize int64,
        src io.Reader,
+       pktName string,
 ) (*Node, error) {
        hops := make([]*Node, 0, 1+len(node.Via))
        hops = append(hops, node)
@@ -81,11 +82,11 @@ func (ctx *Ctx) Tx(
        go func(size int64, src io.Reader, dst io.WriteCloser) {
                ctx.LogD("tx", SDS{
                        "node": hops[0].Id,
-                       "nice": strconv.Itoa(int(nice)),
-                       "size": strconv.FormatInt(size, 10),
+                       "nice": int(nice),
+                       "size": size,
                }, "wrote")
                errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, padSize, src, dst)
-               dst.Close()
+               dst.Close() // #nosec G104
        }(curSize, src, pipeW)
        curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize) + padSize
 
@@ -97,28 +98,32 @@ func (ctx *Ctx) Tx(
                go func(node *Node, pkt *Pkt, size int64, src io.Reader, dst io.WriteCloser) {
                        ctx.LogD("tx", SDS{
                                "node": node.Id,
-                               "nice": strconv.Itoa(int(nice)),
-                               "size": strconv.FormatInt(size, 10),
+                               "nice": int(nice),
+                               "size": size,
                        }, "trns wrote")
                        errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst)
-                       dst.Close()
+                       dst.Close() // #nosec G104
                }(hops[i], pktTrns, curSize, pipeRPrev, pipeW)
                curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize)
        }
        go func() {
-               _, err := io.Copy(tmp.W, pipeR)
+               _, err := CopyProgressed(
+                       tmp.W, pipeR, "Tx",
+                       SDS{"pkt": pktName, "fullsize": curSize},
+                       ctx.ShowPrgrs,
+               )
                errs <- err
        }()
        for i := 0; i <= len(hops); i++ {
                err = <-errs
                if err != nil {
-                       tmp.Fd.Close()
+                       tmp.Fd.Close() // #nosec G104
                        return nil, err
                }
        }
        nodePath := filepath.Join(ctx.Spool, lastNode.Id.String())
        err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
-       os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name))
+       os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name)) // #nosec G104
        return lastNode, err
 }
 
@@ -135,7 +140,7 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize
                        rerr = err
                        return
                }
-               os.Remove(src.Name())
+               os.Remove(src.Name()) // #nosec G104
                tmpW := bufio.NewWriter(src)
                tmpKey := make([]byte, chacha20poly1305.KeySize)
                if _, rerr = rand.Read(tmpKey[:]); rerr != nil {
@@ -154,13 +159,17 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize
                }
                fileSize = int64(written)
                if err = tmpW.Flush(); err != nil {
+                       rerr = err
+                       return
+               }
+               if _, err = src.Seek(0, io.SeekStart); err != nil {
+                       rerr = err
                        return
                }
-               src.Seek(0, io.SeekStart)
                r, w := io.Pipe()
                go func() {
                        if _, err := aeadProcess(aead, nonce, false, bufio.NewReader(src), w); err != nil {
-                               w.CloseWithError(err)
+                               w.CloseWithError(err) // #nosec G104
                        }
                }()
                reader = r
@@ -239,7 +248,7 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize
        closer = DummyCloser{}
        fileSize += 2 * TarBlockSize // termination block
 
-       go func() {
+       go func() error {
                tarWr := tar.NewWriter(w)
                hdr := tar.Header{
                        Typeflag: tar.TypeDir,
@@ -253,7 +262,7 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize
                        hdr.Name = basePath + e.path[len(rootPath):]
                        hdr.ModTime = e.modTime
                        if err = tarWr.WriteHeader(&hdr); err != nil {
-                               w.CloseWithError(err)
+                               return w.CloseWithError(err)
                        }
                }
                hdr.Typeflag = tar.TypeReg
@@ -263,20 +272,23 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize
                        hdr.ModTime = e.modTime
                        hdr.Size = e.size
                        if err = tarWr.WriteHeader(&hdr); err != nil {
-                               w.CloseWithError(err)
+                               return w.CloseWithError(err)
                        }
                        fd, err := os.Open(e.path)
                        if err != nil {
-                               w.CloseWithError(err)
+                               fd.Close() // #nosec G104
+                               return w.CloseWithError(err)
                        }
-                       _, err = io.Copy(tarWr, bufio.NewReader(fd))
-                       if err != nil {
-                               w.CloseWithError(err)
+                       if _, err = io.Copy(tarWr, bufio.NewReader(fd)); err != nil {
+                               fd.Close() // #nosec G104
+                               return w.CloseWithError(err)
                        }
-                       fd.Close()
+                       fd.Close() // #nosec G104
                }
-               tarWr.Close()
-               w.Close()
+               if err = tarWr.Close(); err != nil {
+                       return w.CloseWithError(err)
+               }
+               return w.Close()
        }()
        return
 }
@@ -286,7 +298,7 @@ func (ctx *Ctx) TxFile(
        nice uint8,
        srcPath, dstPath string,
        chunkSize int64,
-       minSize int64,
+       minSize, maxSize int64,
 ) error {
        dstPathSpecified := false
        if dstPath == "" {
@@ -308,6 +320,9 @@ func (ctx *Ctx) TxFile(
        if err != nil {
                return err
        }
+       if fileSize > maxSize {
+               return errors.New("Too big than allowed")
+       }
        if archived && !dstPathSpecified {
                dstPath += TarExt
        }
@@ -317,19 +332,19 @@ func (ctx *Ctx) TxFile(
                if err != nil {
                        return err
                }
-               _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
+               _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader, dstPath)
                sds := SDS{
                        "type": "file",
                        "node": node.Id,
-                       "nice": strconv.Itoa(int(nice)),
+                       "nice": int(nice),
                        "src":  srcPath,
                        "dst":  dstPath,
-                       "size": strconv.FormatInt(fileSize, 10),
+                       "size": fileSize,
                }
                if err == nil {
                        ctx.LogI("tx", sds, "sent")
                } else {
-                       ctx.LogE("tx", SdsAdd(sds, SDS{"err": err}), "sent")
+                       ctx.LogE("tx", sds, err, "sent")
                }
                return err
        }
@@ -372,19 +387,20 @@ func (ctx *Ctx) TxFile(
                        sizeToSend,
                        minSize,
                        io.TeeReader(reader, hsh),
+                       path,
                )
                sds := SDS{
                        "type": "file",
                        "node": node.Id,
-                       "nice": strconv.Itoa(int(nice)),
+                       "nice": int(nice),
                        "src":  srcPath,
                        "dst":  path,
-                       "size": strconv.FormatInt(sizeToSend, 10),
+                       "size": sizeToSend,
                }
                if err == nil {
                        ctx.LogI("tx", sds, "sent")
                } else {
-                       ctx.LogE("tx", SdsAdd(sds, SDS{"err": err}), "sent")
+                       ctx.LogE("tx", sds, err, "sent")
                        return err
                }
                hsh.Sum(metaPkt.Checksums[chunkNum][:0])
@@ -405,19 +421,19 @@ func (ctx *Ctx) TxFile(
                return err
        }
        metaPktSize := int64(metaBuf.Len())
-       _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
+       _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf, path)
        sds := SDS{
                "type": "file",
                "node": node.Id,
-               "nice": strconv.Itoa(int(nice)),
+               "nice": int(nice),
                "src":  srcPath,
                "dst":  path,
-               "size": strconv.FormatInt(metaPktSize, 10),
+               "size": metaPktSize,
        }
        if err == nil {
                ctx.LogI("tx", sds, "sent")
        } else {
-               ctx.LogE("tx", SdsAdd(sds, SDS{"err": err}), "sent")
+               ctx.LogE("tx", sds, err, "sent")
        }
        return err
 }
@@ -441,19 +457,19 @@ func (ctx *Ctx) TxFreq(
        }
        src := strings.NewReader(dstPath)
        size := int64(src.Len())
-       _, err = ctx.Tx(node, pkt, nice, size, minSize, src)
+       _, err = ctx.Tx(node, pkt, nice, size, minSize, src, srcPath)
        sds := SDS{
                "type":      "freq",
                "node":      node.Id,
-               "nice":      strconv.Itoa(int(nice)),
-               "replynice": strconv.Itoa(int(replyNice)),
+               "nice":      int(nice),
+               "replynice": int(replyNice),
                "src":       srcPath,
                "dst":       dstPath,
        }
        if err == nil {
                ctx.LogI("tx", sds, "sent")
        } else {
-               ctx.LogE("tx", SdsAdd(sds, SDS{"err": err}), "sent")
+               ctx.LogE("tx", sds, err, "sent")
        }
        return err
 }
@@ -483,25 +499,27 @@ func (ctx *Ctx) TxExec(
        if err != nil {
                return err
        }
-       _, err = io.Copy(compressor, in)
-       compressor.Close()
-       if err != nil {
+       if _, err = io.Copy(compressor, in); err != nil {
+               compressor.Close() // #nosec G104
+               return err
+       }
+       if err = compressor.Close(); err != nil {
                return err
        }
        size := int64(compressed.Len())
-       _, err = ctx.Tx(node, pkt, nice, size, minSize, &compressed)
+       _, err = ctx.Tx(node, pkt, nice, size, minSize, &compressed, handle)
        sds := SDS{
                "type":      "exec",
                "node":      node.Id,
-               "nice":      strconv.Itoa(int(nice)),
-               "replynice": strconv.Itoa(int(replyNice)),
+               "nice":      int(nice),
+               "replynice": int(replyNice),
                "dst":       strings.Join(append([]string{handle}, args...), " "),
-               "size":      strconv.FormatInt(size, 10),
+               "size":      size,
        }
        if err == nil {
                ctx.LogI("tx", sds, "sent")
        } else {
-               ctx.LogE("tx", SdsAdd(sds, SDS{"err": err}), "sent")
+               ctx.LogE("tx", sds, err, "sent")
        }
        return err
 }
@@ -510,20 +528,24 @@ func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error
        sds := SDS{
                "type": "trns",
                "node": node.Id,
-               "nice": strconv.Itoa(int(nice)),
-               "size": strconv.FormatInt(size, 10),
+               "nice": int(nice),
+               "size": size,
        }
        ctx.LogD("tx", sds, "taken")
        if !ctx.IsEnoughSpace(size) {
                err := errors.New("is not enough space")
-               ctx.LogE("tx", SdsAdd(sds, SDS{"err": err}), err.Error())
+               ctx.LogE("tx", sds, err, err.Error())
                return err
        }
        tmp, err := ctx.NewTmpFileWHash()
        if err != nil {
                return err
        }
-       if _, err = io.Copy(tmp.W, src); err != nil {
+       if _, err = CopyProgressed(
+               tmp.W, src, "Tx trns",
+               SDS{"pkt": node.Id.String(), "fullsize": size},
+               ctx.ShowPrgrs,
+       ); err != nil {
                return err
        }
        nodePath := filepath.Join(ctx.Spool, node.Id.String())
@@ -533,6 +555,6 @@ func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error
        } else {
                ctx.LogI("tx", SdsAdd(sds, SDS{"err": err}), "sent")
        }
-       os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name))
+       os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name)) // #nosec G104
        return err
 }