func usage() {
fmt.Fprintf(os.Stderr, nncp.UsageHeader())
fmt.Fprintln(os.Stderr, "nncp-xfer -- copy inbound and outbounds packets\n")
- fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:", os.Args[0])
+ fmt.Fprintf(os.Stderr, "Usage: %s [options] DIR\nOptions:\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
var (
- cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
- nodeRaw = flag.String("node", "", "Process only that node")
- niceRaw = flag.Int("nice", 255, "Minimal required niceness")
- rxOnly = flag.Bool("rx", false, "Only receive packets")
- txOnly = flag.Bool("tx", false, "Only transfer packets")
- force = flag.Bool("force", false, "Force outbound directories creation")
- keep = flag.Bool("keep", false, "Do not delete transferred packets")
- debug = flag.Bool("debug", false, "Enable debugging information")
- version = flag.Bool("version", false, "Print version information")
- warranty = flag.Bool("warranty", false, "Print warranty information")
+ cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
+ nodeRaw = flag.String("node", "", "Process only that node")
+ niceRaw = flag.Int("nice", 255, "Minimal required niceness")
+ rxOnly = flag.Bool("rx", false, "Only receive packets")
+ txOnly = flag.Bool("tx", false, "Only transfer packets")
+ mkdir = flag.Bool("mkdir", false, "Create necessary outbound directories")
+ keep = flag.Bool("keep", false, "Do not delete transferred packets")
+ spoolPath = flag.String("spool", "", "Override path to spool")
+ logPath = flag.String("log", "", "Override path to logfile")
+ quiet = flag.Bool("quiet", false, "Print only errors")
+ debug = flag.Bool("debug", false, "Print debug messages")
+ version = flag.Bool("version", false, "Print version information")
+ warranty = flag.Bool("warranty", false, "Print warranty information")
)
flag.Usage = usage
flag.Parse()
log.Fatalln("-rx and -tx can not be set simultaneously")
}
- cfgRaw, err := ioutil.ReadFile(*cfgPath)
+ ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
if err != nil {
- log.Fatalln("Can not read config:", err)
+ log.Fatalln("Error during initialization:", err)
}
- ctx, err := nncp.CfgParse(cfgRaw)
- if err != nil {
- log.Fatalln("Can not parse config:", err)
- }
- ctx.Debug = *debug
var nodeOnly *nncp.Node
if *nodeRaw != "" {
}
}
- selfPath := filepath.Join(flag.Arg(0), ctx.Self.Id.String())
- badExit := false
+ selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
+ isBad := false
var dir *os.File
var fis []os.FileInfo
sds := nncp.SDS{}
goto Tx
}
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
- badExit = true
+ isBad = true
goto Tx
}
dir, err = os.Open(selfPath)
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
- badExit = true
+ isBad = true
goto Tx
}
fis, err = dir.Readdir(0)
dir.Close()
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
- badExit = true
+ isBad = true
goto Tx
}
for _, fi := range fis {
dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
- badExit = true
+ isBad = true
continue
}
fisInt, err := dir.Readdir(0)
dir.Close()
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
- badExit = true
+ isBad = true
continue
}
for _, fiInt := range fisInt {
fd, err := os.Open(filename)
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
- badExit = true
+ isBad = true
continue
}
var pktEnc nncp.PktEnc
_, err = xdr.Unmarshal(fd, &pktEnc)
- if err != nil || pktEnc.Magic != nncp.MagicNNCPEv1 {
+ if err != nil || pktEnc.Magic != nncp.MagicNNCPEv3 {
ctx.LogD("nncp-xfer", sds, "is not a packet")
fd.Close()
continue
copied, err := io.Copy(tmp.W, bufio.NewReader(fd))
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
- badExit = true
+ isBad = true
fd.Close()
tmp.Cancel()
continue
if !*keep {
if err = os.Remove(filename); err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
- badExit = true
+ isBad = true
}
}
}
Tx:
if *rxOnly {
- if badExit {
+ if isBad {
os.Exit(1)
}
return
if err != nil {
if os.IsNotExist(err) {
ctx.LogD("nncp-xfer", sds, "does not exist")
- if !*force {
+ if !*mkdir {
ctx.UnlockDir(dirLock)
continue
}
if err = os.Mkdir(nodePath, os.FileMode(0700)); err != nil {
ctx.UnlockDir(dirLock)
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
- badExit = true
+ isBad = true
continue
}
} else {
ctx.UnlockDir(dirLock)
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
- badExit = true
+ isBad = true
continue
}
}
- dstPath := filepath.Join(nodePath, ctx.Self.Id.String())
+ dstPath := filepath.Join(nodePath, ctx.SelfId.String())
sds["dir"] = dstPath
_, err = os.Stat(dstPath)
if err != nil {
if err = os.Mkdir(dstPath, os.FileMode(0700)); err != nil {
ctx.UnlockDir(dirLock)
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
- badExit = true
+ isBad = true
continue
}
} else {
ctx.UnlockDir(dirLock)
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
- badExit = true
+ isBad = true
continue
}
}
job.Fd.Close()
continue
}
+ if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
+ ctx.LogD("nncp-xfer", sds, "already exists")
+ job.Fd.Close()
+ continue
+ }
+ if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
+ ctx.LogD("nncp-xfer", sds, "already exists")
+ job.Fd.Close()
+ continue
+ }
tmp, err := ioutil.TempFile(dstPath, "nncp-xfer")
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp")
job.Fd.Close()
- badExit = true
+ isBad = true
break
}
sds["tmp"] = tmp.Name()
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
tmp.Close()
- badExit = true
+ isBad = true
continue
}
err = bufW.Flush()
tmp.Close()
if err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
- badExit = true
+ isBad = true
continue
}
if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
- badExit = true
+ isBad = true
continue
}
+ os.Remove(filepath.Join(dstPath, pktName+".part"))
delete(sds, "tmp")
ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
"size": strconv.FormatInt(copied, 10),
if !*keep {
if err = os.Remove(job.Fd.Name()); err != nil {
ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
- badExit = true
+ isBad = true
}
}
}
ctx.UnlockDir(dirLock)
}
- if badExit {
+ if isBad {
os.Exit(1)
}
}