]> 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 434613d495773988fb7cdc0273b5d10a2ca8b776..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,7 +96,7 @@ func main() {
                }
        }
 
-       selfPath := filepath.Join(flag.Arg(0), ctx.Self.Id.String())
+       selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
        isBad := false
        var dir *os.File
        var fis []os.FileInfo
@@ -172,7 +174,7 @@ func main() {
                        }
                        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
@@ -239,7 +241,7 @@ Tx:
                if err != nil {
                        if os.IsNotExist(err) {
                                ctx.LogD("nncp-xfer", sds, "does not exist")
-                               if !*force {
+                               if !*mkdir {
                                        ctx.UnlockDir(dirLock)
                                        continue
                                }
@@ -256,7 +258,7 @@ Tx:
                                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 {
@@ -283,6 +285,16 @@ 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")
@@ -314,6 +326,7 @@ Tx:
                                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),