2015-03-28 10:50:23 +00:00
|
|
|
package sftp
|
2014-10-04 17:20:15 +00:00
|
|
|
|
|
|
|
import (
|
2016-08-28 17:53:18 +00:00
|
|
|
"bufio"
|
2014-10-04 17:20:15 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-08-11 17:10:51 +00:00
|
|
|
"path"
|
2015-11-02 13:53:34 +00:00
|
|
|
"strings"
|
2016-08-28 17:17:17 +00:00
|
|
|
"time"
|
2014-10-04 17:20:15 +00:00
|
|
|
|
2016-08-21 15:46:23 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/backend"
|
|
|
|
"restic/debug"
|
2016-08-11 17:10:51 +00:00
|
|
|
|
|
|
|
"github.com/pkg/sftp"
|
2014-10-04 17:20:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tempfileRandomSuffixLength = 10
|
|
|
|
)
|
|
|
|
|
2016-01-24 19:23:50 +00:00
|
|
|
// SFTP is a backend in a directory accessed via SFTP.
|
2014-10-04 17:20:15 +00:00
|
|
|
type SFTP struct {
|
2015-05-03 14:43:27 +00:00
|
|
|
c *sftp.Client
|
|
|
|
p string
|
2014-10-04 17:20:15 +00:00
|
|
|
|
2016-08-28 17:17:17 +00:00
|
|
|
cmd *exec.Cmd
|
|
|
|
result <-chan error
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:39:32 +00:00
|
|
|
func startClient(program string, args ...string) (*SFTP, error) {
|
2014-10-04 17:20:15 +00:00
|
|
|
// Connect to a remote host and request the sftp subsystem via the 'ssh'
|
|
|
|
// command. This assumes that passwordless login is correctly configured.
|
|
|
|
cmd := exec.Command(program, args...)
|
|
|
|
|
2016-08-28 17:53:18 +00:00
|
|
|
// prefix the errors with the program name
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sc := bufio.NewScanner(stderr)
|
|
|
|
for sc.Scan() {
|
|
|
|
fmt.Fprintf(os.Stderr, "subprocess %v: %v\n", program, sc.Text())
|
|
|
|
}
|
|
|
|
}()
|
2014-10-04 17:20:15 +00:00
|
|
|
|
2015-07-05 09:06:28 +00:00
|
|
|
// ignore signals sent to the parent (e.g. SIGINT)
|
2015-08-16 10:51:01 +00:00
|
|
|
cmd.SysProcAttr = ignoreSigIntProcAttr()
|
2015-07-05 09:06:28 +00:00
|
|
|
|
2014-10-04 17:20:15 +00:00
|
|
|
// get stdin and stdout
|
|
|
|
wr, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
2016-08-28 10:34:53 +00:00
|
|
|
return nil, err
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
rd, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2016-08-28 10:34:53 +00:00
|
|
|
return nil, err
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start the process
|
|
|
|
if err := cmd.Start(); err != nil {
|
2016-08-28 10:34:53 +00:00
|
|
|
return nil, err
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 17:17:17 +00:00
|
|
|
// wait in a different goroutine
|
|
|
|
ch := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
err := cmd.Wait()
|
|
|
|
debug.Log("sftp.Wait", "ssh command exited, err %v", err)
|
|
|
|
ch <- err
|
|
|
|
}()
|
|
|
|
|
2014-10-04 17:20:15 +00:00
|
|
|
// open the SFTP session
|
|
|
|
client, err := sftp.NewClientPipe(rd, wr)
|
|
|
|
if err != nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
return nil, errors.Errorf("unable to start the sftp session, error: %v", err)
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 17:17:17 +00:00
|
|
|
return &SFTP{c: client, cmd: cmd, result: ch}, nil
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 21:16:17 +00:00
|
|
|
func paths(dir string) []string {
|
|
|
|
return []string{
|
|
|
|
dir,
|
|
|
|
Join(dir, backend.Paths.Data),
|
|
|
|
Join(dir, backend.Paths.Snapshots),
|
|
|
|
Join(dir, backend.Paths.Index),
|
|
|
|
Join(dir, backend.Paths.Locks),
|
|
|
|
Join(dir, backend.Paths.Keys),
|
|
|
|
Join(dir, backend.Paths.Temp),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-28 17:17:17 +00:00
|
|
|
// clientError returns an error if the client has exited. Otherwise, nil is
|
|
|
|
// returned immediately.
|
|
|
|
func (r *SFTP) clientError() error {
|
|
|
|
select {
|
|
|
|
case err := <-r.result:
|
|
|
|
debug.Log("sftp.clientError", "client has exited with err %v", err)
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Open opens an sftp backend. When the command is started via
|
2014-10-07 21:19:26 +00:00
|
|
|
// exec.Command, it is expected to speak sftp on stdin/stdout. The backend
|
2016-08-11 17:41:47 +00:00
|
|
|
// is expected at the given path. `dir` must be delimited by forward slashes
|
|
|
|
// ("/"), which is required by sftp.
|
2015-03-28 10:50:23 +00:00
|
|
|
func Open(dir string, program string, args ...string) (*SFTP, error) {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.Open", "open backend with program %v, %v at %v", program, args, dir)
|
2015-04-24 23:39:32 +00:00
|
|
|
sftp, err := startClient(program, args...)
|
2014-10-04 17:20:15 +00:00
|
|
|
if err != nil {
|
2016-08-28 17:53:18 +00:00
|
|
|
debug.Log("sftp.Open", "unable to start program: %v", err)
|
2014-10-04 17:20:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// test if all necessary dirs and files are there
|
2016-01-26 21:16:17 +00:00
|
|
|
for _, d := range paths(dir) {
|
2014-10-04 17:20:15 +00:00
|
|
|
if _, err := sftp.c.Lstat(d); err != nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
return nil, errors.Errorf("%s does not exist", d)
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sftp.p = dir
|
|
|
|
return sftp, nil
|
|
|
|
}
|
|
|
|
|
2015-12-28 17:22:19 +00:00
|
|
|
func buildSSHCommand(cfg Config) []string {
|
2016-02-15 18:17:41 +00:00
|
|
|
hostport := strings.Split(cfg.Host, ":")
|
|
|
|
args := []string{hostport[0]}
|
|
|
|
if len(hostport) > 1 {
|
|
|
|
args = append(args, "-p", hostport[1])
|
|
|
|
}
|
2015-12-28 17:22:19 +00:00
|
|
|
if cfg.User != "" {
|
|
|
|
args = append(args, "-l")
|
|
|
|
args = append(args, cfg.User)
|
|
|
|
}
|
|
|
|
args = append(args, "-s")
|
|
|
|
args = append(args, "sftp")
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenWithConfig opens an sftp backend as described by the config by running
|
|
|
|
// "ssh" with the appropiate arguments.
|
|
|
|
func OpenWithConfig(cfg Config) (*SFTP, error) {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.OpenWithConfig", "open with config %v", cfg)
|
2015-12-28 17:22:19 +00:00
|
|
|
return Open(cfg.Dir, "ssh", buildSSHCommand(cfg)...)
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Create creates all the necessary files and directories for a new sftp
|
2016-08-11 17:41:47 +00:00
|
|
|
// backend at dir. Afterwards a new config blob should be created. `dir` must
|
|
|
|
// be delimited by forward slashes ("/"), which is required by sftp.
|
2015-03-28 10:50:23 +00:00
|
|
|
func Create(dir string, program string, args ...string) (*SFTP, error) {
|
2016-08-28 17:17:17 +00:00
|
|
|
debug.Log("sftp.Create", "%v %v", program, args)
|
2015-04-24 23:39:32 +00:00
|
|
|
sftp, err := startClient(program, args...)
|
2014-10-04 17:20:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-04 18:39:45 +00:00
|
|
|
// test if config file already exists
|
2015-11-03 17:47:01 +00:00
|
|
|
_, err = sftp.c.Lstat(Join(dir, backend.Paths.Config))
|
2014-10-04 17:20:15 +00:00
|
|
|
if err == nil {
|
2015-05-03 14:43:27 +00:00
|
|
|
return nil, errors.New("config file already exists")
|
2015-03-14 10:56:45 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 12:22:19 +00:00
|
|
|
// create paths for data, refs and temp blobs
|
2016-01-26 21:16:17 +00:00
|
|
|
for _, d := range paths(dir) {
|
2015-03-28 10:50:23 +00:00
|
|
|
err = sftp.mkdirAll(d, backend.Modes.Dir)
|
2014-10-04 17:20:15 +00:00
|
|
|
if err != nil {
|
2014-11-30 14:50:30 +00:00
|
|
|
return nil, err
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-28 17:17:17 +00:00
|
|
|
err = sftp.Close()
|
2014-10-04 17:20:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:19:26 +00:00
|
|
|
// open backend
|
2015-03-28 10:50:23 +00:00
|
|
|
return Open(dir, program, args...)
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 17:22:19 +00:00
|
|
|
// CreateWithConfig creates an sftp backend as described by the config by running
|
|
|
|
// "ssh" with the appropiate arguments.
|
|
|
|
func CreateWithConfig(cfg Config) (*SFTP, error) {
|
2016-08-28 17:17:17 +00:00
|
|
|
debug.Log("sftp.CreateWithConfig", "config %v", cfg)
|
2015-12-28 17:22:19 +00:00
|
|
|
return Create(cfg.Dir, "ssh", buildSSHCommand(cfg)...)
|
|
|
|
}
|
|
|
|
|
2014-10-04 17:20:15 +00:00
|
|
|
// Location returns this backend's location (the directory name).
|
|
|
|
func (r *SFTP) Location() string {
|
|
|
|
return r.p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return temp directory in correct directory for this backend.
|
|
|
|
func (r *SFTP) tempFile() (string, *sftp.File, error) {
|
|
|
|
// choose random suffix
|
|
|
|
buf := make([]byte, tempfileRandomSuffixLength)
|
2015-05-03 19:06:20 +00:00
|
|
|
_, err := io.ReadFull(rand.Reader, buf)
|
2014-10-04 17:20:15 +00:00
|
|
|
if err != nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
return "", nil, errors.Errorf("unable to read %d random bytes for tempfile name: %v",
|
2016-08-21 15:24:13 +00:00
|
|
|
tempfileRandomSuffixLength, err)
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// construct tempfile name
|
2015-11-02 13:53:34 +00:00
|
|
|
name := Join(r.p, backend.Paths.Temp, "temp-"+hex.EncodeToString(buf))
|
2014-10-04 17:20:15 +00:00
|
|
|
|
|
|
|
// create file in temp dir
|
|
|
|
f, err := r.c.Create(name)
|
|
|
|
if err != nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
return "", nil, errors.Errorf("creating tempfile %q failed: %v", name, err)
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return name, f, nil
|
|
|
|
}
|
|
|
|
|
2014-11-30 14:50:30 +00:00
|
|
|
func (r *SFTP) mkdirAll(dir string, mode os.FileMode) error {
|
|
|
|
// check if directory already exists
|
|
|
|
fi, err := r.c.Lstat(dir)
|
|
|
|
if err == nil {
|
|
|
|
if fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("mkdirAll(%s): entry exists but is not a directory", dir)
|
2014-11-30 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create parent directories
|
2016-08-11 17:41:47 +00:00
|
|
|
errMkdirAll := r.mkdirAll(path.Dir(dir), backend.Modes.Dir)
|
2014-11-30 14:50:30 +00:00
|
|
|
|
|
|
|
// create directory
|
|
|
|
errMkdir := r.c.Mkdir(dir)
|
|
|
|
|
|
|
|
// test if directory was created successfully
|
|
|
|
fi, err = r.c.Lstat(dir)
|
|
|
|
if err != nil {
|
|
|
|
// return previous errors
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("mkdirAll(%s): unable to create directories: %v, %v", dir, errMkdirAll, errMkdir)
|
2014-11-30 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("mkdirAll(%s): entry exists but is not a directory", dir)
|
2014-11-30 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set mode
|
|
|
|
return r.c.Chmod(dir, mode)
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Rename temp file to final name according to type and name.
|
|
|
|
func (r *SFTP) renameFile(oldname string, t backend.Type, name string) error {
|
|
|
|
filename := r.filename(t, name)
|
2014-11-30 14:50:30 +00:00
|
|
|
|
|
|
|
// create directories if necessary
|
2015-04-29 20:30:00 +00:00
|
|
|
if t == backend.Data {
|
2016-08-11 17:41:47 +00:00
|
|
|
err := r.mkdirAll(path.Dir(filename), backend.Modes.Dir)
|
2014-11-30 14:50:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// test if new file exists
|
|
|
|
if _, err := r.c.Lstat(filename); err == nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("Close(): file %v already exists", filename)
|
2015-03-28 10:50:23 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 13:29:32 +00:00
|
|
|
err := r.c.Rename(oldname, filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set mode to read-only
|
|
|
|
fi, err := r.c.Lstat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:19:52 +00:00
|
|
|
return r.c.Chmod(filename, fi.Mode()&os.FileMode(^uint32(0222)))
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 17:10:51 +00:00
|
|
|
// Join joins the given paths and cleans them afterwards. This always uses
|
|
|
|
// forward slashes, which is required by sftp.
|
2015-11-02 13:53:34 +00:00
|
|
|
func Join(parts ...string) string {
|
2016-08-11 17:10:51 +00:00
|
|
|
return path.Clean(path.Join(parts...))
|
2015-11-02 13:53:34 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Construct path for given backend.Type and name.
|
|
|
|
func (r *SFTP) filename(t backend.Type, name string) string {
|
2015-05-03 14:43:27 +00:00
|
|
|
if t == backend.Config {
|
2015-11-03 17:47:01 +00:00
|
|
|
return Join(r.p, "config")
|
2015-05-03 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 13:53:34 +00:00
|
|
|
return Join(r.dirname(t, name), name)
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Construct directory for given backend.Type.
|
|
|
|
func (r *SFTP) dirname(t backend.Type, name string) string {
|
|
|
|
var n string
|
|
|
|
switch t {
|
|
|
|
case backend.Data:
|
|
|
|
n = backend.Paths.Data
|
|
|
|
if len(name) > 2 {
|
2015-11-02 13:53:34 +00:00
|
|
|
n = Join(n, name[:2])
|
2015-02-11 19:17:55 +00:00
|
|
|
}
|
2015-03-28 10:50:23 +00:00
|
|
|
case backend.Snapshot:
|
|
|
|
n = backend.Paths.Snapshots
|
2015-04-26 13:48:35 +00:00
|
|
|
case backend.Index:
|
|
|
|
n = backend.Paths.Index
|
2015-03-28 10:50:23 +00:00
|
|
|
case backend.Lock:
|
|
|
|
n = backend.Paths.Locks
|
|
|
|
case backend.Key:
|
|
|
|
n = backend.Paths.Keys
|
2014-11-24 21:11:09 +00:00
|
|
|
}
|
2015-11-02 13:53:34 +00:00
|
|
|
return Join(r.p, n)
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 13:12:12 +00:00
|
|
|
// Load returns the data stored in the backend for h at the given offset
|
|
|
|
// and saves it in p. Load has the same semantics as io.ReaderAt.
|
|
|
|
func (r *SFTP) Load(h backend.Handle, p []byte, off int64) (n int, err error) {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.Load", "load %v, %d bytes, offset %v", h, len(p), off)
|
2016-08-28 17:17:17 +00:00
|
|
|
if err := r.clientError(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2016-01-23 17:30:02 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2016-01-23 13:12:12 +00:00
|
|
|
f, err := r.c.Open(r.filename(h.Type, h.Name))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
e := f.Close()
|
|
|
|
if err == nil && e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-08-07 12:50:24 +00:00
|
|
|
switch {
|
|
|
|
case off > 0:
|
2016-01-23 13:12:12 +00:00
|
|
|
_, err = f.Seek(off, 0)
|
2016-08-07 12:50:24 +00:00
|
|
|
case off < 0:
|
|
|
|
_, err = f.Seek(off, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2016-01-23 13:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return io.ReadFull(f, p)
|
|
|
|
}
|
|
|
|
|
2016-01-24 00:15:35 +00:00
|
|
|
// Save stores data in the backend at the handle.
|
|
|
|
func (r *SFTP) Save(h backend.Handle, p []byte) (err error) {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.Save", "save %v bytes to %v", h, len(p))
|
2016-08-28 17:17:17 +00:00
|
|
|
if err := r.clientError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-24 00:15:35 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
filename, tmpfile, err := r.tempFile()
|
2016-08-28 10:34:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
debug.Log("sftp.Save", "save %v (%d bytes) to %v", h, len(p), filename)
|
|
|
|
|
|
|
|
n, err := tmpfile.Write(p)
|
2016-01-24 00:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
if n != len(p) {
|
|
|
|
return errors.New("not all bytes writen")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmpfile.Close()
|
2016-01-24 00:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
err = r.renameFile(filename, h.Type, h.Name)
|
|
|
|
debug.Log("sftp.Save", "save %v: rename %v: %v",
|
2016-08-11 17:41:47 +00:00
|
|
|
h, path.Base(filename), err)
|
2016-01-24 15:59:38 +00:00
|
|
|
if err != nil {
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("sftp: renameFile: %v", err)
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 15:59:38 +00:00
|
|
|
return nil
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 22:27:58 +00:00
|
|
|
// Stat returns information about a blob.
|
|
|
|
func (r *SFTP) Stat(h backend.Handle) (backend.BlobInfo, error) {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.Stat", "stat %v", h)
|
2016-08-28 17:17:17 +00:00
|
|
|
if err := r.clientError(); err != nil {
|
|
|
|
return backend.BlobInfo{}, err
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:27:58 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return backend.BlobInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := r.c.Lstat(r.filename(h.Type, h.Name))
|
|
|
|
if err != nil {
|
|
|
|
return backend.BlobInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend.BlobInfo{Size: fi.Size()}, nil
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Test returns true if a blob of the given type and name exists in the backend.
|
|
|
|
func (r *SFTP) Test(t backend.Type, name string) (bool, error) {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.Test", "type %v, name %v", t, name)
|
2016-08-28 17:17:17 +00:00
|
|
|
if err := r.clientError(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
_, err := r.c.Lstat(r.filename(t, name))
|
2016-08-29 17:18:57 +00:00
|
|
|
if os.IsNotExist(errors.Cause(err)) {
|
2016-02-13 18:11:41 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2014-11-30 14:50:30 +00:00
|
|
|
|
2016-02-13 18:11:41 +00:00
|
|
|
if err != nil {
|
2014-11-30 14:50:30 +00:00
|
|
|
return false, err
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
2014-11-30 14:50:30 +00:00
|
|
|
|
|
|
|
return true, nil
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// Remove removes the content stored at name.
|
|
|
|
func (r *SFTP) Remove(t backend.Type, name string) error {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.Remove", "type %v, name %v", t, name)
|
2016-08-28 17:17:17 +00:00
|
|
|
if err := r.clientError(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
return r.c.Remove(r.filename(t, name))
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// List returns a channel that yields all names of blobs of type t. A
|
2015-06-28 07:44:06 +00:00
|
|
|
// goroutine is started for this. If the channel done is closed, sending
|
2015-03-28 10:50:23 +00:00
|
|
|
// stops.
|
|
|
|
func (r *SFTP) List(t backend.Type, done <-chan struct{}) <-chan string {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.List", "list all %v", t)
|
2015-03-28 10:50:23 +00:00
|
|
|
ch := make(chan string)
|
2014-11-30 14:50:30 +00:00
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
2014-11-30 14:50:30 +00:00
|
|
|
|
2015-04-29 20:30:00 +00:00
|
|
|
if t == backend.Data {
|
2015-03-28 10:50:23 +00:00
|
|
|
// read first level
|
|
|
|
basedir := r.dirname(t, "")
|
|
|
|
|
|
|
|
list1, err := r.c.ReadDir(basedir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dirs := make([]string, 0, len(list1))
|
|
|
|
for _, d := range list1 {
|
|
|
|
dirs = append(dirs, d.Name())
|
|
|
|
}
|
2014-11-30 14:50:30 +00:00
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// read files
|
|
|
|
for _, dir := range dirs {
|
2015-11-02 13:53:34 +00:00
|
|
|
entries, err := r.c.ReadDir(Join(basedir, dir))
|
2015-03-28 10:50:23 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
items := make([]string, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
|
|
|
items = append(items, entry.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range items {
|
|
|
|
select {
|
|
|
|
case ch <- file:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entries, err := r.c.ReadDir(r.dirname(t, ""))
|
2014-11-30 14:50:30 +00:00
|
|
|
if err != nil {
|
2015-03-28 10:50:23 +00:00
|
|
|
return
|
2014-11-30 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
items := make([]string, 0, len(entries))
|
2014-11-30 23:52:12 +00:00
|
|
|
for _, entry := range entries {
|
2015-03-28 10:50:23 +00:00
|
|
|
items = append(items, entry.Name())
|
2014-11-30 14:50:30 +00:00
|
|
|
}
|
2014-10-04 17:20:15 +00:00
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
for _, file := range items {
|
|
|
|
select {
|
|
|
|
case ch <- file:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|
2015-03-28 10:50:23 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
2014-10-04 17:20:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-28 17:17:17 +00:00
|
|
|
var closeTimeout = 2 * time.Second
|
|
|
|
|
2014-10-04 17:20:15 +00:00
|
|
|
// Close closes the sftp connection and terminates the underlying command.
|
2016-01-24 19:23:50 +00:00
|
|
|
func (r *SFTP) Close() error {
|
2016-08-28 17:47:12 +00:00
|
|
|
debug.Log("sftp.Close", "")
|
2016-01-24 19:23:50 +00:00
|
|
|
if r == nil {
|
2015-05-01 15:13:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:16:17 +00:00
|
|
|
err := r.c.Close()
|
|
|
|
debug.Log("sftp.Close", "Close returned error %v", err)
|
2015-07-05 09:06:28 +00:00
|
|
|
|
2016-08-28 17:17:17 +00:00
|
|
|
// wait for closeTimeout before killing the process
|
|
|
|
select {
|
|
|
|
case err := <-r.result:
|
|
|
|
return err
|
|
|
|
case <-time.After(closeTimeout):
|
|
|
|
}
|
|
|
|
|
2016-01-24 19:23:50 +00:00
|
|
|
if err := r.cmd.Process.Kill(); err != nil {
|
2015-07-05 09:06:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-28 17:17:17 +00:00
|
|
|
// get the error, but ignore it
|
|
|
|
<-r.result
|
|
|
|
return nil
|
2014-10-04 17:20:15 +00:00
|
|
|
}
|