2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 xdr "github.com/davecgh/go-xdr/xdr2"
45 func (ctx *Ctx) HdrRead(fd *os.File) (*PktEnc, []byte, error) {
47 _, err := xdr.Unmarshal(fd, &pktEnc)
52 if _, err = xdr.Marshal(&raw, pktEnc); err != nil {
55 return &pktEnc, raw.Bytes(), nil
58 func (ctx *Ctx) HdrWrite(pktEncRaw []byte, tgt string) error {
59 tmpHdr, err := ctx.NewTmpFile()
61 ctx.LogE("hdr-write", []LE{}, err, "new")
64 if _, err = tmpHdr.Write(pktEncRaw); err != nil {
65 ctx.LogE("hdr-write", []LE{}, err, "write")
66 os.Remove(tmpHdr.Name())
69 if err = tmpHdr.Close(); err != nil {
70 ctx.LogE("hdr-write", []LE{}, err, "close")
71 os.Remove(tmpHdr.Name())
74 if err = os.Rename(tmpHdr.Name(), tgt+HdrSuffix); err != nil {
75 ctx.LogE("hdr-write", []LE{}, err, "rename")
81 func (ctx *Ctx) jobsFind(nodeId *NodeId, xx TRxTx, nock bool) chan Job {
82 rxPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx))
83 jobs := make(chan Job, 16)
86 dir, err := os.Open(rxPath)
90 fis, err := dir.Readdir(0)
91 dir.Close() // #nosec G104
95 for _, fi := range fis {
99 if !strings.HasSuffix(name, NoCKSuffix) ||
100 len(name) != Base32Encoded32Len+len(NoCKSuffix) {
103 hshValue, err = Base32Codec.DecodeString(
104 strings.TrimSuffix(name, NoCKSuffix),
107 if len(name) != Base32Encoded32Len {
110 hshValue, err = Base32Codec.DecodeString(name)
115 pth := filepath.Join(rxPath, name)
119 fd, err = os.Open(pth)
121 fd, err = os.Open(pth + HdrSuffix)
122 if err != nil && os.IsNotExist(err) {
124 fd, err = os.Open(pth)
130 pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
132 if err != nil || pktEnc.Magic != MagicNNCPEv4 {
135 ctx.LogD("jobs", LEs{
137 {"Node", pktEnc.Sender},
139 {"Nice", int(pktEnc.Nice)},
142 if !hdrExists && ctx.HdrUsage {
143 ctx.HdrWrite(pktEncRaw, pth)
149 HshValue: new([32]byte),
151 copy(job.HshValue[:], hshValue)
158 func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
159 return ctx.jobsFind(nodeId, xx, false)
162 func (ctx *Ctx) JobsNoCK(nodeId *NodeId) chan Job {
163 return ctx.jobsFind(nodeId, TRx, true)