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/>.
28 xdr "github.com/davecgh/go-xdr/xdr2"
29 "github.com/dustin/go-humanize"
45 HshValue *[MTHSize]byte
48 func (ctx *Ctx) HdrRead(r io.Reader) (*PktEnc, []byte, error) {
50 _, err := xdr.Unmarshal(r, &pktEnc)
55 if _, err = xdr.Marshal(&raw, pktEnc); err != nil {
58 return &pktEnc, raw.Bytes(), nil
61 func (ctx *Ctx) HdrWrite(pktEncRaw []byte, tgt string) error {
62 tmpHdr, err := ctx.NewTmpFile()
64 ctx.LogE("hdr-write-tmp-new", nil, err, func(les LEs) string {
65 return "Header writing: new temporary file"
69 if _, err = tmpHdr.Write(pktEncRaw); err != nil {
70 ctx.LogE("hdr-write-write", nil, err, func(les LEs) string {
71 return "Header writing: writing"
73 os.Remove(tmpHdr.Name())
76 if err = tmpHdr.Close(); err != nil {
77 ctx.LogE("hdr-write-close", nil, err, func(les LEs) string {
78 return "Header writing: closing"
80 os.Remove(tmpHdr.Name())
83 if err = os.Rename(tmpHdr.Name(), tgt+HdrSuffix); err != nil {
84 ctx.LogE("hdr-write-rename", nil, err, func(les LEs) string {
85 return "Header writing: renaming"
92 func (ctx *Ctx) jobsFind(nodeId *NodeId, xx TRxTx, nock, part bool) chan Job {
93 rxPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx))
94 jobs := make(chan Job, 16)
97 dir, err := os.Open(rxPath)
101 fis, err := dir.Readdir(0)
106 for _, fi := range fis {
110 if !strings.HasSuffix(name, NoCKSuffix) ||
111 len(name) != Base32Encoded32Len+len(NoCKSuffix) {
114 hshValue, err = Base32Codec.DecodeString(
115 strings.TrimSuffix(name, NoCKSuffix),
118 if !strings.HasSuffix(name, PartSuffix) ||
119 len(name) != Base32Encoded32Len+len(PartSuffix) {
122 hshValue, err = Base32Codec.DecodeString(
123 strings.TrimSuffix(name, PartSuffix),
126 if len(name) != Base32Encoded32Len {
129 hshValue, err = Base32Codec.DecodeString(name)
134 pth := filepath.Join(rxPath, name)
138 fd, err = os.Open(pth)
140 fd, err = os.Open(pth + HdrSuffix)
141 if err != nil && os.IsNotExist(err) {
143 fd, err = os.Open(pth)
153 HshValue: new([MTHSize]byte),
155 copy(job.HshValue[:], hshValue)
159 pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
164 switch pktEnc.Magic {
166 err = MagicNNCPEv1.TooOld()
168 err = MagicNNCPEv2.TooOld()
170 err = MagicNNCPEv3.TooOld()
172 err = MagicNNCPEv4.TooOld()
182 }, err, func(les LEs) string {
184 "Job %s/%s size: %s",
186 humanize.IBytes(uint64(fi.Size())),
193 {"Node", pktEnc.Sender},
195 {"Nice", int(pktEnc.Nice)},
197 }, func(les LEs) string {
199 "Job %s/%s/%s nice: %s size: %s",
200 pktEnc.Sender, string(xx), name,
201 NicenessFmt(pktEnc.Nice),
202 humanize.IBytes(uint64(fi.Size())),
205 if !hdrExists && ctx.HdrUsage {
206 ctx.HdrWrite(pktEncRaw, pth)
212 HshValue: new([MTHSize]byte),
214 copy(job.HshValue[:], hshValue)
221 func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
222 return ctx.jobsFind(nodeId, xx, false, false)
225 func (ctx *Ctx) JobsNoCK(nodeId *NodeId) chan Job {
226 return ctx.jobsFind(nodeId, TRx, true, false)
229 func (ctx *Ctx) JobsPart(nodeId *NodeId) chan Job {
230 return ctx.jobsFind(nodeId, TRx, false, true)