2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2018 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/>.
38 "github.com/davecgh/go-xdr/xdr2"
39 "github.com/dustin/go-humanize"
40 "golang.org/x/crypto/blake2b"
44 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
45 fmt.Fprintf(os.Stderr, "nncp-reass -- reassemble chunked files\n\n")
46 fmt.Fprintf(os.Stderr, "Usage: %s [options] [FILE.nncp.meta]\nOptions:\n", os.Args[0])
48 fmt.Fprint(os.Stderr, `
49 Neither FILE, nor -node nor -all can be set simultaneously,
50 but at least one of them must be specified.
54 func process(ctx *nncp.Ctx, path string, keep, dryRun, stdout, dumpMeta bool) bool {
55 fd, err := os.Open(path)
58 log.Fatalln("Can not open file:", err)
60 var metaPkt nncp.ChunkedMeta
61 if _, err = xdr.Unmarshal(fd, &metaPkt); err != nil {
62 ctx.LogE("nncp-reass", nncp.SDS{"path": path, "err": err}, "bad meta file")
66 if metaPkt.Magic != nncp.MagicNNCPMv1 {
67 ctx.LogE("nncp-reass", nncp.SDS{"path": path, "err": nncp.BadMagic}, "")
71 metaName := filepath.Base(path)
72 if !strings.HasSuffix(metaName, nncp.ChunkedSuffixMeta) {
73 ctx.LogE("nncp-reass", nncp.SDS{
75 "err": "invalid filename suffix",
79 mainName := strings.TrimSuffix(metaName, nncp.ChunkedSuffixMeta)
81 fmt.Printf("Original filename: %s\n", mainName)
83 "File size: %s (%d bytes)\n",
84 humanize.IBytes(metaPkt.FileSize),
88 "Chunk size: %s (%d bytes)\n",
89 humanize.IBytes(metaPkt.ChunkSize),
92 fmt.Printf("Number of chunks: %d\n", len(metaPkt.Checksums))
93 fmt.Println("Checksums:")
94 for chunkNum, checksum := range metaPkt.Checksums {
95 fmt.Printf("\t%d: %s\n", chunkNum, hex.EncodeToString(checksum[:]))
99 mainDir := filepath.Dir(path)
101 chunksPaths := make([]string, 0, len(metaPkt.Checksums))
102 for i := 0; i < len(metaPkt.Checksums); i++ {
103 chunksPaths = append(
105 filepath.Join(mainDir, mainName+nncp.ChunkedSuffixPart+strconv.Itoa(i)),
109 allChunksExist := true
110 for chunkNum, chunkPath := range chunksPaths {
111 fi, err := os.Stat(chunkPath)
112 if err != nil && os.IsNotExist(err) {
113 ctx.LogI("nncp-reass", nncp.SDS{
115 "chunk": strconv.Itoa(chunkNum),
117 allChunksExist = false
121 if chunkNum+1 == len(chunksPaths) {
122 badSize = uint64(fi.Size()) != metaPkt.FileSize%metaPkt.ChunkSize
124 badSize = uint64(fi.Size()) != metaPkt.ChunkSize
127 ctx.LogE("nncp-reass", nncp.SDS{
129 "chunk": strconv.Itoa(chunkNum),
131 allChunksExist = false
139 allChecksumsGood := true
140 for chunkNum, chunkPath := range chunksPaths {
141 fd, err = os.Open(chunkPath)
143 log.Fatalln("Can not open file:", err)
145 hsh, err = blake2b.New256(nil)
149 if _, err = io.Copy(hsh, bufio.NewReader(fd)); err != nil {
153 if bytes.Compare(hsh.Sum(nil), metaPkt.Checksums[chunkNum][:]) != 0 {
154 ctx.LogE("nncp-reass", nncp.SDS{
156 "chunk": strconv.Itoa(chunkNum),
157 }, "checksum is bad")
158 allChecksumsGood = false
161 if !allChecksumsGood {
165 ctx.LogI("nncp-reass", nncp.SDS{"path": path}, "ready")
174 sds = nncp.SDS{"path": path}
176 tmp, err = ioutil.TempFile(mainDir, "nncp-reass")
180 sds = nncp.SDS{"path": path, "tmp": tmp.Name()}
181 ctx.LogD("nncp-reass", sds, "created")
184 dstW := bufio.NewWriter(dst)
187 for chunkNum, chunkPath := range chunksPaths {
188 fd, err = os.Open(chunkPath)
190 log.Fatalln("Can not open file:", err)
192 if _, err = io.Copy(dstW, bufio.NewReader(fd)); err != nil {
197 if err = os.Remove(chunkPath); err != nil {
198 ctx.LogE("nncp-reass", nncp.SdsAdd(sds, nncp.SDS{
199 "chunk": strconv.Itoa(chunkNum),
211 ctx.LogD("nncp-reass", sds, "written")
213 if err = os.Remove(path); err != nil {
214 ctx.LogE("nncp-reass", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "")
219 ctx.LogI("nncp-reass", nncp.SDS{"path": path}, "done")
223 dstPathOrig := filepath.Join(mainDir, mainName)
224 dstPath := dstPathOrig
227 if _, err = os.Stat(dstPath); err != nil {
228 if os.IsNotExist(err) {
233 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
236 if err = os.Rename(tmp.Name(), dstPath); err != nil {
239 ctx.LogI("nncp-reass", nncp.SDS{"path": path}, "done")
243 func findMetas(ctx *nncp.Ctx, dirPath string) []string {
244 dir, err := os.Open(dirPath)
247 ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath, "err": err}, "")
250 fis, err := dir.Readdir(0)
253 ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath, "err": err}, "")
256 metaPaths := make([]string, 0)
257 for _, fi := range fis {
258 if strings.HasSuffix(fi.Name(), nncp.ChunkedSuffixMeta) {
259 metaPaths = append(metaPaths, filepath.Join(dirPath, fi.Name()))
267 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
268 allNodes = flag.Bool("all", false, "Process all found chunked files for all nodes")
269 nodeRaw = flag.String("node", "", "Process all found chunked files for that node")
270 keep = flag.Bool("keep", false, "Do not remove chunks while assembling")
271 dryRun = flag.Bool("dryrun", false, "Do not assemble whole file")
272 dumpMeta = flag.Bool("dump", false, "Print decoded human-readable FILE.nncp.meta")
273 stdout = flag.Bool("stdout", false, "Output reassembled FILE to stdout")
274 spoolPath = flag.String("spool", "", "Override path to spool")
275 logPath = flag.String("log", "", "Override path to logfile")
276 quiet = flag.Bool("quiet", false, "Print only errors")
277 debug = flag.Bool("debug", false, "Print debug messages")
278 version = flag.Bool("version", false, "Print version information")
279 warranty = flag.Bool("warranty", false, "Print warranty information")
284 fmt.Println(nncp.Warranty)
288 fmt.Println(nncp.VersionGet())
292 ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
294 log.Fatalln("Error during initialization:", err)
297 var nodeOnly *nncp.Node
299 nodeOnly, err = ctx.FindNode(*nodeRaw)
301 log.Fatalln("Invalid -node specified:", err)
305 if !(*allNodes || nodeOnly != nil || flag.NArg() > 0) {
309 if flag.NArg() > 0 && (*allNodes || nodeOnly != nil) {
313 if *allNodes && nodeOnly != nil {
319 if !process(ctx, flag.Arg(0), *keep, *dryRun, *stdout, *dumpMeta) {
327 seenMetaPaths := make(map[string]struct{})
328 for _, node := range ctx.Neigh {
329 if node.Incoming == nil {
332 for _, metaPath := range findMetas(ctx, *node.Incoming) {
333 if _, seen := seenMetaPaths[metaPath]; seen {
336 hasErrors = hasErrors || !process(ctx, metaPath, *keep, *dryRun, false, false)
337 seenMetaPaths[metaPath] = struct{}{}
341 if nodeOnly.Incoming == nil {
342 log.Fatalln("Specified -node does not allow incoming")
344 for _, metaPath := range findMetas(ctx, *nodeOnly.Incoming) {
345 hasErrors = hasErrors || !process(ctx, metaPath, *keep, *dryRun, false, false)