1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
// Package scp provides a simple interface to copying files over a
// go.crypto/ssh session.
package scp
import (
"fmt"
"io"
"os"
"path"
shellquote "github.com/kballard/go-shellquote"
"golang.org/x/crypto/ssh"
)
func Copy(size int64, mode os.FileMode, fileName string, contents io.Reader, destinationPath string, session *ssh.Session) error {
return copy(size, mode, fileName, contents, destinationPath, session)
}
func CopyPath(filePath, destinationPath string, session *ssh.Session) error {
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()
s, err := f.Stat()
if err != nil {
return err
}
return copy(s.Size(), s.Mode().Perm(), path.Base(filePath), f, destinationPath, session)
}
func copy(size int64, mode os.FileMode, fileName string, contents io.Reader, destination string, session *ssh.Session) error {
defer session.Close()
w, err := session.StdinPipe()
if err != nil {
return err
}
cmd := shellquote.Join("scp", "-t", destination)
if err := session.Start(cmd); err != nil {
w.Close()
return err
}
errors := make(chan error)
go func() {
errors <- session.Wait()
}()
fmt.Fprintf(w, "C%#o %d %s\n", mode, size, fileName)
io.Copy(w, contents)
fmt.Fprint(w, "\x00")
w.Close()
return <-errors
}
|