added large file support

This commit is contained in:
Todd Gruben 2014-03-29 15:44:42 +00:00
parent d3a7b060be
commit 521542bbe1
2 changed files with 22 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
@ -18,7 +19,7 @@ func TestRemote(t *testing.T) {
err = ssh.Launch("sleep 10 && date", "background")
So(err, ShouldEqual, nil)
content, _ := ssh.Run("date", false)
ssh.CopyTo([]byte(content), "uploadfile")
ssh.CopyTo(strings.NewReader(content), "uploadfile")
results, _ := ssh.CopyFrom("uploadfile")
ok := bytes.Equal([]byte(content), results)
So(ok, ShouldEqual, true)

View file

@ -85,7 +85,7 @@ func (self *SSH) Run(command string, sudo bool) (string, error) {
return b.String(), nil
}
func (self *SSH) CopyTo(content []byte, dest_name string) error {
func (self *SSH) CopyTo(content io.Reader, dest_name string) error {
session, err := self.client.NewSession()
defer session.Close()
@ -93,7 +93,25 @@ func (self *SSH) CopyTo(content []byte, dest_name string) error {
go func() {
w, _ := session.StdinPipe()
defer w.Close()
w.Write(content)
// w.Write(content)
buf := make([]byte, 4096)
for {
// read a chunk
n, err := content.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}
// write a chunk
if _, err := w.Write(buf[:n]); err != nil {
panic(err)
}
}
}()
err = session.Run(fmt.Sprintf("/bin/cat >%s", dest_name))
return err