"bufio"
"bytes"
"compress/zlib"
+ "crypto/rand"
"errors"
"hash"
"io"
+ "io/ioutil"
"os"
"path/filepath"
"strconv"
return lastNode, err
}
+func prepareTxFile(srcPath string) (io.Reader, *os.File, int64, error) {
+ var reader io.Reader
+ var src *os.File
+ var fileSize int64
+ var err error
+ if srcPath == "-" {
+ src, err = ioutil.TempFile("", "nncp-file")
+ if err != nil {
+ return nil, nil, 0, err
+ }
+ os.Remove(src.Name())
+ tmpW := bufio.NewWriter(src)
+ tmpKey := new([32]byte)
+ if _, err = rand.Read(tmpKey[:]); err != nil {
+ return nil, nil, 0, err
+ }
+ written, err := ae(tmpKey, bufio.NewReader(os.Stdin), tmpW)
+ if err != nil {
+ return nil, nil, 0, err
+ }
+ fileSize = int64(written)
+ tmpW.Flush()
+ src.Seek(0, 0)
+ r, w := io.Pipe()
+ go ae(tmpKey, bufio.NewReader(src), w)
+ reader = r
+ } else {
+ src, err = os.Open(srcPath)
+ if err != nil {
+ return nil, nil, 0, err
+ }
+ srcStat, err := src.Stat()
+ if err != nil {
+ return nil, nil, 0, err
+ }
+ fileSize = srcStat.Size()
+ reader = bufio.NewReader(src)
+ }
+ return reader, src, fileSize, nil
+}
+
func (ctx *Ctx) TxFile(node *Node, nice uint8, srcPath, dstPath string, minSize int64) error {
if dstPath == "" {
+ if srcPath == "-" {
+ return errors.New("Must provide destination filename")
+ }
dstPath = filepath.Base(srcPath)
}
dstPath = filepath.Clean(dstPath)
if err != nil {
return err
}
- src, err := os.Open(srcPath)
- if err != nil {
- return err
+ reader, src, fileSize, err := prepareTxFile(srcPath)
+ if src != nil {
+ defer src.Close()
}
- defer src.Close()
- srcStat, err := src.Stat()
if err != nil {
return err
}
- _, err = ctx.Tx(node, pkt, nice, srcStat.Size(), minSize, bufio.NewReader(src))
+ _, err = ctx.Tx(node, pkt, nice, fileSize, minSize, reader)
if err == nil {
ctx.LogI("tx", SDS{
"type": "file",
"nice": strconv.Itoa(int(nice)),
"src": srcPath,
"dst": dstPath,
- "size": strconv.FormatInt(srcStat.Size(), 10),
+ "size": strconv.FormatInt(fileSize, 10),
}, "sent")
} else {
ctx.LogE("tx", SDS{
"nice": strconv.Itoa(int(nice)),
"src": srcPath,
"dst": dstPath,
- "size": strconv.FormatInt(srcStat.Size(), 10),
+ "size": strconv.FormatInt(fileSize, 10),
"err": err,
}, "sent")
}
func (ctx *Ctx) TxFileChunked(node *Node, nice uint8, srcPath, dstPath string, minSize int64, chunkSize int64) error {
if dstPath == "" {
+ if srcPath == "-" {
+ return errors.New("Must provide destination filename")
+ }
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
+ reader, src, fileSize, err := prepareTxFile(srcPath)
+ if src != nil {
+ defer src.Close()
}
- 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,
nice,
sizeToSend,
minSize,
- io.TeeReader(srcReader, hsh),
+ io.TeeReader(reader, hsh),
)
if err == nil {
- ctx.LogD("tx", SDS{
+ ctx.LogI("tx", SDS{
"type": "file",
"node": node.Id,
"nice": strconv.Itoa(int(nice)),
metaPktSize := int64(metaBuf.Len())
_, err = ctx.Tx(node, pkt, nice, metaPktSize, minSize, &metaBuf)
if err == nil {
- ctx.LogD("tx", SDS{
+ ctx.LogI("tx", SDS{
"type": "file",
"node": node.Id,
"nice": strconv.Itoa(int(nice)),
"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",