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/>.
27 xdr "github.com/davecgh/go-xdr/xdr2"
28 "github.com/dustin/go-humanize"
44 HshValue *[MTHSize]byte
47 func (ctx *Ctx) HdrRead(fd *os.File) (*PktEnc, []byte, error) {
49 _, err := xdr.Unmarshal(fd, &pktEnc)
54 if _, err = xdr.Marshal(&raw, pktEnc); err != nil {
57 return &pktEnc, raw.Bytes(), nil
60 func (ctx *Ctx) HdrWrite(pktEncRaw []byte, tgt string) error {
61 tmpHdr, err := ctx.NewTmpFile()
63 ctx.LogE("hdr-write-tmp-new", nil, err, func(les LEs) string {
64 return "Header writing: new temporary file"
68 if _, err = tmpHdr.Write(pktEncRaw); err != nil {
69 ctx.LogE("hdr-write-write", nil, err, func(les LEs) string {
70 return "Header writing: writing"
72 os.Remove(tmpHdr.Name())
75 if err = tmpHdr.Close(); err != nil {
76 ctx.LogE("hdr-write-close", nil, err, func(les LEs) string {
77 return "Header writing: closing"
79 os.Remove(tmpHdr.Name())
82 if err = os.Rename(tmpHdr.Name(), tgt+HdrSuffix); err != nil {
83 ctx.LogE("hdr-write-rename", nil, err, func(les LEs) string {
84 return "Header writing: renaming"
91 func (ctx *Ctx) jobsFind(nodeId *NodeId, xx TRxTx, nock bool) chan Job {
92 rxPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx))
93 jobs := make(chan Job, 16)
96 dir, err := os.Open(rxPath)
100 fis, err := dir.Readdir(0)
101 dir.Close() // #nosec G104
105 for _, fi := range fis {
109 if !strings.HasSuffix(name, NoCKSuffix) ||
110 len(name) != Base32Encoded32Len+len(NoCKSuffix) {
113 hshValue, err = Base32Codec.DecodeString(
114 strings.TrimSuffix(name, NoCKSuffix),
117 if len(name) != Base32Encoded32Len {
120 hshValue, err = Base32Codec.DecodeString(name)
125 pth := filepath.Join(rxPath, name)
129 fd, err = os.Open(pth)
131 fd, err = os.Open(pth + HdrSuffix)
132 if err != nil && os.IsNotExist(err) {
134 fd, err = os.Open(pth)
140 pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
142 if err != nil || pktEnc.Magic != MagicNNCPEv5.B {
147 {"Node", pktEnc.Sender},
149 {"Nice", int(pktEnc.Nice)},
151 }, func(les LEs) string {
153 "Job %s/%s/%s nice: %s size: %s",
154 pktEnc.Sender, string(xx), name,
155 NicenessFmt(pktEnc.Nice),
156 humanize.IBytes(uint64(fi.Size())),
159 if !hdrExists && ctx.HdrUsage {
160 ctx.HdrWrite(pktEncRaw, pth)
166 HshValue: new([MTHSize]byte),
168 copy(job.HshValue[:], hshValue)
175 func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
176 return ctx.jobsFind(nodeId, xx, false)
179 func (ctx *Ctx) JobsNoCK(nodeId *NodeId) chan Job {
180 return ctx.jobsFind(nodeId, TRx, true)