]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/toss.go
fsnotify usage
[nncp.git] / src / toss.go
index 3fc4865538e7133c0a39300539dd285d847e6c3e..9b8a6d18b087415eddf61a3dc889b6e42412d1f9 100644 (file)
@@ -120,8 +120,8 @@ func jobProcess(
                }
                argsStr := strings.Join(append([]string{handle}, args...), " ")
                les = append(les, LE{"Type", "exec"}, LE{"Dst", argsStr})
-               cmdline, exists := sender.Exec[handle]
-               if !exists || len(cmdline) == 0 {
+               cmdline := sender.Exec[handle]
+               if len(cmdline) == 0 {
                        err = errors.New("No handle found")
                        ctx.LogE(
                                "rx-no-handle", les, err,
@@ -165,11 +165,11 @@ func jobProcess(
                                return err
                        }
                        if len(sendmail) > 0 && ctx.NotifyExec != nil {
-                               notify, exists := ctx.NotifyExec[sender.Name+"."+handle]
-                               if !exists {
-                                       notify, exists = ctx.NotifyExec["*."+handle]
+                               notify := ctx.NotifyExec[sender.Name+"."+handle]
+                               if notify == nil {
+                                       notify = ctx.NotifyExec["*."+handle]
                                }
-                               if exists {
+                               if notify != nil {
                                        cmd := exec.Command(
                                                sendmail[0],
                                                append(sendmail[1:], notify.To)...,
@@ -309,7 +309,7 @@ func jobProcess(
                                return err
                        }
                        if err = bufW.Flush(); err != nil {
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                ctx.LogE("rx-flush", les, err, func(les LEs) string {
                                        return fmt.Sprintf(
                                                "Tossing file %s/%s (%s): %s: flushing",
@@ -320,7 +320,7 @@ func jobProcess(
                                return err
                        }
                        if err = tmp.Sync(); err != nil {
-                               tmp.Close() // #nosec G104
+                               tmp.Close()
                                ctx.LogE("rx-sync", les, err, func(les LEs) string {
                                        return fmt.Sprintf(
                                                "Tossing file %s/%s (%s): %s: syncing",
@@ -588,11 +588,35 @@ func jobProcess(
                }
                ctx.LogD("rx-tx", les, logMsg)
                if !dryRun {
-                       if err = ctx.TxTrns(node, nice, int64(pktSize), pipeR); err != nil {
-                               ctx.LogE("rx", les, err, func(les LEs) string {
-                                       return logMsg(les) + ": txing"
-                               })
-                               return err
+                       if len(node.Via) == 0 {
+                               if err = ctx.TxTrns(node, nice, int64(pktSize), pipeR); err != nil {
+                                       ctx.LogE("rx", les, err, func(les LEs) string {
+                                               return logMsg(les) + ": txing"
+                                       })
+                                       return err
+                               }
+                       } else {
+                               via := node.Via[:len(node.Via)-1]
+                               node = ctx.Neigh[*node.Via[len(node.Via)-1]]
+                               node = &Node{Id: node.Id, Via: via, ExchPub: node.ExchPub}
+                               pktTrns, err := NewPkt(PktTypeTrns, 0, nodeId[:])
+                               if err != nil {
+                                       panic(err)
+                               }
+                               if _, err = ctx.Tx(
+                                       node,
+                                       pktTrns,
+                                       nice,
+                                       int64(pktSize), 0,
+                                       pipeR,
+                                       pktName,
+                                       nil,
+                               ); err != nil {
+                                       ctx.LogE("rx", les, err, func(les LEs) string {
+                                               return logMsg(les) + ": txing"
+                                       })
+                                       return err
+                               }
                        }
                }
                ctx.LogI("rx", les, func(les LEs) string {
@@ -705,7 +729,7 @@ func jobProcess(
                                        })
                                        continue
                                }
-                               if nodeId != sender.Id {
+                               if nodeId != sender.Id && nodeId != pktEnc.Sender {
                                        ctx.LogI("rx-area-echo", lesEcho, logMsgNode)
                                        if _, err = ctx.Tx(
                                                node, &pkt, nice, int64(pktSize), 0, fullPipeR, pktName, nil,
@@ -996,6 +1020,13 @@ func (ctx *Ctx) AutoToss(
        nice uint8,
        doSeen, noFile, noFreq, noExec, noTrns, noArea bool,
 ) (chan struct{}, chan bool) {
+       dw, err := ctx.NewDirWatcher(
+               filepath.Join(ctx.Spool, nodeId.String(), string(TRx)),
+               time.Second,
+       )
+       if err != nil {
+               log.Fatalln(err)
+       }
        finish := make(chan struct{})
        badCode := make(chan bool)
        go func() {
@@ -1003,14 +1034,14 @@ func (ctx *Ctx) AutoToss(
                for {
                        select {
                        case <-finish:
+                               dw.Close()
                                badCode <- bad
-                               break
-                       default:
+                               return
+                       case <-dw.C:
+                               bad = !ctx.Toss(
+                                       nodeId, TRx, nice, false,
+                                       doSeen, noFile, noFreq, noExec, noTrns, noArea) || bad
                        }
-                       time.Sleep(time.Second)
-                       bad = !ctx.Toss(
-                               nodeId, TRx, nice, false,
-                               doSeen, noFile, noFreq, noExec, noTrns, noArea) || bad
                }
        }()
        return finish, badCode