]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/jobs.go
Intermediate .nock packets step
[nncp.git] / src / jobs.go
index 705bb7e551f09c68e04e922a4570ba6e81b9c31b..3e97b2ea52f89860a22b2c4c4081ff774cd1e8ac 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 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() {
@@ -49,35 +49,46 @@ func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
                        return
                }
                fis, err := dir.Readdir(0)
-               dir.Close()
+               dir.Close() // #nosec G104
                if err != nil {
                        return
                }
                for _, fi := range fis {
-                       hshValue, err := FromBase32(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()
+                       _, err = xdr.Unmarshal(fd, &pktEnc)
+                       fd.Close()
+                       if err != nil || pktEnc.Magic != MagicNNCPEv4 {
                                continue
                        }
-                       fd.Seek(0, io.SeekStart)
-                       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),
                        }
@@ -87,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)
+}