]> Cypherpunks.ru repositories - nncp.git/commitdiff
-dryrun for tosser
authorSergey Matveev <stargrave@stargrave.org>
Sun, 8 Jan 2017 09:47:16 +0000 (12:47 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 8 Jan 2017 09:47:16 +0000 (12:47 +0300)
src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go
src/cypherpunks.ru/nncp/toss.go
src/cypherpunks.ru/nncp/toss_test.go

index 5914d03f0d3a0a1f1199b3d814032615884ba6cb..79262bf6a2c814c9be842f298a9b163c5ceca9ac 100644 (file)
@@ -41,6 +41,7 @@ func main() {
                cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
                nodeRaw  = flag.String("node", "", "Process only that node")
                niceRaw  = flag.Int("nice", 255, "Minimal required niceness")
+               dryRun   = flag.Bool("dryrun", false, "Do not actually write any tossed data")
                debug    = flag.Bool("debug", false, "Enable debugging information")
                version  = flag.Bool("version", false, "Print version information")
                warranty = flag.Bool("warranty", false, "Print warranty information")
@@ -82,6 +83,6 @@ func main() {
                if nodeOnly != nil && nodeId != *nodeOnly.Id {
                        continue
                }
-               ctx.Toss(node.Id, nice)
+               ctx.Toss(node.Id, nice, *dryRun)
        }
 }
index c426987a747c54a754b132a44ac57e66753eedf6..bf2527a32b7ff5c89bfd6d8ab186b131b4b7d976 100644 (file)
@@ -76,7 +76,7 @@ func (ctx *Ctx) UnlockDir(fd *os.File) {
        }
 }
 
-func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8) {
+func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) {
        dirLock, err := ctx.LockDir(nodeId, TRx)
        if err != nil {
                return
@@ -131,21 +131,25 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8) {
                                log.Fatalln(err)
                        }
                        sendmail := ctx.Neigh[*job.PktEnc.Sender].Sendmail
-                       cmd := exec.Command(
-                               sendmail[0],
-                               append(
-                                       sendmail[1:len(sendmail)],
-                                       strings.Split(recipients, " ")...,
-                               )...,
-                       )
-                       cmd.Stdin = decompressor
-                       if err = cmd.Run(); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
-                               goto Closing
+                       if !dryRun {
+                               cmd := exec.Command(
+                                       sendmail[0],
+                                       append(
+                                               sendmail[1:len(sendmail)],
+                                               strings.Split(recipients, " ")...,
+                                       )...,
+                               )
+                               cmd.Stdin = decompressor
+                               if err = cmd.Run(); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail")
+                                       goto Closing
+                               }
                        }
                        ctx.LogI("rx", sds, "")
-                       if err = os.Remove(job.Fd.Name()); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                       if !dryRun {
+                               if err = os.Remove(job.Fd.Name()); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                               }
                        }
                case PktTypeFile:
                        dst := string(pkt.Path[:int(pkt.PathLen)])
@@ -160,56 +164,60 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8) {
                                ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir")
                                goto Closing
                        }
-                       tmp, err := ioutil.TempFile(dir, "nncp-file")
-                       sds["tmp"] = tmp.Name()
-                       ctx.LogD("rx", sds, "created")
-                       if err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
-                               goto Closing
-                       }
-                       bufW := bufio.NewWriter(tmp)
-                       if _, err = io.Copy(bufW, pipeR); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
-                               goto Closing
-                       }
-                       bufW.Flush()
-                       tmp.Sync()
-                       tmp.Close()
-                       dstPathOrig := filepath.Join(*incoming, dst)
-                       dstPath := dstPathOrig
-                       dstPathCtr := 0
-                       for {
-                               if _, err = os.Stat(dstPath); err != nil {
-                                       if os.IsNotExist(err) {
-                                               break
-                                       }
-                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
+                       if !dryRun {
+                               tmp, err := ioutil.TempFile(dir, "nncp-file")
+                               sds["tmp"] = tmp.Name()
+                               ctx.LogD("rx", sds, "created")
+                               if err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp")
                                        goto Closing
                                }
-                               dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
-                               dstPathCtr++
-                       }
-                       if err = os.Rename(tmp.Name(), dstPath); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
+                               bufW := bufio.NewWriter(tmp)
+                               if _, err = io.Copy(bufW, pipeR); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy")
+                                       goto Closing
+                               }
+                               bufW.Flush()
+                               tmp.Sync()
+                               tmp.Close()
+                               dstPathOrig := filepath.Join(*incoming, dst)
+                               dstPath := dstPathOrig
+                               dstPathCtr := 0
+                               for {
+                                       if _, err = os.Stat(dstPath); err != nil {
+                                               if os.IsNotExist(err) {
+                                                       break
+                                               }
+                                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat")
+                                               goto Closing
+                                       }
+                                       dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
+                                       dstPathCtr++
+                               }
+                               if err = os.Rename(tmp.Name(), dstPath); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename")
+                               }
+                               delete(sds, "tmp")
                        }
-                       delete(sds, "tmp")
                        ctx.LogI("rx", sds, "")
-                       if err = os.Remove(job.Fd.Name()); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
-                       }
-                       sendmail := ctx.Neigh[*ctx.Self.Id].Sendmail
-                       if ctx.NotifyFile != nil {
-                               cmd := exec.Command(
-                                       sendmail[0],
-                                       append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
-                               )
-                               cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
-                                       "File from %s: %s (%s)",
-                                       ctx.Neigh[*job.PktEnc.Sender].Name,
-                                       dst,
-                                       humanize.IBytes(uint64(pktSize)),
-                               ))
-                               cmd.Run()
+                       if !dryRun {
+                               if err = os.Remove(job.Fd.Name()); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                               }
+                               sendmail := ctx.Neigh[*ctx.Self.Id].Sendmail
+                               if ctx.NotifyFile != nil {
+                                       cmd := exec.Command(
+                                               sendmail[0],
+                                               append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
+                                       )
+                                       cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
+                                               "File from %s: %s (%s)",
+                                               ctx.Neigh[*job.PktEnc.Sender].Name,
+                                               dst,
+                                               humanize.IBytes(uint64(pktSize)),
+                                       ))
+                                       cmd.Run()
+                               }
                        }
                case PktTypeFreq:
                        src := string(pkt.Path[:int(pkt.PathLen)])
@@ -227,27 +235,30 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8) {
                                ctx.LogE("rx", sds, "freqing is not allowed")
                                goto Closing
                        }
-                       err = ctx.TxFile(sender, job.PktEnc.Nice, filepath.Join(*freq, src), dst)
-                       if err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
-                               goto Closing
+                       if !dryRun {
+                               if err = ctx.TxFile(sender, job.PktEnc.Nice, filepath.Join(*freq, src), dst); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file")
+                                       goto Closing
+                               }
                        }
                        ctx.LogI("rx", sds, "")
-                       if err = os.Remove(job.Fd.Name()); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
-                       }
-                       if ctx.NotifyFreq != nil {
-                               sendmail := ctx.Neigh[*ctx.Self.Id].Sendmail
-                               cmd := exec.Command(
-                                       sendmail[0],
-                                       append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
-                               )
-                               cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
-                                       "Freq from %s: %s",
-                                       ctx.Neigh[*job.PktEnc.Sender].Name,
-                                       src,
-                               ))
-                               cmd.Run()
+                       if !dryRun {
+                               if err = os.Remove(job.Fd.Name()); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                               }
+                               if ctx.NotifyFreq != nil {
+                                       sendmail := ctx.Neigh[*ctx.Self.Id].Sendmail
+                                       cmd := exec.Command(
+                                               sendmail[0],
+                                               append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
+                                       )
+                                       cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
+                                               "Freq from %s: %s",
+                                               ctx.Neigh[*job.PktEnc.Sender].Name,
+                                               src,
+                                       ))
+                                       cmd.Run()
+                               }
                        }
                case PktTypeTrns:
                        dst := new([blake2b.Size256]byte)
@@ -260,13 +271,17 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8) {
                                goto Closing
                        }
                        ctx.LogD("rx", sds, "taken")
-                       if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
-                               goto Closing
+                       if !dryRun {
+                               if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns")
+                                       goto Closing
+                               }
                        }
                        ctx.LogI("rx", sds, "")
-                       if err = os.Remove(job.Fd.Name()); err != nil {
-                               ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                       if !dryRun {
+                               if err = os.Remove(job.Fd.Name()); err != nil {
+                                       ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove")
+                               }
                        }
                default:
                        ctx.LogE("rx", sds, "unknown type")
index 001a25b5618191230e322f8f85ec6b4795ec929a..ee8ae80e6488c9c8a9a9e2b347c3a04f13fe3e04 100644 (file)
@@ -103,12 +103,12 @@ func TestTossEmail(t *testing.T) {
                        if len(dirFiles(rxPath)) == 0 {
                                continue
                        }
-                       ctx.Toss(ctx.Self.Id, DefaultNiceMail-1)
+                       ctx.Toss(ctx.Self.Id, DefaultNiceMail-1, false)
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
                        ctx.Neigh[*nodeOur.Id].Sendmail = []string{"/bin/sh", "-c", "false"}
-                       ctx.Toss(ctx.Self.Id, DefaultNiceMail)
+                       ctx.Toss(ctx.Self.Id, DefaultNiceMail, false)
                        if len(dirFiles(rxPath)) == 0 {
                                return false
                        }
@@ -116,7 +116,7 @@ func TestTossEmail(t *testing.T) {
                                "/bin/sh", "-c",
                                fmt.Sprintf("cat >> %s", filepath.Join(spool, "mbox")),
                        }
-                       ctx.Toss(ctx.Self.Id, DefaultNiceMail)
+                       ctx.Toss(ctx.Self.Id, DefaultNiceMail, false)
                        if len(dirFiles(rxPath)) != 0 {
                                return false
                        }
@@ -186,12 +186,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, DefaultNiceFile)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFile, false)
                if len(dirFiles(rxPath)) == 0 {
                        return false
                }
                ctx.Neigh[*nodeOur.Id].Incoming = &incomingPath
-               ctx.Toss(ctx.Self.Id, DefaultNiceFile)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFile, false)
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }
@@ -256,7 +256,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, DefaultNiceFile)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFile, false)
                expected := make(map[string]struct{})
                expected["samefile"] = struct{}{}
                for i := 0; i < files-1; i++ {
@@ -322,12 +322,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, DefaultNiceFreq)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false)
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
                ctx.Neigh[*nodeOur.Id].Freq = &spool
-               ctx.Toss(ctx.Self.Id, DefaultNiceFreq)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false)
                if len(dirFiles(txPath)) != 0 || len(dirFiles(rxPath)) == 0 {
                        return false
                }
@@ -340,7 +340,7 @@ func TestTossFreq(t *testing.T) {
                                panic(err)
                        }
                }
-               ctx.Toss(ctx.Self.Id, DefaultNiceFreq)
+               ctx.Toss(ctx.Self.Id, DefaultNiceFreq, false)
                if len(dirFiles(txPath)) == 0 || len(dirFiles(rxPath)) != 0 {
                        return false
                }
@@ -431,7 +431,7 @@ func TestTossTrns(t *testing.T) {
                                panic(err)
                        }
                }
-               ctx.Toss(ctx.Self.Id, 123)
+               ctx.Toss(ctx.Self.Id, 123, false)
                if len(dirFiles(rxPath)) != 0 {
                        return false
                }