]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/jobs.go
Intermediate .nock packets step
[nncp.git] / src / jobs.go
index d7363ed6565e9d5decb417c339130be06da65b84..3e97b2ea52f89860a22b2c4c4081ff774cd1e8ac 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2020 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -18,9 +18,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package nncp
 
 import (
-       "io"
        "os"
        "path/filepath"
+       "strings"
 
        xdr "github.com/davecgh/go-xdr/xdr2"
 )
@@ -34,12 +34,12 @@ const (
 
 type Job struct {
        PktEnc   *PktEnc
-       Fd       *os.File
+       Path     string
        Size     int64
        HshValue *[32]byte
 }
 
-func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
+func (ctx *Ctx) jobsFind(nodeId *NodeId, xx TRxTx, nock bool) chan Job {
        rxPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx))
        jobs := make(chan Job, 16)
        go func() {
@@ -54,33 +54,41 @@ func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
                        return
                }
                for _, fi := range fis {
-                       hshValue, err := Base32Codec.DecodeString(fi.Name())
+                       var hshValue []byte
+                       if nock {
+                               if !strings.HasSuffix(fi.Name(), NoCKSuffix) {
+                                       continue
+                               }
+                               hshValue, err = Base32Codec.DecodeString(
+                                       strings.TrimSuffix(fi.Name(), NoCKSuffix),
+                               )
+                       } else {
+                               hshValue, err = Base32Codec.DecodeString(fi.Name())
+                       }
                        if err != nil {
                                continue
                        }
-                       fd, err := os.Open(filepath.Join(rxPath, fi.Name()))
+                       pth := filepath.Join(rxPath, fi.Name())
+                       fd, err := os.Open(pth)
                        if err != nil {
                                continue
                        }
                        var pktEnc PktEnc
-                       if _, err = xdr.Unmarshal(fd, &pktEnc); err != nil || pktEnc.Magic != MagicNNCPEv4 {
-                               fd.Close() // #nosec G104
+                       _, err = xdr.Unmarshal(fd, &pktEnc)
+                       fd.Close()
+                       if err != nil || pktEnc.Magic != MagicNNCPEv4 {
                                continue
                        }
-                       if _, err = fd.Seek(0, io.SeekStart); err != nil {
-                               fd.Close() // #nosec G104
-                               continue
-                       }
-                       ctx.LogD("jobs", SDS{
-                               "xx":   string(xx),
-                               "node": pktEnc.Sender,
-                               "name": fi.Name(),
-                               "nice": int(pktEnc.Nice),
-                               "size": fi.Size(),
+                       ctx.LogD("jobs", LEs{
+                               {"XX", string(xx)},
+                               {"Node", pktEnc.Sender},
+                               {"Name", fi.Name()},
+                               {"Nice", int(pktEnc.Nice)},
+                               {"Size", fi.Size()},
                        }, "taken")
                        job := Job{
                                PktEnc:   &pktEnc,
-                               Fd:       fd,
+                               Path:     pth,
                                Size:     fi.Size(),
                                HshValue: new([32]byte),
                        }
@@ -90,3 +98,11 @@ func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
        }()
        return jobs
 }
+
+func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
+       return ctx.jobsFind(nodeId, xx, false)
+}
+
+func (ctx *Ctx) JobsNoCK(nodeId *NodeId) chan Job {
+       return ctx.jobsFind(nodeId, TRx, true)
+}