]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go
Replace Twofish/HKDF with ChaCha20/BLAKE2X for speed and simplicity
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-xfer / main.go
index 5abac1f02db6789aad34d628cb9b5acdd7c1662e..28d2eb27ff40bfa81db7d7d6f5e69b747a1df5d5 100644 (file)
@@ -43,17 +43,19 @@ func usage() {
 
 func main() {
        var (
-               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")
-               rxOnly   = flag.Bool("rx", false, "Only receive packets")
-               txOnly   = flag.Bool("tx", false, "Only transfer packets")
-               mkdir    = flag.Bool("mkdir", false, "Create necessary outbound directories")
-               keep     = flag.Bool("keep", false, "Do not delete transferred packets")
-               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")
+               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")
+               rxOnly    = flag.Bool("rx", false, "Only receive packets")
+               txOnly    = flag.Bool("tx", false, "Only transfer packets")
+               mkdir     = flag.Bool("mkdir", false, "Create necessary outbound directories")
+               keep      = flag.Bool("keep", false, "Do not delete transferred packets")
+               spoolPath = flag.String("spool", "", "Override path to spool")
+               logPath   = flag.String("log", "", "Override path to logfile")
+               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")
        )
        flag.Usage = usage
        flag.Parse()
@@ -77,16 +79,10 @@ func main() {
                log.Fatalln("-rx and -tx can not be set simultaneously")
        }
 
-       cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
+       ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
        if err != nil {
-               log.Fatalln("Can not read config:", err)
+               log.Fatalln("Error during initialization:", err)
        }
-       ctx, err := nncp.CfgParse(cfgRaw)
-       if err != nil {
-               log.Fatalln("Can not parse config:", err)
-       }
-       ctx.Quiet = *quiet
-       ctx.Debug = *debug
 
        var nodeOnly *nncp.Node
        if *nodeRaw != "" {
@@ -174,7 +170,7 @@ func main() {
                        }
                        var pktEnc nncp.PktEnc
                        _, err = xdr.Unmarshal(fd, &pktEnc)
-                       if err != nil || pktEnc.Magic != nncp.MagicNNCPEv2 {
+                       if err != nil || pktEnc.Magic != nncp.MagicNNCPEv3 {
                                ctx.LogD("nncp-xfer", sds, "is not a packet")
                                fd.Close()
                                continue
@@ -285,6 +281,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.SeenSuffix)); 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")