]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/tx.go
.hdr files
[nncp.git] / src / tx.go
index 3352f861a8465d63e95cfdacdce8a724fb203db5..3e57f9b865b1a56d99a0623edd602942f17a1dcf 100644 (file)
--- a/src/tx.go
+++ b/src/tx.go
@@ -62,7 +62,9 @@ func (ctx *Ctx) Tx(
        }
        expectedSize := size
        for i := 0; i < len(hops); i++ {
-               expectedSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+expectedSize)
+               expectedSize = PktEncOverhead +
+                       PktSizeOverhead +
+                       sizeWithTags(PktOverhead+expectedSize)
        }
        padSize := minSize - expectedSize
        if padSize < 0 {
@@ -79,16 +81,23 @@ func (ctx *Ctx) Tx(
        errs := make(chan error)
        curSize := size
        pipeR, pipeW := io.Pipe()
+       var pktEncRaw []byte
        go func(size int64, src io.Reader, dst io.WriteCloser) {
-               ctx.LogD("tx", SDS{
-                       "node": hops[0].Id,
-                       "nice": int(nice),
-                       "size": size,
+               ctx.LogD("tx", LEs{
+                       {"Node", hops[0].Id},
+                       {"Nice", int(nice)},
+                       {"Size", size},
                }, "wrote")
-               errs <- PktEncWrite(ctx.Self, hops[0], pkt, nice, size, padSize, src, dst)
+               pktEncRaw, err = PktEncWrite(
+                       ctx.Self, hops[0], pkt, nice, size, padSize, src, dst,
+               )
+               errs <- err
                dst.Close() // #nosec G104
        }(curSize, src, pipeW)
-       curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize) + padSize
+       curSize = PktEncOverhead +
+               PktSizeOverhead +
+               sizeWithTags(PktOverhead+curSize) +
+               padSize
 
        var pipeRPrev io.Reader
        for i := 1; i < len(hops); i++ {
@@ -96,12 +105,13 @@ func (ctx *Ctx) Tx(
                pipeRPrev = pipeR
                pipeR, pipeW = io.Pipe()
                go func(node *Node, pkt *Pkt, size int64, src io.Reader, dst io.WriteCloser) {
-                       ctx.LogD("tx", SDS{
-                               "node": node.Id,
-                               "nice": int(nice),
-                               "size": size,
+                       ctx.LogD("tx", LEs{
+                               {"Node", node.Id},
+                               {"Nice", int(nice)},
+                               {"Size", size},
                        }, "trns wrote")
-                       errs <- PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst)
+                       _, err := PktEncWrite(ctx.Self, node, pkt, nice, size, 0, src, dst)
+                       errs <- err
                        dst.Close() // #nosec G104
                }(hops[i], pktTrns, curSize, pipeRPrev, pipeW)
                curSize = PktEncOverhead + PktSizeOverhead + sizeWithTags(PktOverhead+curSize)
@@ -109,7 +119,7 @@ func (ctx *Ctx) Tx(
        go func() {
                _, err := CopyProgressed(
                        tmp.W, pipeR, "Tx",
-                       SDS{"pkt": pktName, "fullsize": curSize},
+                       LEs{{"Pkt", pktName}, {"FullSize", curSize}},
                        ctx.ShowPrgrs,
                )
                errs <- err
@@ -124,6 +134,12 @@ func (ctx *Ctx) Tx(
        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)) // #nosec G104
+       if err != nil {
+               return lastNode, err
+       }
+       if ctx.HdrUsage {
+               ctx.HdrWrite(pktEncRaw, filepath.Join(nodePath, string(TTx), tmp.Checksum()))
+       }
        return lastNode, err
 }
 
@@ -347,18 +363,18 @@ func (ctx *Ctx) TxFile(
                        return err
                }
                _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader, dstPath)
-               sds := SDS{
-                       "type": "file",
-                       "node": node.Id,
-                       "nice": int(nice),
-                       "src":  srcPath,
-                       "dst":  dstPath,
-                       "size": fileSize,
+               les := LEs{
+                       {"Type", "file"},
+                       {"Node", node.Id},
+                       {"Nice", int(nice)},
+                       {"Src", srcPath},
+                       {"Dst", dstPath},
+                       {"Size", fileSize},
                }
                if err == nil {
-                       ctx.LogI("tx", sds, "sent")
+                       ctx.LogI("tx", les, "sent")
                } else {
-                       ctx.LogE("tx", sds, err, "sent")
+                       ctx.LogE("tx", les, err, "sent")
                }
                return err
        }
@@ -403,18 +419,18 @@ func (ctx *Ctx) TxFile(
                        io.TeeReader(reader, hsh),
                        path,
                )
-               sds := SDS{
-                       "type": "file",
-                       "node": node.Id,
-                       "nice": int(nice),
-                       "src":  srcPath,
-                       "dst":  path,
-                       "size": sizeToSend,
+               les := LEs{
+                       {"Type", "file"},
+                       {"Node", node.Id},
+                       {"Nice", int(nice)},
+                       {"Src", srcPath},
+                       {"Dst", path},
+                       {"Size", sizeToSend},
                }
                if err == nil {
-                       ctx.LogI("tx", sds, "sent")
+                       ctx.LogI("tx", les, "sent")
                } else {
-                       ctx.LogE("tx", sds, err, "sent")
+                       ctx.LogE("tx", les, err, "sent")
                        return err
                }
                hsh.Sum(metaPkt.Checksums[chunkNum][:0])
@@ -436,18 +452,18 @@ func (ctx *Ctx) TxFile(
        }
        metaPktSize := int64(metaBuf.Len())
        _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf, path)
-       sds := SDS{
-               "type": "file",
-               "node": node.Id,
-               "nice": int(nice),
-               "src":  srcPath,
-               "dst":  path,
-               "size": metaPktSize,
+       les := LEs{
+               {"Type", "file"},
+               {"Node", node.Id},
+               {"Nice", int(nice)},
+               {"Src", srcPath},
+               {"Dst", path},
+               {"Size", metaPktSize},
        }
        if err == nil {
-               ctx.LogI("tx", sds, "sent")
+               ctx.LogI("tx", les, "sent")
        } else {
-               ctx.LogE("tx", sds, err, "sent")
+               ctx.LogE("tx", les, err, "sent")
        }
        return err
 }
@@ -472,18 +488,18 @@ func (ctx *Ctx) TxFreq(
        src := strings.NewReader(dstPath)
        size := int64(src.Len())
        _, err = ctx.Tx(node, pkt, nice, size, minSize, src, srcPath)
-       sds := SDS{
-               "type":      "freq",
-               "node":      node.Id,
-               "nice":      int(nice),
-               "replynice": int(replyNice),
-               "src":       srcPath,
-               "dst":       dstPath,
+       les := LEs{
+               {"Type", "freq"},
+               {"Node", node.Id},
+               {"Nice", int(nice)},
+               {"ReplyNice", int(replyNice)},
+               {"Src", srcPath},
+               {"Dst", dstPath},
        }
        if err == nil {
-               ctx.LogI("tx", sds, "sent")
+               ctx.LogI("tx", les, "sent")
        } else {
-               ctx.LogE("tx", sds, err, "sent")
+               ctx.LogE("tx", les, err, "sent")
        }
        return err
 }
@@ -583,33 +599,33 @@ func (ctx *Ctx) TxExec(
                _, err = ctx.Tx(node, pkt, nice, size, minSize, tmpReader, handle)
        }
 
-       sds := SDS{
-               "type":      "exec",
-               "node":      node.Id,
-               "nice":      int(nice),
-               "replynice": int(replyNice),
-               "dst":       strings.Join(append([]string{handle}, args...), " "),
-               "size":      size,
+       les := LEs{
+               {"Type", "exec"},
+               {"Node", node.Id},
+               {"Nice", int(nice)},
+               {"ReplyNice", int(replyNice)},
+               {"Dst", strings.Join(append([]string{handle}, args...), " ")},
+               {"Size", size},
        }
        if err == nil {
-               ctx.LogI("tx", sds, "sent")
+               ctx.LogI("tx", les, "sent")
        } else {
-               ctx.LogE("tx", sds, err, "sent")
+               ctx.LogE("tx", les, err, "sent")
        }
        return err
 }
 
 func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error {
-       sds := SDS{
-               "type": "trns",
-               "node": node.Id,
-               "nice": int(nice),
-               "size": size,
+       les := LEs{
+               {"Type", "trns"},
+               {"Node", node.Id},
+               {"Nice", int(nice)},
+               {"Size", size},
        }
-       ctx.LogD("tx", sds, "taken")
+       ctx.LogD("tx", les, "taken")
        if !ctx.IsEnoughSpace(size) {
                err := errors.New("is not enough space")
-               ctx.LogE("tx", sds, err, err.Error())
+               ctx.LogE("tx", les, err, err.Error())
                return err
        }
        tmp, err := ctx.NewTmpFileWHash()
@@ -618,7 +634,7 @@ func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error
        }
        if _, err = CopyProgressed(
                tmp.W, src, "Tx trns",
-               SDS{"pkt": node.Id.String(), "fullsize": size},
+               LEs{{"Pkt", node.Id.String()}, {"FullSize", size}},
                ctx.ShowPrgrs,
        ); err != nil {
                return err
@@ -626,9 +642,9 @@ func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error
        nodePath := filepath.Join(ctx.Spool, node.Id.String())
        err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
        if err == nil {
-               ctx.LogI("tx", sds, "sent")
+               ctx.LogI("tx", les, "sent")
        } else {
-               ctx.LogI("tx", SdsAdd(sds, SDS{"err": err}), "sent")
+               ctx.LogI("tx", append(les, LE{"Err", err}), "sent")
        }
        os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name)) // #nosec G104
        return err