]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go
Ability to create and deal with .seen files after tossing
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-xfer / main.go
index e775ec77fe8d641531238d20adf05093c42f1c8f..05a37489b8299a5de76e47b52e422d96661cf28e 100644 (file)
@@ -37,7 +37,7 @@ import (
 func usage() {
        fmt.Fprintf(os.Stderr, nncp.UsageHeader())
        fmt.Fprintln(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n")
-       fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:", os.Args[0])
+       fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
        flag.PrintDefaults()
 }
 
@@ -48,9 +48,10 @@ func main() {
                niceRaw  = flag.Int("nice", 255, "Minimal required niceness")
                rxOnly   = flag.Bool("rx", false, "Only receive packets")
                txOnly   = flag.Bool("tx", false, "Only transfer packets")
-               force    = flag.Bool("force", false, "Force outbound directories creation")
+               mkdir    = flag.Bool("mkdir", false, "Create necessary outbound directories")
                keep     = flag.Bool("keep", false, "Do not delete transferred packets")
-               debug    = flag.Bool("debug", false, "Enable debugging information")
+               quiet    = flag.Bool("quiet", false, "Print only errors")
+               debug    = flag.Bool("debug", false, "Print debug messages")
                version  = flag.Bool("version", false, "Print version information")
                warranty = flag.Bool("warranty", false, "Print warranty information")
        )
@@ -76,7 +77,7 @@ func main() {
                log.Fatalln("-rx and -tx can not be set simultaneously")
        }
 
-       cfgRaw, err := ioutil.ReadFile(*cfgPath)
+       cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
        if err != nil {
                log.Fatalln("Can not read config:", err)
        }
@@ -84,6 +85,7 @@ func main() {
        if err != nil {
                log.Fatalln("Can not parse config:", err)
        }
+       ctx.Quiet = *quiet
        ctx.Debug = *debug
 
        var nodeOnly *nncp.Node
@@ -94,8 +96,8 @@ func main() {
                }
        }
 
-       selfPath := filepath.Join(flag.Arg(0), ctx.Self.Id.String())
-       badExit := false
+       selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
+       isBad := false
        var dir *os.File
        var fis []os.FileInfo
        sds := nncp.SDS{}
@@ -111,20 +113,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 +150,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,12 +169,12 @@ 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
                        _, err = xdr.Unmarshal(fd, &pktEnc)
-                       if err != nil || pktEnc.Magic != nncp.MagicNNCPEv1 {
+                       if err != nil || pktEnc.Magic != nncp.MagicNNCPEv2 {
                                ctx.LogD("nncp-xfer", sds, "is not a packet")
                                fd.Close()
                                continue
@@ -190,7 +192,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 +211,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 +219,7 @@ func main() {
 
 Tx:
        if *rxOnly {
-               if badExit {
+               if isBad {
                        os.Exit(1)
                }
                return
@@ -239,24 +241,24 @@ Tx:
                if err != nil {
                        if os.IsNotExist(err) {
                                ctx.LogD("nncp-xfer", sds, "does not exist")
-                               if !*force {
+                               if !*mkdir {
                                        ctx.UnlockDir(dirLock)
                                        continue
                                }
                                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
                        }
                }
-               dstPath := filepath.Join(nodePath, ctx.Self.Id.String())
+               dstPath := filepath.Join(nodePath, ctx.SelfId.String())
                sds["dir"] = dstPath
                _, err = os.Stat(dstPath)
                if err != nil {
@@ -264,13 +266,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
                        }
                }
@@ -283,11 +285,21 @@ Tx:
                                job.Fd.Close()
                                continue
                        }
+                       if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
+                               ctx.LogD("nncp-xfer", sds, "already exists")
+                               job.Fd.Close()
+                               continue
+                       }
+                       if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenPostfix)); err == nil || !os.IsNotExist(err) {
+                               ctx.LogD("nncp-xfer", sds, "already exists")
+                               job.Fd.Close()
+                               continue
+                       }
                        tmp, err := ioutil.TempFile(dstPath, "nncp-xfer")
                        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 +310,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,14 +318,15 @@ 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
                        }
+                       os.Remove(filepath.Join(dstPath, pktName+".part"))
                        delete(sds, "tmp")
                        ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
                                "size": strconv.FormatInt(copied, 10),
@@ -321,13 +334,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)
        }
 }