2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2017 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/>.
37 "github.com/davecgh/go-xdr/xdr2"
38 "golang.org/x/crypto/blake2b"
42 fmt.Fprintf(os.Stderr, nncp.UsageHeader())
43 fmt.Fprintln(os.Stderr, "nncp-reass -- reassemble chunked files\n")
44 fmt.Fprintf(os.Stderr, "Usage: %s [options] [FILE]\nOptions:\n", os.Args[0])
46 fmt.Fprint(os.Stderr, `
47 Neither FILE, nor -node nor -all can be set simultaneously,
48 but at least one of them must be specified.
52 func process(ctx *nncp.Ctx, path string, keep, dryRun bool) bool {
53 fd, err := os.Open(path)
56 log.Fatalln("Can not open file:", err)
58 var metaPkt nncp.ChunkedMeta
59 if _, err = xdr.Unmarshal(fd, &metaPkt); err != nil {
60 ctx.LogE("nncp-reass", nncp.SDS{"path": path, "err": err}, "bad meta file")
64 if metaPkt.Magic != nncp.MagicNNCPMv1 {
65 ctx.LogE("nncp-reass", nncp.SDS{"path": path, "err": nncp.BadMagic}, "")
68 metaName := filepath.Base(path)
69 if !strings.HasSuffix(metaName, nncp.ChunkedSuffixMeta) {
70 ctx.LogE("nncp-reass", nncp.SDS{
72 "err": "invalid filename suffix",
76 mainName := strings.TrimSuffix(metaName, nncp.ChunkedSuffixMeta)
77 mainDir := filepath.Dir(path)
79 chunksPaths := make([]string, 0, len(metaPkt.Checksums))
80 for i := 0; i < len(metaPkt.Checksums); i++ {
83 filepath.Join(mainDir, mainName+nncp.ChunkedSuffixPart+strconv.Itoa(i)),
87 allChunksExist := true
88 for chunkNum, chunkPath := range chunksPaths {
89 fi, err := os.Stat(chunkPath)
90 if err != nil && os.IsNotExist(err) {
91 ctx.LogI("nncp-reass", nncp.SDS{
93 "chunk": strconv.Itoa(chunkNum),
95 allChunksExist = false
98 if chunkNum+1 != len(chunksPaths) && uint64(fi.Size()) != metaPkt.ChunkSize {
99 ctx.LogE("nncp-reass", nncp.SDS{
101 "chunk": strconv.Itoa(chunkNum),
103 allChunksExist = false
111 allChecksumsGood := true
112 for chunkNum, chunkPath := range chunksPaths {
113 fd, err = os.Open(chunkPath)
115 log.Fatalln("Can not open file:", err)
117 hsh, err = blake2b.New256(nil)
121 if _, err = io.Copy(hsh, bufio.NewReader(fd)); err != nil {
125 if bytes.Compare(hsh.Sum(nil), metaPkt.Checksums[chunkNum][:]) != 0 {
126 ctx.LogE("nncp-reass", nncp.SDS{
128 "chunk": strconv.Itoa(chunkNum),
129 "err": "checksum is bad",
131 allChecksumsGood = false
134 if !allChecksumsGood {
138 ctx.LogI("nncp-reass", nncp.SDS{"path": path}, "ready")
142 tmp, err := ioutil.TempFile(mainDir, "nncp-reass")
146 sds := nncp.SDS{"path": path, "tmp": tmp.Name()}
147 ctx.LogD("nncp-reass", sds, "created")
148 tmpW := bufio.NewWriter(tmp)
151 for chunkNum, chunkPath := range chunksPaths {
152 fd, err = os.Open(chunkPath)
154 log.Fatalln("Can not open file:", err)
156 if _, err = io.Copy(tmpW, bufio.NewReader(fd)); err != nil {
161 if err = os.Remove(chunkPath); err != nil {
162 ctx.LogE("nncp-reass", nncp.SdsAdd(sds, nncp.SDS{
163 "chunk": strconv.Itoa(chunkNum),
173 ctx.LogD("nncp-reass", sds, "written")
175 if err = os.Remove(path); err != nil {
176 ctx.LogE("nncp-reass", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "")
181 dstPathOrig := filepath.Join(mainDir, mainName)
182 dstPath := dstPathOrig
185 if _, err = os.Stat(dstPath); err != nil {
186 if os.IsNotExist(err) {
191 dstPath = dstPathOrig + strconv.Itoa(dstPathCtr)
194 if err = os.Rename(tmp.Name(), dstPath); err != nil {
197 ctx.LogI("nncp-reass", nncp.SDS{"path": path}, "done")
201 func findMetas(ctx *nncp.Ctx, dirPath string) []string {
202 dir, err := os.Open(dirPath)
205 ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath, "err": err}, "")
208 fis, err := dir.Readdir(0)
211 ctx.LogE("nncp-reass", nncp.SDS{"path": dirPath, "err": err}, "")
214 metaPaths := make([]string, 0)
215 for _, fi := range fis {
216 if strings.HasSuffix(fi.Name(), nncp.ChunkedSuffixMeta) {
217 metaPaths = append(metaPaths, filepath.Join(dirPath, fi.Name()))
225 cfgPath = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
226 allNodes = flag.Bool("all", false, "Process all found chunked files for all nodes")
227 nodeRaw = flag.String("node", "", "Process all found chunked files for that node")
228 keep = flag.Bool("keep", false, "Do not remove chunks while assembling")
229 dryRun = flag.Bool("dryrun", false, "Do not assemble whole file")
230 quiet = flag.Bool("quiet", false, "Print only errors")
231 debug = flag.Bool("debug", false, "Print debug messages")
232 version = flag.Bool("version", false, "Print version information")
233 warranty = flag.Bool("warranty", false, "Print warranty information")
238 fmt.Println(nncp.Warranty)
242 fmt.Println(nncp.VersionGet())
246 cfgRaw, err := ioutil.ReadFile(nncp.CfgPathFromEnv(cfgPath))
248 log.Fatalln("Can not read config:", err)
250 ctx, err := nncp.CfgParse(cfgRaw)
252 log.Fatalln("Can not parse config:", err)
257 var nodeOnly *nncp.Node
259 nodeOnly, err = ctx.FindNode(*nodeRaw)
261 log.Fatalln("Invalid -node specified:", err)
265 if !(*allNodes || nodeOnly != nil || flag.NArg() > 0) {
269 if flag.NArg() > 0 && (*allNodes || nodeOnly != nil) {
273 if *allNodes && nodeOnly != nil {
279 if !process(ctx, flag.Arg(0), *keep, *dryRun) {
287 seenMetaPaths := make(map[string]struct{})
288 for _, node := range ctx.Neigh {
289 if node.Incoming == nil {
292 for _, metaPath := range findMetas(ctx, *node.Incoming) {
293 if _, seen := seenMetaPaths[metaPath]; seen {
296 hasErrors = hasErrors || !process(ctx, metaPath, *keep, *dryRun)
297 seenMetaPaths[metaPath] = struct{}{}
301 if nodeOnly.Incoming == nil {
302 log.Fatalln("Specified -node does not allow incoming")
304 for _, metaPath := range findMetas(ctx, *nodeOnly.Incoming) {
305 hasErrors = hasErrors || !process(ctx, metaPath, *keep, *dryRun)