]> Cypherpunks.ru repositories - nncp.git/blob - src/jobs.go
Merge branch 'develop'
[nncp.git] / src / jobs.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package nncp
19
20 import (
21         "bytes"
22         "errors"
23         "fmt"
24         "io"
25         "io/fs"
26         "os"
27         "path/filepath"
28         "strings"
29
30         xdr "github.com/davecgh/go-xdr/xdr2"
31         "github.com/dustin/go-humanize"
32 )
33
34 type TRxTx string
35
36 const (
37         TRx TRxTx = "rx"
38         TTx TRxTx = "tx"
39
40         HdrDir = "hdr"
41 )
42
43 type Job struct {
44         PktEnc   *PktEnc
45         Path     string
46         Size     int64
47         HshValue *[MTHSize]byte
48 }
49
50 func JobPath2Hdr(jobPath string) string {
51         return filepath.Join(filepath.Dir(jobPath), HdrDir, filepath.Base(jobPath))
52 }
53
54 func (ctx *Ctx) HdrRead(r io.Reader) (*PktEnc, []byte, error) {
55         var pktEnc PktEnc
56         _, err := xdr.Unmarshal(r, &pktEnc)
57         if err != nil {
58                 return nil, nil, err
59         }
60         var raw bytes.Buffer
61         if _, err = xdr.Marshal(&raw, pktEnc); err != nil {
62                 panic(err)
63         }
64         return &pktEnc, raw.Bytes(), nil
65 }
66
67 func (ctx *Ctx) HdrWrite(pktEncRaw []byte, tgt string) error {
68         tmpHdr, err := ctx.NewTmpFile()
69         if err != nil {
70                 ctx.LogE("hdr-write-tmp-new", nil, err, func(les LEs) string {
71                         return "Header writing: new temporary file"
72                 })
73                 return err
74         }
75         if _, err = tmpHdr.Write(pktEncRaw); err != nil {
76                 ctx.LogE("hdr-write-write", nil, err, func(les LEs) string {
77                         return "Header writing: writing"
78                 })
79                 os.Remove(tmpHdr.Name())
80                 return err
81         }
82         if err = tmpHdr.Close(); err != nil {
83                 ctx.LogE("hdr-write-close", nil, err, func(les LEs) string {
84                         return "Header writing: closing"
85                 })
86                 os.Remove(tmpHdr.Name())
87                 return err
88         }
89         if err = ensureDir(filepath.Dir(tgt), HdrDir); err != nil {
90                 ctx.LogE("hdr-write-ensure-mkdir", nil, err, func(les LEs) string {
91                         return "Header writing: ensuring directory"
92                 })
93                 return err
94         }
95         if err = os.Rename(tmpHdr.Name(), JobPath2Hdr(tgt)); err != nil {
96                 ctx.LogE("hdr-write-rename", nil, err, func(les LEs) string {
97                         return "Header writing: renaming"
98                 })
99                 return err
100         }
101         return err
102 }
103
104 func (ctx *Ctx) jobsFind(nodeId *NodeId, xx TRxTx, nock, part bool) chan Job {
105         rxPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx))
106         jobs := make(chan Job, 16)
107         go func() {
108                 defer close(jobs)
109                 dir, err := os.Open(rxPath)
110                 if err != nil {
111                         return
112                 }
113                 fis, err := dir.Readdir(0)
114                 dir.Close()
115                 if err != nil {
116                         return
117                 }
118                 for _, fi := range fis {
119                         name := fi.Name()
120                         var hshValue []byte
121                         if nock {
122                                 if !strings.HasSuffix(name, NoCKSuffix) ||
123                                         len(name) != Base32Encoded32Len+len(NoCKSuffix) {
124                                         continue
125                                 }
126                                 hshValue, err = Base32Codec.DecodeString(
127                                         strings.TrimSuffix(name, NoCKSuffix),
128                                 )
129                         } else if part {
130                                 if !strings.HasSuffix(name, PartSuffix) ||
131                                         len(name) != Base32Encoded32Len+len(PartSuffix) {
132                                         continue
133                                 }
134                                 hshValue, err = Base32Codec.DecodeString(
135                                         strings.TrimSuffix(name, PartSuffix),
136                                 )
137                         } else {
138                                 if len(name) != Base32Encoded32Len {
139                                         continue
140                                 }
141                                 hshValue, err = Base32Codec.DecodeString(name)
142                         }
143                         if err != nil {
144                                 continue
145                         }
146                         pth := filepath.Join(rxPath, name)
147                         hdrExists := true
148                         var fd *os.File
149                         if nock || part {
150                                 fd, err = os.Open(pth)
151                         } else {
152                                 fd, err = os.Open(JobPath2Hdr(pth))
153                                 if err != nil && errors.Is(err, fs.ErrNotExist) {
154                                         hdrExists = false
155                                         fd, err = os.Open(pth)
156                                 }
157                         }
158                         if err != nil {
159                                 continue
160                         }
161                         if part {
162                                 job := Job{
163                                         Path:     pth,
164                                         Size:     fi.Size(),
165                                         HshValue: new([MTHSize]byte),
166                                 }
167                                 copy(job.HshValue[:], hshValue)
168                                 jobs <- job
169                                 continue
170                         }
171                         pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
172                         fd.Close()
173                         if err != nil {
174                                 continue
175                         }
176                         switch pktEnc.Magic {
177                         case MagicNNCPEv1.B:
178                                 err = MagicNNCPEv1.TooOld()
179                         case MagicNNCPEv2.B:
180                                 err = MagicNNCPEv2.TooOld()
181                         case MagicNNCPEv3.B:
182                                 err = MagicNNCPEv3.TooOld()
183                         case MagicNNCPEv4.B:
184                                 err = MagicNNCPEv4.TooOld()
185                         case MagicNNCPEv5.B:
186                                 err = MagicNNCPEv5.TooOld()
187                         case MagicNNCPEv6.B:
188                         default:
189                                 err = BadMagic
190                         }
191                         if err != nil {
192                                 ctx.LogE("job", LEs{
193                                         {"XX", string(xx)},
194                                         {"Name", name},
195                                         {"Size", fi.Size()},
196                                 }, err, func(les LEs) string {
197                                         return fmt.Sprintf(
198                                                 "Job %s/%s size: %s",
199                                                 string(xx), name,
200                                                 humanize.IBytes(uint64(fi.Size())),
201                                         )
202                                 })
203                                 continue
204                         }
205                         ctx.LogD("job", LEs{
206                                 {"XX", string(xx)},
207                                 {"Node", pktEnc.Sender},
208                                 {"Name", name},
209                                 {"Nice", int(pktEnc.Nice)},
210                                 {"Size", fi.Size()},
211                         }, func(les LEs) string {
212                                 return fmt.Sprintf(
213                                         "Job %s/%s/%s nice: %s size: %s",
214                                         pktEnc.Sender, string(xx), name,
215                                         NicenessFmt(pktEnc.Nice),
216                                         humanize.IBytes(uint64(fi.Size())),
217                                 )
218                         })
219                         if !hdrExists && ctx.HdrUsage {
220                                 ctx.HdrWrite(pktEncRaw, pth)
221                         }
222                         job := Job{
223                                 PktEnc:   pktEnc,
224                                 Path:     pth,
225                                 Size:     fi.Size(),
226                                 HshValue: new([MTHSize]byte),
227                         }
228                         copy(job.HshValue[:], hshValue)
229                         jobs <- job
230                 }
231         }()
232         return jobs
233 }
234
235 func (ctx *Ctx) Jobs(nodeId *NodeId, xx TRxTx) chan Job {
236         return ctx.jobsFind(nodeId, xx, false, false)
237 }
238
239 func (ctx *Ctx) JobsNoCK(nodeId *NodeId) chan Job {
240         return ctx.jobsFind(nodeId, TRx, true, false)
241 }
242
243 func (ctx *Ctx) JobsPart(nodeId *NodeId) chan Job {
244         return ctx.jobsFind(nodeId, TRx, false, true)
245 }