2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // Exchange NNCP inbound and outbounds packets with external directory.
34 "github.com/davecgh/go-xdr/xdr2"
38 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
39 fmt.Fprintf(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n\n")
40 fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
46 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
47 nodeRaw = flag.String("node", "", "Process only that node")
48 niceRaw = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
49 rxOnly = flag.Bool("rx", false, "Only receive packets")
50 txOnly = flag.Bool("tx", false, "Only transfer packets")
51 mkdir = flag.Bool("mkdir", false, "Create necessary outbound directories")
52 keep = flag.Bool("keep", false, "Do not delete transferred packets")
53 spoolPath = flag.String("spool", "", "Override path to spool")
54 logPath = flag.String("log", "", "Override path to logfile")
55 quiet = flag.Bool("quiet", false, "Print only errors")
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")
63 fmt.Println(nncp.Warranty)
67 fmt.Println(nncp.VersionGet())
74 nice, err := nncp.NicenessParse(*niceRaw)
78 if *rxOnly && *txOnly {
79 log.Fatalln("-rx and -tx can not be set simultaneously")
82 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
84 log.Fatalln("Error during initialization:", err)
87 var nodeOnly *nncp.Node
89 nodeOnly, err = ctx.FindNode(*nodeRaw)
91 log.Fatalln("Invalid -node specified:", err)
95 selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
103 sds["xx"] = string(nncp.TRx)
104 sds["dir"] = selfPath
105 ctx.LogD("nncp-xfer", sds, "self")
106 if _, err = os.Stat(selfPath); err != nil {
107 if os.IsNotExist(err) {
108 ctx.LogD("nncp-xfer", sds, "no dir")
111 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
115 dir, err = os.Open(selfPath)
117 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
121 fis, err = dir.Readdir(0)
124 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
128 for _, fi := range fis {
132 nodeId, err := nncp.NodeIdFromString(fi.Name())
133 sds["node"] = fi.Name()
135 ctx.LogD("nncp-xfer", sds, "is not NodeId")
138 if nodeOnly != nil && *nodeId != *nodeOnly.Id {
139 ctx.LogD("nncp-xfer", sds, "skip")
142 if _, known := ctx.Neigh[*nodeId]; !known {
143 ctx.LogD("nncp-xfer", sds, "unknown")
146 dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
148 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
152 fisInt, err := dir.Readdir(0)
155 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
159 for _, fiInt := range fisInt {
163 filename := filepath.Join(dir.Name(), fiInt.Name())
164 sds["file"] = filename
166 fd, err := os.Open(filename)
168 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
172 var pktEnc nncp.PktEnc
173 _, err = xdr.Unmarshal(fd, &pktEnc)
174 if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
175 ctx.LogD("nncp-xfer", sds, "is not a packet")
179 if pktEnc.Nice > nice {
180 ctx.LogD("nncp-xfer", sds, "too nice")
184 sds["size"] = strconv.FormatInt(fiInt.Size(), 10)
185 if !ctx.IsEnoughSpace(fiInt.Size()) {
186 ctx.LogE("nncp-xfer", sds, "is not enough space")
191 tmp, err := ctx.NewTmpFileWHash()
195 if _, err = io.CopyN(tmp.W, bufio.NewReader(fd), fiInt.Size()); err != nil {
196 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
203 if err = tmp.Commit(filepath.Join(
210 ctx.LogI("nncp-xfer", sds, "")
212 if err = os.Remove(filename); err != nil {
213 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
227 sds["xx"] = string(nncp.TTx)
228 for nodeId, _ := range ctx.Neigh {
230 if nodeOnly != nil && nodeId != *nodeOnly.Id {
231 ctx.LogD("nncp-xfer", sds, "skip")
234 dirLock, err := ctx.LockDir(&nodeId, nncp.TTx)
238 nodePath := filepath.Join(flag.Arg(0), nodeId.String())
239 sds["dir"] = nodePath
240 _, err = os.Stat(nodePath)
242 if os.IsNotExist(err) {
243 ctx.LogD("nncp-xfer", sds, "does not exist")
245 ctx.UnlockDir(dirLock)
248 if err = os.Mkdir(nodePath, os.FileMode(0700)); err != nil {
249 ctx.UnlockDir(dirLock)
250 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
255 ctx.UnlockDir(dirLock)
256 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
261 dstPath := filepath.Join(nodePath, ctx.SelfId.String())
263 _, err = os.Stat(dstPath)
265 if os.IsNotExist(err) {
266 if err = os.Mkdir(dstPath, os.FileMode(0700)); err != nil {
267 ctx.UnlockDir(dirLock)
268 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
273 ctx.UnlockDir(dirLock)
274 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
280 for job := range ctx.Jobs(&nodeId, nncp.TTx) {
281 pktName := filepath.Base(job.Fd.Name())
283 if job.PktEnc.Nice > nice {
284 ctx.LogD("nncp-xfer", sds, "too nice")
288 if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
289 ctx.LogD("nncp-xfer", sds, "already exists")
293 if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
294 ctx.LogD("nncp-xfer", sds, "already exists")
298 tmp, err := ioutil.TempFile(dstPath, "nncp-xfer")
300 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp")
305 sds["tmp"] = tmp.Name()
306 ctx.LogD("nncp-xfer", sds, "created")
307 bufW := bufio.NewWriter(tmp)
308 copied, err := io.Copy(bufW, bufio.NewReader(job.Fd))
311 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
316 if err = bufW.Flush(); err != nil {
318 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "flush")
322 if err = tmp.Sync(); err != nil {
324 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "sync")
329 if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
330 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
334 os.Remove(filepath.Join(dstPath, pktName+".part"))
336 ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
337 "size": strconv.FormatInt(copied, 10),
340 if err = os.Remove(job.Fd.Name()); err != nil {
341 ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
346 ctx.UnlockDir(dirLock)