]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-xfer/main.go
Use explicitly larger bufio's buffer
[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-2022 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/v8"
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         log.SetFlags(log.Lshortfile)
61         flag.Usage = usage
62         flag.Parse()
63         if *warranty {
64                 fmt.Println(nncp.Warranty)
65                 return
66         }
67         if *version {
68                 fmt.Println(nncp.VersionGet())
69                 return
70         }
71         if flag.NArg() != 1 {
72                 usage()
73                 os.Exit(1)
74         }
75         nice, err := nncp.NicenessParse(*niceRaw)
76         if err != nil {
77                 log.Fatalln(err)
78         }
79         if *rxOnly && *txOnly {
80                 log.Fatalln("-rx and -tx can not be set simultaneously")
81         }
82
83         ctx, err := nncp.CtxFromCmdline(
84                 *cfgPath,
85                 *spoolPath,
86                 *logPath,
87                 *quiet,
88                 *showPrgrs,
89                 *omitPrgrs,
90                 *debug,
91         )
92         if err != nil {
93                 log.Fatalln("Error during initialization:", err)
94         }
95
96         var nodeOnly *nncp.Node
97         if *nodeRaw != "" {
98                 nodeOnly, err = ctx.FindNode(*nodeRaw)
99                 if err != nil {
100                         log.Fatalln("Invalid -node specified:", err)
101                 }
102         }
103
104         ctx.Umask()
105         selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
106         isBad := false
107         var dir *os.File
108         var fis []os.FileInfo
109         var les nncp.LEs
110         var logMsg func(les nncp.LEs) string
111         if *txOnly {
112                 goto Tx
113         }
114         les = nncp.LEs{
115                 {K: "XX", V: string(nncp.TRx)},
116                 {K: "Dir", V: selfPath},
117         }
118         logMsg = func(les nncp.LEs) string {
119                 return "Packet transfer, received from self"
120         }
121         ctx.LogD("xfer-self", les, logMsg)
122         if _, err = os.Stat(selfPath); err != nil {
123                 if os.IsNotExist(err) {
124                         ctx.LogD("xfer-self-no-dir", les, func(les nncp.LEs) string {
125                                 return logMsg(les) + ": no directory"
126                         })
127                         goto Tx
128                 }
129                 ctx.LogE("xfer-self-stat", les, err, func(les nncp.LEs) string {
130                         return logMsg(les) + ": stating"
131                 })
132                 isBad = true
133                 goto Tx
134         }
135         dir, err = os.Open(selfPath)
136         if err != nil {
137                 ctx.LogE("xfer-self-open", les, err, func(les nncp.LEs) string {
138                         return logMsg(les) + ": opening"
139                 })
140                 isBad = true
141                 goto Tx
142         }
143         fis, err = dir.Readdir(0)
144         dir.Close()
145         if err != nil {
146                 ctx.LogE("xfer-self-read", les, err, func(les nncp.LEs) string {
147                         return logMsg(les) + ": reading"
148                 })
149                 isBad = true
150                 goto Tx
151         }
152         for _, fi := range fis {
153                 if !fi.IsDir() {
154                         continue
155                 }
156                 nodeId, err := nncp.NodeIdFromString(fi.Name())
157                 les := append(les, nncp.LE{K: "Node", V: fi.Name()})
158                 logMsg := func(les nncp.LEs) string {
159                         return "Packet transfer, received from " + ctx.NodeName(nodeId)
160                 }
161                 if err != nil {
162                         ctx.LogD("xfer-rx-not-node", les, func(les nncp.LEs) string {
163                                 return logMsg(les) + ": is not NodeId"
164                         })
165                         continue
166                 }
167                 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
168                         ctx.LogD("xfer-rx-skip", les, func(les nncp.LEs) string {
169                                 return logMsg(les) + ": skipping"
170                         })
171                         continue
172                 }
173                 if _, known := ctx.Neigh[*nodeId]; !known {
174                         ctx.LogD("xfer-rx-unknown", les, func(les nncp.LEs) string {
175                                 return logMsg(les) + ": unknown"
176                         })
177                         continue
178                 }
179                 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
180                 if err != nil {
181                         ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
182                                 return logMsg(les) + ": opening"
183                         })
184                         isBad = true
185                         continue
186                 }
187                 fisInt, err := dir.Readdir(0)
188                 dir.Close()
189                 if err != nil {
190                         ctx.LogE("xfer-rx-read", les, err, func(les nncp.LEs) string {
191                                 return logMsg(les) + ": reading"
192                         })
193                         isBad = true
194                         continue
195                 }
196                 for _, fiInt := range fisInt {
197                         if !fi.IsDir() {
198                                 continue
199                         }
200                         // Check that it is valid Base32 encoding
201                         if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
202                                 continue
203                         }
204                         filename := filepath.Join(dir.Name(), fiInt.Name())
205                         les := append(les, nncp.LE{K: "File", V: filename})
206                         logMsg := func(les nncp.LEs) string {
207                                 return fmt.Sprintf(
208                                         "Packet transfer, received from %s: %s",
209                                         ctx.NodeName(nodeId), filename,
210                                 )
211                         }
212                         if _, err = os.Stat(filepath.Join(
213                                 ctx.Spool,
214                                 nodeId.String(),
215                                 string(nncp.TRx),
216                                 nncp.SeenDir,
217                                 fiInt.Name(),
218                         )); err == nil || !os.IsNotExist(err) {
219                                 ctx.LogI("xfer-rx-seen", les, func(les nncp.LEs) string {
220                                         return logMsg(les) + ": packet already seen"
221                                 })
222                                 if !*keep {
223                                         if err = os.Remove(filename); err != nil {
224                                                 ctx.LogE("xfer-rx-remove", les, err, logMsg)
225                                                 isBad = true
226                                         }
227                                 }
228                                 continue
229                         }
230                         fd, err := os.Open(filename)
231                         if err != nil {
232                                 ctx.LogE("xfer-rx-open", les, err, func(les nncp.LEs) string {
233                                         return logMsg(les) + ": opening"
234                                 })
235                                 isBad = true
236                                 continue
237                         }
238                         pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
239                         if err == nil {
240                                 switch pktEnc.Magic {
241                                 case nncp.MagicNNCPEv1.B:
242                                         err = nncp.MagicNNCPEv1.TooOld()
243                                 case nncp.MagicNNCPEv2.B:
244                                         err = nncp.MagicNNCPEv2.TooOld()
245                                 case nncp.MagicNNCPEv3.B:
246                                         err = nncp.MagicNNCPEv3.TooOld()
247                                 case nncp.MagicNNCPEv4.B:
248                                         err = nncp.MagicNNCPEv4.TooOld()
249                                 case nncp.MagicNNCPEv5.B:
250                                         err = nncp.MagicNNCPEv5.TooOld()
251                                 case nncp.MagicNNCPEv6.B:
252                                 default:
253                                         err = errors.New("is not an encrypted packet")
254                                 }
255                         }
256                         if err != nil {
257                                 ctx.LogD(
258                                         "xfer-rx-not-packet",
259                                         append(les, nncp.LE{K: "Err", V: err}),
260                                         func(les nncp.LEs) string {
261                                                 return logMsg(les) + ": not valid packet: " + err.Error()
262                                         },
263                                 )
264                                 fd.Close()
265                                 continue
266                         }
267                         if pktEnc.Nice > nice {
268                                 ctx.LogD("xfer-rx-too-nice", les, func(les nncp.LEs) string {
269                                         return logMsg(les) + ": too nice"
270                                 })
271                                 fd.Close()
272                                 continue
273                         }
274                         les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
275                         logMsg = func(les nncp.LEs) string {
276                                 return fmt.Sprintf(
277                                         "Packet transfer, received from %s: %s (%s)",
278                                         ctx.NodeName(nodeId), filename,
279                                         humanize.IBytes(uint64(fiInt.Size())),
280                                 )
281                         }
282                         if !ctx.IsEnoughSpace(fiInt.Size()) {
283                                 ctx.LogE("xfer-rx", les, errors.New("is not enough space"), logMsg)
284                                 fd.Close()
285                                 continue
286                         }
287                         if _, err = fd.Seek(0, io.SeekStart); err != nil {
288                                 log.Fatalln(err)
289                         }
290                         tmp, err := ctx.NewTmpFileWHash()
291                         if err != nil {
292                                 log.Fatalln(err)
293                         }
294                         r, w := io.Pipe()
295                         go func() {
296                                 _, err := io.CopyN(
297                                         w, bufio.NewReaderSize(fd, nncp.MTHBlockSize), fiInt.Size(),
298                                 )
299                                 if err == nil {
300                                         err = w.Close()
301                                 }
302                                 if err != nil {
303                                         ctx.LogE("xfer-rx", les, err, logMsg)
304                                         w.CloseWithError(err)
305                                 }
306                         }()
307                         _, err = nncp.CopyProgressed(
308                                 tmp.W, r, "Rx",
309                                 append(
310                                         les,
311                                         nncp.LE{K: "Pkt", V: filename},
312                                         nncp.LE{K: "FullSize", V: fiInt.Size()},
313                                 ),
314                                 ctx.ShowPrgrs,
315                         )
316                         fd.Close()
317                         if err != nil {
318                                 ctx.LogE("xfer-rx", les, err, logMsg)
319                                 tmp.Cancel()
320                                 isBad = true
321                                 continue
322                         }
323                         if err = tmp.W.Flush(); err != nil {
324                                 ctx.LogE("xfer-rx", les, err, logMsg)
325                                 tmp.Cancel()
326                                 isBad = true
327                                 continue
328                         }
329                         if tmp.Checksum() != fiInt.Name() {
330                                 ctx.LogE("xfer-rx", les, errors.New("checksum mismatch"), logMsg)
331                                 tmp.Cancel()
332                                 isBad = true
333                                 continue
334                         }
335                         if err = tmp.Commit(filepath.Join(
336                                 ctx.Spool,
337                                 nodeId.String(),
338                                 string(nncp.TRx),
339                         )); err != nil {
340                                 log.Fatalln(err)
341                         }
342                         ctx.LogI("xfer-rx", les, logMsg)
343                         if !*keep {
344                                 if err = os.Remove(filename); err != nil {
345                                         ctx.LogE("xfer-rx-remove", les, err, logMsg)
346                                         isBad = true
347                                 }
348                         }
349                         if ctx.HdrUsage {
350                                 ctx.HdrWrite(pktEncRaw, filepath.Join(
351                                         ctx.Spool,
352                                         nodeId.String(),
353                                         string(nncp.TRx),
354                                         tmp.Checksum(),
355                                 ))
356                         }
357                 }
358         }
359
360 Tx:
361         if *rxOnly {
362                 if isBad {
363                         os.Exit(1)
364                 }
365                 return
366         }
367         for nodeId := range ctx.Neigh {
368                 les := nncp.LEs{{K: "XX", V: string(nncp.TTx)}, {K: "Node", V: nodeId}}
369                 logMsg := func(les nncp.LEs) string {
370                         return "Packet transfer, sent to " + ctx.NodeName(&nodeId)
371                 }
372                 if nodeOnly != nil && nodeId != *nodeOnly.Id {
373                         ctx.LogD("xfer-tx-skip", les, func(les nncp.LEs) string {
374                                 return logMsg(les) + ": skipping"
375                         })
376                         continue
377                 }
378                 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
379                 if err != nil {
380                         continue
381                 }
382                 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
383                 les = append(les, nncp.LE{K: "Dir", V: nodePath})
384                 logMsg = func(les nncp.LEs) string {
385                         return fmt.Sprintf(
386                                 "Packet transfer, sent to %s: directory %s",
387                                 ctx.NodeName(&nodeId), nodePath,
388                         )
389                 }
390                 _, err = os.Stat(nodePath)
391                 if err != nil {
392                         if os.IsNotExist(err) {
393                                 ctx.LogD("xfer-tx-not-exist", les, func(les nncp.LEs) string {
394                                         return logMsg(les) + ": does not exist"
395                                 })
396                                 if !*mkdir {
397                                         ctx.UnlockDir(dirLock)
398                                         continue
399                                 }
400                                 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
401                                         ctx.UnlockDir(dirLock)
402                                         ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
403                                         isBad = true
404                                         continue
405                                 }
406                         } else {
407                                 ctx.UnlockDir(dirLock)
408                                 ctx.LogE("xfer-tx", les, err, logMsg)
409                                 isBad = true
410                                 continue
411                         }
412                 }
413                 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
414                 les[len(les)-1].V = dstPath
415                 logMsg = func(les nncp.LEs) string {
416                         return fmt.Sprintf(
417                                 "Packet transfer, sent to %s: directory %s",
418                                 ctx.NodeName(&nodeId), dstPath,
419                         )
420                 }
421                 _, err = os.Stat(dstPath)
422                 if err != nil {
423                         if os.IsNotExist(err) {
424                                 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
425                                         ctx.UnlockDir(dirLock)
426                                         ctx.LogE("xfer-tx-mkdir", les, err, logMsg)
427                                         isBad = true
428                                         continue
429                                 }
430                         } else {
431                                 ctx.UnlockDir(dirLock)
432                                 ctx.LogE("xfer-tx", les, err, logMsg)
433                                 isBad = true
434                                 continue
435                         }
436                 }
437                 les = les[:len(les)-1]
438                 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
439                         pktName := filepath.Base(job.Path)
440                         les := append(les, nncp.LE{K: "Pkt", V: pktName})
441                         logMsg = func(les nncp.LEs) string {
442                                 return fmt.Sprintf(
443                                         "Packet transfer, sent to %s: %s",
444                                         ctx.NodeName(&nodeId), pktName,
445                                 )
446                         }
447                         if job.PktEnc.Nice > nice {
448                                 ctx.LogD("xfer-tx-too-nice", les, func(les nncp.LEs) string {
449                                         return logMsg(les) + ": too nice"
450                                 })
451                                 continue
452                         }
453                         if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
454                                 ctx.LogD("xfer-tx-exists", les, func(les nncp.LEs) string {
455                                         return logMsg(les) + ": already exists"
456                                 })
457                                 continue
458                         }
459                         tmp, err := nncp.TempFile(dstPath, "xfer")
460                         if err != nil {
461                                 ctx.LogE("xfer-tx-mktemp", les, err, func(les nncp.LEs) string {
462                                         return logMsg(les) + ": mktemp"
463                                 })
464                                 isBad = true
465                                 break
466                         }
467                         les = append(les, nncp.LE{K: "Tmp", V: tmp.Name()})
468                         ctx.LogD("xfer-tx-tmp-create", les, func(les nncp.LEs) string {
469                                 return fmt.Sprintf("%s: temporary %s created", logMsg(les), tmp.Name())
470                         })
471                         fd, err := os.Open(job.Path)
472                         if err != nil {
473                                 ctx.LogE("xfer-tx-open", les, err, func(les nncp.LEs) string {
474                                         return logMsg(les) + ": opening"
475                                 })
476                                 tmp.Close()
477                                 isBad = true
478                                 continue
479                         }
480                         bufW := bufio.NewWriter(tmp)
481                         copied, err := nncp.CopyProgressed(
482                                 bufW, bufio.NewReaderSize(fd, nncp.MTHBlockSize), "Tx",
483                                 append(les, nncp.LE{K: "FullSize", V: job.Size}),
484                                 ctx.ShowPrgrs,
485                         )
486                         fd.Close()
487                         if err != nil {
488                                 ctx.LogE("xfer-tx-copy", les, err, func(les nncp.LEs) string {
489                                         return logMsg(les) + ": copying"
490                                 })
491                                 tmp.Close()
492                                 isBad = true
493                                 continue
494                         }
495                         if err = bufW.Flush(); err != nil {
496                                 tmp.Close()
497                                 ctx.LogE("xfer-tx-flush", les, err, func(les nncp.LEs) string {
498                                         return logMsg(les) + ": flushing"
499                                 })
500                                 isBad = true
501                                 continue
502                         }
503                         if !nncp.NoSync {
504                                 if err = tmp.Sync(); err != nil {
505                                         tmp.Close()
506                                         ctx.LogE("xfer-tx-sync", les, err, func(les nncp.LEs) string {
507                                                 return logMsg(les) + ": syncing"
508                                         })
509                                         isBad = true
510                                         continue
511                                 }
512                         }
513                         if err = tmp.Close(); err != nil {
514                                 ctx.LogE("xfer-tx-close", les, err, func(les nncp.LEs) string {
515                                         return logMsg(les) + ": closing"
516                                 })
517                         }
518                         if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
519                                 ctx.LogE("xfer-tx-rename", les, err, func(les nncp.LEs) string {
520                                         return logMsg(les) + ": renaming"
521                                 })
522                                 isBad = true
523                                 continue
524                         }
525                         if err = nncp.DirSync(dstPath); err != nil {
526                                 ctx.LogE("xfer-tx-dirsync", les, err, func(les nncp.LEs) string {
527                                         return logMsg(les) + ": dirsyncing"
528                                 })
529                                 isBad = true
530                                 continue
531                         }
532                         os.Remove(filepath.Join(dstPath, pktName+".part"))
533                         les = les[:len(les)-1]
534                         ctx.LogI(
535                                 "xfer-tx",
536                                 append(les, nncp.LE{K: "Size", V: copied}),
537                                 func(les nncp.LEs) string {
538                                         return fmt.Sprintf(
539                                                 "%s (%s)", logMsg(les), humanize.IBytes(uint64(copied)),
540                                         )
541                                 },
542                         )
543                         if !*keep {
544                                 if err = os.Remove(job.Path); err != nil {
545                                         ctx.LogE("xfer-tx-remove", les, err, func(les nncp.LEs) string {
546                                                 return logMsg(les) + ": removing"
547                                         })
548                                         isBad = true
549                                 } else if ctx.HdrUsage {
550                                         os.Remove(nncp.JobPath2Hdr(job.Path))
551                                 }
552                         }
553                 }
554                 ctx.UnlockDir(dirLock)
555         }
556         if isBad {
557                 os.Exit(1)
558         }
559 }