2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 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 JobPath2Hdr(jobPath string) string {
49 return filepath.Join(filepath.Dir(jobPath), HdrDir, filepath.Base(jobPath))
52 func (ctx *Ctx) HdrRead(r io.Reader) (*PktEnc, []byte, error) {
54 _, err := xdr.Unmarshal(r, &pktEnc)
59 if _, err = xdr.Marshal(&raw, pktEnc); err != nil {
62 return &pktEnc, raw.Bytes(), nil
65 func (ctx *Ctx) HdrWrite(pktEncRaw []byte, tgt string) error {
66 tmpHdr, err := ctx.NewTmpFile()
68 ctx.LogE("hdr-write-tmp-new", nil, err, func(les LEs) string {
69 return "Header writing: new temporary file"
73 if _, err = tmpHdr.Write(pktEncRaw); err != nil {
74 ctx.LogE("hdr-write-write", nil, err, func(les LEs) string {
75 return "Header writing: writing"
77 os.Remove(tmpHdr.Name())
80 if err = tmpHdr.Close(); err != nil {
81 ctx.LogE("hdr-write-close", nil, err, func(les LEs) string {
82 return "Header writing: closing"
84 os.Remove(tmpHdr.Name())
87 if err = ensureDir(filepath.Dir(tgt), HdrDir); err != nil {
88 ctx.LogE("hdr-write-ensure-mkdir", nil, err, func(les LEs) string {
89 return "Header writing: ensuring directory"
93 if err = os.Rename(tmpHdr.Name(), JobPath2Hdr(tgt)); err != nil {
94 ctx.LogE("hdr-write-rename", nil, err, func(les LEs) string {
95 return "Header writing: renaming"
102 func (ctx *Ctx) jobsFind(nodeId *NodeId, xx TRxTx, nock, part bool) chan Job {
103 rxPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx))
104 jobs := make(chan Job, 16)
107 dir, err := os.Open(rxPath)
111 fis, err := dir.Readdir(0)
116 for _, fi := range fis {
120 if !strings.HasSuffix(name, NoCKSuffix) ||
121 len(name) != Base32Encoded32Len+len(NoCKSuffix) {
124 hshValue, err = Base32Codec.DecodeString(
125 strings.TrimSuffix(name, NoCKSuffix),
128 if !strings.HasSuffix(name, PartSuffix) ||
129 len(name) != Base32Encoded32Len+len(PartSuffix) {
132 hshValue, err = Base32Codec.DecodeString(
133 strings.TrimSuffix(name, PartSuffix),
136 if len(name) != Base32Encoded32Len {
139 hshValue, err = Base32Codec.DecodeString(name)
144 pth := filepath.Join(rxPath, name)
148 fd, err = os.Open(pth)
150 fd, err = os.Open(JobPath2Hdr(pth))
151 if err != nil && os.IsNotExist(err) {
153 fd, err = os.Open(pth)
163 HshValue: new([MTHSize]byte),
165 copy(job.HshValue[:], hshValue)
169 pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
174 switch pktEnc.Magic {
176 err = MagicNNCPEv1.TooOld()
178 err = MagicNNCPEv2.TooOld()
180 err = MagicNNCPEv3.TooOld()
182 err = MagicNNCPEv4.TooOld()
184 err = MagicNNCPEv5.TooOld()
194 }, err, func(les LEs) string {
196 "Job %s/%s size: %s",
198 humanize.IBytes(uint64(fi.Size())),
205 {"Node", pktEnc.Sender},
207 {"Nice", int(pktEnc.Nice)},
209 }, func(les LEs) string {
211 "Job %s/%s/%s nice: %s size: %s",
212 pktEnc.Sender, string(xx), name,
213 NicenessFmt(pktEnc.Nice),
214 humanize.IBytes(uint64(fi.Size())),
217 if !hdrExists && ctx.HdrUsage {
218 ctx.HdrWrite(pktEncRaw, pth)
224 HshValue: new([MTHSize]byte),
226 copy(job.HshValue[:], hshValue)
233 func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
234 return ctx.jobsFind(nodeId, xx, false, false)
237 func (ctx *Ctx) JobsNoCK(nodeId *NodeId) chan Job {
238 return ctx.jobsFind(nodeId, TRx, true, false)
241 func (ctx *Ctx) JobsPart(nodeId *NodeId) chan Job {
242 return ctx.jobsFind(nodeId, TRx, false, true)