]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/toss.go
recfile log format
[nncp.git] / src / toss.go
index 682c46122331518a4858eb26b9c4fa0204eeea67..cc49784b335ffb7bfdecf1dd395b17fac68c5ca1 100644 (file)
@@ -71,7 +71,7 @@ func (ctx *Ctx) Toss(
 ) bool {
        dirLock, err := ctx.LockDir(nodeId, "toss")
        if err != nil {
-               ctx.LogE("rx", SDS{}, err, "lock")
+               ctx.LogE("rx", LEs{}, err, "lock")
                return false
        }
        defer ctx.UnlockDir(dirLock)
@@ -84,9 +84,10 @@ func (ctx *Ctx) Toss(
        defer decompressor.Close()
        for job := range ctx.Jobs(nodeId, TRx) {
                pktName := filepath.Base(job.Fd.Name())
-               sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName}
+               les := LEs{{"Node", job.PktEnc.Sender}, {"Pkt", pktName}}
                if job.PktEnc.Nice > nice {
-                       ctx.LogD("rx", SdsAdd(sds, SDS{"nice": int(job.PktEnc.Nice)}), "too nice")
+                       ctx.LogD("rx", append(les, LE{"Nice", int(job.PktEnc.Nice)}), "too nice")
+                       job.Fd.Close() // #nosec G104
                        continue
                }
                pipeR, pipeW := io.Pipe()
@@ -112,7 +113,7 @@ func (ctx *Ctx) Toss(
                var pktSize int64
                var pktSizeBlocks int64
                if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil {
-                       ctx.LogE("rx", sds, err, "unmarshal")
+                       ctx.LogE("rx", les, err, "unmarshal")
                        isBad = true
                        goto Closing
                }
@@ -122,8 +123,8 @@ func (ctx *Ctx) Toss(
                        pktSize -= poly1305.TagSize
                }
                pktSize -= pktSizeBlocks * poly1305.TagSize
-               sds["size"] = pktSize
-               ctx.LogD("rx", sds, "taken")
+               les = append(les, LE{"Size", pktSize})
+               ctx.LogD("rx", les, "taken")
                switch pkt.Type {
                case PktTypeExec, PktTypeExecFat:
                        if noExec {
@@ -136,14 +137,14 @@ func (ctx *Ctx) Toss(
                                args = append(args, string(p))
                        }
                        argsStr := strings.Join(append([]string{handle}, args...), " ")
-                       sds := SdsAdd(sds, SDS{
-                               "type": "exec",
-                               "dst":  argsStr,
-                       })
+                       les = append(les, LEs{
+                               {"Type", "exec"},
+                               {"Dst", argsStr},
+                       }...)
                        sender := ctx.Neigh[*job.PktEnc.Sender]
                        cmdline, exists := sender.Exec[handle]
                        if !exists || len(cmdline) == 0 {
-                               ctx.LogE("rx", sds, errors.New("No handle found"), "")
+                               ctx.LogE("rx", les, errors.New("No handle found"), "")
                                isBad = true
                                goto Closing
                        }
@@ -155,7 +156,7 @@ func (ctx *Ctx) Toss(
                        if !dryRun {
                                cmd := exec.Command(
                                        cmdline[0],
-                                       append(cmdline[1:len(cmdline)], args...)...,
+                                       append(cmdline[1:], args...)...,
                                )
                                cmd.Env = append(
                                        cmd.Env,
@@ -170,7 +171,7 @@ func (ctx *Ctx) Toss(
                                }
                                output, err := cmd.Output()
                                if err != nil {
-                                       ctx.LogE("rx", sds, err, "handle")
+                                       ctx.LogE("rx", les, err, "handle")
                                        isBad = true
                                        goto Closing
                                }
@@ -182,18 +183,18 @@ func (ctx *Ctx) Toss(
                                        if exists {
                                                cmd := exec.Command(
                                                        sendmail[0],
-                                                       append(sendmail[1:len(sendmail)], notify.To)...,
+                                                       append(sendmail[1:], notify.To)...,
                                                )
                                                cmd.Stdin = newNotification(notify, fmt.Sprintf(
                                                        "Exec from %s: %s", sender.Name, argsStr,
                                                ), output)
                                                if err = cmd.Run(); err != nil {
-                                                       ctx.LogE("rx", sds, err, "notify")
+                                                       ctx.LogE("rx", les, err, "notify")
                                                }
                                        }
                                }
                        }
-                       ctx.LogI("rx", sds, "")
+                       ctx.LogI("rx", les, "")
                        if !dryRun {
                                if doSeen {
                                        if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
@@ -201,7 +202,7 @@ func (ctx *Ctx) Toss(
                                        }
                                }
                                if err = os.Remove(job.Fd.Name()); err != nil {
-                                       ctx.LogE("rx", sds, err, "remove")
+                                       ctx.LogE("rx", les, err, "remove")
                                        isBad = true
                                }
                        }
@@ -210,57 +211,57 @@ func (ctx *Ctx) Toss(
                                goto Closing
                        }
                        dst := string(pkt.Path[:int(pkt.PathLen)])
-                       sds := SdsAdd(sds, SDS{"type": "file", "dst": dst})
+                       les = append(les, LEs{{"Type", "file"}, {"Dst", dst}}...)
                        if filepath.IsAbs(dst) {
-                               ctx.LogE("rx", sds, errors.New("non-relative destination path"), "")
+                               ctx.LogE("rx", les, errors.New("non-relative destination path"), "")
                                isBad = true
                                goto Closing
                        }
                        incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming
                        if incoming == nil {
-                               ctx.LogE("rx", sds, errors.New("incoming is not allowed"), "")
+                               ctx.LogE("rx", les, errors.New("incoming is not allowed"), "")
                                isBad = true
                                goto Closing
                        }
                        dir := filepath.Join(*incoming, path.Dir(dst))
                        if err = os.MkdirAll(dir, os.FileMode(0777)); err != nil {
-                               ctx.LogE("rx", sds, err, "mkdir")
+                               ctx.LogE("rx", les, err, "mkdir")
                                isBad = true
                                goto Closing
                        }
                        if !dryRun {
                                tmp, err := TempFile(dir, "file")
                                if err != nil {
-                                       ctx.LogE("rx", sds, err, "mktemp")
+                                       ctx.LogE("rx", les, err, "mktemp")
                                        isBad = true
                                        goto Closing
                                }
-                               sds["tmp"] = tmp.Name()
-                               ctx.LogD("rx", sds, "created")
+                               les = append(les, LE{"Tmp", tmp.Name()})
+                               ctx.LogD("rx", les, "created")
                                bufW := bufio.NewWriter(tmp)
                                if _, err = CopyProgressed(
                                        bufW, pipeR, "Rx file",
-                                       SdsAdd(sds, SDS{"fullsize": sds["size"]}),
+                                       append(les, LE{"FullSize", pktSize}),
                                        ctx.ShowPrgrs,
                                ); err != nil {
-                                       ctx.LogE("rx", sds, err, "copy")
+                                       ctx.LogE("rx", les, err, "copy")
                                        isBad = true
                                        goto Closing
                                }
                                if err = bufW.Flush(); err != nil {
                                        tmp.Close() // #nosec G104
-                                       ctx.LogE("rx", sds, err, "copy")
+                                       ctx.LogE("rx", les, err, "copy")
                                        isBad = true
                                        goto Closing
                                }
                                if err = tmp.Sync(); err != nil {
                                        tmp.Close() // #nosec G104
-                                       ctx.LogE("rx", sds, err, "copy")
+                                       ctx.LogE("rx", les, err, "copy")
                                        isBad = true
                                        goto Closing
                                }
                                if err = tmp.Close(); err != nil {
-                                       ctx.LogE("rx", sds, err, "copy")
+                                       ctx.LogE("rx", les, err, "copy")
                                        isBad = true
                                        goto Closing
                                }
@@ -272,7 +273,7 @@ func (ctx *Ctx) Toss(
                                                if os.IsNotExist(err) {
                                                        break
                                                }
-                                               ctx.LogE("rx", sds, err, "stat")
+                                               ctx.LogE("rx", les, err, "stat")
                                                isBad = true
                                                goto Closing
                                        }
@@ -280,16 +281,16 @@ func (ctx *Ctx) Toss(
                                        dstPathCtr++
                                }
                                if err = os.Rename(tmp.Name(), dstPath); err != nil {
-                                       ctx.LogE("rx", sds, err, "rename")
+                                       ctx.LogE("rx", les, err, "rename")
                                        isBad = true
                                }
                                if err = DirSync(*incoming); err != nil {
-                                       ctx.LogE("rx", sds, err, "sync")
+                                       ctx.LogE("rx", les, err, "sync")
                                        isBad = true
                                }
-                               delete(sds, "tmp")
+                               les = les[:len(les)-1] // delete Tmp
                        }
-                       ctx.LogI("rx", sds, "")
+                       ctx.LogI("rx", les, "")
                        if !dryRun {
                                if doSeen {
                                        if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
@@ -297,13 +298,13 @@ func (ctx *Ctx) Toss(
                                        }
                                }
                                if err = os.Remove(job.Fd.Name()); err != nil {
-                                       ctx.LogE("rx", sds, err, "remove")
+                                       ctx.LogE("rx", les, err, "remove")
                                        isBad = true
                                }
                                if len(sendmail) > 0 && ctx.NotifyFile != nil {
                                        cmd := exec.Command(
                                                sendmail[0],
-                                               append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
+                                               append(sendmail[1:], ctx.NotifyFile.To)...,
                                        )
                                        cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
                                                "File from %s: %s (%s)",
@@ -312,7 +313,7 @@ func (ctx *Ctx) Toss(
                                                humanize.IBytes(uint64(pktSize)),
                                        ), nil)
                                        if err = cmd.Run(); err != nil {
-                                               ctx.LogE("rx", sds, err, "notify")
+                                               ctx.LogE("rx", les, err, "notify")
                                        }
                                }
                        }
@@ -322,23 +323,23 @@ func (ctx *Ctx) Toss(
                        }
                        src := string(pkt.Path[:int(pkt.PathLen)])
                        if filepath.IsAbs(src) {
-                               ctx.LogE("rx", sds, errors.New("non-relative source path"), "")
+                               ctx.LogE("rx", les, errors.New("non-relative source path"), "")
                                isBad = true
                                goto Closing
                        }
-                       sds := SdsAdd(sds, SDS{"type": "freq", "src": src})
+                       les := append(les, LEs{{"Type", "freq"}, {"Src", src}}...)
                        dstRaw, err := ioutil.ReadAll(pipeR)
                        if err != nil {
-                               ctx.LogE("rx", sds, err, "read")
+                               ctx.LogE("rx", les, err, "read")
                                isBad = true
                                goto Closing
                        }
                        dst := string(dstRaw)
-                       sds["dst"] = dst
+                       les = append(les, LE{"Dst", dst})
                        sender := ctx.Neigh[*job.PktEnc.Sender]
                        freqPath := sender.FreqPath
                        if freqPath == nil {
-                               ctx.LogE("rx", sds, errors.New("freqing is not allowed"), "")
+                               ctx.LogE("rx", les, errors.New("freqing is not allowed"), "")
                                isBad = true
                                goto Closing
                        }
@@ -353,12 +354,12 @@ func (ctx *Ctx) Toss(
                                        sender.FreqMaxSize,
                                )
                                if err != nil {
-                                       ctx.LogE("rx", sds, err, "tx file")
+                                       ctx.LogE("rx", les, err, "tx file")
                                        isBad = true
                                        goto Closing
                                }
                        }
-                       ctx.LogI("rx", sds, "")
+                       ctx.LogI("rx", les, "")
                        if !dryRun {
                                if doSeen {
                                        if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
@@ -366,19 +367,19 @@ func (ctx *Ctx) Toss(
                                        }
                                }
                                if err = os.Remove(job.Fd.Name()); err != nil {
-                                       ctx.LogE("rx", sds, err, "remove")
+                                       ctx.LogE("rx", les, err, "remove")
                                        isBad = true
                                }
                                if len(sendmail) > 0 && ctx.NotifyFreq != nil {
                                        cmd := exec.Command(
                                                sendmail[0],
-                                               append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
+                                               append(sendmail[1:], ctx.NotifyFreq.To)...,
                                        )
                                        cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
                                                "Freq from %s: %s", sender.Name, src,
                                        ), nil)
                                        if err = cmd.Run(); err != nil {
-                                               ctx.LogE("rx", sds, err, "notify")
+                                               ctx.LogE("rx", les, err, "notify")
                                        }
                                }
                        }
@@ -390,21 +391,21 @@ func (ctx *Ctx) Toss(
                        copy(dst[:], pkt.Path[:int(pkt.PathLen)])
                        nodeId := NodeId(*dst)
                        node, known := ctx.Neigh[nodeId]
-                       sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId})
+                       les := append(les, LEs{{"Type", "trns"}, {"Dst", nodeId}}...)
                        if !known {
-                               ctx.LogE("rx", sds, errors.New("unknown node"), "")
+                               ctx.LogE("rx", les, errors.New("unknown node"), "")
                                isBad = true
                                goto Closing
                        }
-                       ctx.LogD("rx", sds, "taken")
+                       ctx.LogD("rx", les, "taken")
                        if !dryRun {
                                if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
-                                       ctx.LogE("rx", sds, err, "tx trns")
+                                       ctx.LogE("rx", les, err, "tx trns")
                                        isBad = true
                                        goto Closing
                                }
                        }
-                       ctx.LogI("rx", sds, "")
+                       ctx.LogI("rx", les, "")
                        if !dryRun {
                                if doSeen {
                                        if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
@@ -412,12 +413,12 @@ func (ctx *Ctx) Toss(
                                        }
                                }
                                if err = os.Remove(job.Fd.Name()); err != nil {
-                                       ctx.LogE("rx", sds, err, "remove")
+                                       ctx.LogE("rx", les, err, "remove")
                                        isBad = true
                                }
                        }
                default:
-                       ctx.LogE("rx", sds, errors.New("unknown type"), "")
+                       ctx.LogE("rx", les, errors.New("unknown type"), "")
                        isBad = true
                }
        Closing: