]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-xfer/main.go
Logging refactoring, no centralized humanizer
[nncp.git] / src / cmd / nncp-xfer / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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 // Exchange NNCP inbound and outbounds packets with external directory.
19 package main
20
21 import (
22         "bufio"
23         "errors"
24         "flag"
25         "fmt"
26         "io"
27         "log"
28         "os"
29         "path/filepath"
30
31         "github.com/dustin/go-humanize"
32         "go.cypherpunks.ru/nncp/v6"
33 )
34
35 func usage() {
36         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
37         fmt.Fprintf(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
38         fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
39         flag.PrintDefaults()
40 }
41
42 func main() {
43         var (
44                 cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45                 nodeRaw   = flag.String("node", "", "Process only that node")
46                 niceRaw   = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
47                 rxOnly    = flag.Bool("rx", false, "Only receive packets")
48                 txOnly    = flag.Bool("tx", false, "Only transfer packets")
49                 mkdir     = flag.Bool("mkdir", false, "Create necessary outbound directories")
50                 keep      = flag.Bool("keep", false, "Do not delete transferred packets")
51                 spoolPath = flag.String("spool", "", "Override path to spool")
52                 logPath   = flag.String("log", "", "Override path to logfile")
53                 quiet     = flag.Bool("quiet", false, "Print only errors")
54                 showPrgrs = flag.Bool("progress", false, "Force progress showing")
55                 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
56                 debug     = flag.Bool("debug", false, "Print debug messages")
57                 version   = flag.Bool("version", false, "Print version information")
58                 warranty  = flag.Bool("warranty", false, "Print warranty information")
59         )
60         flag.Usage = usage
61         flag.Parse()
62         if *warranty {
63                 fmt.Println(nncp.Warranty)
64                 return
65         }
66         if *version {
67                 fmt.Println(nncp.VersionGet())
68                 return
69         }
70         if flag.NArg() != 1 {
71                 usage()
72                 os.Exit(1)
73         }
74         nice, err := nncp.NicenessParse(*niceRaw)
75         if err != nil {
76                 log.Fatalln(err)
77         }
78         if *rxOnly && *txOnly {
79                 log.Fatalln("-rx and -tx can not be set simultaneously")
80         }
81
82         ctx, err := nncp.CtxFromCmdline(
83                 *cfgPath,
84                 *spoolPath,
85                 *logPath,
86                 *quiet,
87                 *showPrgrs,
88                 *omitPrgrs,
89                 *debug,
90         )
91         if err != nil {
92                 log.Fatalln("Error during initialization:", err)
93         }
94
95         var nodeOnly *nncp.Node
96         if *nodeRaw != "" {
97                 nodeOnly, err = ctx.FindNode(*nodeRaw)
98                 if err != nil {
99                         log.Fatalln("Invalid -node specified:", err)
100                 }
101         }
102
103         ctx.Umask()
104         selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
105         isBad := false
106         var dir *os.File
107         var fis []os.FileInfo
108         var les nncp.LEs
109         var logMsg func(les nncp.LEs) string
110         if *txOnly {
111                 goto Tx
112         }
113         les = nncp.LEs{
114                 {K: "XX", V: string(nncp.TRx)},
115                 {K: "Dir", V: selfPath},
116         }
117         logMsg = func(les nncp.LEs) string {
118                 return "Packet transfer, received from self"
119         }
120         ctx.LogD("xfer-self", les, logMsg)
121         if _, err = os.Stat(selfPath); err != nil {
122                 if os.IsNotExist(err) {
123                         ctx.LogD("xfer-self-no-dir", les, func(les nncp.LEs) string {
124                                 return logMsg(les) + ": no directory"
125                         })
126                         goto Tx
127                 }
128                 ctx.LogE("xfer-self-stat", les, err, func(les nncp.LEs) string {
129                         return logMsg(les) + ": stating"
130                 })
131                 isBad = true
132                 goto Tx
133         }
134         dir, err = os.Open(selfPath)
135         if err != nil {
136                 ctx.LogE("xfer-self-open", les, err, func(les nncp.LEs) string {
137                         return logMsg(les) + ": opening"
138                 })
139                 isBad = true
140                 goto Tx
141         }
142         fis, err = dir.Readdir(0)
143         dir.Close() // #nosec G104
144         if err != nil {
145                 ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
146                         return logMsg(les) + ": reading"
147                 })
148                 isBad = true
149                 goto Tx
150         }
151         for _, fi := range fis {
152                 if !fi.IsDir() {
153                         continue
154                 }
155                 nodeId, err := nncp.NodeIdFromString(fi.Name())
156                 les := append(les, nncp.LE{K: "Node", V: fi.Name()})
157                 logMsg := func(les nncp.LEs) string {
158                         return "Packet transfer, received from " + ctx.NodeName(nodeId)
159                 }
160                 if err != nil {
161                         ctx.LogD("xfer-rx-not-node", les, func(les nncp.LEs) string {
162                                 return logMsg(les) + ": is not NodeId"
163                         })
164                         continue
165                 }
166                 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
167                         ctx.LogD("xfer-rx-skip", les, func(les nncp.LEs) string {
168                                 return logMsg(les) + ": skipping"
169                         })
170                         continue
171                 }
172                 if _, known := ctx.Neigh[*nodeId]; !known {
173                         ctx.LogD("xfer-rx-unknown", les, func(les nncp.LEs) string {
174                                 return logMsg(les) + ": unknown"
175                         })
176                         continue
177                 }
178                 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
179                 if err != nil {
180                         ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
181                                 return logMsg(les) + ": opening"
182                         })
183                         isBad = true
184                         continue
185                 }
186                 fisInt, err := dir.Readdir(0)
187                 dir.Close() // #nosec G104
188                 if err != nil {
189                         ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
190                                 return logMsg(les) + ": reading"
191                         })
192                         isBad = true
193                         continue
194                 }
195                 for _, fiInt := range fisInt {
196                         if !fi.IsDir() {
197                                 continue
198                         }
199                         // Check that it is valid Base32 encoding
200                         if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
201                                 continue
202                         }
203                         filename := filepath.Join(dir.Name(), fiInt.Name())
204                         les := append(les, nncp.LE{K: "File", V: filename})
205                         logMsg := func(les nncp.LEs) string {
206                                 return fmt.Sprintf(
207                                         "Packet transfer, received from %s: %s",
208                                         ctx.NodeName(nodeId), filename,
209                                 )
210                         }
211                         fd, err := os.Open(filename)
212                         if err != nil {
213                                 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
214                                         return logMsg(les) + ": opening"
215                                 })
216                                 isBad = true
217                                 continue
218                         }
219                         pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
220                         if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
221                                 ctx.LogD("xfer-rx-not-packet", les, func(les nncp.LEs) string {
222                                         return logMsg(les) + ": is not a packet"
223                                 })
224                                 fd.Close() // #nosec G104
225                                 continue
226                         }
227                         if pktEnc.Nice > nice {
228                                 ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
229                                         return logMsg(les) + ": too nice"
230                                 })
231                                 fd.Close() // #nosec G104
232                                 continue
233                         }
234                         les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
235                         logMsg = func(les nncp.LEs) string {
236                                 return fmt.Sprintf(
237                                         "Packet transfer, received from %s: %s (%s)",
238                                         ctx.NodeName(nodeId), filename,
239                                         humanize.IBytes(uint64(fiInt.Size())),
240                                 )
241                         }
242                         if !ctx.IsEnoughSpace(fiInt.Size()) {
243                                 ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
244                                 fd.Close() // #nosec G104
245                                 continue
246                         }
247                         if _, err = fd.Seek(0, 0); err != nil {
248                                 log.Fatalln(err)
249                         }
250                         tmp, err := ctx.NewTmpFileWHash()
251                         if err != nil {
252                                 log.Fatalln(err)
253                         }
254                         r, w := io.Pipe()
255                         go func() {
256                                 _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
257                                 if err == nil {
258                                         err = w.Close()
259                                 }
260                                 if err != nil {
261                                         ctx.LogE("xfer-rx", les, err, logMsg)
262                                         w.CloseWithError(err) // #nosec G104
263                                 }
264                         }()
265                         if _, err = nncp.CopyProgressed(
266                                 tmp.W, r, "Rx",
267                                 append(
268                                         les,
269                                         nncp.LE{K: "Pkt", V: filename},
270                                         nncp.LE{K: "FullSize", V: fiInt.Size()},
271                                 ),
272                                 ctx.ShowPrgrs,
273                         ); err != nil {
274                                 ctx.LogE("xfer-rx", les, err, logMsg)
275                                 isBad = true
276                         }
277                         fd.Close() // #nosec G104
278                         if isBad {
279                                 tmp.Cancel()
280                                 continue
281                         }
282                         if err = tmp.Commit(filepath.Join(
283                                 ctx.Spool,
284                                 nodeId.String(),
285                                 string(nncp.TRx),
286                         )); err != nil {
287                                 log.Fatalln(err)
288                         }
289                         ctx.LogI("xfer-rx", les, logMsg)
290                         if !*keep {
291                                 if err = os.Remove(filename); err != nil {
292                                         ctx.LogE("xfer-rx-remove", les, err, logMsg)
293                                         isBad = true
294                                 }
295                         }
296                         if ctx.HdrUsage {
297                                 ctx.HdrWrite(pktEncRaw, filepath.Join(
298                                         ctx.Spool,
299                                         nodeId.String(),
300                                         string(nncp.TRx),
301                                         tmp.Checksum(),
302                                 ))
303                         }
304                 }
305         }
306
307 Tx:
308         if *rxOnly {
309                 if isBad {
310                         os.Exit(1)
311                 }
312                 return
313         }
314         for nodeId := range ctx.Neigh {
315                 les := nncp.LEs{{K: "XX", V: string(nncp.TTx)}, {K: "Node", V: nodeId}}
316                 logMsg := func(les nncp.LEs) string {
317                         return "Packet transfer, sent to " + ctx.NodeName(&nodeId)
318                 }
319                 if nodeOnly != nil && nodeId != *nodeOnly.Id {
320                         ctx.LogD("xfer-tx-skip", les, func(les nncp.LEs) string {
321                                 return logMsg(les) + ": skipping"
322                         })
323                         continue
324                 }
325                 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
326                 if err != nil {
327                         continue
328                 }
329                 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
330                 les = append(les, nncp.LE{K: "Dir", V: nodePath})
331                 logMsg = func(les nncp.LEs) string {
332                         return fmt.Sprintf(
333                                 "Packet transfer, sent to %s: directory %s",
334                                 ctx.NodeName(&nodeId), nodePath,
335                         )
336                 }
337                 _, err = os.Stat(nodePath)
338                 if err != nil {
339                         if os.IsNotExist(err) {
340                                 ctx.LogD("xfer-tx-not-exist", les, func(les nncp.LEs) string {
341                                         return logMsg(les) + ": does not exist"
342                                 })
343                                 if !*mkdir {
344                                         ctx.UnlockDir(dirLock)
345                                         continue
346                                 }
347                                 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
348                                         ctx.UnlockDir(dirLock)
349                                         ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
350                                         isBad = true
351                                         continue
352                                 }
353                         } else {
354                                 ctx.UnlockDir(dirLock)
355                                 ctx.LogE("xfer-tx", les, err, logMsg)
356                                 isBad = true
357                                 continue
358                         }
359                 }
360                 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
361                 les[len(les)-1].V = dstPath
362                 logMsg = func(les nncp.LEs) string {
363                         return fmt.Sprintf(
364                                 "Packet transfer, sent to %s: directory %s",
365                                 ctx.NodeName(&nodeId), dstPath,
366                         )
367                 }
368                 _, err = os.Stat(dstPath)
369                 if err != nil {
370                         if os.IsNotExist(err) {
371                                 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
372                                         ctx.UnlockDir(dirLock)
373                                         ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
374                                         isBad = true
375                                         continue
376                                 }
377                         } else {
378                                 ctx.UnlockDir(dirLock)
379                                 ctx.LogE("xfer-tx", les, err, logMsg)
380                                 isBad = true
381                                 continue
382                         }
383                 }
384                 les = les[:len(les)-1]
385                 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
386                         pktName := filepath.Base(job.Path)
387                         les := append(les, nncp.LE{K: "Pkt", V: pktName})
388                         logMsg = func(les nncp.LEs) string {
389                                 return fmt.Sprintf(
390                                         "Packet transfer, sent to %s: %s",
391                                         ctx.NodeName(&nodeId), pktName,
392                                 )
393                         }
394                         if job.PktEnc.Nice > nice {
395                                 ctx.LogD("xfer-tx-too-nice", les, func(les nncp.LEs) string {
396                                         return logMsg(les) + ": too nice"
397                                 })
398                                 continue
399                         }
400                         if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
401                                 ctx.LogD("xfer-tx-exists", les, func(les nncp.LEs) string {
402                                         return logMsg(les) + ": already exists"
403                                 })
404                                 continue
405                         }
406                         if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
407                                 ctx.LogD("xfer-tx-seen", les, func(les nncp.LEs) string {
408                                         return logMsg(les) + ": already seen"
409                                 })
410                                 continue
411                         }
412                         tmp, err := nncp.TempFile(dstPath, "xfer")
413                         if err != nil {
414                                 ctx.LogE("xfer-tx-mktemp", les, err, func(les nncp.LEs) string {
415                                         return logMsg(les) + ": mktemp"
416                                 })
417                                 isBad = true
418                                 break
419                         }
420                         les = append(les, nncp.LE{K: "Tmp", V: tmp.Name()})
421                         ctx.LogD("xfer-tx-tmp-create", les, func(les nncp.LEs) string {
422                                 return fmt.Sprintf("%s: temporary %s created", logMsg(les), tmp.Name())
423                         })
424                         fd, err := os.Open(job.Path)
425                         if err != nil {
426                                 ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
427                                         return logMsg(les) + ": opening"
428                                 })
429                                 tmp.Close() // #nosec G104
430                                 isBad = true
431                                 continue
432                         }
433                         bufW := bufio.NewWriter(tmp)
434                         copied, err := nncp.CopyProgressed(
435                                 bufW, bufio.NewReader(fd), "Tx",
436                                 append(les, nncp.LE{K: "FullSize", V: job.Size}),
437                                 ctx.ShowPrgrs,
438                         )
439                         fd.Close() // #nosec G104
440                         if err != nil {
441                                 ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
442                                         return logMsg(les) + ": copying"
443                                 })
444                                 tmp.Close() // #nosec G104
445                                 isBad = true
446                                 continue
447                         }
448                         if err = bufW.Flush(); err != nil {
449                                 tmp.Close() // #nosec G104
450                                 ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
451                                         return logMsg(les) + ": flushing"
452                                 })
453                                 isBad = true
454                                 continue
455                         }
456                         if err = tmp.Sync(); err != nil {
457                                 tmp.Close() // #nosec G104
458                                 ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
459                                         return logMsg(les) + ": syncing"
460                                 })
461                                 isBad = true
462                                 continue
463                         }
464                         if err = tmp.Close(); err != nil {
465                                 ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string {
466                                         return logMsg(les) + ": closing"
467                                 })
468                         }
469                         if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
470                                 ctx.LogE("xfer-tx-rename", les, err, func(les nncp.LEs) string {
471                                         return logMsg(les) + ": renaming"
472                                 })
473                                 isBad = true
474                                 continue
475                         }
476                         if err = nncp.DirSync(dstPath); err != nil {
477                                 ctx.LogE("xfer-tx-dirsync", les, err, func(les nncp.LEs) string {
478                                         return logMsg(les) + ": dirsyncing"
479                                 })
480                                 isBad = true
481                                 continue
482                         }
483                         os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
484                         les = les[:len(les)-1]
485                         ctx.LogI(
486                                 "xfer-tx",
487                                 append(les, nncp.LE{K: "Size", V: copied}),
488                                 func(les nncp.LEs) string {
489                                         return fmt.Sprintf(
490                                                 "%s (%s)", logMsg(les), humanize.IBytes(uint64(copied)),
491                                         )
492                                 },
493                         )
494                         if !*keep {
495                                 if err = os.Remove(job.Path); err != nil {
496                                         ctx.LogE("xfer-tx-remove", les, err, func(les nncp.LEs) string {
497                                                 return logMsg(les) + ": removing"
498                                         })
499                                         isBad = true
500                                 } else if ctx.HdrUsage {
501                                         os.Remove(job.Path + nncp.HdrSuffix)
502                                 }
503                         }
504                 }
505                 ctx.UnlockDir(dirLock)
506         }
507         if isBad {
508                 os.Exit(1)
509         }
510 }