/*
NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2020 Sergey Matveev <stargrave@stargrave.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
import (
"bufio"
+ "errors"
"flag"
"fmt"
"io"
- "io/ioutil"
"log"
"os"
"path/filepath"
- "strconv"
- "github.com/davecgh/go-xdr/xdr2"
- "go.cypherpunks.ru/nncp/v4"
+ xdr "github.com/davecgh/go-xdr/xdr2"
+ "go.cypherpunks.ru/nncp/v5"
)
func usage() {
spoolPath = flag.String("spool", "", "Override path to spool")
logPath = flag.String("log", "", "Override path to logfile")
quiet = flag.Bool("quiet", false, "Print only errors")
+ showPrgrs = flag.Bool("progress", false, "Force progress showing")
+ omitPrgrs = flag.Bool("noprogress", false, "Omit progress showing")
debug = flag.Bool("debug", false, "Print debug messages")
version = flag.Bool("version", false, "Print version information")
warranty = flag.Bool("warranty", false, "Print warranty information")
log.Fatalln("-rx and -tx can not be set simultaneously")
}
- ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
+ ctx, err := nncp.CtxFromCmdline(
+ *cfgPath,
+ *spoolPath,
+ *logPath,
+ *quiet,
+ *showPrgrs,
+ *omitPrgrs,
+ *debug,
+ )
if err != nil {
log.Fatalln("Error during initialization:", err)
}
}
}
+ ctx.Umask()
selfPath := filepath.Join(flag.Arg(0), ctx.SelfId.String())
isBad := false
var dir *os.File
ctx.LogD("nncp-xfer", sds, "no dir")
goto Tx
}
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
+ ctx.LogE("nncp-xfer", sds, err, "stat")
isBad = true
goto Tx
}
dir, err = os.Open(selfPath)
if err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
+ ctx.LogE("nncp-xfer", sds, err, "open")
isBad = true
goto Tx
}
fis, err = dir.Readdir(0)
- dir.Close()
+ dir.Close() // #nosec G104
if err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
+ ctx.LogE("nncp-xfer", sds, err, "read")
isBad = true
goto Tx
}
}
dir, err = os.Open(filepath.Join(selfPath, fi.Name()))
if err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
+ ctx.LogE("nncp-xfer", sds, err, "open")
isBad = true
continue
}
fisInt, err := dir.Readdir(0)
- dir.Close()
+ dir.Close() // #nosec G104
if err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "read")
+ ctx.LogE("nncp-xfer", sds, err, "read")
isBad = true
continue
}
if !fi.IsDir() {
continue
}
+ // Check that it is valid Base32 encoding
+ if _, err = nncp.NodeIdFromString(fiInt.Name()); err != nil {
+ continue
+ }
filename := filepath.Join(dir.Name(), fiInt.Name())
sds["file"] = filename
delete(sds, "size")
fd, err := os.Open(filename)
if err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open")
+ ctx.LogE("nncp-xfer", sds, err, "open")
isBad = true
continue
}
_, err = xdr.Unmarshal(fd, &pktEnc)
if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
ctx.LogD("nncp-xfer", sds, "is not a packet")
- fd.Close()
+ fd.Close() // #nosec G104
continue
}
if pktEnc.Nice > nice {
ctx.LogD("nncp-xfer", sds, "too nice")
- fd.Close()
+ fd.Close() // #nosec G104
continue
}
- sds["size"] = strconv.FormatInt(fiInt.Size(), 10)
+ sds["size"] = fiInt.Size()
if !ctx.IsEnoughSpace(fiInt.Size()) {
- ctx.LogE("nncp-xfer", sds, "is not enough space")
- fd.Close()
+ ctx.LogE("nncp-xfer", sds, errors.New("is not enough space"), "")
+ fd.Close() // #nosec G104
continue
}
- fd.Seek(0, 0)
+ if _, err = fd.Seek(0, 0); err != nil {
+ log.Fatalln(err)
+ }
tmp, err := ctx.NewTmpFileWHash()
if err != nil {
log.Fatalln(err)
}
- if _, err = io.CopyN(tmp.W, bufio.NewReader(fd), fiInt.Size()); err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
+ r, w := io.Pipe()
+ go func() {
+ _, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
+ if err == nil {
+ err = w.Close()
+ }
+ if err != nil {
+ ctx.LogE("nncp-xfer", sds, err, "copy")
+ w.CloseWithError(err) // #nosec G104
+ }
+ }()
+ if _, err = nncp.CopyProgressed(
+ tmp.W, r, "Rx",
+ nncp.SdsAdd(sds, nncp.SDS{
+ "pkt": filename,
+ "fullsize": sds["size"],
+ }),
+ ctx.ShowPrgrs,
+ ); err != nil {
+ ctx.LogE("nncp-xfer", sds, err, "copy")
isBad = true
- fd.Close()
+ }
+ fd.Close() // #nosec G104
+ if isBad {
tmp.Cancel()
continue
}
- fd.Close()
if err = tmp.Commit(filepath.Join(
ctx.Spool,
nodeId.String(),
ctx.LogI("nncp-xfer", sds, "")
if !*keep {
if err = os.Remove(filename); err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
+ ctx.LogE("nncp-xfer", sds, err, "remove")
isBad = true
}
}
ctx.LogD("nncp-xfer", sds, "skip")
continue
}
- dirLock, err := ctx.LockDir(&nodeId, nncp.TTx)
+ dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
if err != nil {
continue
}
ctx.UnlockDir(dirLock)
continue
}
- if err = os.Mkdir(nodePath, os.FileMode(0700)); err != nil {
+ if err = os.Mkdir(nodePath, os.FileMode(0777)); err != nil {
ctx.UnlockDir(dirLock)
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
+ ctx.LogE("nncp-xfer", sds, err, "mkdir")
isBad = true
continue
}
} else {
ctx.UnlockDir(dirLock)
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
+ ctx.LogE("nncp-xfer", sds, err, "stat")
isBad = true
continue
}
_, err = os.Stat(dstPath)
if err != nil {
if os.IsNotExist(err) {
- if err = os.Mkdir(dstPath, os.FileMode(0700)); err != nil {
+ if err = os.Mkdir(dstPath, os.FileMode(0777)); err != nil {
ctx.UnlockDir(dirLock)
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mkdir")
+ ctx.LogE("nncp-xfer", sds, err, "mkdir")
isBad = true
continue
}
} else {
ctx.UnlockDir(dirLock)
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "stat")
+ ctx.LogE("nncp-xfer", sds, err, "stat")
isBad = true
continue
}
sds["pkt"] = pktName
if job.PktEnc.Nice > nice {
ctx.LogD("nncp-xfer", sds, "too nice")
- job.Fd.Close()
+ job.Fd.Close() // #nosec G104
continue
}
if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
ctx.LogD("nncp-xfer", sds, "already exists")
- job.Fd.Close()
+ job.Fd.Close() // #nosec G104
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()
+ job.Fd.Close() // #nosec G104
continue
}
- tmp, err := ioutil.TempFile(dstPath, "nncp-xfer")
+ tmp, err := nncp.TempFile(dstPath, "xfer")
if err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "mktemp")
- job.Fd.Close()
+ ctx.LogE("nncp-xfer", sds, err, "mktemp")
+ job.Fd.Close() // #nosec G104
isBad = true
break
}
sds["tmp"] = tmp.Name()
ctx.LogD("nncp-xfer", sds, "created")
bufW := bufio.NewWriter(tmp)
- copied, err := io.Copy(bufW, bufio.NewReader(job.Fd))
- job.Fd.Close()
+ copied, err := nncp.CopyProgressed(
+ bufW, bufio.NewReader(job.Fd), "Tx",
+ nncp.SdsAdd(sds, nncp.SDS{"fullsize": job.Size}),
+ ctx.ShowPrgrs,
+ )
+ job.Fd.Close() // #nosec G104
if err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy")
- tmp.Close()
+ ctx.LogE("nncp-xfer", sds, err, "copy")
+ tmp.Close() // #nosec G104
isBad = true
continue
}
if err = bufW.Flush(); err != nil {
- tmp.Close()
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "flush")
+ tmp.Close() // #nosec G104
+ ctx.LogE("nncp-xfer", sds, err, "flush")
isBad = true
continue
}
if err = tmp.Sync(); err != nil {
- tmp.Close()
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "sync")
+ tmp.Close() // #nosec G104
+ ctx.LogE("nncp-xfer", sds, err, "sync")
isBad = true
continue
}
- tmp.Close()
+ if err = tmp.Close(); err != nil {
+ ctx.LogE("nncp-xfer", sds, err, "sync")
+ }
if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename")
+ ctx.LogE("nncp-xfer", sds, err, "rename")
+ isBad = true
+ continue
+ }
+ if err = nncp.DirSync(dstPath); err != nil {
+ ctx.LogE("nncp-xfer", sds, err, "sync")
isBad = true
continue
}
- os.Remove(filepath.Join(dstPath, pktName+".part"))
+ os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
delete(sds, "tmp")
- ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{
- "size": strconv.FormatInt(copied, 10),
- }), "")
+ ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"size": copied}), "")
if !*keep {
if err = os.Remove(job.Fd.Name()); err != nil {
- ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove")
+ ctx.LogE("nncp-xfer", sds, err, "remove")
isBad = true
}
}