X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcmd%2Fnncp-reass%2Fmain.go;h=75aaf5a0aa06b036373e8aff2b10c9efc45b41ab;hb=1d2ce674b042d07fd9b37a46578c8b62bb0345b7;hp=a798bd37f0e02f2044fb4de036b45e9c7ad7dfb2;hpb=c8b26fe06596d26bdb14c5be85760fb3ddb197b3;p=nncp.git diff --git a/src/cmd/nncp-reass/main.go b/src/cmd/nncp-reass/main.go index a798bd3..75aaf5a 100644 --- a/src/cmd/nncp-reass/main.go +++ b/src/cmd/nncp-reass/main.go @@ -22,20 +22,20 @@ import ( "bufio" "bytes" "encoding/hex" + "errors" "flag" "fmt" "hash" "io" - "io/ioutil" "log" "os" "path/filepath" "strconv" "strings" - "github.com/davecgh/go-xdr/xdr2" + xdr "github.com/davecgh/go-xdr/xdr2" "github.com/dustin/go-humanize" - "go.cypherpunks.ru/nncp/v4" + "go.cypherpunks.ru/nncp/v5" "golang.org/x/crypto/blake2b" ) @@ -58,21 +58,18 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo } var metaPkt nncp.ChunkedMeta if _, err = xdr.Unmarshal(fd, &metaPkt); err != nil { - ctx.LogE("nncp-reass", nncp.SDS{"path": path, "err": err}, "bad meta file") + ctx.LogE("nncp-reass", nncp.SDS{"path": path}, err, "bad meta file") return false } fd.Close() if metaPkt.Magic != nncp.MagicNNCPMv1 { - ctx.LogE("nncp-reass", nncp.SDS{"path": path, "err": nncp.BadMagic}, "") + ctx.LogE("nncp-reass", nncp.SDS{"path": path}, nncp.BadMagic, "") return false } metaName := filepath.Base(path) if !strings.HasSuffix(metaName, nncp.ChunkedSuffixMeta) { - ctx.LogE("nncp-reass", nncp.SDS{ - "path": path, - "err": "invalid filename suffix", - }, "") + ctx.LogE("nncp-reass", nncp.SDS{"path": path}, errors.New("invalid filename suffix"), "") return false } mainName := strings.TrimSuffix(metaName, nncp.ChunkedSuffixMeta) @@ -109,10 +106,7 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo for chunkNum, chunkPath := range chunksPaths { fi, err := os.Stat(chunkPath) if err != nil && os.IsNotExist(err) { - ctx.LogI("nncp-reass", nncp.SDS{ - "path": path, - "chunk": strconv.Itoa(chunkNum), - }, "missing") + ctx.LogI("nncp-reass", nncp.SDS{"path": path, "chunk": chunkNum}, "missing") allChunksExist = false continue } @@ -123,10 +117,11 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo badSize = uint64(fi.Size()) != metaPkt.ChunkSize } if badSize { - ctx.LogE("nncp-reass", nncp.SDS{ - "path": path, - "chunk": strconv.Itoa(chunkNum), - }, "invalid size") + ctx.LogE( + "nncp-reass", + nncp.SDS{"path": path, "chunk": chunkNum}, + errors.New("invalid size"), "", + ) allChunksExist = false } } @@ -141,19 +136,27 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo if err != nil { log.Fatalln("Can not open file:", err) } + fi, err := fd.Stat() + if err != nil { + log.Fatalln("Can not stat file:", err) + } hsh, err = blake2b.New256(nil) if err != nil { log.Fatalln(err) } - if _, err = io.Copy(hsh, bufio.NewReader(fd)); err != nil { + if _, err = nncp.CopyProgressed(hsh, bufio.NewReader(fd), nncp.SDS{ + "pkt": chunkPath, + "fullsize": fi.Size(), + }, ctx.ShowPrgrs); err != nil { log.Fatalln(err) } fd.Close() if bytes.Compare(hsh.Sum(nil), metaPkt.Checksums[chunkNum][:]) != 0 { - ctx.LogE("nncp-reass", nncp.SDS{ - "path": path, - "chunk": strconv.Itoa(chunkNum), - }, "checksum is bad") + ctx.LogE( + "nncp-reass", + nncp.SDS{"path": path, "chunk": chunkNum}, + errors.New("checksum is bad"), "", + ) allChecksumsGood = false } } @@ -172,7 +175,7 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo dst = os.Stdout sds = nncp.SDS{"path": path} } else { - tmp, err = ioutil.TempFile(mainDir, "nncp-reass") + tmp, err = nncp.TempFile(mainDir, "reass") if err != nil { log.Fatalln(err) } @@ -188,16 +191,20 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo if err != nil { log.Fatalln("Can not open file:", err) } - if _, err = io.Copy(dstW, bufio.NewReader(fd)); err != nil { + fi, err := fd.Stat() + if err != nil { + log.Fatalln("Can not stat file:", err) + } + if _, err = nncp.CopyProgressed(dstW, bufio.NewReader(fd), nncp.SDS{ + "pkt": chunkPath, + "fullsize": fi.Size(), + }, ctx.ShowPrgrs); err != nil { log.Fatalln(err) } fd.Close() if !keep { if err = os.Remove(chunkPath); err != nil { - ctx.LogE("nncp-reass", nncp.SdsAdd(sds, nncp.SDS{ - "chunk": strconv.Itoa(chunkNum), - "err": err, - }), "") + ctx.LogE("nncp-reass", nncp.SdsAdd(sds, nncp.SDS{"chunk": chunkNum}), err, "") hasErrors = true } } @@ -214,7 +221,7 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo ctx.LogD("nncp-reass", sds, "written") if !keep { if err = os.Remove(path); err != nil { - ctx.LogE("nncp-reass", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "") + ctx.LogE("nncp-reass", sds, err, "") hasErrors = true } } @@ -233,12 +240,15 @@ func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bo } log.Fatalln(err) } - dstPath = dstPathOrig + strconv.Itoa(dstPathCtr) + dstPath = dstPathOrig + "." + strconv.Itoa(dstPathCtr) dstPathCtr++ } if err = os.Rename(tmp.Name(), dstPath); err != nil { log.Fatalln(err) } + if err = nncp.DirSync(mainDir); err != nil { + log.Fatalln(err) + } ctx.LogI("nncp-reass", nncp.SDS{"path": path}, "done") return !hasErrors } @@ -247,13 +257,13 @@ func findMetas(ctx *nncp.Ctx, dirPath string) []string { dir, err := os.Open(dirPath) defer dir.Close() if err != nil { - ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath, "err": err}, "") + ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath}, err, "") return nil } fis, err := dir.Readdir(0) dir.Close() if err != nil { - ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath, "err": err}, "") + ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath}, err, "") return nil } metaPaths := make([]string, 0) @@ -277,6 +287,8 @@ func main() { 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") @@ -292,7 +304,15 @@ func main() { return } - 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) } @@ -318,6 +338,8 @@ func main() { os.Exit(1) } + ctx.Umask() + if flag.NArg() > 0 { if process(ctx, flag.Arg(0), *keep, *dryRun, *stdout, *dumpMeta) { return