From 73d91f1e7801701b7b21c80768b020945657389e Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 8 Jan 2017 12:47:16 +0300 Subject: [PATCH] -dryrun for tosser --- src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go | 3 +- src/cypherpunks.ru/nncp/toss.go | 183 ++++++++++-------- src/cypherpunks.ru/nncp/toss_test.go | 20 +- 3 files changed, 111 insertions(+), 95 deletions(-) diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go index 5914d03..79262bf 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go @@ -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) } } diff --git a/src/cypherpunks.ru/nncp/toss.go b/src/cypherpunks.ru/nncp/toss.go index c426987..bf2527a 100644 --- a/src/cypherpunks.ru/nncp/toss.go +++ b/src/cypherpunks.ru/nncp/toss.go @@ -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") diff --git a/src/cypherpunks.ru/nncp/toss_test.go b/src/cypherpunks.ru/nncp/toss_test.go index 001a25b..ee8ae80 100644 --- a/src/cypherpunks.ru/nncp/toss_test.go +++ b/src/cypherpunks.ru/nncp/toss_test.go @@ -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 } -- 2.48.1