]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cypherpunks.ru/nncp/tx.go
Initial nncp-reass utility
[nncp.git] / src / cypherpunks.ru / nncp / tx.go
index 65ad7ec3b58fbcadb1fcce340b7f2bdcf65e144d..a04f5bdb2dadaaf59012b3ab54939d14bfaff95c 100644 (file)
@@ -23,12 +23,14 @@ import (
        "bytes"
        "compress/zlib"
        "errors"
+       "hash"
        "io"
        "os"
        "path/filepath"
        "strconv"
        "strings"
 
+       "github.com/davecgh/go-xdr/xdr2"
        "golang.org/x/crypto/blake2b"
 )
 
@@ -146,6 +148,135 @@ func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize
        return err
 }
 
+func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, minSize int64, chunkSize int64) error {
+       if dstPath == "" {
+               dstPath = filepath.Base(srcPath)
+       }
+       dstPath = filepath.Clean(dstPath)
+       if filepath.IsAbs(dstPath) {
+               return errors.New("Relative destination path required")
+       }
+       src, err := os.Open(srcPath)
+       if err != nil {
+               return err
+       }
+       defer src.Close()
+       srcStat, err := src.Stat()
+       if err != nil {
+               return err
+       }
+       srcReader := bufio.NewReader(src)
+       fileSize := srcStat.Size()
+       leftSize := fileSize
+       metaPkt := ChunkedMeta{
+               Magic:     MagicNNCPMv1,
+               FileSize:  uint64(fileSize),
+               ChunkSize: uint64(chunkSize),
+               Checksums: make([][32]byte, 0, (fileSize/chunkSize)+1),
+       }
+       for i := int64(0); i < (fileSize/chunkSize)+1; i++ {
+               hsh := new([32]byte)
+               metaPkt.Checksums = append(metaPkt.Checksums, *hsh)
+       }
+       var sizeToSend int64
+       var hsh hash.Hash
+       var pkt *Pkt
+       var chunkNum int
+       var path string
+       for {
+               if leftSize <= chunkSize {
+                       sizeToSend = leftSize
+               } else {
+                       sizeToSend = chunkSize
+               }
+               path = dstPath + ChunkedSuffixPart + strconv.Itoa(chunkNum)
+               pkt, err = NewPkt(PktTypeFile, path)
+               if err != nil {
+                       return err
+               }
+               hsh, err = blake2b.New256(nil)
+               if err != nil {
+                       return err
+               }
+               _, err = ctx.Tx(
+                       node,
+                       pkt,
+                       nice,
+                       sizeToSend,
+                       minSize,
+                       io.TeeReader(srcReader, hsh),
+               )
+               if err == nil {
+                       ctx.LogD("tx", SDS{
+                               "type": "file",
+                               "node": node.Id,
+                               "nice": strconv.Itoa(int(nice)),
+                               "src":  srcPath,
+                               "dst":  path,
+                               "size": strconv.FormatInt(sizeToSend, 10),
+                       }, "sent")
+               } else {
+                       ctx.LogE("tx", SDS{
+                               "type": "file",
+                               "node": node.Id,
+                               "nice": strconv.Itoa(int(nice)),
+                               "src":  srcPath,
+                               "dst":  path,
+                               "size": strconv.FormatInt(sizeToSend, 10),
+                               "err":  err,
+                       }, "sent")
+                       return err
+               }
+               hsh.Sum(metaPkt.Checksums[chunkNum][:0])
+               leftSize -= sizeToSend
+               chunkNum++
+               if leftSize == 0 {
+                       break
+               }
+       }
+       var metaBuf bytes.Buffer
+       _, err = xdr.Marshal(&metaBuf, metaPkt)
+       if err != nil {
+               return err
+       }
+       path = dstPath + ChunkedSuffixMeta
+       pkt, err = NewPkt(PktTypeFile, path)
+       if err != nil {
+               return err
+       }
+       metaPktSize := int64(metaBuf.Len())
+       _, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
+       if err == nil {
+               ctx.LogD("tx", SDS{
+                       "type": "file",
+                       "node": node.Id,
+                       "nice": strconv.Itoa(int(nice)),
+                       "src":  srcPath,
+                       "dst":  path,
+                       "size": strconv.FormatInt(metaPktSize, 10),
+               }, "sent")
+               ctx.LogI("tx", SDS{
+                       "type": "file",
+                       "node": node.Id,
+                       "nice": strconv.Itoa(int(nice)),
+                       "src":  srcPath,
+                       "dst":  dstPath,
+                       "size": strconv.FormatInt(fileSize, 10),
+               }, "sent")
+       } else {
+               ctx.LogE("tx", SDS{
+                       "type": "file",
+                       "node": node.Id,
+                       "nice": strconv.Itoa(int(nice)),
+                       "src":  srcPath,
+                       "dst":  path,
+                       "size": strconv.FormatInt(metaPktSize, 10),
+                       "err":  err,
+               }, "sent")
+       }
+       return err
+}
+
 func (ctx *Ctx) TxFreq(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
        dstPath = filepath.Clean(dstPath)
        if filepath.IsAbs(dstPath) {