]> Cypherpunks.ru repositories - nncp.git/commitdiff
Move arguments to TossOpts
authorSergey Matveev <stargrave@stargrave.org>
Thu, 10 Aug 2023 12:49:25 +0000 (15:49 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 13 Aug 2023 16:50:38 +0000 (19:50 +0300)
src/cmd/nncp-call/main.go
src/cmd/nncp-caller/main.go
src/cmd/nncp-daemon/main.go
src/cmd/nncp-toss/main.go
src/toss.go
src/toss_test.go

index 8ff3a0e548523135d52a564772dd150d639e91bb..f21a6e72212979da119273bba413b17c61590f5c 100644 (file)
@@ -211,14 +211,16 @@ func main() {
        if *autoToss {
                autoTossFinish, autoTossBadCode = ctx.AutoToss(
                        node.Id,
-                       nice,
-                       *autoTossDoSeen,
-                       *autoTossNoFile,
-                       *autoTossNoFreq,
-                       *autoTossNoExec,
-                       *autoTossNoTrns,
-                       *autoTossNoArea,
-                       *autoTossNoACK,
+                       &nncp.TossOpts{
+                               Nice:   nice,
+                               DoSeen: *autoTossDoSeen,
+                               NoFile: *autoTossNoFile,
+                               NoFreq: *autoTossNoFreq,
+                               NoExec: *autoTossNoExec,
+                               NoTrns: *autoTossNoTrns,
+                               NoArea: *autoTossNoArea,
+                               NoACK:  *autoTossNoACK,
+                       },
                )
        }
 
index 7859377f34c309285e05979ea9a2022cfb8ffd6e..8f11cc0922e5aac43647cd169c0cf07b8417ebbe 100644 (file)
@@ -213,14 +213,16 @@ func main() {
                                                if call.AutoToss || *autoToss {
                                                        autoTossFinish, autoTossBadCode = ctx.AutoToss(
                                                                node.Id,
-                                                               call.Nice,
-                                                               call.AutoTossDoSeen || *autoTossDoSeen,
-                                                               call.AutoTossNoFile || *autoTossNoFile,
-                                                               call.AutoTossNoFreq || *autoTossNoFreq,
-                                                               call.AutoTossNoExec || *autoTossNoExec,
-                                                               call.AutoTossNoTrns || *autoTossNoTrns,
-                                                               call.AutoTossNoArea || *autoTossNoArea,
-                                                               call.AutoTossNoACK || *autoTossNoACK,
+                                                               &nncp.TossOpts{
+                                                                       Nice:   call.Nice,
+                                                                       DoSeen: call.AutoTossDoSeen || *autoTossDoSeen,
+                                                                       NoFile: call.AutoTossNoFile || *autoTossNoFile,
+                                                                       NoFreq: call.AutoTossNoFreq || *autoTossNoFreq,
+                                                                       NoExec: call.AutoTossNoExec || *autoTossNoExec,
+                                                                       NoTrns: call.AutoTossNoTrns || *autoTossNoTrns,
+                                                                       NoArea: call.AutoTossNoArea || *autoTossNoArea,
+                                                                       NoACK:  call.AutoTossNoACK || *autoTossNoACK,
+                                                               },
                                                        )
                                                }
 
index 6ae89c0385021a938e1aec210c299276fb5ec92a..5ac2d73e8bdf7f02b9be6a134dcc69d4b25e0789 100644 (file)
@@ -211,14 +211,16 @@ func main() {
                if *autoToss && nodeId != nil {
                        autoTossFinish, autoTossBadCode = ctx.AutoToss(
                                nodeId,
-                               nice,
-                               *autoTossDoSeen,
-                               *autoTossNoFile,
-                               *autoTossNoFreq,
-                               *autoTossNoExec,
-                               *autoTossNoTrns,
-                               *autoTossNoArea,
-                               *autoTossNoACK,
+                               &nncp.TossOpts{
+                                       Nice:   nice,
+                                       DoSeen: *autoTossDoSeen,
+                                       NoFile: *autoTossNoFile,
+                                       NoFreq: *autoTossNoFreq,
+                                       NoExec: *autoTossNoExec,
+                                       NoTrns: *autoTossNoTrns,
+                                       NoArea: *autoTossNoArea,
+                                       NoACK:  *autoTossNoACK,
+                               },
                        )
                }
                <-nodeIdC // call completion
@@ -298,14 +300,16 @@ func main() {
                        if *autoToss && nodeId != nil {
                                autoTossFinish, autoTossBadCode = ctx.AutoToss(
                                        nodeId,
-                                       nice,
-                                       *autoTossDoSeen,
-                                       *autoTossNoFile,
-                                       *autoTossNoFreq,
-                                       *autoTossNoExec,
-                                       *autoTossNoTrns,
-                                       *autoTossNoArea,
-                                       *autoTossNoACK,
+                                       &nncp.TossOpts{
+                                               Nice:   nice,
+                                               DoSeen: *autoTossDoSeen,
+                                               NoFile: *autoTossNoFile,
+                                               NoFreq: *autoTossNoFreq,
+                                               NoExec: *autoTossNoExec,
+                                               NoTrns: *autoTossNoTrns,
+                                               NoArea: *autoTossNoArea,
+                                               NoACK:  *autoTossNoACK,
+                                       },
                                )
                        }
                        <-nodeIdC // call completion
index 6a960368143aa8bca59548a171d9972bbd755596..e2bf96844a82ea0d39714d0f0d7fd2cf9a6dbfe4 100644 (file)
@@ -110,15 +110,30 @@ func main() {
                        isBad = ctx.Toss(
                                node.Id,
                                nncp.TRx,
-                               nice,
-                               *dryRun, *doSeen, *noFile, *noFreq, *noExec, *noTrns, *noArea, *noACK,
+                               &nncp.TossOpts{
+                                       Nice:   nice,
+                                       DoSeen: *doSeen,
+                                       NoFile: *noFile,
+                                       NoFreq: *noFreq,
+                                       NoExec: *noExec,
+                                       NoTrns: *noTrns,
+                                       NoArea: *noArea,
+                                       NoACK:  *noACK,
+                               },
                        ) || isBad
                        if nodeId == *ctx.SelfId {
                                isBad = ctx.Toss(
                                        node.Id,
                                        nncp.TTx,
-                                       nice,
-                                       *dryRun, false, true, true, true, true, *noArea, *noACK,
+                                       &nncp.TossOpts{
+                                               Nice:   nice,
+                                               NoFile: true,
+                                               NoFreq: true,
+                                               NoExec: true,
+                                               NoTrns: true,
+                                               NoArea: *noArea,
+                                               NoACK:  *noACK,
+                                       },
                                ) || isBad
                        }
                }
@@ -150,15 +165,31 @@ func main() {
                ctx.Toss(
                        nodeId,
                        nncp.TRx,
-                       nice,
-                       *dryRun, *doSeen, *noFile, *noFreq, *noExec, *noTrns, *noArea, *noACK,
+                       &nncp.TossOpts{
+                               Nice:   nice,
+                               DryRun: *dryRun,
+                               DoSeen: *doSeen,
+                               NoFile: *noFile,
+                               NoFreq: *noFreq,
+                               NoExec: *noExec,
+                               NoTrns: *noTrns,
+                               NoArea: *noArea,
+                               NoACK:  *noACK,
+                       },
                )
                if *nodeId == *ctx.SelfId {
                        ctx.Toss(
                                nodeId,
                                nncp.TTx,
-                               nice,
-                               *dryRun, false, true, true, true, true, *noArea, *noACK,
+                               &nncp.TossOpts{
+                                       Nice:   nice,
+                                       NoFile: true,
+                                       NoFreq: true,
+                                       NoExec: true,
+                                       NoTrns: true,
+                                       NoArea: *noArea,
+                                       NoACK:  *noACK,
+                               },
                        )
                }
        }
index b4363f713ea7856a16df74e26ff5a10ae0413ae6..d365e2efd5c47c172f607bee0f8919817da83189 100644 (file)
@@ -46,6 +46,18 @@ const (
        SeenDir = "seen"
 )
 
+type TossOpts struct {
+       Nice   uint8
+       DryRun bool
+       DoSeen bool
+       NoFile bool
+       NoFreq bool
+       NoExec bool
+       NoTrns bool
+       NoArea bool
+       NoACK  bool
+}
+
 func jobPath2Seen(jobPath string) string {
        return filepath.Join(filepath.Dir(jobPath), SeenDir, filepath.Base(jobPath))
 }
@@ -91,7 +103,7 @@ func jobProcess(
        pktSize uint64,
        jobPath string,
        decompressor *zstd.Decoder,
-       dryRun, doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK bool,
+       opts *TossOpts,
 ) error {
        defer pipeR.Close()
        sendmail := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
@@ -113,7 +125,7 @@ func jobProcess(
        })
        switch pkt.Type {
        case PktTypeExec, PktTypeExecFat:
-               if noExec {
+               if opts.NoExec {
                        return nil
                }
                path := bytes.Split(pkt.Path[:int(pkt.PathLen)], []byte{0})
@@ -144,7 +156,7 @@ func jobProcess(
                                log.Fatalln(err)
                        }
                }
-               if !dryRun {
+               if !opts.DryRun {
                        cmd := exec.Command(cmdline[0], append(cmdline[1:], args...)...)
                        cmd.Env = append(
                                cmd.Env,
@@ -203,8 +215,8 @@ func jobProcess(
                                humanize.IBytes(pktSize),
                        )
                })
-               if !dryRun && jobPath != "" {
-                       if doSeen {
+               if !opts.DryRun && jobPath != "" {
+                       if opts.DoSeen {
                                if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
                                        return err
                                }
@@ -238,7 +250,7 @@ func jobProcess(
                }
 
        case PktTypeFile:
-               if noFile {
+               if opts.NoFile {
                        return nil
                }
                dst := string(pkt.Path[:int(pkt.PathLen)])
@@ -283,7 +295,7 @@ func jobProcess(
                        })
                        return err
                }
-               if !dryRun {
+               if !opts.DryRun {
                        tmp, err := TempFile(dir, "file")
                        if err != nil {
                                ctx.LogE("rx-mktemp", les, err, func(les LEs) string {
@@ -400,9 +412,9 @@ func jobProcess(
                                dst, humanize.IBytes(pktSize), sender.Name,
                        )
                })
-               if !dryRun {
+               if !opts.DryRun {
                        if jobPath != "" {
-                               if doSeen {
+                               if opts.DoSeen {
                                        if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
                                                return err
                                        }
@@ -456,7 +468,7 @@ func jobProcess(
                }
 
        case PktTypeFreq:
-               if noFreq {
+               if opts.NoFreq {
                        return nil
                }
                src := string(pkt.Path[:int(pkt.PathLen)])
@@ -503,7 +515,7 @@ func jobProcess(
                        )
                        return err
                }
-               if !dryRun {
+               if !opts.DryRun {
                        err = ctx.TxFile(
                                sender,
                                pkt.Nice,
@@ -528,9 +540,9 @@ func jobProcess(
                ctx.LogI("rx", les, func(les LEs) string {
                        return fmt.Sprintf("Got file request %s to %s", src, sender.Name)
                })
-               if !dryRun {
+               if !opts.DryRun {
                        if jobPath != "" {
-                               if doSeen {
+                               if opts.DoSeen {
                                        if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
                                                return err
                                        }
@@ -583,7 +595,7 @@ func jobProcess(
                }
 
        case PktTypeTrns:
-               if noTrns {
+               if opts.NoTrns {
                        return nil
                }
                dst := new([MTHSize]byte)
@@ -605,7 +617,7 @@ func jobProcess(
                        return err
                }
                ctx.LogD("rx-tx", les, logMsg)
-               if !dryRun {
+               if !opts.DryRun {
                        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 {
@@ -645,8 +657,8 @@ func jobProcess(
                                humanize.IBytes(pktSize),
                        )
                })
-               if !dryRun && jobPath != "" {
-                       if doSeen {
+               if !opts.DryRun && jobPath != "" {
+                       if opts.DoSeen {
                                if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
                                        return err
                                }
@@ -681,7 +693,7 @@ func jobProcess(
                }
 
        case PktTypeArea:
-               if noArea {
+               if opts.NoArea {
                        return nil
                }
                areaId := new(AreaId)
@@ -712,7 +724,7 @@ func jobProcess(
                les = append(les, LE{"AreaMsg", msgHash})
                ctx.LogD("rx-area", les, logMsg)
 
-               if dryRun {
+               if opts.DryRun {
                        for _, nodeId := range area.Subs {
                                node := ctx.Neigh[*nodeId]
                                lesEcho := append(les, LE{"Echo", nodeId})
@@ -791,7 +803,7 @@ func jobProcess(
                        ctx.LogD("rx-area-seen", les, func(les LEs) string {
                                return logMsg(les) + ": already seen"
                        })
-                       if !dryRun && jobPath != "" {
+                       if !opts.DryRun && jobPath != "" {
                                if err = os.Remove(jobPath); err != nil {
                                        ctx.LogE("rx-area-remove", les, err, func(les LEs) string {
                                                return fmt.Sprintf(
@@ -858,7 +870,7 @@ func jobProcess(
                                        uint64(pktSizeWithoutEnc(int64(pktSize))),
                                        "",
                                        decompressor,
-                                       dryRun, doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK,
+                                       opts,
                                )
                        }()
                        _, _, _, err = PktEncRead(
@@ -881,7 +893,7 @@ func jobProcess(
                        }
                }
 
-               if !dryRun && jobPath != "" {
+               if !opts.DryRun && jobPath != "" {
                        if err = os.MkdirAll(seenDir, os.FileMode(0777)); err != nil {
                                ctx.LogE("rx-area-mkdir", les, err, logMsg)
                                return err
@@ -909,7 +921,7 @@ func jobProcess(
                }
 
        case PktTypeACK:
-               if noACK {
+               if opts.NoACK {
                        return nil
                }
                hsh := Base32Codec.EncodeToString(pkt.Path[:MTHSize])
@@ -920,7 +932,7 @@ func jobProcess(
                ctx.LogD("rx-ack", les, logMsg)
                pktPath := filepath.Join(ctx.Spool, sender.Id.String(), string(TTx), hsh)
                if _, err := os.Stat(pktPath); err == nil {
-                       if !dryRun {
+                       if !opts.DryRun {
                                if err = os.Remove(pktPath); err != nil {
                                        ctx.LogE("rx-ack", les, err, func(les LEs) string {
                                                return logMsg(les) + ": removing packet"
@@ -935,7 +947,7 @@ func jobProcess(
                                return logMsg(les) + ": already disappeared"
                        })
                }
-               if !dryRun && doSeen {
+               if !opts.DryRun && opts.DoSeen {
                        if err := ensureDir(filepath.Dir(jobPath), SeenDir); err != nil {
                                return err
                        }
@@ -954,7 +966,7 @@ func jobProcess(
                                }
                        }
                }
-               if !dryRun {
+               if !opts.DryRun {
                        if err = os.Remove(jobPath); err != nil {
                                ctx.LogE("rx", les, err, func(les LEs) string {
                                        return logMsg(les) + ": removing job"
@@ -984,12 +996,7 @@ func jobProcess(
        return nil
 }
 
-func (ctx *Ctx) Toss(
-       nodeId *NodeId,
-       xx TRxTx,
-       nice uint8,
-       dryRun, doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK bool,
-) bool {
+func (ctx *Ctx) Toss(nodeId *NodeId, xx TRxTx, opts *TossOpts) bool {
        dirLock, err := ctx.LockDir(nodeId, "toss")
        if err != nil {
                return false
@@ -1008,7 +1015,7 @@ func (ctx *Ctx) Toss(
                        {"Pkt", pktName},
                        {"Nice", int(job.PktEnc.Nice)},
                }
-               if job.PktEnc.Nice > nice {
+               if job.PktEnc.Nice > opts.Nice {
                        ctx.LogD("rx-too-nice", les, func(les LEs) string {
                                return fmt.Sprintf(
                                        "Tossing %s/%s: too nice: %s",
@@ -1056,7 +1063,7 @@ func (ctx *Ctx) Toss(
                                uint64(pktSizeWithoutEnc(job.Size)),
                                job.Path,
                                decompressor,
-                               dryRun, doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK,
+                               opts,
                        )
                }()
                pipeWB := bufio.NewWriter(pipeW)
@@ -1103,11 +1110,7 @@ func (ctx *Ctx) Toss(
        return isBad
 }
 
-func (ctx *Ctx) AutoToss(
-       nodeId *NodeId,
-       nice uint8,
-       doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK bool,
-) (chan struct{}, chan bool) {
+func (ctx *Ctx) AutoToss(nodeId *NodeId, opts *TossOpts) (chan struct{}, chan bool) {
        dw, err := ctx.NewDirWatcher(
                filepath.Join(ctx.Spool, nodeId.String(), string(TRx)),
                time.Second,
@@ -1126,9 +1129,7 @@ func (ctx *Ctx) AutoToss(
                                badCode <- bad
                                return
                        case <-dw.C:
-                               bad = !ctx.Toss(
-                                       nodeId, TRx, nice, false,
-                                       doSeen, noFile, noFreq, noExec, noTrns, noArea, noACK) || bad
+                               bad = !ctx.Toss(nodeId, TRx, opts) || bad
                        }
                }
        }()
index aea765d9ec602057ec9cc6193966bf374417a823..eb83309e19b2c77ec3ae87e80c4ba4d7e7a12ee5 100644 (file)
@@ -111,15 +111,13 @@ func TestTossExec(t *testing.T) {
                        if len(dirFiles(rxPath)) == 0 {
                                continue
                        }
-                       ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec-1,
-                               false, false, false, false, false, false, false, false)
+                       ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceExec - 1})
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
                        ctx.Neigh[*nodeOur.Id].Exec = make(map[string][]string)
                        ctx.Neigh[*nodeOur.Id].Exec[handle] = []string{"/bin/sh", "-c", "false"}
-                       ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec,
-                               false, false, false, false, false, false, false, false)
+                       ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceExec})
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
@@ -131,8 +129,7 @@ func TestTossExec(t *testing.T) {
                                        filepath.Join(spool, "mbox"),
                                ),
                        }
-                       ctx.Toss(ctx.Self.Id, TRx, DefaultNiceExec,
-                               false, false, false, false, false, false, false, false)
+                       ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceExec})
                        if len(dirFiles(rxPath)) != 0 {
                                return false
                        }
@@ -218,14 +215,12 @@ func TestTossFile(t *testing.T) {
                }
                rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
                os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
-               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
-                       false, false, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFile})
                if len(dirFiles(rxPath)) == 0 {
                        return false
                }
                ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
-               if ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
-                       false, false, false, false, false, false, false, false) {
+               if ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFile}) {
                        return false
                }
                if len(dirFiles(rxPath)) != 0 {
@@ -301,8 +296,7 @@ func TestTossFileSameName(t *testing.T) {
                rxPath := filepath.Join(spool, ctx.Self.Id.String(), string(TRx))
                os.Rename(filepath.Join(spool, ctx.Self.Id.String(), string(TTx)), rxPath)
                ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
-               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFile,
-                       false, false, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFile})
                expected := make(map[string]struct{})
                expected["samefile"] = struct{}{}
                for i := 0; i < files-1; i++ {
@@ -374,14 +368,12 @@ func TestTossFreq(t *testing.T) {
                txPath := filepath.Join(spool, ctx.Self.Id.String(), string(TTx))
                os.Rename(txPath, rxPath)
                os.MkdirAll(txPath, os.FileMode(0700))
-               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
-                       false, false, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFreq})
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
                ctx.Neigh[*nodeOur.Id].FreqPath = &spool
-               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
-                       false, false, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFreq})
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
@@ -394,8 +386,7 @@ func TestTossFreq(t *testing.T) {
                                panic(err)
                        }
                }
-               ctx.Toss(ctx.Self.Id, TRx, DefaultNiceFreq,
-                       false, false, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: DefaultNiceFreq})
                if len(dirFiles(txPath)) == 0 || len(dirFiles(rxPath)) != 0 {
                        return false
                }
@@ -498,8 +489,7 @@ func TestTossTrns(t *testing.T) {
                                panic(err)
                        }
                }
-               ctx.Toss(ctx.Self.Id, TRx, 123,
-                       false, false, false, false, false, false, false, false)
+               ctx.Toss(ctx.Self.Id, TRx, &TossOpts{Nice: 123})
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }