/*
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
package nncp
import (
- "io"
"os"
"path/filepath"
+ "strings"
xdr "github.com/davecgh/go-xdr/xdr2"
)
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() {
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),
}
}()
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)
+}