]> 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 940d8f54549948993299b14ceee7597c5ede2187..05a37489b8299a5de76e47b52e422d96661cf28e 100644 (file)
@@ -48,7 +48,7 @@ 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")
                quiet    = flag.Bool("quiet", false, "Print only errors")
                debug    = flag.Bool("debug", false, "Print debug messages")
@@ -96,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
@@ -174,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
@@ -184,7 +184,7 @@ func main() {
                                fd.Close()
                                continue
                        }
-                       fd.Seek(0, io.SeekStart)
+                       fd.Seek(0, 0)
                        tmp, err := ctx.NewTmpFileWHash()
                        if err != nil {
                                log.Fatalln(err)
@@ -241,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
                                }
@@ -258,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 {
@@ -285,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")
@@ -316,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),