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/>.
18 // Exchange NNCP inbound and outbounds packets with external directory.
31 "go.cypherpunks.ru/nncp/v5"
35 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
36 fmt.Fprintf(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
37 fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
43 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
44 nodeRaw = flag.String("node", "", "Process only that node")
45 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
46 rxOnly = flag.Bool("rx", false, "Only receive packets")
47 txOnly = flag.Bool("tx", false, "Only transfer packets")
48 mkdir = flag.Bool("mkdir", false, "Create necessary outbound directories")
49 keep = flag.Bool("keep", false, "Do not delete transferred packets")
50 spoolPath = flag.String("spool", "", "Override path to spool")
51 logPath = flag.String("log", "", "Override path to logfile")
52 quiet = flag.Bool("quiet", false, "Print only errors")
53 showPrgrs = flag.Bool("progress", false, "Force progress showing")
54 omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
55 debug = flag.Bool("debug", false, "Print debug messages")
56 version = flag.Bool("version", false, "Print version information")
57 warranty = flag.Bool("warranty", false, "Print warranty information")
62 fmt.Println(nncp.Warranty)
66 fmt.Println(nncp.VersionGet())
73 nice, err := nncp.NicenessParse(*niceRaw)
77 if *rxOnly && *txOnly {
78 log.Fatalln("-rx and -tx can not be set simultaneously")
81 ctx, err := nncp.CtxFromCmdline(
91 log.Fatalln("Error during initialization:", err)
94 var nodeOnly *nncp.Node
96 nodeOnly, err = ctx.FindNode(*nodeRaw)
98 log.Fatalln("Invalid -node specified:", err)
103 selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
106 var fis []os.FileInfo
112 {K: "XX", V: string(nncp.TRx)},
113 {K: "Dir", V: selfPath},
115 ctx.LogD("nncp-xfer", les, "self")
116 if _, err = os.Stat(selfPath); err != nil {
117 if os.IsNotExist(err) {
118 ctx.LogD("nncp-xfer", les, "no dir")
121 ctx.LogE("nncp-xfer", les, err, "stat")
125 dir, err = os.Open(selfPath)
127 ctx.LogE("nncp-xfer", les, err, "open")
131 fis, err = dir.Readdir(0)
132 dir.Close() // #nosec G104
134 ctx.LogE("nncp-xfer", les, err, "read")
138 for _, fi := range fis {
142 nodeId, err := nncp.NodeIdFromString(fi.Name())
143 les := append(les, nncp.LE{K: "Node", V: fi.Name()})
145 ctx.LogD("nncp-xfer", les, "is not NodeId")
148 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
149 ctx.LogD("nncp-xfer", les, "skip")
152 if _, known := ctx.Neigh[*nodeId]; !known {
153 ctx.LogD("nncp-xfer", les, "unknown")
156 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
158 ctx.LogE("nncp-xfer", les, err, "open")
162 fisInt, err := dir.Readdir(0)
163 dir.Close() // #nosec G104
165 ctx.LogE("nncp-xfer", les, err, "read")
169 for _, fiInt := range fisInt {
173 // Check that it is valid Base32 encoding
174 if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
177 filename := filepath.Join(dir.Name(), fiInt.Name())
178 les := append(les, nncp.LE{K: "File", V: filename})
179 fd, err := os.Open(filename)
181 ctx.LogE("nncp-xfer", les, err, "open")
185 pktEnc, pktEncRaw, err := ctx.HdrRead(fd)
186 if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
187 ctx.LogD("nncp-xfer", les, "is not a packet")
188 fd.Close() // #nosec G104
191 if pktEnc.Nice > nice {
192 ctx.LogD("nncp-xfer", les, "too nice")
193 fd.Close() // #nosec G104
196 les = append(les, nncp.LE{K: "Size", V: fiInt.Size()})
197 if !ctx.IsEnoughSpace(fiInt.Size()) {
198 ctx.LogE("nncp-xfer", les, errors.New("is not enough space"), "")
199 fd.Close() // #nosec G104
202 if _, err = fd.Seek(0, 0); err != nil {
205 tmp, err := ctx.NewTmpFileWHash()
211 _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
216 ctx.LogE("nncp-xfer", les, err, "copy")
217 w.CloseWithError(err) // #nosec G104
220 if _, err = nncp.CopyProgressed(
222 append(les, nncp.LEs{
223 {K: "Pkt", V: filename},
224 {K: "FullSize", V: fiInt.Size()},
228 ctx.LogE("nncp-xfer", les, err, "copy")
231 fd.Close() // #nosec G104
236 if err = tmp.Commit(filepath.Join(
243 ctx.LogI("nncp-xfer", les, "")
245 if err = os.Remove(filename); err != nil {
246 ctx.LogE("nncp-xfer", les, err, "remove")
251 ctx.HdrWrite(pktEncRaw, filepath.Join(
268 for nodeId := range ctx.Neigh {
270 {K: "XX", V: string(nncp.TTx)},
271 {K: "Node", V: nodeId},
273 if nodeOnly != nil && nodeId != *nodeOnly.Id {
274 ctx.LogD("nncp-xfer", les, "skip")
277 dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
281 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
282 les = append(les, nncp.LE{K: "Dir", V: nodePath})
283 _, err = os.Stat(nodePath)
285 if os.IsNotExist(err) {
286 ctx.LogD("nncp-xfer", les, "does not exist")
288 ctx.UnlockDir(dirLock)
291 if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
292 ctx.UnlockDir(dirLock)
293 ctx.LogE("nncp-xfer", les, err, "mkdir")
298 ctx.UnlockDir(dirLock)
299 ctx.LogE("nncp-xfer", les, err, "stat")
304 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
305 les[len(les)-1].V = dstPath
306 _, err = os.Stat(dstPath)
308 if os.IsNotExist(err) {
309 if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
310 ctx.UnlockDir(dirLock)
311 ctx.LogE("nncp-xfer", les, err, "mkdir")
316 ctx.UnlockDir(dirLock)
317 ctx.LogE("nncp-xfer", les, err, "stat")
322 les = les[:len(les)-1]
323 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
324 pktName := filepath.Base(job.Path)
325 les := append(les, nncp.LE{K: "Pkt", V: pktName})
326 if job.PktEnc.Nice > nice {
327 ctx.LogD("nncp-xfer", les, "too nice")
330 if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
331 ctx.LogD("nncp-xfer", les, "already exists")
334 if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
335 ctx.LogD("nncp-xfer", les, "already exists")
338 tmp, err := nncp.TempFile(dstPath, "xfer")
340 ctx.LogE("nncp-xfer", les, err, "mktemp")
344 les = append(les, nncp.LE{K: "Tmp", V: tmp.Name()})
345 ctx.LogD("nncp-xfer", les, "created")
346 fd, err := os.Open(job.Path)
348 ctx.LogE("nncp-xfer", les, err, "open")
349 tmp.Close() // #nosec G104
353 bufW := bufio.NewWriter(tmp)
354 copied, err := nncp.CopyProgressed(
355 bufW, bufio.NewReader(fd), "Tx",
356 append(les, nncp.LE{K: "FullSize", V: job.Size}),
359 fd.Close() // #nosec G104
361 ctx.LogE("nncp-xfer", les, err, "copy")
362 tmp.Close() // #nosec G104
366 if err = bufW.Flush(); err != nil {
367 tmp.Close() // #nosec G104
368 ctx.LogE("nncp-xfer", les, err, "flush")
372 if err = tmp.Sync(); err != nil {
373 tmp.Close() // #nosec G104
374 ctx.LogE("nncp-xfer", les, err, "sync")
378 if err = tmp.Close(); err != nil {
379 ctx.LogE("nncp-xfer", les, err, "sync")
381 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
382 ctx.LogE("nncp-xfer", les, err, "rename")
386 if err = nncp.DirSync(dstPath); err != nil {
387 ctx.LogE("nncp-xfer", les, err, "sync")
391 os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
392 les = les[:len(les)-1]
393 ctx.LogI("nncp-xfer", append(les, nncp.LE{K: "Size", V: copied}), "")
395 if err = os.Remove(job.Path); err != nil {
396 ctx.LogE("nncp-xfer", les, err, "remove")
398 } else if ctx.HdrUsage {
399 os.Remove(job.Path + nncp.HdrSuffix)
403 ctx.UnlockDir(dirLock)