]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/tx.go
Replace TxFile with TxFileChunked
[nncp.git] / src / tx.go
index 88d4e9af4b4dfd9fe3f570fbca88b6d65296a951..5c5c446171122b62620a1b246b7477e2fa16d5ff 100644 (file)
--- a/src/tx.go
+++ b/src/tx.go
@@ -39,6 +39,8 @@ import (
 )
 
 const (
+       MaxFileSize = 1 << 62
+
        TarBlockSize = 512
        TarExt       = ".tar"
 )
@@ -50,10 +52,6 @@ func (ctx *Ctx) Tx(
        size, minSize int64,
        src io.Reader,
 ) (*Node, error) {
-       tmp, err := ctx.NewTmpFileWHash()
-       if err != nil {
-               return nil, err
-       }
        hops := make([]*Node, 0, 1+len(node.Via))
        hops = append(hops, node)
        lastNode := node
@@ -69,6 +67,14 @@ func (ctx *Ctx) Tx(
        if padSize < 0 {
                padSize = 0
        }
+       if !ctx.IsEnoughSpace(size + padSize) {
+               return nil, errors.New("is not enough space")
+       }
+       tmp, err := ctx.NewTmpFileWHash()
+       if err != nil {
+               return nil, err
+       }
+
        errs := make(chan error)
        curSize := size
        pipeR, pipeW := io.Pipe()
@@ -275,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 == "" {
@@ -559,6 +519,11 @@ func (ctx *Ctx) TxTrns(node *Node, nice uint8, size int64, src io.Reader) error
                "size": strconv.FormatInt(size, 10),
        }
        ctx.LogD("tx", sds, "taken")
+       if !ctx.IsEnoughSpace(size) {
+               err := errors.New("is not enough space")
+               ctx.LogE("tx", sds, err.Error())
+               return err
+       }
        tmp, err := ctx.NewTmpFileWHash()
        if err != nil {
                return err