]> 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 0a21611a7590cb0125dbdfe51782fd067f9e7fdd..28d2eb27ff40bfa81db7d7d6f5e69b747a1df5d5 100644 (file)
@@ -1,5 +1,5 @@
 /*
-NNCP -- Node-to-Node CoPy
+NNCP -- Node to Node copy, utilities for store-and-forward data exchange
 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
@@ -37,22 +37,25 @@ 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()
 }
 
 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")
-               force    = flag.Bool("force", false, "Force outbound directories creation")
-               keep     = flag.Bool("keep", false, "Do not delete transferred packets")
-               debug    = flag.Bool("debug", false, "Enable debugging information")
-               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()
@@ -76,15 +79,10 @@ func main() {
                log.Fatalln("-rx and -tx can not be set simultaneously")
        }
 
-       cfgRaw, err := ioutil.ReadFile(*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.Debug = *debug
 
        var nodeOnly *nncp.Node
        if *nodeRaw != "" {
@@ -94,8 +92,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 +109,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 +146,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 +165,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.MagicNNCPEv3 {
                                ctx.LogD("nncp-xfer", sds, "is not a packet")
                                fd.Close()
                                continue
@@ -190,7 +188,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 +207,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 +215,7 @@ func main() {
 
 Tx:
        if *rxOnly {
-               if badExit {
+               if isBad {
                        os.Exit(1)
                }
                return
@@ -239,24 +237,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 +262,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 +281,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.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")
                                job.Fd.Close()
-                               badExit = true
+                               isBad = true
                                break
                        }
                        sds["tmp"] = tmp.Name()
@@ -298,7 +306,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 +314,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 +330,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)
        }
 }