]> Cypherpunks.ru repositories - nncp.git/blob - src/tx.go
Generate list of created ACKs
[nncp.git] / src / tx.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2022 Sergey Matveev <stargrave@stargrave.org>
4
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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package nncp
19
20 import (
21         "archive/tar"
22         "bufio"
23         "bytes"
24         "errors"
25         "fmt"
26         "io"
27         "os"
28         "path/filepath"
29         "strconv"
30         "strings"
31         "time"
32
33         xdr "github.com/davecgh/go-xdr/xdr2"
34         "github.com/dustin/go-humanize"
35         "github.com/klauspost/compress/zstd"
36         "golang.org/x/crypto/blake2b"
37 )
38
39 const (
40         MaxFileSize = 1 << 62
41
42         TarBlockSize = 512
43         TarExt       = ".tar"
44 )
45
46 type PktEncWriteResult struct {
47         pktEncRaw []byte
48         size      int64
49         err       error
50 }
51
52 func (ctx *Ctx) Tx(
53         node *Node,
54         pkt *Pkt,
55         nice uint8,
56         srcSize, minSize, maxSize int64,
57         src io.Reader,
58         pktName string,
59         areaId *AreaId,
60 ) (*Node, int64, string, error) {
61         var area *Area
62         if areaId != nil {
63                 area = ctx.AreaId2Area[*areaId]
64                 if area.Prv == nil {
65                         return nil, 0, "", errors.New("area has no encryption keys")
66                 }
67         }
68         hops := make([]*Node, 0, 1+len(node.Via))
69         hops = append(hops, node)
70         lastNode := node
71         for i := len(node.Via); i > 0; i-- {
72                 lastNode = ctx.Neigh[*node.Via[i-1]]
73                 hops = append(hops, lastNode)
74         }
75         wrappers := len(hops)
76         if area != nil {
77                 wrappers++
78         }
79         var expectedSize int64
80         if srcSize > 0 {
81                 expectedSize = srcSize + PktOverhead
82                 expectedSize += sizePadCalc(expectedSize, minSize, wrappers)
83                 expectedSize = PktEncOverhead + sizeWithTags(expectedSize)
84                 if maxSize != 0 && expectedSize > maxSize {
85                         return nil, 0, "", TooBig
86                 }
87                 if !ctx.IsEnoughSpace(expectedSize) {
88                         return nil, 0, "", errors.New("is not enough space")
89                 }
90         }
91         tmp, err := ctx.NewTmpFileWHash()
92         if err != nil {
93                 return nil, 0, "", err
94         }
95
96         results := make(chan PktEncWriteResult)
97         pipeR, pipeW := io.Pipe()
98         var pipeRPrev io.Reader
99         if area == nil {
100                 go func(src io.Reader, dst io.WriteCloser) {
101                         ctx.LogD("tx", LEs{
102                                 {"Node", hops[0].Id},
103                                 {"Nice", int(nice)},
104                                 {"Size", expectedSize},
105                         }, func(les LEs) string {
106                                 return fmt.Sprintf(
107                                         "Tx packet to %s (source %s) nice: %s",
108                                         ctx.NodeName(hops[0].Id),
109                                         humanize.IBytes(uint64(expectedSize)),
110                                         NicenessFmt(nice),
111                                 )
112                         })
113                         pktEncRaw, size, err := PktEncWrite(
114                                 ctx.Self, hops[0], pkt, nice, minSize, maxSize, wrappers, src, dst,
115                         )
116                         results <- PktEncWriteResult{pktEncRaw, size, err}
117                         dst.Close()
118                 }(src, pipeW)
119         } else {
120                 go func(src io.Reader, dst io.WriteCloser) {
121                         ctx.LogD("tx", LEs{
122                                 {"Area", area.Id},
123                                 {"Nice", int(nice)},
124                                 {"Size", expectedSize},
125                         }, func(les LEs) string {
126                                 return fmt.Sprintf(
127                                         "Tx area packet to %s (source %s) nice: %s",
128                                         ctx.AreaName(areaId),
129                                         humanize.IBytes(uint64(expectedSize)),
130                                         NicenessFmt(nice),
131                                 )
132                         })
133                         areaNode := Node{Id: new(NodeId), ExchPub: new([32]byte)}
134                         copy(areaNode.Id[:], area.Id[:])
135                         copy(areaNode.ExchPub[:], area.Pub[:])
136                         pktEncRaw, size, err := PktEncWrite(
137                                 ctx.Self, &areaNode, pkt, nice, 0, maxSize, 0, src, dst,
138                         )
139                         results <- PktEncWriteResult{pktEncRaw, size, err}
140                         dst.Close()
141                 }(src, pipeW)
142                 pipeRPrev = pipeR
143                 pipeR, pipeW = io.Pipe()
144                 go func(src io.Reader, dst io.WriteCloser) {
145                         pktArea, err := NewPkt(PktTypeArea, 0, area.Id[:])
146                         if err != nil {
147                                 panic(err)
148                         }
149                         ctx.LogD("tx", LEs{
150                                 {"Node", hops[0].Id},
151                                 {"Nice", int(nice)},
152                                 {"Size", expectedSize},
153                         }, func(les LEs) string {
154                                 return fmt.Sprintf(
155                                         "Tx packet to %s (source %s) nice: %s",
156                                         ctx.NodeName(hops[0].Id),
157                                         humanize.IBytes(uint64(expectedSize)),
158                                         NicenessFmt(nice),
159                                 )
160                         })
161                         pktEncRaw, size, err := PktEncWrite(
162                                 ctx.Self, hops[0], pktArea, nice, minSize, maxSize, wrappers, src, dst,
163                         )
164                         results <- PktEncWriteResult{pktEncRaw, size, err}
165                         dst.Close()
166                 }(pipeRPrev, pipeW)
167         }
168         for i := 1; i < len(hops); i++ {
169                 pktTrns, err := NewPkt(PktTypeTrns, 0, hops[i-1].Id[:])
170                 if err != nil {
171                         panic(err)
172                 }
173                 pipeRPrev = pipeR
174                 pipeR, pipeW = io.Pipe()
175                 go func(node *Node, pkt *Pkt, src io.Reader, dst io.WriteCloser) {
176                         ctx.LogD("tx", LEs{
177                                 {"Node", node.Id},
178                                 {"Nice", int(nice)},
179                         }, func(les LEs) string {
180                                 return fmt.Sprintf(
181                                         "Tx trns packet to %s nice: %s",
182                                         ctx.NodeName(node.Id),
183                                         NicenessFmt(nice),
184                                 )
185                         })
186                         pktEncRaw, size, err := PktEncWrite(
187                                 ctx.Self, node, pkt, nice, 0, MaxFileSize, 0, src, dst,
188                         )
189                         results <- PktEncWriteResult{pktEncRaw, size, err}
190                         dst.Close()
191                 }(hops[i], pktTrns, pipeRPrev, pipeW)
192         }
193         go func() {
194                 _, err := CopyProgressed(
195                         tmp.W, pipeR, "Tx",
196                         LEs{{"Pkt", pktName}, {"FullSize", expectedSize}},
197                         ctx.ShowPrgrs,
198                 )
199                 results <- PktEncWriteResult{err: err}
200         }()
201         var pktEncRaw []byte
202         var pktEncMsg []byte
203         var payloadSize int64
204         if area != nil {
205                 r := <-results
206                 payloadSize = r.size
207                 pktEncMsg = r.pktEncRaw
208                 wrappers--
209         }
210         for i := 0; i <= wrappers; i++ {
211                 r := <-results
212                 if r.err != nil {
213                         tmp.Fd.Close()
214                         return nil, 0, "", r.err
215                 }
216                 if r.pktEncRaw != nil {
217                         pktEncRaw = r.pktEncRaw
218                         if payloadSize == 0 {
219                                 payloadSize = r.size
220                         }
221                 }
222         }
223         nodePath := filepath.Join(ctx.Spool, lastNode.Id.String())
224         err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
225         os.Symlink(nodePath, filepath.Join(ctx.Spool, lastNode.Name))
226         if err != nil {
227                 return lastNode, 0, "", err
228         }
229         if ctx.HdrUsage {
230                 ctx.HdrWrite(pktEncRaw, filepath.Join(nodePath, string(TTx), tmp.Checksum()))
231         }
232         if area != nil {
233                 msgHashRaw := blake2b.Sum256(pktEncMsg)
234                 msgHash := Base32Codec.EncodeToString(msgHashRaw[:])
235                 seenDir := filepath.Join(
236                         ctx.Spool, ctx.SelfId.String(), AreaDir, areaId.String(),
237                 )
238                 seenPath := filepath.Join(seenDir, msgHash)
239                 les := LEs{
240                         {"Node", node.Id},
241                         {"Nice", int(nice)},
242                         {"Size", expectedSize},
243                         {"Area", areaId},
244                         {"AreaMsg", msgHash},
245                 }
246                 logMsg := func(les LEs) string {
247                         return fmt.Sprintf(
248                                 "Tx area packet to %s (source %s) nice: %s, area %s: %s",
249                                 ctx.NodeName(node.Id),
250                                 humanize.IBytes(uint64(expectedSize)),
251                                 NicenessFmt(nice),
252                                 area.Name,
253                                 msgHash,
254                         )
255                 }
256                 if err = ensureDir(seenDir); err != nil {
257                         ctx.LogE("tx-mkdir", les, err, logMsg)
258                         return lastNode, 0, "", err
259                 }
260                 if fd, err := os.Create(seenPath); err == nil {
261                         fd.Close()
262                         if err = DirSync(seenDir); err != nil {
263                                 ctx.LogE("tx-dirsync", les, err, logMsg)
264                                 return lastNode, 0, "", err
265                         }
266                 }
267                 ctx.LogI("tx-area", les, logMsg)
268         }
269         return lastNode, payloadSize, tmp.Checksum(), err
270 }
271
272 type DummyCloser struct{}
273
274 func (dc DummyCloser) Close() error { return nil }
275
276 func prepareTxFile(srcPath string) (
277         reader io.Reader,
278         closer io.Closer,
279         srcSize int64,
280         archived bool,
281         rerr error,
282 ) {
283         if srcPath == "-" {
284                 reader = os.Stdin
285                 closer = os.Stdin
286                 return
287         }
288
289         srcStat, err := os.Stat(srcPath)
290         if err != nil {
291                 rerr = err
292                 return
293         }
294         mode := srcStat.Mode()
295
296         if mode.IsRegular() {
297                 // It is regular file, just send it
298                 src, err := os.Open(srcPath)
299                 if err != nil {
300                         rerr = err
301                         return
302                 }
303                 reader = src
304                 closer = src
305                 srcSize = srcStat.Size()
306                 return
307         }
308
309         if !mode.IsDir() {
310                 rerr = errors.New("unsupported file type")
311                 return
312         }
313
314         // It is directory, create PAX archive with its contents
315         archived = true
316         basePath := filepath.Base(srcPath)
317         rootPath, err := filepath.Abs(srcPath)
318         if err != nil {
319                 rerr = err
320                 return
321         }
322         type einfo struct {
323                 path    string
324                 modTime time.Time
325                 size    int64
326         }
327         dirs := make([]einfo, 0, 1<<10)
328         files := make([]einfo, 0, 1<<10)
329         rerr = filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
330                 if err != nil {
331                         return err
332                 }
333                 if info.Mode().IsDir() {
334                         // directory header, PAX record header+contents
335                         srcSize += TarBlockSize + 2*TarBlockSize
336                         dirs = append(dirs, einfo{path: path, modTime: info.ModTime()})
337                 } else if info.Mode().IsRegular() {
338                         // file header, PAX record header+contents, file content
339                         srcSize += TarBlockSize + 2*TarBlockSize + info.Size()
340                         if n := info.Size() % TarBlockSize; n != 0 {
341                                 srcSize += TarBlockSize - n // padding
342                         }
343                         files = append(files, einfo{
344                                 path:    path,
345                                 modTime: info.ModTime(),
346                                 size:    info.Size(),
347                         })
348                 }
349                 return nil
350         })
351         if rerr != nil {
352                 return
353         }
354
355         r, w := io.Pipe()
356         reader = r
357         closer = DummyCloser{}
358         srcSize += 2 * TarBlockSize // termination block
359
360         go func() error {
361                 tarWr := tar.NewWriter(w)
362                 hdr := tar.Header{
363                         Typeflag: tar.TypeDir,
364                         Mode:     0777,
365                         PAXRecords: map[string]string{
366                                 "comment": "Autogenerated by " + VersionGet(),
367                         },
368                         Format: tar.FormatPAX,
369                 }
370                 for _, e := range dirs {
371                         hdr.Name = basePath + e.path[len(rootPath):]
372                         hdr.ModTime = e.modTime
373                         if err = tarWr.WriteHeader(&hdr); err != nil {
374                                 return w.CloseWithError(err)
375                         }
376                 }
377                 hdr.Typeflag = tar.TypeReg
378                 hdr.Mode = 0666
379                 for _, e := range files {
380                         hdr.Name = basePath + e.path[len(rootPath):]
381                         hdr.ModTime = e.modTime
382                         hdr.Size = e.size
383                         if err = tarWr.WriteHeader(&hdr); err != nil {
384                                 return w.CloseWithError(err)
385                         }
386                         fd, err := os.Open(e.path)
387                         if err != nil {
388                                 fd.Close()
389                                 return w.CloseWithError(err)
390                         }
391                         if _, err = io.Copy(tarWr, bufio.NewReader(fd)); err != nil {
392                                 fd.Close()
393                                 return w.CloseWithError(err)
394                         }
395                         fd.Close()
396                 }
397                 if err = tarWr.Close(); err != nil {
398                         return w.CloseWithError(err)
399                 }
400                 return w.Close()
401         }()
402         return
403 }
404
405 func (ctx *Ctx) TxFile(
406         node *Node,
407         nice uint8,
408         srcPath, dstPath string,
409         chunkSize, minSize, maxSize int64,
410         areaId *AreaId,
411 ) error {
412         dstPathSpecified := false
413         if dstPath == "" {
414                 if srcPath == "-" {
415                         return errors.New("Must provide destination filename")
416                 }
417                 dstPath = filepath.Base(srcPath)
418         } else {
419                 dstPathSpecified = true
420         }
421         dstPath = filepath.Clean(dstPath)
422         if filepath.IsAbs(dstPath) {
423                 return errors.New("Relative destination path required")
424         }
425         reader, closer, srcSize, archived, err := prepareTxFile(srcPath)
426         if closer != nil {
427                 defer closer.Close()
428         }
429         if err != nil {
430                 return err
431         }
432         if archived && !dstPathSpecified {
433                 dstPath += TarExt
434         }
435
436         if chunkSize == 0 || (srcSize > 0 && srcSize <= chunkSize) {
437                 pkt, err := NewPkt(PktTypeFile, nice, []byte(dstPath))
438                 if err != nil {
439                         return err
440                 }
441                 _, finalSize, pktName, err := ctx.Tx(
442                         node, pkt, nice,
443                         srcSize, minSize, maxSize,
444                         bufio.NewReader(reader), dstPath, areaId,
445                 )
446                 les := LEs{
447                         {"Type", "file"},
448                         {"Node", node.Id},
449                         {"Nice", int(nice)},
450                         {"Src", srcPath},
451                         {"Dst", dstPath},
452                         {"Size", finalSize},
453                         {"Pkt", pktName},
454                 }
455                 logMsg := func(les LEs) string {
456                         return fmt.Sprintf(
457                                 "File %s (%s) is sent to %s:%s",
458                                 srcPath,
459                                 humanize.IBytes(uint64(finalSize)),
460                                 ctx.NodeName(node.Id),
461                                 dstPath,
462                         )
463                 }
464                 if err == nil {
465                         ctx.LogI("tx", les, logMsg)
466                 } else {
467                         ctx.LogE("tx", les, err, logMsg)
468                 }
469                 return err
470         }
471
472         br := bufio.NewReader(reader)
473         var sizeFull int64
474         var chunkNum int
475         checksums := [][MTHSize]byte{}
476         for {
477                 lr := io.LimitReader(br, chunkSize)
478                 path := dstPath + ChunkedSuffixPart + strconv.Itoa(chunkNum)
479                 pkt, err := NewPkt(PktTypeFile, nice, []byte(path))
480                 if err != nil {
481                         return err
482                 }
483                 hsh := MTHNew(0, 0)
484                 _, size, pktName, err := ctx.Tx(
485                         node, pkt, nice,
486                         0, minSize, maxSize,
487                         io.TeeReader(lr, hsh),
488                         path, areaId,
489                 )
490
491                 les := LEs{
492                         {"Type", "file"},
493                         {"Node", node.Id},
494                         {"Nice", int(nice)},
495                         {"Src", srcPath},
496                         {"Dst", path},
497                         {"Size", size},
498                         {"Pkt", pktName},
499                 }
500                 logMsg := func(les LEs) string {
501                         return fmt.Sprintf(
502                                 "File %s (%s) is sent to %s:%s",
503                                 srcPath,
504                                 humanize.IBytes(uint64(size)),
505                                 ctx.NodeName(node.Id),
506                                 path,
507                         )
508                 }
509                 if err == nil {
510                         ctx.LogI("tx", les, logMsg)
511                 } else {
512                         ctx.LogE("tx", les, err, logMsg)
513                         return err
514                 }
515
516                 sizeFull += size - PktOverhead
517                 var checksum [MTHSize]byte
518                 hsh.Sum(checksum[:0])
519                 checksums = append(checksums, checksum)
520                 chunkNum++
521                 if size < chunkSize {
522                         break
523                 }
524                 if _, err = br.Peek(1); err != nil {
525                         break
526                 }
527         }
528
529         metaPkt := ChunkedMeta{
530                 Magic:     MagicNNCPMv2.B,
531                 FileSize:  uint64(sizeFull),
532                 ChunkSize: uint64(chunkSize),
533                 Checksums: checksums,
534         }
535         var buf bytes.Buffer
536         _, err = xdr.Marshal(&buf, metaPkt)
537         if err != nil {
538                 return err
539         }
540         path := dstPath + ChunkedSuffixMeta
541         pkt, err := NewPkt(PktTypeFile, nice, []byte(path))
542         if err != nil {
543                 return err
544         }
545         metaPktSize := int64(buf.Len())
546         _, _, pktName, err := ctx.Tx(
547                 node,
548                 pkt,
549                 nice,
550                 metaPktSize, minSize, maxSize,
551                 &buf, path, areaId,
552         )
553         les := LEs{
554                 {"Type", "file"},
555                 {"Node", node.Id},
556                 {"Nice", int(nice)},
557                 {"Src", srcPath},
558                 {"Dst", path},
559                 {"Size", metaPktSize},
560                 {"Pkt", pktName},
561         }
562         logMsg := func(les LEs) string {
563                 return fmt.Sprintf(
564                         "File %s (%s) is sent to %s:%s",
565                         srcPath,
566                         humanize.IBytes(uint64(metaPktSize)),
567                         ctx.NodeName(node.Id),
568                         path,
569                 )
570         }
571         if err == nil {
572                 ctx.LogI("tx", les, logMsg)
573         } else {
574                 ctx.LogE("tx", les, err, logMsg)
575         }
576         return err
577 }
578
579 func (ctx *Ctx) TxFreq(
580         node *Node,
581         nice, replyNice uint8,
582         srcPath, dstPath string,
583         minSize int64,
584 ) error {
585         dstPath = filepath.Clean(dstPath)
586         if filepath.IsAbs(dstPath) {
587                 return errors.New("Relative destination path required")
588         }
589         srcPath = filepath.Clean(srcPath)
590         if filepath.IsAbs(srcPath) {
591                 return errors.New("Relative source path required")
592         }
593         pkt, err := NewPkt(PktTypeFreq, replyNice, []byte(srcPath))
594         if err != nil {
595                 return err
596         }
597         src := strings.NewReader(dstPath)
598         size := int64(src.Len())
599         _, _, pktName, err := ctx.Tx(
600                 node, pkt, nice, size, minSize, MaxFileSize, src, srcPath, nil,
601         )
602         les := LEs{
603                 {"Type", "freq"},
604                 {"Node", node.Id},
605                 {"Nice", int(nice)},
606                 {"ReplyNice", int(replyNice)},
607                 {"Src", srcPath},
608                 {"Dst", dstPath},
609                 {"Pkt", pktName},
610         }
611         logMsg := func(les LEs) string {
612                 return fmt.Sprintf(
613                         "File request from %s:%s to %s is sent",
614                         ctx.NodeName(node.Id), srcPath,
615                         dstPath,
616                 )
617         }
618         if err == nil {
619                 ctx.LogI("tx", les, logMsg)
620         } else {
621                 ctx.LogE("tx", les, err, logMsg)
622         }
623         return err
624 }
625
626 func (ctx *Ctx) TxExec(
627         node *Node,
628         nice, replyNice uint8,
629         handle string,
630         args []string,
631         in io.Reader,
632         minSize int64, maxSize int64,
633         noCompress bool,
634         areaId *AreaId,
635 ) error {
636         path := make([][]byte, 0, 1+len(args))
637         path = append(path, []byte(handle))
638         for _, arg := range args {
639                 path = append(path, []byte(arg))
640         }
641         pktType := PktTypeExec
642         if noCompress {
643                 pktType = PktTypeExecFat
644         }
645         pkt, err := NewPkt(pktType, replyNice, bytes.Join(path, []byte{0}))
646         if err != nil {
647                 return err
648         }
649         compressErr := make(chan error, 1)
650         if !noCompress {
651                 pr, pw := io.Pipe()
652                 compressor, err := zstd.NewWriter(pw, zstd.WithEncoderLevel(zstd.SpeedDefault))
653                 if err != nil {
654                         return err
655                 }
656                 go func(r io.Reader) {
657                         if _, err := io.Copy(compressor, r); err != nil {
658                                 compressErr <- err
659                                 return
660                         }
661                         compressErr <- compressor.Close()
662                         pw.Close()
663                 }(in)
664                 in = pr
665         }
666         _, size, pktName, err := ctx.Tx(
667                 node, pkt, nice, 0, minSize, maxSize, in, handle, areaId,
668         )
669         if !noCompress {
670                 e := <-compressErr
671                 if err == nil {
672                         err = e
673                 }
674         }
675         dst := strings.Join(append([]string{handle}, args...), " ")
676         les := LEs{
677                 {"Type", "exec"},
678                 {"Node", node.Id},
679                 {"Nice", int(nice)},
680                 {"ReplyNice", int(replyNice)},
681                 {"Dst", dst},
682                 {"Size", size},
683                 {"Pkt", pktName},
684         }
685         logMsg := func(les LEs) string {
686                 return fmt.Sprintf(
687                         "Exec is sent to %s@%s (%s)",
688                         ctx.NodeName(node.Id), dst, humanize.IBytes(uint64(size)),
689                 )
690         }
691         if err == nil {
692                 ctx.LogI("tx", les, logMsg)
693         } else {
694                 ctx.LogE("tx", les, err, logMsg)
695         }
696         return err
697 }
698
699 func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error {
700         les := LEs{
701                 {"Type", "trns"},
702                 {"Node", node.Id},
703                 {"Nice", int(nice)},
704                 {"Size", size},
705         }
706         logMsg := func(les LEs) string {
707                 return fmt.Sprintf(
708                         "Transitional packet to %s (%s) (nice %s)",
709                         ctx.NodeName(node.Id),
710                         humanize.IBytes(uint64(size)),
711                         NicenessFmt(nice),
712                 )
713         }
714         ctx.LogD("tx", les, logMsg)
715         if !ctx.IsEnoughSpace(size) {
716                 err := errors.New("is not enough space")
717                 ctx.LogE("tx", les, err, logMsg)
718                 return err
719         }
720         tmp, err := ctx.NewTmpFileWHash()
721         if err != nil {
722                 return err
723         }
724         if _, err = CopyProgressed(
725                 tmp.W, src, "Tx trns",
726                 LEs{{"Pkt", node.Id.String()}, {"FullSize", size}},
727                 ctx.ShowPrgrs,
728         ); err != nil {
729                 return err
730         }
731         nodePath := filepath.Join(ctx.Spool, node.Id.String())
732         err = tmp.Commit(filepath.Join(nodePath, string(TTx)))
733         if err == nil {
734                 ctx.LogI("tx", les, logMsg)
735         } else {
736                 ctx.LogI("tx", append(les, LE{"Err", err}), logMsg)
737         }
738         os.Symlink(nodePath, filepath.Join(ctx.Spool, node.Name))
739         return err
740 }
741
742 func (ctx *Ctx) TxACK(
743         node *Node,
744         nice uint8,
745         hsh string,
746         minSize int64,
747 ) (pktName string, err error) {
748         hshRaw, err := Base32Codec.DecodeString(hsh)
749         if err != nil {
750                 return "", err
751         }
752         if len(hshRaw) != MTHSize {
753                 return "", errors.New("Invalid packet id size")
754         }
755         pkt, err := NewPkt(PktTypeACK, nice, []byte(hshRaw))
756         if err != nil {
757                 return "", err
758         }
759         src := bytes.NewReader([]byte{})
760         _, _, pktName, err = ctx.Tx(
761                 node, pkt, nice, 0, minSize, MaxFileSize, src, hsh, nil,
762         )
763         les := LEs{
764                 {"Type", "ack"},
765                 {"Node", node.Id},
766                 {"Nice", int(nice)},
767                 {"Pkt", hsh},
768                 {"NewPkt", pktName},
769         }
770         logMsg := func(les LEs) string {
771                 return fmt.Sprintf("ACK to %s of %s is sent", ctx.NodeName(node.Id), hsh)
772         }
773         if err == nil {
774                 ctx.LogI("tx", les, logMsg)
775         } else {
776                 ctx.LogE("tx", les, err, logMsg)
777         }
778         return
779 }