From: Sergey Matveev Date: Sun, 8 Jan 2017 10:00:35 +0000 (+0300) Subject: Add bad return codes X-Git-Tag: 0.1~22 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=38b218d06629c91cf5ad5d80f1719ba84e473bd0;p=nncp.git Add bad return codes --- diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go index 813f165..ad28b76 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-call/main.go @@ -128,6 +128,8 @@ func main() { }, "") } else { ctx.LogE("call-start", nncp.SDS{"node": state.NodeId, "err": err}, "") + conn.Close() + os.Exit(1) } conn.Close() } diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-check/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-check/main.go index 3af4877..32625fd 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-check/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-check/main.go @@ -73,15 +73,14 @@ func main() { } } - checkIsBad := false + isBad := false for nodeId, node := range ctx.Neigh { if nodeOnly != nil && nodeId != *nodeOnly.Id { continue } - checkIsBad = checkIsBad || ctx.Check(node.Id) + isBad = isBad || ctx.Check(node.Id) } - - if checkIsBad { + if isBad { os.Exit(1) } } diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go index 79262bf..1691a8f 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-toss/main.go @@ -79,10 +79,14 @@ func main() { } } + isBad := false for nodeId, node := range ctx.Neigh { if nodeOnly != nil && nodeId != *nodeOnly.Id { continue } - ctx.Toss(node.Id, nice, *dryRun) + isBad = ctx.Toss(node.Id, nice, *dryRun) + } + if isBad { + os.Exit(1) } } diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go index e775ec7..434613d 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go @@ -95,7 +95,7 @@ func main() { } selfPath := filepath.Join(flag.Arg(0), ctx.Self.Id.String()) - badExit := false + isBad := false var dir *os.File var fis []os.FileInfo sds := nncp.SDS{} @@ -111,20 +111,20 @@ func main() { goto Tx } ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat") - badExit = true + isBad = true goto Tx } dir, err = os.Open(selfPath) if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open") - badExit = true + isBad = true goto Tx } fis, err = dir.Readdir(0) dir.Close() if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read") - badExit = true + isBad = true goto Tx } for _, fi := range fis { @@ -148,14 +148,14 @@ func main() { dir, err = os.Open(filepath.Join(selfPath, fi.Name())) if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open") - badExit = true + isBad = true continue } fisInt, err := dir.Readdir(0) dir.Close() if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read") - badExit = true + isBad = true continue } for _, fiInt := range fisInt { @@ -167,7 +167,7 @@ func main() { fd, err := os.Open(filename) if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open") - badExit = true + isBad = true continue } var pktEnc nncp.PktEnc @@ -190,7 +190,7 @@ func main() { copied, err := io.Copy(tmp.W, bufio.NewReader(fd)) if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy") - badExit = true + isBad = true fd.Close() tmp.Cancel() continue @@ -209,7 +209,7 @@ func main() { if !*keep { if err = os.Remove(filename); err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove") - badExit = true + isBad = true } } } @@ -217,7 +217,7 @@ func main() { Tx: if *rxOnly { - if badExit { + if isBad { os.Exit(1) } return @@ -246,13 +246,13 @@ Tx: if err = os.Mkdir(nodePath, os.FileMode(0700)); err != nil { ctx.UnlockDir(dirLock) ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir") - badExit = true + isBad = true continue } } else { ctx.UnlockDir(dirLock) ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat") - badExit = true + isBad = true continue } } @@ -264,13 +264,13 @@ Tx: if err = os.Mkdir(dstPath, os.FileMode(0700)); err != nil { ctx.UnlockDir(dirLock) ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir") - badExit = true + isBad = true continue } } else { ctx.UnlockDir(dirLock) ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat") - badExit = true + isBad = true continue } } @@ -287,7 +287,7 @@ Tx: if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp") job.Fd.Close() - badExit = true + isBad = true break } sds["tmp"] = tmp.Name() @@ -298,7 +298,7 @@ Tx: if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy") tmp.Close() - badExit = true + isBad = true continue } err = bufW.Flush() @@ -306,12 +306,12 @@ Tx: tmp.Close() if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy") - badExit = true + isBad = true continue } if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename") - badExit = true + isBad = true continue } delete(sds, "tmp") @@ -321,13 +321,13 @@ Tx: if !*keep { if err = os.Remove(job.Fd.Name()); err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove") - badExit = true + isBad = true } } } ctx.UnlockDir(dirLock) } - if badExit { + if isBad { os.Exit(1) } } diff --git a/src/cypherpunks.ru/nncp/toss.go b/src/cypherpunks.ru/nncp/toss.go index bf2527a..66e5812 100644 --- a/src/cypherpunks.ru/nncp/toss.go +++ b/src/cypherpunks.ru/nncp/toss.go @@ -76,12 +76,13 @@ func (ctx *Ctx) UnlockDir(fd *os.File) { } } -func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { +func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) bool { dirLock, err := ctx.LockDir(nodeId, TRx) if err != nil { - return + return false } defer ctx.UnlockDir(dirLock) + isBad := false for job := range ctx.Jobs(nodeId, TRx) { pktName := filepath.Base(job.Fd.Name()) sds := SDS{"node": job.PktEnc.Sender, "pkt": pktName} @@ -114,6 +115,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { var pktSize int64 if _, err = xdr.Unmarshal(pipeR, &pkt); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "unmarshal") + isBad = true goto Closing } pktSize = job.Size - PktEncOverhead - PktOverhead @@ -142,6 +144,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { cmd.Stdin = decompressor if err = cmd.Run(); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "sendmail") + isBad = true goto Closing } } @@ -149,6 +152,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { if !dryRun { if err = os.Remove(job.Fd.Name()); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove") + isBad = true } } case PktTypeFile: @@ -157,11 +161,13 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { incoming := ctx.Neigh[*job.PktEnc.Sender].Incoming if incoming == nil { ctx.LogE("rx", sds, "incoming is not allowed") + isBad = true goto Closing } dir := filepath.Join(*incoming, path.Dir(dst)) if err = os.MkdirAll(dir, os.FileMode(0700)); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mkdir") + isBad = true goto Closing } if !dryRun { @@ -170,11 +176,13 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { ctx.LogD("rx", sds, "created") if err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "mktemp") + isBad = true goto Closing } bufW := bufio.NewWriter(tmp) if _, err = io.Copy(bufW, pipeR); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "copy") + isBad = true goto Closing } bufW.Flush() @@ -189,6 +197,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { break } ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "stat") + isBad = true goto Closing } dstPath = dstPathOrig + strconv.Itoa(dstPathCtr) @@ -196,6 +205,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { } if err = os.Rename(tmp.Name(), dstPath); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "rename") + isBad = true } delete(sds, "tmp") } @@ -203,6 +213,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { if !dryRun { if err = os.Remove(job.Fd.Name()); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove") + isBad = true } sendmail := ctx.Neigh[*ctx.Self.Id].Sendmail if ctx.NotifyFile != nil { @@ -225,6 +236,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { dstRaw, err := ioutil.ReadAll(pipeR) if err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "read") + isBad = true goto Closing } dst := string(dstRaw) @@ -233,11 +245,13 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { freq := sender.Freq if freq == nil { ctx.LogE("rx", sds, "freqing is not allowed") + isBad = true 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") + isBad = true goto Closing } } @@ -245,6 +259,7 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { if !dryRun { if err = os.Remove(job.Fd.Name()); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove") + isBad = true } if ctx.NotifyFreq != nil { sendmail := ctx.Neigh[*ctx.Self.Id].Sendmail @@ -268,12 +283,14 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { sds := SdsAdd(sds, SDS{"type": "trns", "dst": nodeId}) if !known { ctx.LogE("rx", sds, "unknown node") + isBad = true goto Closing } ctx.LogD("rx", sds, "taken") if !dryRun { if err = ctx.TxTrns(node, job.PktEnc.Nice, pktSize, pipeR); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx trns") + isBad = true goto Closing } } @@ -281,12 +298,15 @@ func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) { if !dryRun { if err = os.Remove(job.Fd.Name()); err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "remove") + isBad = true } } default: ctx.LogE("rx", sds, "unknown type") + isBad = true } Closing: pipeR.Close() } + return isBad }