From d529dd1ff29a8dc0a2d22a75e044e9162a0afb78 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Fri, 22 Nov 2019 22:40:50 +0300 Subject: [PATCH] Replace TxFile with TxFileChunked If chunk size is really huge, then no chunked data transmission will occur, behaving exactly as TxFile. --- src/cfg.go | 2 +- src/cmd/nncp-file/main.go | 27 ++++++-------------- src/node.go | 9 ++++--- src/toss.go | 26 ++++++-------------- src/toss_test.go | 2 ++ src/tx.go | 52 +++------------------------------------ 6 files changed, 28 insertions(+), 90 deletions(-) diff --git a/src/cfg.go b/src/cfg.go index ca8099e..ab8ec7f 100644 --- a/src/cfg.go +++ b/src/cfg.go @@ -155,7 +155,7 @@ func NewNode(name string, yml NodeJSON) (*Node, error) { } var freqPath *string - var freqChunked int64 + freqChunked := int64(MaxFileSize) var freqMinSize int64 if yml.Freq != nil { f := yml.Freq diff --git a/src/cmd/nncp-file/main.go b/src/cmd/nncp-file/main.go index 8274d0e..8e1b7f8 100644 --- a/src/cmd/nncp-file/main.go +++ b/src/cmd/nncp-file/main.go @@ -109,25 +109,14 @@ func main() { chunkSize = *argChunkSize * 1024 } - if chunkSize == 0 { - err = ctx.TxFile( - node, - nice, - flag.Arg(0), - splitted[1], - minSize, - ) - } else { - err = ctx.TxFileChunked( - node, - nice, - flag.Arg(0), - splitted[1], - minSize, - chunkSize, - ) - } - if err != nil { + if err = ctx.TxFile( + node, + nice, + flag.Arg(0), + splitted[1], + chunkSize, + minSize, + ); err != nil { log.Fatalln(err) } } diff --git a/src/node.go b/src/node.go index 165cd86..dd2442b 100644 --- a/src/node.go +++ b/src/node.go @@ -100,10 +100,11 @@ func NewNodeGenerate() (*NodeOur, error) { func (nodeOur *NodeOur) Their() *Node { return &Node{ - Name: "self", - Id: nodeOur.Id, - ExchPub: nodeOur.ExchPub, - SignPub: nodeOur.SignPub, + Name: "self", + Id: nodeOur.Id, + ExchPub: nodeOur.ExchPub, + SignPub: nodeOur.SignPub, + FreqChunked: MaxFileSize, } } diff --git a/src/toss.go b/src/toss.go index 5652c68..eaf7401 100644 --- a/src/toss.go +++ b/src/toss.go @@ -287,24 +287,14 @@ func (ctx *Ctx) Toss( goto Closing } if !dryRun { - if sender.FreqChunked == 0 { - err = ctx.TxFile( - sender, - pkt.Nice, - filepath.Join(*freqPath, src), - dst, - sender.FreqMinSize, - ) - } else { - err = ctx.TxFileChunked( - sender, - pkt.Nice, - filepath.Join(*freqPath, src), - dst, - sender.FreqMinSize, - sender.FreqChunked, - ) - } + err = ctx.TxFile( + sender, + pkt.Nice, + filepath.Join(*freqPath, src), + dst, + sender.FreqChunked, + sender.FreqMinSize, + ) if err != nil { ctx.LogE("rx", SdsAdd(sds, SDS{"err": err}), "tx file") isBad = true diff --git a/src/toss_test.go b/src/toss_test.go index 4bb5980..d11c589 100644 --- a/src/toss_test.go +++ b/src/toss_test.go @@ -199,6 +199,7 @@ func TestTossFile(t *testing.T) { DefaultNiceFile, src, fileName, + MaxFileSize, 1<<15, ); err != nil { t.Error(err) @@ -273,6 +274,7 @@ func TestTossFileSameName(t *testing.T) { DefaultNiceFile, srcPath, "samefile", + MaxFileSize, 1<<15, ); err != nil { t.Error(err) diff --git a/src/tx.go b/src/tx.go index 4b7cb34..5c5c446 100644 --- a/src/tx.go +++ b/src/tx.go @@ -39,6 +39,8 @@ import ( ) const ( + MaxFileSize = 1 << 62 + TarBlockSize = 512 TarExt = ".tar" ) @@ -279,58 +281,12 @@ func prepareTxFile(srcPath string) (reader io.Reader, closer io.Closer, fileSize return } -func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error { - dstPathSpecified := false - if dstPath == "" { - if srcPath == "-" { - return errors.New("Must provide destination filename") - } - dstPath = filepath.Base(srcPath) - } else { - dstPathSpecified = true - } - dstPath = filepath.Clean(dstPath) - if filepath.IsAbs(dstPath) { - return errors.New("Relative destination path required") - } - reader, closer, fileSize, archived, err := prepareTxFile(srcPath) - if closer != nil { - defer closer.Close() - } - if err != nil { - return err - } - if archived && !dstPathSpecified { - dstPath += TarExt - } - pkt, err := NewPkt(PktTypeFile, nice, []byte(dstPath)) - if err != nil { - return err - } - _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader) - sds := SDS{ - "type": "file", - "node": node.Id, - "nice": strconv.Itoa(int(nice)), - "src": srcPath, - "dst": dstPath, - "size": strconv.FormatInt(fileSize, 10), - } - if err == nil { - ctx.LogI("tx", sds, "sent") - } else { - sds["err"] = err - ctx.LogE("tx", sds, "sent") - } - return err -} - -func (ctx *Ctx) TxFileChunked( +func (ctx *Ctx) TxFile( node *Node, nice uint8, srcPath, dstPath string, - minSize int64, chunkSize int64, + minSize int64, ) error { dstPathSpecified := false if dstPath == "" { -- 2.44.0