diff --git a/src/restic/backend/sftp/config.go b/src/restic/backend/sftp/config.go index d48525344..e6dfd0d27 100644 --- a/src/restic/backend/sftp/config.go +++ b/src/restic/backend/sftp/config.go @@ -2,6 +2,7 @@ package sftp import ( "errors" + "fmt" "net/url" "path" "strings" @@ -31,7 +32,12 @@ func ParseConfig(s string) (interface{}, error) { user = url.User.Username() } host = url.Host - dir = url.Path[1:] + dir = url.Path + if dir == "" { + return nil, fmt.Errorf("invalid backend %q, no directory specified", s) + } + + dir = dir[1:] case strings.HasPrefix(s, "sftp:"): // parse the sftp:user@host:path format, which means we'll get // "user@host:path" in s diff --git a/src/restic/backend/sftp/config_test.go b/src/restic/backend/sftp/config_test.go index 54ff3b91b..d0350745c 100644 --- a/src/restic/backend/sftp/config_test.go +++ b/src/restic/backend/sftp/config_test.go @@ -74,3 +74,17 @@ func TestParseConfig(t *testing.T) { } } } + +var configTestsInvalid = []string{ + "sftp://host:dir", +} + +func TestParseConfigInvalid(t *testing.T) { + for i, test := range configTestsInvalid { + _, err := ParseConfig(test) + if err == nil { + t.Errorf("test %d: invalid config %s did not return an error", i, test) + continue + } + } +} diff --git a/src/restic/backend/sftp/sftp.go b/src/restic/backend/sftp/sftp.go index fb08294bc..0a79d4c6b 100644 --- a/src/restic/backend/sftp/sftp.go +++ b/src/restic/backend/sftp/sftp.go @@ -1,6 +1,7 @@ package sftp import ( + "bufio" "crypto/rand" "encoding/hex" "fmt" @@ -9,6 +10,7 @@ import ( "os/exec" "path" "strings" + "time" "restic/backend" "restic/debug" @@ -26,7 +28,8 @@ type SFTP struct { c *sftp.Client p string - cmd *exec.Cmd + cmd *exec.Cmd + result <-chan error } func startClient(program string, args ...string) (*SFTP, error) { @@ -34,8 +37,18 @@ func startClient(program string, args ...string) (*SFTP, error) { // command. This assumes that passwordless login is correctly configured. cmd := exec.Command(program, args...) - // send errors from ssh to stderr - cmd.Stderr = os.Stderr + // 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()) + } + }() // ignore signals sent to the parent (e.g. SIGINT) cmd.SysProcAttr = ignoreSigIntProcAttr() @@ -55,13 +68,21 @@ func startClient(program string, args ...string) (*SFTP, error) { return nil, err } + // 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 + }() + // open the SFTP session client, err := sftp.NewClientPipe(rd, wr) if err != nil { - return nil, err + return nil, fmt.Errorf("unable to start the sftp session, error: %v", err) } - return &SFTP{c: client, cmd: cmd}, nil + return &SFTP{c: client, cmd: cmd, result: ch}, nil } func paths(dir string) []string { @@ -76,13 +97,28 @@ func paths(dir string) []string { } } +// 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 +} + // Open opens an sftp backend. When the command is started via // exec.Command, it is expected to speak sftp on stdin/stdout. The backend // is expected at the given path. `dir` must be delimited by forward slashes // ("/"), which is required by sftp. func Open(dir string, program string, args ...string) (*SFTP, error) { + debug.Log("sftp.Open", "open backend with program %v, %v at %v", program, args, dir) sftp, err := startClient(program, args...) if err != nil { + debug.Log("sftp.Open", "unable to start program: %v", err) return nil, err } @@ -115,6 +151,7 @@ func buildSSHCommand(cfg Config) []string { // OpenWithConfig opens an sftp backend as described by the config by running // "ssh" with the appropiate arguments. func OpenWithConfig(cfg Config) (*SFTP, error) { + debug.Log("sftp.OpenWithConfig", "open with config %v", cfg) return Open(cfg.Dir, "ssh", buildSSHCommand(cfg)...) } @@ -122,6 +159,7 @@ func OpenWithConfig(cfg Config) (*SFTP, error) { // backend at dir. Afterwards a new config blob should be created. `dir` must // be delimited by forward slashes ("/"), which is required by sftp. func Create(dir string, program string, args ...string) (*SFTP, error) { + debug.Log("sftp.Create", "%v %v", program, args) sftp, err := startClient(program, args...) if err != nil { return nil, err @@ -141,12 +179,7 @@ func Create(dir string, program string, args ...string) (*SFTP, error) { } } - err = sftp.c.Close() - if err != nil { - return nil, err - } - - err = sftp.cmd.Wait() + err = sftp.Close() if err != nil { return nil, err } @@ -158,6 +191,7 @@ func Create(dir string, program string, args ...string) (*SFTP, error) { // CreateWithConfig creates an sftp backend as described by the config by running // "ssh" with the appropiate arguments. func CreateWithConfig(cfg Config) (*SFTP, error) { + debug.Log("sftp.CreateWithConfig", "config %v", cfg) return Create(cfg.Dir, "ssh", buildSSHCommand(cfg)...) } @@ -291,6 +325,11 @@ func (r *SFTP) dirname(t backend.Type, name string) string { // 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) { + debug.Log("sftp.Load", "load %v, %d bytes, offset %v", h, len(p), off) + if err := r.clientError(); err != nil { + return 0, err + } + if err := h.Valid(); err != nil { return 0, err } @@ -323,6 +362,11 @@ func (r *SFTP) Load(h backend.Handle, p []byte, off int64) (n int, err error) { // Save stores data in the backend at the handle. func (r *SFTP) Save(h backend.Handle, p []byte) (err error) { + debug.Log("sftp.Save", "save %v bytes to %v", h, len(p)) + if err := r.clientError(); err != nil { + return err + } + if err := h.Valid(); err != nil { return err } @@ -360,6 +404,11 @@ func (r *SFTP) Save(h backend.Handle, p []byte) (err error) { // Stat returns information about a blob. func (r *SFTP) Stat(h backend.Handle) (backend.BlobInfo, error) { + debug.Log("sftp.Stat", "stat %v", h) + if err := r.clientError(); err != nil { + return backend.BlobInfo{}, err + } + if err := h.Valid(); err != nil { return backend.BlobInfo{}, err } @@ -374,6 +423,11 @@ func (r *SFTP) Stat(h backend.Handle) (backend.BlobInfo, error) { // 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) { + debug.Log("sftp.Test", "type %v, name %v", t, name) + if err := r.clientError(); err != nil { + return false, err + } + _, err := r.c.Lstat(r.filename(t, name)) if os.IsNotExist(err) { return false, nil @@ -388,6 +442,11 @@ func (r *SFTP) Test(t backend.Type, name string) (bool, error) { // Remove removes the content stored at name. func (r *SFTP) Remove(t backend.Type, name string) error { + debug.Log("sftp.Remove", "type %v, name %v", t, name) + if err := r.clientError(); err != nil { + return err + } + return r.c.Remove(r.filename(t, name)) } @@ -395,6 +454,7 @@ func (r *SFTP) Remove(t backend.Type, name string) error { // goroutine is started for this. If the channel done is closed, sending // stops. func (r *SFTP) List(t backend.Type, done <-chan struct{}) <-chan string { + debug.Log("sftp.List", "list all %v", t) ch := make(chan string) go func() { @@ -459,8 +519,11 @@ func (r *SFTP) List(t backend.Type, done <-chan struct{}) <-chan string { } +var closeTimeout = 2 * time.Second + // Close closes the sftp connection and terminates the underlying command. func (r *SFTP) Close() error { + debug.Log("sftp.Close", "") if r == nil { return nil } @@ -468,9 +531,18 @@ func (r *SFTP) Close() error { err := r.c.Close() debug.Log("sftp.Close", "Close returned error %v", err) + // wait for closeTimeout before killing the process + select { + case err := <-r.result: + return err + case <-time.After(closeTimeout): + } + if err := r.cmd.Process.Kill(); err != nil { return err } - return r.cmd.Wait() + // get the error, but ignore it + <-r.result + return nil } diff --git a/src/restic/backend/sftp/sftp_backend_test.go b/src/restic/backend/sftp/sftp_backend_test.go index 1d7199a88..52b50f6f2 100644 --- a/src/restic/backend/sftp/sftp_backend_test.go +++ b/src/restic/backend/sftp/sftp_backend_test.go @@ -1,7 +1,6 @@ package sftp_test import ( - "fmt" "io/ioutil" "os" "path/filepath" @@ -28,7 +27,6 @@ func createTempdir() error { return err } - fmt.Printf("created new test backend at %v\n", tempdir) tempBackendDir = tempdir return nil } @@ -50,13 +48,15 @@ func init() { return } + args := []string{"-e"} + test.CreateFn = func() (backend.Backend, error) { err := createTempdir() if err != nil { return nil, err } - return sftp.Create(tempBackendDir, sftpserver) + return sftp.Create(tempBackendDir, sftpserver, args...) } test.OpenFn = func() (backend.Backend, error) { @@ -64,7 +64,7 @@ func init() { if err != nil { return nil, err } - return sftp.Open(tempBackendDir, sftpserver) + return sftp.Open(tempBackendDir, sftpserver, args...) } test.CleanupFn = func() error { @@ -72,7 +72,6 @@ func init() { return nil } - fmt.Printf("removing test backend at %v\n", tempBackendDir) err := os.RemoveAll(tempBackendDir) tempBackendDir = "" return err diff --git a/vendor/manifest b/vendor/manifest index ebc422b1c..b0953efaa 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -37,11 +37,17 @@ "revision": "9e734013294ab153b0bdbe182738bcddd46f1947", "branch": "master" }, + { + "importpath": "github.com/pkg/errors", + "repository": "https://github.com/pkg/errors", + "revision": "17b591df37844cde689f4d5813e5cea0927d8dd2", + "branch": "master" + }, { "importpath": "github.com/pkg/sftp", "repository": "https://github.com/pkg/sftp", - "revision": "e84cc8c755ca39b7b64f510fe1fffc1b51f210a5", - "branch": "HEAD" + "revision": "a71e8f580e3b622ebff585309160b1cc549ef4d2", + "branch": "master" }, { "importpath": "github.com/restic/chunker", diff --git a/vendor/src/github.com/pkg/errors/LICENSE b/vendor/src/github.com/pkg/errors/LICENSE new file mode 100644 index 000000000..835ba3e75 --- /dev/null +++ b/vendor/src/github.com/pkg/errors/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) 2015, Dave Cheney +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/src/github.com/pkg/errors/README.md b/vendor/src/github.com/pkg/errors/README.md new file mode 100644 index 000000000..273db3c98 --- /dev/null +++ b/vendor/src/github.com/pkg/errors/README.md @@ -0,0 +1,52 @@ +# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) + +Package errors provides simple error handling primitives. + +`go get github.com/pkg/errors` + +The traditional error handling idiom in Go is roughly akin to +```go +if err != nil { + return err +} +``` +which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error. + +## Adding context to an error + +The errors.Wrap function returns a new error that adds context to the original error. For example +```go +_, err := ioutil.ReadAll(r) +if err != nil { + return errors.Wrap(err, "read failed") +} +``` +## Retrieving the cause of an error + +Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`. +```go +type causer interface { + Cause() error +} +``` +`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example: +```go +switch err := errors.Cause(err).(type) { +case *MyError: + // handle specifically +default: + // unknown error +} +``` + +[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors). + +## Contributing + +We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high. + +Before proposing a change, please discuss your change by raising an issue. + +## Licence + +BSD-2-Clause diff --git a/vendor/src/github.com/pkg/errors/appveyor.yml b/vendor/src/github.com/pkg/errors/appveyor.yml new file mode 100644 index 000000000..a932eade0 --- /dev/null +++ b/vendor/src/github.com/pkg/errors/appveyor.yml @@ -0,0 +1,32 @@ +version: build-{build}.{branch} + +clone_folder: C:\gopath\src\github.com\pkg\errors +shallow_clone: true # for startup speed + +environment: + GOPATH: C:\gopath + +platform: + - x64 + +# http://www.appveyor.com/docs/installed-software +install: + # some helpful output for debugging builds + - go version + - go env + # pre-installed MinGW at C:\MinGW is 32bit only + # but MSYS2 at C:\msys64 has mingw64 + - set PATH=C:\msys64\mingw64\bin;%PATH% + - gcc --version + - g++ --version + +build_script: + - go install -v ./... + +test_script: + - set PATH=C:\gopath\bin;%PATH% + - go test -v ./... + +#artifacts: +# - path: '%GOPATH%\bin\*.exe' +deploy: off diff --git a/vendor/src/github.com/pkg/errors/bench_test.go b/vendor/src/github.com/pkg/errors/bench_test.go new file mode 100644 index 000000000..0416a3cbb --- /dev/null +++ b/vendor/src/github.com/pkg/errors/bench_test.go @@ -0,0 +1,59 @@ +// +build go1.7 + +package errors + +import ( + "fmt" + "testing" + + stderrors "errors" +) + +func noErrors(at, depth int) error { + if at >= depth { + return stderrors.New("no error") + } + return noErrors(at+1, depth) +} +func yesErrors(at, depth int) error { + if at >= depth { + return New("ye error") + } + return yesErrors(at+1, depth) +} + +func BenchmarkErrors(b *testing.B) { + var toperr error + type run struct { + stack int + std bool + } + runs := []run{ + {10, false}, + {10, true}, + {100, false}, + {100, true}, + {1000, false}, + {1000, true}, + } + for _, r := range runs { + part := "pkg/errors" + if r.std { + part = "errors" + } + name := fmt.Sprintf("%s-stack-%d", part, r.stack) + b.Run(name, func(b *testing.B) { + var err error + f := yesErrors + if r.std { + f = noErrors + } + b.ReportAllocs() + for i := 0; i < b.N; i++ { + err = f(0, r.stack) + } + b.StopTimer() + toperr = err + }) + } +} diff --git a/vendor/src/github.com/pkg/errors/errors.go b/vendor/src/github.com/pkg/errors/errors.go new file mode 100644 index 000000000..1c9731ac6 --- /dev/null +++ b/vendor/src/github.com/pkg/errors/errors.go @@ -0,0 +1,238 @@ +// Package errors provides simple error handling primitives. +// +// The traditional error handling idiom in Go is roughly akin to +// +// if err != nil { +// return err +// } +// +// which applied recursively up the call stack results in error reports +// without context or debugging information. The errors package allows +// programmers to add context to the failure path in their code in a way +// that does not destroy the original value of the error. +// +// Adding context to an error +// +// The errors.Wrap function returns a new error that adds context to the +// original error. For example +// +// _, err := ioutil.ReadAll(r) +// if err != nil { +// return errors.Wrap(err, "read failed") +// } +// +// Retrieving the cause of an error +// +// Using errors.Wrap constructs a stack of errors, adding context to the +// preceding error. Depending on the nature of the error it may be necessary +// to reverse the operation of errors.Wrap to retrieve the original error +// for inspection. Any error value which implements this interface +// +// type causer interface { +// Cause() error +// } +// +// can be inspected by errors.Cause. errors.Cause will recursively retrieve +// the topmost error which does not implement causer, which is assumed to be +// the original cause. For example: +// +// switch err := errors.Cause(err).(type) { +// case *MyError: +// // handle specifically +// default: +// // unknown error +// } +// +// causer interface is not exported by this package, but is considered a part +// of stable public API. +// +// Formatted printing of errors +// +// All error values returned from this package implement fmt.Formatter and can +// be formatted by the fmt package. The following verbs are supported +// +// %s print the error. If the error has a Cause it will be +// printed recursively +// %v see %s +// %+v extended format. Each Frame of the error's StackTrace will +// be printed in detail. +// +// Retrieving the stack trace of an error or wrapper +// +// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are +// invoked. This information can be retrieved with the following interface. +// +// type stackTracer interface { +// StackTrace() errors.StackTrace +// } +// +// Where errors.StackTrace is defined as +// +// type StackTrace []Frame +// +// The Frame type represents a call site in the stack trace. Frame supports +// the fmt.Formatter interface that can be used for printing information about +// the stack trace of this error. For example: +// +// if err, ok := err.(stackTracer); ok { +// for _, f := range err.StackTrace() { +// fmt.Printf("%+s:%d", f) +// } +// } +// +// stackTracer interface is not exported by this package, but is considered a part +// of stable public API. +// +// See the documentation for Frame.Format for more details. +package errors + +import ( + "fmt" + "io" +) + +// New returns an error with the supplied message. +// New also records the stack trace at the point it was called. +func New(message string) error { + return &fundamental{ + msg: message, + stack: callers(), + } +} + +// Errorf formats according to a format specifier and returns the string +// as a value that satisfies error. +// Errorf also records the stack trace at the point it was called. +func Errorf(format string, args ...interface{}) error { + return &fundamental{ + msg: fmt.Sprintf(format, args...), + stack: callers(), + } +} + +// fundamental is an error that has a message and a stack, but no caller. +type fundamental struct { + msg string + *stack +} + +func (f *fundamental) Error() string { return f.msg } + +func (f *fundamental) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + io.WriteString(s, f.msg) + f.stack.Format(s, verb) + return + } + fallthrough + case 's': + io.WriteString(s, f.msg) + case 'q': + fmt.Fprintf(s, "%q", f.msg) + } +} + +type withStack struct { + error + *stack +} + +func (w *withStack) Cause() error { return w.error } + +func (w *withStack) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + fmt.Fprintf(s, "%+v", w.Cause()) + w.stack.Format(s, verb) + return + } + fallthrough + case 's': + io.WriteString(s, w.Error()) + case 'q': + fmt.Fprintf(s, "%q", w.Error()) + } +} + +// Wrap returns an error annotating err with message. +// If err is nil, Wrap returns nil. +func Wrap(err error, message string) error { + if err == nil { + return nil + } + err = &withMessage{ + cause: err, + msg: message, + } + return &withStack{ + err, + callers(), + } +} + +// Wrapf returns an error annotating err with the format specifier. +// If err is nil, Wrapf returns nil. +func Wrapf(err error, format string, args ...interface{}) error { + if err == nil { + return nil + } + err = &withMessage{ + cause: err, + msg: fmt.Sprintf(format, args...), + } + return &withStack{ + err, + callers(), + } +} + +type withMessage struct { + cause error + msg string +} + +func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() } +func (w *withMessage) Cause() error { return w.cause } + +func (w *withMessage) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + fmt.Fprintf(s, "%+v\n", w.Cause()) + io.WriteString(s, w.msg) + return + } + fallthrough + case 's', 'q': + io.WriteString(s, w.Error()) + } +} + +// Cause returns the underlying cause of the error, if possible. +// An error value has a cause if it implements the following +// interface: +// +// type causer interface { +// Cause() error +// } +// +// If the error does not implement Cause, the original error will +// be returned. If the error is nil, nil will be returned without further +// investigation. +func Cause(err error) error { + type causer interface { + Cause() error + } + + for err != nil { + cause, ok := err.(causer) + if !ok { + break + } + err = cause.Cause() + } + return err +} diff --git a/vendor/src/github.com/pkg/errors/errors_test.go b/vendor/src/github.com/pkg/errors/errors_test.go new file mode 100644 index 000000000..11d455542 --- /dev/null +++ b/vendor/src/github.com/pkg/errors/errors_test.go @@ -0,0 +1,159 @@ +package errors + +import ( + "errors" + "fmt" + "io" + "reflect" + "testing" +) + +func TestNew(t *testing.T) { + tests := []struct { + err string + want error + }{ + {"", fmt.Errorf("")}, + {"foo", fmt.Errorf("foo")}, + {"foo", New("foo")}, + {"string with format specifiers: %v", errors.New("string with format specifiers: %v")}, + } + + for _, tt := range tests { + got := New(tt.err) + if got.Error() != tt.want.Error() { + t.Errorf("New.Error(): got: %q, want %q", got, tt.want) + } + } +} + +func TestWrapNil(t *testing.T) { + got := Wrap(nil, "no error") + if got != nil { + t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got) + } +} + +func TestWrap(t *testing.T) { + tests := []struct { + err error + message string + want string + }{ + {io.EOF, "read error", "read error: EOF"}, + {Wrap(io.EOF, "read error"), "client error", "client error: read error: EOF"}, + } + + for _, tt := range tests { + got := Wrap(tt.err, tt.message).Error() + if got != tt.want { + t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want) + } + } +} + +type nilError struct{} + +func (nilError) Error() string { return "nil error" } + +func TestCause(t *testing.T) { + x := New("error") + tests := []struct { + err error + want error + }{{ + // nil error is nil + err: nil, + want: nil, + }, { + // explicit nil error is nil + err: (error)(nil), + want: nil, + }, { + // typed nil is nil + err: (*nilError)(nil), + want: (*nilError)(nil), + }, { + // uncaused error is unaffected + err: io.EOF, + want: io.EOF, + }, { + // caused error returns cause + err: Wrap(io.EOF, "ignored"), + want: io.EOF, + }, { + err: x, // return from errors.New + want: x, + }} + + for i, tt := range tests { + got := Cause(tt.err) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want) + } + } +} + +func TestWrapfNil(t *testing.T) { + got := Wrapf(nil, "no error") + if got != nil { + t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got) + } +} + +func TestWrapf(t *testing.T) { + tests := []struct { + err error + message string + want string + }{ + {io.EOF, "read error", "read error: EOF"}, + {Wrapf(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"}, + {Wrapf(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"}, + } + + for _, tt := range tests { + got := Wrapf(tt.err, tt.message).Error() + if got != tt.want { + t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want) + } + } +} + +func TestErrorf(t *testing.T) { + tests := []struct { + err error + want string + }{ + {Errorf("read error without format specifiers"), "read error without format specifiers"}, + {Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"}, + } + + for _, tt := range tests { + got := tt.err.Error() + if got != tt.want { + t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want) + } + } +} + +// errors.New, etc values are not expected to be compared by value +// but the change in errors#27 made them incomparable. Assert that +// various kinds of errors have a functional equality operator, even +// if the result of that equality is always false. +func TestErrorEquality(t *testing.T) { + tests := []struct { + err1, err2 error + }{ + {io.EOF, io.EOF}, + {io.EOF, nil}, + {io.EOF, errors.New("EOF")}, + {io.EOF, New("EOF")}, + {New("EOF"), New("EOF")}, + {New("EOF"), Errorf("EOF")}, + {New("EOF"), Wrap(io.EOF, "EOF")}, + } + for _, tt := range tests { + _ = tt.err1 == tt.err2 // mustn't panic + } +} diff --git a/vendor/src/github.com/pkg/errors/example_test.go b/vendor/src/github.com/pkg/errors/example_test.go new file mode 100644 index 000000000..5bec3ad43 --- /dev/null +++ b/vendor/src/github.com/pkg/errors/example_test.go @@ -0,0 +1,152 @@ +package errors_test + +import ( + "fmt" + + "github.com/pkg/errors" +) + +func ExampleNew() { + err := errors.New("whoops") + fmt.Println(err) + + // Output: whoops +} + +func ExampleNew_printf() { + err := errors.New("whoops") + fmt.Printf("%+v", err) + + // Example output: + // whoops + // github.com/pkg/errors_test.ExampleNew_printf + // /home/dfc/src/github.com/pkg/errors/example_test.go:17 + // testing.runExample + // /home/dfc/go/src/testing/example.go:114 + // testing.RunExamples + // /home/dfc/go/src/testing/example.go:38 + // testing.(*M).Run + // /home/dfc/go/src/testing/testing.go:744 + // main.main + // /github.com/pkg/errors/_test/_testmain.go:106 + // runtime.main + // /home/dfc/go/src/runtime/proc.go:183 + // runtime.goexit + // /home/dfc/go/src/runtime/asm_amd64.s:2059 +} + +func ExampleWrap() { + cause := errors.New("whoops") + err := errors.Wrap(cause, "oh noes") + fmt.Println(err) + + // Output: oh noes: whoops +} + +func fn() error { + e1 := errors.New("error") + e2 := errors.Wrap(e1, "inner") + e3 := errors.Wrap(e2, "middle") + return errors.Wrap(e3, "outer") +} + +func ExampleCause() { + err := fn() + fmt.Println(err) + fmt.Println(errors.Cause(err)) + + // Output: outer: middle: inner: error + // error +} + +func ExampleWrap_extended() { + err := fn() + fmt.Printf("%+v\n", err) + + // Example output: + // error + // github.com/pkg/errors_test.fn + // /home/dfc/src/github.com/pkg/errors/example_test.go:47 + // github.com/pkg/errors_test.ExampleCause_printf + // /home/dfc/src/github.com/pkg/errors/example_test.go:63 + // testing.runExample + // /home/dfc/go/src/testing/example.go:114 + // testing.RunExamples + // /home/dfc/go/src/testing/example.go:38 + // testing.(*M).Run + // /home/dfc/go/src/testing/testing.go:744 + // main.main + // /github.com/pkg/errors/_test/_testmain.go:104 + // runtime.main + // /home/dfc/go/src/runtime/proc.go:183 + // runtime.goexit + // /home/dfc/go/src/runtime/asm_amd64.s:2059 + // github.com/pkg/errors_test.fn + // /home/dfc/src/github.com/pkg/errors/example_test.go:48: inner + // github.com/pkg/errors_test.fn + // /home/dfc/src/github.com/pkg/errors/example_test.go:49: middle + // github.com/pkg/errors_test.fn + // /home/dfc/src/github.com/pkg/errors/example_test.go:50: outer +} + +func ExampleWrapf() { + cause := errors.New("whoops") + err := errors.Wrapf(cause, "oh noes #%d", 2) + fmt.Println(err) + + // Output: oh noes #2: whoops +} + +func ExampleErrorf_extended() { + err := errors.Errorf("whoops: %s", "foo") + fmt.Printf("%+v", err) + + // Example output: + // whoops: foo + // github.com/pkg/errors_test.ExampleErrorf + // /home/dfc/src/github.com/pkg/errors/example_test.go:101 + // testing.runExample + // /home/dfc/go/src/testing/example.go:114 + // testing.RunExamples + // /home/dfc/go/src/testing/example.go:38 + // testing.(*M).Run + // /home/dfc/go/src/testing/testing.go:744 + // main.main + // /github.com/pkg/errors/_test/_testmain.go:102 + // runtime.main + // /home/dfc/go/src/runtime/proc.go:183 + // runtime.goexit + // /home/dfc/go/src/runtime/asm_amd64.s:2059 +} + +func Example_stackTrace() { + type stackTracer interface { + StackTrace() errors.StackTrace + } + + err, ok := errors.Cause(fn()).(stackTracer) + if !ok { + panic("oops, err does not implement stackTracer") + } + + st := err.StackTrace() + fmt.Printf("%+v", st[0:2]) // top two frames + + // Example output: + // github.com/pkg/errors_test.fn + // /home/dfc/src/github.com/pkg/errors/example_test.go:47 + // github.com/pkg/errors_test.Example_stackTrace + // /home/dfc/src/github.com/pkg/errors/example_test.go:127 +} + +func ExampleCause_printf() { + err := errors.Wrap(func() error { + return func() error { + return errors.Errorf("hello %s", fmt.Sprintf("world")) + }() + }(), "failed") + + fmt.Printf("%v", err) + + // Output: failed: hello world +} diff --git a/vendor/src/github.com/pkg/errors/format_test.go b/vendor/src/github.com/pkg/errors/format_test.go new file mode 100644 index 000000000..fd1758194 --- /dev/null +++ b/vendor/src/github.com/pkg/errors/format_test.go @@ -0,0 +1,172 @@ +package errors + +import ( + "fmt" + "io" + "regexp" + "strings" + "testing" +) + +func TestFormatNew(t *testing.T) { + tests := []struct { + error + format string + want string + }{{ + New("error"), + "%s", + "error", + }, { + New("error"), + "%v", + "error", + }, { + New("error"), + "%+v", + "error\n" + + "github.com/pkg/errors.TestFormatNew\n" + + "\t.+/github.com/pkg/errors/format_test.go:25", + }, { + New("error"), + "%q", + `"error"`, + }} + + for i, tt := range tests { + testFormatRegexp(t, i, tt.error, tt.format, tt.want) + } +} + +func TestFormatErrorf(t *testing.T) { + tests := []struct { + error + format string + want string + }{{ + Errorf("%s", "error"), + "%s", + "error", + }, { + Errorf("%s", "error"), + "%v", + "error", + }, { + Errorf("%s", "error"), + "%+v", + "error\n" + + "github.com/pkg/errors.TestFormatErrorf\n" + + "\t.+/github.com/pkg/errors/format_test.go:55", + }} + + for i, tt := range tests { + testFormatRegexp(t, i, tt.error, tt.format, tt.want) + } +} + +func TestFormatWrap(t *testing.T) { + tests := []struct { + error + format string + want string + }{{ + Wrap(New("error"), "error2"), + "%s", + "error2: error", + }, { + Wrap(New("error"), "error2"), + "%v", + "error2: error", + }, { + Wrap(New("error"), "error2"), + "%+v", + "error\n" + + "github.com/pkg/errors.TestFormatWrap\n" + + "\t.+/github.com/pkg/errors/format_test.go:81", + }, { + Wrap(io.EOF, "error"), + "%s", + "error: EOF", + }, { + Wrap(io.EOF, "error"), + "%v", + "error: EOF", + }, { + Wrap(io.EOF, "error"), + "%+v", + "EOF\n" + + "error\n" + + "github.com/pkg/errors.TestFormatWrap\n" + + "\t.+/github.com/pkg/errors/format_test.go:95", + }, { + Wrap(Wrap(io.EOF, "error1"), "error2"), + "%+v", + "EOF\n" + + "error1\n" + + "github.com/pkg/errors.TestFormatWrap\n" + + "\t.+/github.com/pkg/errors/format_test.go:102\n", + }, { + Wrap(New("error with space"), "context"), + "%q", + `"context: error with space"`, + }} + + for i, tt := range tests { + testFormatRegexp(t, i, tt.error, tt.format, tt.want) + } +} + +func TestFormatWrapf(t *testing.T) { + tests := []struct { + error + format string + want string + }{{ + Wrapf(io.EOF, "error%d", 2), + "%s", + "error2: EOF", + }, { + Wrapf(io.EOF, "error%d", 2), + "%v", + "error2: EOF", + }, { + Wrapf(io.EOF, "error%d", 2), + "%+v", + "EOF\n" + + "error2\n" + + "github.com/pkg/errors.TestFormatWrapf\n" + + "\t.+/github.com/pkg/errors/format_test.go:133", + }, { + Wrapf(New("error"), "error%d", 2), + "%s", + "error2: error", + }, { + Wrapf(New("error"), "error%d", 2), + "%v", + "error2: error", + }, { + Wrapf(New("error"), "error%d", 2), + "%+v", + "error\n" + + "github.com/pkg/errors.TestFormatWrapf\n" + + "\t.+/github.com/pkg/errors/format_test.go:148", + }} + + for i, tt := range tests { + testFormatRegexp(t, i, tt.error, tt.format, tt.want) + } +} + +func testFormatRegexp(t *testing.T, n int, arg interface{}, format, want string) { + got := fmt.Sprintf(format, arg) + lines := strings.SplitN(got, "\n", -1) + for i, w := range strings.SplitN(want, "\n", -1) { + match, err := regexp.MatchString(w, lines[i]) + if err != nil { + t.Fatal(err) + } + if !match { + t.Errorf("test %d: line %d: fmt.Sprintf(%q, err): got: %q, want: %q", n+1, i+1, format, got, want) + } + } +} diff --git a/vendor/src/github.com/pkg/errors/stack.go b/vendor/src/github.com/pkg/errors/stack.go new file mode 100644 index 000000000..6b1f2891a --- /dev/null +++ b/vendor/src/github.com/pkg/errors/stack.go @@ -0,0 +1,178 @@ +package errors + +import ( + "fmt" + "io" + "path" + "runtime" + "strings" +) + +// Frame represents a program counter inside a stack frame. +type Frame uintptr + +// pc returns the program counter for this frame; +// multiple frames may have the same PC value. +func (f Frame) pc() uintptr { return uintptr(f) - 1 } + +// file returns the full path to the file that contains the +// function for this Frame's pc. +func (f Frame) file() string { + fn := runtime.FuncForPC(f.pc()) + if fn == nil { + return "unknown" + } + file, _ := fn.FileLine(f.pc()) + return file +} + +// line returns the line number of source code of the +// function for this Frame's pc. +func (f Frame) line() int { + fn := runtime.FuncForPC(f.pc()) + if fn == nil { + return 0 + } + _, line := fn.FileLine(f.pc()) + return line +} + +// Format formats the frame according to the fmt.Formatter interface. +// +// %s source file +// %d source line +// %n function name +// %v equivalent to %s:%d +// +// Format accepts flags that alter the printing of some verbs, as follows: +// +// %+s path of source file relative to the compile time GOPATH +// %+v equivalent to %+s:%d +func (f Frame) Format(s fmt.State, verb rune) { + switch verb { + case 's': + switch { + case s.Flag('+'): + pc := f.pc() + fn := runtime.FuncForPC(pc) + if fn == nil { + io.WriteString(s, "unknown") + } else { + file, _ := fn.FileLine(pc) + fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file) + } + default: + io.WriteString(s, path.Base(f.file())) + } + case 'd': + fmt.Fprintf(s, "%d", f.line()) + case 'n': + name := runtime.FuncForPC(f.pc()).Name() + io.WriteString(s, funcname(name)) + case 'v': + f.Format(s, 's') + io.WriteString(s, ":") + f.Format(s, 'd') + } +} + +// StackTrace is stack of Frames from innermost (newest) to outermost (oldest). +type StackTrace []Frame + +func (st StackTrace) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + switch { + case s.Flag('+'): + for _, f := range st { + fmt.Fprintf(s, "\n%+v", f) + } + case s.Flag('#'): + fmt.Fprintf(s, "%#v", []Frame(st)) + default: + fmt.Fprintf(s, "%v", []Frame(st)) + } + case 's': + fmt.Fprintf(s, "%s", []Frame(st)) + } +} + +// stack represents a stack of program counters. +type stack []uintptr + +func (s *stack) Format(st fmt.State, verb rune) { + switch verb { + case 'v': + switch { + case st.Flag('+'): + for _, pc := range *s { + f := Frame(pc) + fmt.Fprintf(st, "\n%+v", f) + } + } + } +} + +func (s *stack) StackTrace() StackTrace { + f := make([]Frame, len(*s)) + for i := 0; i < len(f); i++ { + f[i] = Frame((*s)[i]) + } + return f +} + +func callers() *stack { + const depth = 32 + var pcs [depth]uintptr + n := runtime.Callers(3, pcs[:]) + var st stack = pcs[0:n] + return &st +} + +// funcname removes the path prefix component of a function's name reported by func.Name(). +func funcname(name string) string { + i := strings.LastIndex(name, "/") + name = name[i+1:] + i = strings.Index(name, ".") + return name[i+1:] +} + +func trimGOPATH(name, file string) string { + // Here we want to get the source file path relative to the compile time + // GOPATH. As of Go 1.6.x there is no direct way to know the compiled + // GOPATH at runtime, but we can infer the number of path segments in the + // GOPATH. We note that fn.Name() returns the function name qualified by + // the import path, which does not include the GOPATH. Thus we can trim + // segments from the beginning of the file path until the number of path + // separators remaining is one more than the number of path separators in + // the function name. For example, given: + // + // GOPATH /home/user + // file /home/user/src/pkg/sub/file.go + // fn.Name() pkg/sub.Type.Method + // + // We want to produce: + // + // pkg/sub/file.go + // + // From this we can easily see that fn.Name() has one less path separator + // than our desired output. We count separators from the end of the file + // path until it finds two more than in the function name and then move + // one character forward to preserve the initial path segment without a + // leading separator. + const sep = "/" + goal := strings.Count(name, sep) + 2 + i := len(file) + for n := 0; n < goal; n++ { + i = strings.LastIndex(file[:i], sep) + if i == -1 { + // not enough separators found, set i so that the slice expression + // below leaves file unmodified + i = -len(sep) + break + } + } + // get back to 0 or trim the leading separator + file = file[i+len(sep):] + return file +} diff --git a/vendor/src/github.com/pkg/errors/stack_test.go b/vendor/src/github.com/pkg/errors/stack_test.go new file mode 100644 index 000000000..915d11fe3 --- /dev/null +++ b/vendor/src/github.com/pkg/errors/stack_test.go @@ -0,0 +1,292 @@ +package errors + +import ( + "fmt" + "runtime" + "testing" +) + +var initpc, _, _, _ = runtime.Caller(0) + +func TestFrameLine(t *testing.T) { + var tests = []struct { + Frame + want int + }{{ + Frame(initpc), + 9, + }, { + func() Frame { + var pc, _, _, _ = runtime.Caller(0) + return Frame(pc) + }(), + 20, + }, { + func() Frame { + var pc, _, _, _ = runtime.Caller(1) + return Frame(pc) + }(), + 28, + }, { + Frame(0), // invalid PC + 0, + }} + + for _, tt := range tests { + got := tt.Frame.line() + want := tt.want + if want != got { + t.Errorf("Frame(%v): want: %v, got: %v", uintptr(tt.Frame), want, got) + } + } +} + +type X struct{} + +func (x X) val() Frame { + var pc, _, _, _ = runtime.Caller(0) + return Frame(pc) +} + +func (x *X) ptr() Frame { + var pc, _, _, _ = runtime.Caller(0) + return Frame(pc) +} + +func TestFrameFormat(t *testing.T) { + var tests = []struct { + Frame + format string + want string + }{{ + Frame(initpc), + "%s", + "stack_test.go", + }, { + Frame(initpc), + "%+s", + "github.com/pkg/errors.init\n" + + "\t.+/github.com/pkg/errors/stack_test.go", + }, { + Frame(0), + "%s", + "unknown", + }, { + Frame(0), + "%+s", + "unknown", + }, { + Frame(initpc), + "%d", + "9", + }, { + Frame(0), + "%d", + "0", + }, { + Frame(initpc), + "%n", + "init", + }, { + func() Frame { + var x X + return x.ptr() + }(), + "%n", + `\(\*X\).ptr`, + }, { + func() Frame { + var x X + return x.val() + }(), + "%n", + "X.val", + }, { + Frame(0), + "%n", + "", + }, { + Frame(initpc), + "%v", + "stack_test.go:9", + }, { + Frame(initpc), + "%+v", + "github.com/pkg/errors.init\n" + + "\t.+/github.com/pkg/errors/stack_test.go:9", + }, { + Frame(0), + "%v", + "unknown:0", + }} + + for i, tt := range tests { + testFormatRegexp(t, i, tt.Frame, tt.format, tt.want) + } +} + +func TestFuncname(t *testing.T) { + tests := []struct { + name, want string + }{ + {"", ""}, + {"runtime.main", "main"}, + {"github.com/pkg/errors.funcname", "funcname"}, + {"funcname", "funcname"}, + {"io.copyBuffer", "copyBuffer"}, + {"main.(*R).Write", "(*R).Write"}, + } + + for _, tt := range tests { + got := funcname(tt.name) + want := tt.want + if got != want { + t.Errorf("funcname(%q): want: %q, got %q", tt.name, want, got) + } + } +} + +func TestTrimGOPATH(t *testing.T) { + var tests = []struct { + Frame + want string + }{{ + Frame(initpc), + "github.com/pkg/errors/stack_test.go", + }} + + for i, tt := range tests { + pc := tt.Frame.pc() + fn := runtime.FuncForPC(pc) + file, _ := fn.FileLine(pc) + got := trimGOPATH(fn.Name(), file) + testFormatRegexp(t, i, got, "%s", tt.want) + } +} + +func TestStackTrace(t *testing.T) { + tests := []struct { + err error + want []string + }{{ + New("ooh"), []string{ + "github.com/pkg/errors.TestStackTrace\n" + + "\t.+/github.com/pkg/errors/stack_test.go:172", + }, + }, { + Wrap(New("ooh"), "ahh"), []string{ + "github.com/pkg/errors.TestStackTrace\n" + + "\t.+/github.com/pkg/errors/stack_test.go:177", // this is the stack of Wrap, not New + }, + }, { + Cause(Wrap(New("ooh"), "ahh")), []string{ + "github.com/pkg/errors.TestStackTrace\n" + + "\t.+/github.com/pkg/errors/stack_test.go:182", // this is the stack of New + }, + }, { + func() error { return New("ooh") }(), []string{ + `github.com/pkg/errors.(func·005|TestStackTrace.func1)` + + "\n\t.+/github.com/pkg/errors/stack_test.go:187", // this is the stack of New + "github.com/pkg/errors.TestStackTrace\n" + + "\t.+/github.com/pkg/errors/stack_test.go:187", // this is the stack of New's caller + }, + }, { + Cause(func() error { + return func() error { + return Errorf("hello %s", fmt.Sprintf("world")) + }() + }()), []string{ + `github.com/pkg/errors.(func·006|TestStackTrace.func2.1)` + + "\n\t.+/github.com/pkg/errors/stack_test.go:196", // this is the stack of Errorf + `github.com/pkg/errors.(func·007|TestStackTrace.func2)` + + "\n\t.+/github.com/pkg/errors/stack_test.go:197", // this is the stack of Errorf's caller + "github.com/pkg/errors.TestStackTrace\n" + + "\t.+/github.com/pkg/errors/stack_test.go:198", // this is the stack of Errorf's caller's caller + }, + }} + for i, tt := range tests { + x, ok := tt.err.(interface { + StackTrace() StackTrace + }) + if !ok { + t.Errorf("expected %#v to implement StackTrace() StackTrace", tt.err) + continue + } + st := x.StackTrace() + for j, want := range tt.want { + testFormatRegexp(t, i, st[j], "%+v", want) + } + } +} + +func stackTrace() StackTrace { + const depth = 8 + var pcs [depth]uintptr + n := runtime.Callers(1, pcs[:]) + var st stack = pcs[0:n] + return st.StackTrace() +} + +func TestStackTraceFormat(t *testing.T) { + tests := []struct { + StackTrace + format string + want string + }{{ + nil, + "%s", + `\[\]`, + }, { + nil, + "%v", + `\[\]`, + }, { + nil, + "%+v", + "", + }, { + nil, + "%#v", + `\[\]errors.Frame\(nil\)`, + }, { + make(StackTrace, 0), + "%s", + `\[\]`, + }, { + make(StackTrace, 0), + "%v", + `\[\]`, + }, { + make(StackTrace, 0), + "%+v", + "", + }, { + make(StackTrace, 0), + "%#v", + `\[\]errors.Frame{}`, + }, { + stackTrace()[:2], + "%s", + `\[stack_test.go stack_test.go\]`, + }, { + stackTrace()[:2], + "%v", + `\[stack_test.go:225 stack_test.go:272\]`, + }, { + stackTrace()[:2], + "%+v", + "\n" + + "github.com/pkg/errors.stackTrace\n" + + "\t.+/github.com/pkg/errors/stack_test.go:225\n" + + "github.com/pkg/errors.TestStackTraceFormat\n" + + "\t.+/github.com/pkg/errors/stack_test.go:276", + }, { + stackTrace()[:2], + "%#v", + `\[\]errors.Frame{stack_test.go:225, stack_test.go:284}`, + }} + + for i, tt := range tests { + testFormatRegexp(t, i, tt.StackTrace, tt.format, tt.want) + } +} diff --git a/vendor/src/github.com/pkg/sftp/README.md b/vendor/src/github.com/pkg/sftp/README.md index 875bf2747..e406b1e56 100644 --- a/vendor/src/github.com/pkg/sftp/README.md +++ b/vendor/src/github.com/pkg/sftp/README.md @@ -17,9 +17,7 @@ The Walker interface for directory traversal is heavily inspired by Keith Rarick roadmap ------- - * Currently all traffic with the server is serialized, this can be improved by allowing overlapping requests/responses. * There is way too much duplication in the Client methods. If there was an unmarshal(interface{}) method this would reduce a heap of the duplication. - * Implement integration tests by talking directly to a real opensftp-server process. This shouldn't be too difficult to implement with a small refactoring to the sftp.NewClient method. These tests should be gated on an -sftp.integration test flag. _in progress_ contributing ------------ diff --git a/vendor/src/github.com/pkg/sftp/client.go b/vendor/src/github.com/pkg/sftp/client.go index f0d67e801..41f10639c 100644 --- a/vendor/src/github.com/pkg/sftp/client.go +++ b/vendor/src/github.com/pkg/sftp/client.go @@ -2,19 +2,15 @@ package sftp import ( "bytes" - "encoding" "encoding/binary" - "errors" - "fmt" "io" "os" "path" - "sync" "sync/atomic" "time" "github.com/kr/fs" - + "github.com/pkg/errors" "golang.org/x/crypto/ssh" ) @@ -22,7 +18,7 @@ import ( func MaxPacket(size int) func(*Client) error { return func(c *Client) error { if size < 1<<15 { - return fmt.Errorf("size must be greater or equal to 32k") + return errors.Errorf("size must be greater or equal to 32k") } c.maxPacket = size return nil @@ -56,11 +52,14 @@ func NewClient(conn *ssh.Client, opts ...func(*Client) error) (*Client, error) { // the system's ssh client program (e.g. via exec.Command). func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...func(*Client) error) (*Client, error) { sftp := &Client{ - w: wr, - r: rd, - maxPacket: 1 << 15, - inflight: make(map[uint32]chan<- result), - recvClosed: make(chan struct{}), + clientConn: clientConn{ + conn: conn{ + Reader: rd, + WriteCloser: wr, + }, + inflight: make(map[uint32]chan<- result), + }, + maxPacket: 1 << 15, } if err := sftp.applyOptions(opts...); err != nil { wr.Close() @@ -74,7 +73,8 @@ func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...func(*Client) error) wr.Close() return nil, err } - go sftp.recv() + sftp.clientConn.wg.Add(1) + go sftp.loop() return sftp, nil } @@ -84,22 +84,10 @@ func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...func(*Client) error) // // Client implements the github.com/kr/fs.FileSystem interface. type Client struct { - w io.WriteCloser - r io.Reader + clientConn maxPacket int // max packet size read or written. nextid uint32 - - mu sync.Mutex // ensures only on request is in flight to the server at once - inflight map[uint32]chan<- result // outstanding requests - recvClosed chan struct{} // remote end has closed the connection -} - -// Close closes the SFTP session. -func (c *Client) Close() error { - err := c.w.Close() - <-c.recvClosed - return err } // Create creates the named file mode 0666 (before umask), truncating it if @@ -112,7 +100,7 @@ func (c *Client) Create(path string) (*File, error) { const sftpProtocolVersion = 3 // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 func (c *Client) sendInit() error { - return sendPacket(c.w, sshFxInitPacket{ + return c.clientConn.conn.sendPacket(sshFxInitPacket{ Version: sftpProtocolVersion, // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 }) } @@ -123,7 +111,7 @@ func (c *Client) nextID() uint32 { } func (c *Client) recvVersion() error { - typ, data, err := recvPacket(c.r) + typ, data, err := c.recvPacket() if err != nil { return err } @@ -139,46 +127,6 @@ func (c *Client) recvVersion() error { return nil } -// broadcastErr sends an error to all goroutines waiting for a response. -func (c *Client) broadcastErr(err error) { - c.mu.Lock() - listeners := make([]chan<- result, 0, len(c.inflight)) - for _, ch := range c.inflight { - listeners = append(listeners, ch) - } - c.mu.Unlock() - for _, ch := range listeners { - ch <- result{err: err} - } -} - -// recv continuously reads from the server and forwards responses to the -// appropriate channel. -func (c *Client) recv() { - defer close(c.recvClosed) - for { - typ, data, err := recvPacket(c.r) - if err != nil { - // Return the error to all listeners. - c.broadcastErr(err) - return - } - sid, _ := unmarshalUint32(data) - c.mu.Lock() - ch, ok := c.inflight[sid] - delete(c.inflight, sid) - c.mu.Unlock() - if !ok { - // This is an unexpected occurrence. Send the error - // back to all listeners so that they terminate - // gracefully. - c.broadcastErr(fmt.Errorf("sid: %v not fond", sid)) - return - } - ch <- result{typ: typ, data: data} - } -} - // Walk returns a new Walker rooted at root. func (c *Client) Walk(root string) *fs.Walker { return fs.WalkFS(root, c) @@ -196,7 +144,7 @@ func (c *Client) ReadDir(p string) ([]os.FileInfo, error) { var done = false for !done { id := c.nextID() - typ, data, err1 := c.sendRequest(sshFxpReaddirPacket{ + typ, data, err1 := c.sendPacket(sshFxpReaddirPacket{ ID: id, Handle: handle, }) @@ -239,7 +187,7 @@ func (c *Client) ReadDir(p string) ([]os.FileInfo, error) { func (c *Client) opendir(path string) (string, error) { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpOpendirPacket{ + typ, data, err := c.sendPacket(sshFxpOpendirPacket{ ID: id, Path: path, }) @@ -265,7 +213,7 @@ func (c *Client) opendir(path string) (string, error) { // If 'p' is a symbolic link, the returned FileInfo structure describes the referent file. func (c *Client) Stat(p string) (os.FileInfo, error) { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpStatPacket{ + typ, data, err := c.sendPacket(sshFxpStatPacket{ ID: id, Path: p, }) @@ -291,7 +239,7 @@ func (c *Client) Stat(p string) (os.FileInfo, error) { // If 'p' is a symbolic link, the returned FileInfo structure describes the symbolic link. func (c *Client) Lstat(p string) (os.FileInfo, error) { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpLstatPacket{ + typ, data, err := c.sendPacket(sshFxpLstatPacket{ ID: id, Path: p, }) @@ -316,7 +264,7 @@ func (c *Client) Lstat(p string) (os.FileInfo, error) { // ReadLink reads the target of a symbolic link. func (c *Client) ReadLink(p string) (string, error) { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpReadlinkPacket{ + typ, data, err := c.sendPacket(sshFxpReadlinkPacket{ ID: id, Path: p, }) @@ -345,7 +293,7 @@ func (c *Client) ReadLink(p string) (string, error) { // Symlink creates a symbolic link at 'newname', pointing at target 'oldname' func (c *Client) Symlink(oldname, newname string) error { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpSymlinkPacket{ + typ, data, err := c.sendPacket(sshFxpSymlinkPacket{ ID: id, Linkpath: newname, Targetpath: oldname, @@ -364,7 +312,7 @@ func (c *Client) Symlink(oldname, newname string) error { // setstat is a convience wrapper to allow for changing of various parts of the file descriptor. func (c *Client) setstat(path string, flags uint32, attrs interface{}) error { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpSetstatPacket{ + typ, data, err := c.sendPacket(sshFxpSetstatPacket{ ID: id, Path: path, Flags: flags, @@ -430,7 +378,7 @@ func (c *Client) OpenFile(path string, f int) (*File, error) { func (c *Client) open(path string, pflags uint32) (*File, error) { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpOpenPacket{ + typ, data, err := c.sendPacket(sshFxpOpenPacket{ ID: id, Path: path, Pflags: pflags, @@ -458,7 +406,7 @@ func (c *Client) open(path string, pflags uint32) (*File, error) { // immediately after this request has been sent. func (c *Client) close(handle string) error { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpClosePacket{ + typ, data, err := c.sendPacket(sshFxpClosePacket{ ID: id, Handle: handle, }) @@ -475,7 +423,7 @@ func (c *Client) close(handle string) error { func (c *Client) fstat(handle string) (*FileStat, error) { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpFstatPacket{ + typ, data, err := c.sendPacket(sshFxpFstatPacket{ ID: id, Handle: handle, }) @@ -504,7 +452,7 @@ func (c *Client) fstat(handle string) (*FileStat, error) { func (c *Client) StatVFS(path string) (*StatVFS, error) { // send the StatVFS packet to the server id := c.nextID() - typ, data, err := c.sendRequest(sshFxpStatvfsPacket{ + typ, data, err := c.sendPacket(sshFxpStatvfsPacket{ ID: id, Path: path, }) @@ -560,7 +508,7 @@ func (c *Client) Remove(path string) error { func (c *Client) removeFile(path string) error { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpRemovePacket{ + typ, data, err := c.sendPacket(sshFxpRemovePacket{ ID: id, Filename: path, }) @@ -577,7 +525,7 @@ func (c *Client) removeFile(path string) error { func (c *Client) removeDirectory(path string) error { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpRmdirPacket{ + typ, data, err := c.sendPacket(sshFxpRmdirPacket{ ID: id, Path: path, }) @@ -595,7 +543,7 @@ func (c *Client) removeDirectory(path string) error { // Rename renames a file. func (c *Client) Rename(oldname, newname string) error { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpRenamePacket{ + typ, data, err := c.sendPacket(sshFxpRenamePacket{ ID: id, Oldpath: oldname, Newpath: newname, @@ -613,7 +561,7 @@ func (c *Client) Rename(oldname, newname string) error { func (c *Client) realpath(path string) (string, error) { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpRealpathPacket{ + typ, data, err := c.sendPacket(sshFxpRealpathPacket{ ID: id, Path: path, }) @@ -645,41 +593,12 @@ func (c *Client) Getwd() (string, error) { return c.realpath(".") } -// result captures the result of receiving the a packet from the server -type result struct { - typ byte - data []byte - err error -} - -type idmarshaler interface { - id() uint32 - encoding.BinaryMarshaler -} - -func (c *Client) sendRequest(p idmarshaler) (byte, []byte, error) { - ch := make(chan result, 1) - c.dispatchRequest(ch, p) - s := <-ch - return s.typ, s.data, s.err -} - -func (c *Client) dispatchRequest(ch chan<- result, p idmarshaler) { - c.mu.Lock() - c.inflight[p.id()] = ch - if err := sendPacket(c.w, p); err != nil { - delete(c.inflight, p.id()) - ch <- result{err: err} - } - c.mu.Unlock() -} - // Mkdir creates the specified directory. An error will be returned if a file or // directory with the specified path already exists, or if the directory's // parent folder does not exist (the method cannot create complete paths). func (c *Client) Mkdir(path string) error { id := c.nextID() - typ, data, err := c.sendRequest(sshFxpMkdirPacket{ + typ, data, err := c.sendPacket(sshFxpMkdirPacket{ ID: id, Path: path, }) @@ -784,7 +703,7 @@ func (f *File) Read(b []byte) (int, error) { reqID, data := unmarshalUint32(res.data) req, ok := reqs[reqID] if !ok { - firstErr = offsetErr{offset: 0, err: fmt.Errorf("sid: %v not found", reqID)} + firstErr = offsetErr{offset: 0, err: errors.Errorf("sid: %v not found", reqID)} break } delete(reqs, reqID) @@ -885,7 +804,7 @@ func (f *File) WriteTo(w io.Writer) (int64, error) { reqID, data := unmarshalUint32(res.data) req, ok := reqs[reqID] if !ok { - firstErr = offsetErr{offset: 0, err: fmt.Errorf("sid: %v not found", reqID)} + firstErr = offsetErr{offset: 0, err: errors.Errorf("sid: %v not found", reqID)} break } delete(reqs, reqID) @@ -1166,7 +1085,10 @@ func unmarshalStatus(id uint32, data []byte) error { return &unexpectedIDErr{id, sid} } code, data := unmarshalUint32(data) - msg, data := unmarshalString(data) + msg, data, err := unmarshalStringSafe(data) + if err != nil { + return err + } lang, _, _ := unmarshalStringSafe(data) return &StatusError{ Code: code, diff --git a/vendor/src/github.com/pkg/sftp/client_integration_darwin_test.go b/vendor/src/github.com/pkg/sftp/client_integration_darwin_test.go index 0f7cd01cd..6c72536f3 100644 --- a/vendor/src/github.com/pkg/sftp/client_integration_darwin_test.go +++ b/vendor/src/github.com/pkg/sftp/client_integration_darwin_test.go @@ -39,8 +39,4 @@ func TestClientStatVFS(t *testing.T) { if vfs.Favail != uint64(s.Ffree) { t.Fatal("f_namemax does not match") } - - if vfs.Bavail != s.Bavail { - t.Fatal("f_bavail does not match") - } } diff --git a/vendor/src/github.com/pkg/sftp/client_integration_linux_test.go b/vendor/src/github.com/pkg/sftp/client_integration_linux_test.go index 1663ad42a..aeca79780 100644 --- a/vendor/src/github.com/pkg/sftp/client_integration_linux_test.go +++ b/vendor/src/github.com/pkg/sftp/client_integration_linux_test.go @@ -29,18 +29,14 @@ func TestClientStatVFS(t *testing.T) { // check some stats if vfs.Frsize != uint64(s.Frsize) { - t.Fatal("fr_size does not match") + t.Fatal("fr_size does not match, expected: %v, got: %v", s.Frsize, vfs.Frsize) } if vfs.Bsize != uint64(s.Bsize) { - t.Fatal("f_bsize does not match") + t.Fatal("f_bsize does not match, expected: %v, got: %v", s.Bsize, vfs.Bsize) } if vfs.Namemax != uint64(s.Namelen) { - t.Fatal("f_namemax does not match") - } - - if vfs.Bavail != s.Bavail { - t.Fatal("f_bavail does not match") + t.Fatal("f_namemax does not match, expected: %v, got: %v", s.Namelen, vfs.Namemax) } } diff --git a/vendor/src/github.com/pkg/sftp/client_integration_test.go b/vendor/src/github.com/pkg/sftp/client_integration_test.go index 8fa7f95a0..be16add8e 100644 --- a/vendor/src/github.com/pkg/sftp/client_integration_test.go +++ b/vendor/src/github.com/pkg/sftp/client_integration_test.go @@ -9,6 +9,7 @@ import ( "io" "io/ioutil" "math/rand" + "net" "os" "os/exec" "os/user" @@ -84,37 +85,61 @@ func (w delayedWriter) Close() error { return nil } +// netPipe provides a pair of io.ReadWriteClosers connected to each other. +// The functions is identical to os.Pipe with the exception that netPipe +// provides the Read/Close guarentees that os.File derrived pipes do not. +func netPipe(t testing.TB) (io.ReadWriteCloser, io.ReadWriteCloser) { + type result struct { + net.Conn + error + } + + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + + ch := make(chan result, 1) + go func() { + conn, err := l.Accept() + ch <- result{conn, err} + err = l.Close() + if err != nil { + t.Error(err) + } + }() + c1, err := net.Dial("tcp", l.Addr().String()) + if err != nil { + l.Close() // might cause another in the listening goroutine, but too bad + t.Fatal(err) + } + r := <-ch + if r.error != nil { + t.Fatal(err) + } + return c1, r.Conn +} + func testClientGoSvr(t testing.TB, readonly bool, delay time.Duration) (*Client, *exec.Cmd) { - txPipeRd, txPipeWr, err := os.Pipe() - if err != nil { - t.Fatal(err) - } - rxPipeRd, rxPipeWr, err := os.Pipe() - if err != nil { - t.Fatal(err) - } + c1, c2 := netPipe(t) options := []ServerOption{WithDebug(os.Stderr)} if readonly { options = append(options, ReadOnly()) } - server, err := NewServer( - txPipeRd, - rxPipeWr, - options..., - ) + server, err := NewServer(c1, options...) if err != nil { t.Fatal(err) } go server.Serve() - var ctx io.WriteCloser = txPipeWr + var ctx io.WriteCloser = c2 if delay > NO_DELAY { ctx = newDelayedWriter(ctx, delay) } - client, err := NewClientPipe(rxPipeRd, ctx) + client, err := NewClientPipe(c2, ctx) if err != nil { t.Fatal(err) } @@ -465,6 +490,66 @@ func TestClientFileStat(t *testing.T) { } } +func TestClientStatLink(t *testing.T) { + sftp, cmd := testClient(t, READONLY, NO_DELAY) + defer cmd.Wait() + defer sftp.Close() + + f, err := ioutil.TempFile("", "sftptest") + if err != nil { + t.Fatal(err) + } + defer os.Remove(f.Name()) + + realName := f.Name() + linkName := f.Name() + ".softlink" + + // create a symlink that points at sftptest + if err := os.Symlink(realName, linkName); err != nil { + t.Fatal(err) + } + defer os.Remove(linkName) + + // compare Lstat of links + wantLstat, err := os.Lstat(linkName) + if err != nil { + t.Fatal(err) + } + wantStat, err := os.Stat(linkName) + if err != nil { + t.Fatal(err) + } + + gotLstat, err := sftp.Lstat(linkName) + if err != nil { + t.Fatal(err) + } + gotStat, err := sftp.Stat(linkName) + if err != nil { + t.Fatal(err) + } + + // check that stat is not lstat from os package + if sameFile(wantLstat, wantStat) { + t.Fatalf("Lstat / Stat(%q): both %#v %#v", f.Name(), wantLstat, wantStat) + } + + // compare Lstat of links + if !sameFile(wantLstat, gotLstat) { + t.Fatalf("Lstat(%q): want %#v, got %#v", f.Name(), wantLstat, gotLstat) + } + + // compare Stat of links + if !sameFile(wantStat, gotStat) { + t.Fatalf("Stat(%q): want %#v, got %#v", f.Name(), wantStat, gotStat) + } + + // check that stat is not lstat + if sameFile(gotLstat, gotStat) { + t.Fatalf("Lstat / Stat(%q): both %#v %#v", f.Name(), gotLstat, gotStat) + } +} + func TestClientRemove(t *testing.T) { sftp, cmd := testClient(t, READWRITE, NO_DELAY) defer cmd.Wait() @@ -1076,10 +1161,6 @@ func TestClientWrite(t *testing.T) { // taken from github.com/kr/fs/walk_test.go -type PathTest struct { - path, result string -} - type Node struct { name string entries []*Node // nil if the entry is a file diff --git a/vendor/src/github.com/pkg/sftp/client_test.go b/vendor/src/github.com/pkg/sftp/client_test.go index 8d5f0803a..597fdb8e4 100644 --- a/vendor/src/github.com/pkg/sftp/client_test.go +++ b/vendor/src/github.com/pkg/sftp/client_test.go @@ -4,6 +4,7 @@ import ( "errors" "io" "os" + "reflect" "testing" "github.com/kr/fs" @@ -87,13 +88,58 @@ func TestFlags(t *testing.T) { } } -func TestMissingLangTag(t *testing.T) { - defer func() { - if r := recover(); r != nil { - t.Fail() +func TestUnmarshalStatus(t *testing.T) { + requestID := uint32(1) + + id := marshalUint32([]byte{}, requestID) + idCode := marshalUint32(id, ssh_FX_FAILURE) + idCodeMsg := marshalString(idCode, "err msg") + idCodeMsgLang := marshalString(idCodeMsg, "lang tag") + + var tests = []struct { + desc string + reqID uint32 + status []byte + want error + }{ + { + desc: "well-formed status", + reqID: 1, + status: idCodeMsgLang, + want: &StatusError{ + Code: ssh_FX_FAILURE, + msg: "err msg", + lang: "lang tag", + }, + }, + { + desc: "missing error message and language tag", + reqID: 1, + status: idCode, + want: errShortPacket, + }, + { + desc: "missing language tag", + reqID: 1, + status: idCodeMsg, + want: &StatusError{ + Code: ssh_FX_FAILURE, + msg: "err msg", + }, + }, + { + desc: "request identifier mismatch", + reqID: 2, + status: idCodeMsgLang, + want: &unexpectedIDErr{2, requestID}, + }, + } + + for _, tt := range tests { + got := unmarshalStatus(tt.reqID, tt.status) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("unmarshalStatus(%v, %v), test %q\n- want: %#v\n- got: %#v", + requestID, tt.status, tt.desc, tt.want, got) } - }() - buf := marshalUint32([]byte{}, 0) - buf = marshalStatus(buf, StatusError{}) - _ = unmarshalStatus(0, buf[:len(buf)-4]) + } } diff --git a/vendor/src/github.com/pkg/sftp/conn.go b/vendor/src/github.com/pkg/sftp/conn.go new file mode 100644 index 000000000..9b03d112f --- /dev/null +++ b/vendor/src/github.com/pkg/sftp/conn.go @@ -0,0 +1,122 @@ +package sftp + +import ( + "encoding" + "io" + "sync" + + "github.com/pkg/errors" +) + +// conn implements a bidirectional channel on which client and server +// connections are multiplexed. +type conn struct { + io.Reader + io.WriteCloser + sync.Mutex // used to serialise writes to sendPacket +} + +func (c *conn) recvPacket() (uint8, []byte, error) { + return recvPacket(c) +} + +func (c *conn) sendPacket(m encoding.BinaryMarshaler) error { + c.Lock() + defer c.Unlock() + return sendPacket(c, m) +} + +type clientConn struct { + conn + wg sync.WaitGroup + sync.Mutex // protects inflight + inflight map[uint32]chan<- result // outstanding requests +} + +// Close closes the SFTP session. +func (c *clientConn) Close() error { + defer c.wg.Wait() + return c.conn.Close() +} + +func (c *clientConn) loop() { + defer c.wg.Done() + err := c.recv() + if err != nil { + c.broadcastErr(err) + } +} + +// recv continuously reads from the server and forwards responses to the +// appropriate channel. +func (c *clientConn) recv() error { + defer c.conn.Close() + for { + typ, data, err := c.recvPacket() + if err != nil { + return err + } + sid, _ := unmarshalUint32(data) + c.Lock() + ch, ok := c.inflight[sid] + delete(c.inflight, sid) + c.Unlock() + if !ok { + // This is an unexpected occurrence. Send the error + // back to all listeners so that they terminate + // gracefully. + return errors.Errorf("sid: %v not fond", sid) + } + ch <- result{typ: typ, data: data} + } +} + +// result captures the result of receiving the a packet from the server +type result struct { + typ byte + data []byte + err error +} + +type idmarshaler interface { + id() uint32 + encoding.BinaryMarshaler +} + +func (c *clientConn) sendPacket(p idmarshaler) (byte, []byte, error) { + ch := make(chan result, 1) + c.dispatchRequest(ch, p) + s := <-ch + return s.typ, s.data, s.err +} + +func (c *clientConn) dispatchRequest(ch chan<- result, p idmarshaler) { + c.Lock() + c.inflight[p.id()] = ch + if err := c.conn.sendPacket(p); err != nil { + delete(c.inflight, p.id()) + ch <- result{err: err} + } + c.Unlock() +} + +// broadcastErr sends an error to all goroutines waiting for a response. +func (c *clientConn) broadcastErr(err error) { + c.Lock() + listeners := make([]chan<- result, 0, len(c.inflight)) + for _, ch := range c.inflight { + listeners = append(listeners, ch) + } + c.Unlock() + for _, ch := range listeners { + ch <- result{err: err} + } +} + +type serverConn struct { + conn +} + +func (s *serverConn) sendError(p id, err error) error { + return s.sendPacket(statusFromError(p, err)) +} diff --git a/vendor/src/github.com/pkg/sftp/examples/sftp-server/main.go b/vendor/src/github.com/pkg/sftp/examples/sftp-server/main.go index 115d35c35..acc4d90bd 100644 --- a/vendor/src/github.com/pkg/sftp/examples/sftp-server/main.go +++ b/vendor/src/github.com/pkg/sftp/examples/sftp-server/main.go @@ -118,11 +118,20 @@ func main() { } }(requests) + serverOptions := []sftp.ServerOption{ + sftp.WithDebug(debugStream), + } + + if readOnly { + serverOptions = append(serverOptions, sftp.ReadOnly()) + fmt.Fprintf(debugStream, "Read-only server\n") + } else { + fmt.Fprintf(debugStream, "Read write server\n") + } + server, err := sftp.NewServer( channel, - channel, - sftp.WithDebug(debugStream), - sftp.ReadOnly(), + serverOptions..., ) if err != nil { log.Fatal(err) diff --git a/vendor/src/github.com/pkg/sftp/packet.go b/vendor/src/github.com/pkg/sftp/packet.go index cd95c7246..fba415995 100644 --- a/vendor/src/github.com/pkg/sftp/packet.go +++ b/vendor/src/github.com/pkg/sftp/packet.go @@ -1,16 +1,20 @@ package sftp import ( + "bytes" "encoding" - "errors" + "encoding/binary" "fmt" "io" "os" "reflect" + + "github.com/pkg/errors" ) var ( - errShortPacket = errors.New("packet too short") + errShortPacket = errors.New("packet too short") + errUnknownExtendedPacket = errors.New("unknown extended packet") ) const ( @@ -114,7 +118,7 @@ func unmarshalStringSafe(b []byte) (string, []byte, error) { func sendPacket(w io.Writer, m encoding.BinaryMarshaler) error { bb, err := m.MarshalBinary() if err != nil { - return fmt.Errorf("marshal2(%#v): binary marshaller failed", err) + return errors.Errorf("binary marshaller failed: %v", err) } if debugDumpTxPacketBytes { debug("send packet: %s %d bytes %x", fxp(bb[0]), len(bb), bb[1:]) @@ -125,17 +129,13 @@ func sendPacket(w io.Writer, m encoding.BinaryMarshaler) error { hdr := []byte{byte(l >> 24), byte(l >> 16), byte(l >> 8), byte(l)} _, err = w.Write(hdr) if err != nil { - return err + return errors.Errorf("failed to send packet header: %v", err) } _, err = w.Write(bb) - return err -} - -func (svr *Server) sendPacket(m encoding.BinaryMarshaler) error { - // any responder can call sendPacket(); actual socket access must be serialized - svr.outMutex.Lock() - defer svr.outMutex.Unlock() - return sendPacket(svr.out, m) + if err != nil { + return errors.Errorf("failed to send packet body: %v", err) + } + return nil } func recvPacket(r io.Reader) (uint8, []byte, error) { @@ -259,10 +259,7 @@ func unmarshalIDString(b []byte, id *uint32, str *string) error { return err } *str, b, err = unmarshalStringSafe(b) - if err != nil { - return err - } - return nil + return err } type sshFxpReaddirPacket struct { @@ -318,7 +315,7 @@ type sshFxpStatPacket struct { func (p sshFxpStatPacket) id() uint32 { return p.ID } func (p sshFxpStatPacket) MarshalBinary() ([]byte, error) { - return marshalIDString(ssh_FXP_LSTAT, p.ID, p.Path) + return marshalIDString(ssh_FXP_STAT, p.ID, p.Path) } func (p *sshFxpStatPacket) UnmarshalBinary(b []byte) error { @@ -838,3 +835,67 @@ func (p *StatVFS) TotalSpace() uint64 { func (p *StatVFS) FreeSpace() uint64 { return p.Frsize * p.Bfree } + +// Convert to ssh_FXP_EXTENDED_REPLY packet binary format +func (p *StatVFS) MarshalBinary() ([]byte, error) { + var buf bytes.Buffer + buf.Write([]byte{ssh_FXP_EXTENDED_REPLY}) + err := binary.Write(&buf, binary.BigEndian, p) + return buf.Bytes(), err +} + +type sshFxpExtendedPacket struct { + ID uint32 + ExtendedRequest string + SpecificPacket interface { + serverRespondablePacket + readonly() bool + } +} + +func (p sshFxpExtendedPacket) id() uint32 { return p.ID } +func (p sshFxpExtendedPacket) readonly() bool { return p.SpecificPacket.readonly() } + +func (p sshFxpExtendedPacket) respond(svr *Server) error { + return p.SpecificPacket.respond(svr) +} + +func (p *sshFxpExtendedPacket) UnmarshalBinary(b []byte) error { + var err error + bOrig := b + if p.ID, b, err = unmarshalUint32Safe(b); err != nil { + return err + } else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil { + return err + } + + // specific unmarshalling + switch p.ExtendedRequest { + case "statvfs@openssh.com": + p.SpecificPacket = &sshFxpExtendedPacketStatVFS{} + default: + return errUnknownExtendedPacket + } + + return p.SpecificPacket.UnmarshalBinary(bOrig) +} + +type sshFxpExtendedPacketStatVFS struct { + ID uint32 + ExtendedRequest string + Path string +} + +func (p sshFxpExtendedPacketStatVFS) id() uint32 { return p.ID } +func (p sshFxpExtendedPacketStatVFS) readonly() bool { return true } +func (p *sshFxpExtendedPacketStatVFS) UnmarshalBinary(b []byte) error { + var err error + if p.ID, b, err = unmarshalUint32Safe(b); err != nil { + return err + } else if p.ExtendedRequest, b, err = unmarshalStringSafe(b); err != nil { + return err + } else if p.Path, b, err = unmarshalStringSafe(b); err != nil { + return err + } + return nil +} diff --git a/vendor/src/github.com/pkg/sftp/server.go b/vendor/src/github.com/pkg/sftp/server.go index 81b165917..e097bda30 100644 --- a/vendor/src/github.com/pkg/sftp/server.go +++ b/vendor/src/github.com/pkg/sftp/server.go @@ -13,6 +13,8 @@ import ( "sync" "syscall" "time" + + "github.com/pkg/errors" ) const ( @@ -24,18 +26,14 @@ const ( // This implementation currently supports most of sftp server protocol version 3, // as specified at http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02 type Server struct { - in io.Reader - out io.WriteCloser - outMutex *sync.Mutex + serverConn debugStream io.Writer readOnly bool - lastID uint32 pktChan chan rxPacket openFiles map[string]*os.File - openFilesLock *sync.RWMutex + openFilesLock sync.RWMutex handleCount int maxTxPacket uint32 - workerCount int } func (svr *Server) nextHandle(f *os.File) string { @@ -69,7 +67,6 @@ type serverRespondablePacket interface { encoding.BinaryUnmarshaler id() uint32 respond(svr *Server) error - readonly() bool } // NewServer creates a new Server instance around the provided streams, serving @@ -77,17 +74,18 @@ type serverRespondablePacket interface { // functions may be specified to further configure the Server. // // A subsequent call to Serve() is required to begin serving files over SFTP. -func NewServer(in io.Reader, out io.WriteCloser, options ...ServerOption) (*Server, error) { +func NewServer(rwc io.ReadWriteCloser, options ...ServerOption) (*Server, error) { s := &Server{ - in: in, - out: out, - outMutex: &sync.Mutex{}, - debugStream: ioutil.Discard, - pktChan: make(chan rxPacket, sftpServerWorkerCount), - openFiles: map[string]*os.File{}, - openFilesLock: &sync.RWMutex{}, - maxTxPacket: 1 << 15, - workerCount: sftpServerWorkerCount, + serverConn: serverConn{ + conn: conn{ + Reader: rwc, + WriteCloser: rwc, + }, + }, + debugStream: ioutil.Discard, + pktChan: make(chan rxPacket, sftpServerWorkerCount), + openFiles: make(map[string]*os.File), + maxTxPacket: 1 << 15, } for _, o := range options { @@ -123,123 +121,261 @@ type rxPacket struct { pktBytes []byte } -// Unmarshal a single logical packet from the secure channel -func (svr *Server) rxPackets() error { - defer close(svr.pktChan) - - for { - pktType, pktBytes, err := recvPacket(svr.in) - switch err { - case nil: - svr.pktChan <- rxPacket{fxp(pktType), pktBytes} - case io.EOF: - return nil +// Up to N parallel servers +func (svr *Server) sftpServerWorker() error { + for p := range svr.pktChan { + var pkt interface { + encoding.BinaryUnmarshaler + id() uint32 + } + var readonly = true + switch p.pktType { + case ssh_FXP_INIT: + pkt = &sshFxInitPacket{} + case ssh_FXP_LSTAT: + pkt = &sshFxpLstatPacket{} + case ssh_FXP_OPEN: + pkt = &sshFxpOpenPacket{} + // readonly handled specially below + case ssh_FXP_CLOSE: + pkt = &sshFxpClosePacket{} + case ssh_FXP_READ: + pkt = &sshFxpReadPacket{} + case ssh_FXP_WRITE: + pkt = &sshFxpWritePacket{} + readonly = false + case ssh_FXP_FSTAT: + pkt = &sshFxpFstatPacket{} + case ssh_FXP_SETSTAT: + pkt = &sshFxpSetstatPacket{} + readonly = false + case ssh_FXP_FSETSTAT: + pkt = &sshFxpFsetstatPacket{} + readonly = false + case ssh_FXP_OPENDIR: + pkt = &sshFxpOpendirPacket{} + case ssh_FXP_READDIR: + pkt = &sshFxpReaddirPacket{} + case ssh_FXP_REMOVE: + pkt = &sshFxpRemovePacket{} + readonly = false + case ssh_FXP_MKDIR: + pkt = &sshFxpMkdirPacket{} + readonly = false + case ssh_FXP_RMDIR: + pkt = &sshFxpRmdirPacket{} + readonly = false + case ssh_FXP_REALPATH: + pkt = &sshFxpRealpathPacket{} + case ssh_FXP_STAT: + pkt = &sshFxpStatPacket{} + case ssh_FXP_RENAME: + pkt = &sshFxpRenamePacket{} + readonly = false + case ssh_FXP_READLINK: + pkt = &sshFxpReadlinkPacket{} + case ssh_FXP_SYMLINK: + pkt = &sshFxpSymlinkPacket{} + readonly = false + case ssh_FXP_EXTENDED: + pkt = &sshFxpExtendedPacket{} default: - fmt.Fprintf(svr.debugStream, "recvPacket error: %v\n", err) + return errors.Errorf("unhandled packet type: %s", p.pktType) + } + if err := pkt.UnmarshalBinary(p.pktBytes); err != nil { return err } - } -} -// Up to N parallel servers -func (svr *Server) sftpServerWorker(doneChan chan error) { - for pkt := range svr.pktChan { - dPkt, err := svr.decodePacket(pkt.pktType, pkt.pktBytes) - if err != nil { - fmt.Fprintf(svr.debugStream, "decodePacket error: %v\n", err) - doneChan <- err - return + // handle FXP_OPENDIR specially + switch pkt := pkt.(type) { + case *sshFxpOpenPacket: + readonly = pkt.readonly() + case *sshFxpExtendedPacket: + readonly = pkt.SpecificPacket.readonly() } // If server is operating read-only and a write operation is requested, // return permission denied - if !dPkt.readonly() && svr.readOnly { - _ = svr.sendPacket(statusFromError(dPkt.id(), syscall.EPERM)) + if !readonly && svr.readOnly { + if err := svr.sendError(pkt, syscall.EPERM); err != nil { + return errors.Wrap(err, "failed to send read only packet response") + } continue } - _ = dPkt.respond(svr) + if err := handlePacket(svr, pkt); err != nil { + return err + } + } + return nil +} + +func handlePacket(s *Server, p interface{}) error { + switch p := p.(type) { + case *sshFxInitPacket: + return s.sendPacket(sshFxVersionPacket{sftpProtocolVersion, nil}) + case *sshFxpStatPacket: + // stat the requested file + info, err := os.Stat(p.Path) + if err != nil { + return s.sendError(p, err) + } + return s.sendPacket(sshFxpStatResponse{ + ID: p.ID, + info: info, + }) + case *sshFxpLstatPacket: + // stat the requested file + info, err := os.Lstat(p.Path) + if err != nil { + return s.sendError(p, err) + } + return s.sendPacket(sshFxpStatResponse{ + ID: p.ID, + info: info, + }) + case *sshFxpFstatPacket: + f, ok := s.getHandle(p.Handle) + if !ok { + return s.sendError(p, syscall.EBADF) + } + + info, err := f.Stat() + if err != nil { + return s.sendError(p, err) + } + + return s.sendPacket(sshFxpStatResponse{ + ID: p.ID, + info: info, + }) + case *sshFxpMkdirPacket: + // TODO FIXME: ignore flags field + err := os.Mkdir(p.Path, 0755) + return s.sendError(p, err) + case *sshFxpRmdirPacket: + err := os.Remove(p.Path) + return s.sendError(p, err) + case *sshFxpRemovePacket: + err := os.Remove(p.Filename) + return s.sendError(p, err) + case *sshFxpRenamePacket: + err := os.Rename(p.Oldpath, p.Newpath) + return s.sendError(p, err) + case *sshFxpSymlinkPacket: + err := os.Symlink(p.Targetpath, p.Linkpath) + return s.sendError(p, err) + case *sshFxpClosePacket: + return s.sendError(p, s.closeHandle(p.Handle)) + case *sshFxpReadlinkPacket: + f, err := os.Readlink(p.Path) + if err != nil { + return s.sendError(p, err) + } + + return s.sendPacket(sshFxpNamePacket{ + ID: p.ID, + NameAttrs: []sshFxpNameAttr{{ + Name: f, + LongName: f, + Attrs: emptyFileStat, + }}, + }) + + case *sshFxpRealpathPacket: + f, err := filepath.Abs(p.Path) + if err != nil { + return s.sendError(p, err) + } + f = filepath.Clean(f) + return s.sendPacket(sshFxpNamePacket{ + ID: p.ID, + NameAttrs: []sshFxpNameAttr{{ + Name: f, + LongName: f, + Attrs: emptyFileStat, + }}, + }) + case *sshFxpOpendirPacket: + return sshFxpOpenPacket{ + ID: p.ID, + Path: p.Path, + Pflags: ssh_FXF_READ, + }.respond(s) + case *sshFxpReadPacket: + f, ok := s.getHandle(p.Handle) + if !ok { + return s.sendError(p, syscall.EBADF) + } + + data := make([]byte, clamp(p.Len, s.maxTxPacket)) + n, err := f.ReadAt(data, int64(p.Offset)) + if err != nil && (err != io.EOF || n == 0) { + return s.sendError(p, err) + } + return s.sendPacket(sshFxpDataPacket{ + ID: p.ID, + Length: uint32(n), + Data: data[:n], + }) + case *sshFxpWritePacket: + f, ok := s.getHandle(p.Handle) + if !ok { + return s.sendError(p, syscall.EBADF) + } + + _, err := f.WriteAt(p.Data, int64(p.Offset)) + return s.sendError(p, err) + case serverRespondablePacket: + err := p.respond(s) + return errors.Wrap(err, "pkt.respond failed") + default: + return errors.Errorf("unexpected packet type %T", p) } - doneChan <- nil } // Serve serves SFTP connections until the streams stop or the SFTP subsystem // is stopped. func (svr *Server) Serve() error { - go svr.rxPackets() - doneChan := make(chan error) - for i := 0; i < svr.workerCount; i++ { - go svr.sftpServerWorker(doneChan) + var wg sync.WaitGroup + wg.Add(sftpServerWorkerCount) + for i := 0; i < sftpServerWorkerCount; i++ { + go func() { + defer wg.Done() + if err := svr.sftpServerWorker(); err != nil { + svr.conn.Close() // shuts down recvPacket + } + }() } - for i := 0; i < svr.workerCount; i++ { - if err := <-doneChan; err != nil { - // abort early and shut down the session on un-decodable packets + + var err error + var pktType uint8 + var pktBytes []byte + for { + pktType, pktBytes, err = svr.recvPacket() + if err != nil { break } + svr.pktChan <- rxPacket{fxp(pktType), pktBytes} } + + close(svr.pktChan) // shuts down sftpServerWorkers + wg.Wait() // wait for all workers to exit + // close any still-open files for handle, file := range svr.openFiles { - fmt.Fprintf(svr.debugStream, "sftp server file with handle '%v' left open: %v\n", handle, file.Name()) + fmt.Fprintf(svr.debugStream, "sftp server file with handle %q left open: %v\n", handle, file.Name()) file.Close() } - return svr.out.Close() + return err // error from recvPacket } -func (svr *Server) decodePacket(pktType fxp, pktBytes []byte) (serverRespondablePacket, error) { - var pkt serverRespondablePacket - switch pktType { - case ssh_FXP_INIT: - pkt = &sshFxInitPacket{} - case ssh_FXP_LSTAT: - pkt = &sshFxpLstatPacket{} - case ssh_FXP_OPEN: - pkt = &sshFxpOpenPacket{} - case ssh_FXP_CLOSE: - pkt = &sshFxpClosePacket{} - case ssh_FXP_READ: - pkt = &sshFxpReadPacket{} - case ssh_FXP_WRITE: - pkt = &sshFxpWritePacket{} - case ssh_FXP_FSTAT: - pkt = &sshFxpFstatPacket{} - case ssh_FXP_SETSTAT: - pkt = &sshFxpSetstatPacket{} - case ssh_FXP_FSETSTAT: - pkt = &sshFxpFsetstatPacket{} - case ssh_FXP_OPENDIR: - pkt = &sshFxpOpendirPacket{} - case ssh_FXP_READDIR: - pkt = &sshFxpReaddirPacket{} - case ssh_FXP_REMOVE: - pkt = &sshFxpRemovePacket{} - case ssh_FXP_MKDIR: - pkt = &sshFxpMkdirPacket{} - case ssh_FXP_RMDIR: - pkt = &sshFxpRmdirPacket{} - case ssh_FXP_REALPATH: - pkt = &sshFxpRealpathPacket{} - case ssh_FXP_STAT: - pkt = &sshFxpStatPacket{} - case ssh_FXP_RENAME: - pkt = &sshFxpRenamePacket{} - case ssh_FXP_READLINK: - pkt = &sshFxpReadlinkPacket{} - case ssh_FXP_SYMLINK: - pkt = &sshFxpSymlinkPacket{} - default: - return nil, fmt.Errorf("unhandled packet type: %s", pktType) - } - err := pkt.UnmarshalBinary(pktBytes) - return pkt, err -} - -func (p sshFxInitPacket) respond(svr *Server) error { - return svr.sendPacket(sshFxVersionPacket{sftpProtocolVersion, nil}) +type id interface { + id() uint32 } // The init packet has no ID, so we just return a zero-value ID -func (p sshFxInitPacket) id() uint32 { return 0 } -func (p sshFxInitPacket) readonly() bool { return true } +func (p sshFxInitPacket) id() uint32 { return 0 } type sshFxpStatResponse struct { ID uint32 @@ -253,141 +389,8 @@ func (p sshFxpStatResponse) MarshalBinary() ([]byte, error) { return b, nil } -func (p sshFxpLstatPacket) readonly() bool { return true } - -func (p sshFxpLstatPacket) respond(svr *Server) error { - // stat the requested file - info, err := os.Lstat(p.Path) - if err != nil { - return svr.sendPacket(statusFromError(p.ID, err)) - } - - return svr.sendPacket(sshFxpStatResponse{ - ID: p.ID, - info: info, - }) -} - -func (p sshFxpStatPacket) readonly() bool { return true } - -func (p sshFxpStatPacket) respond(svr *Server) error { - // stat the requested file - info, err := os.Stat(p.Path) - if err != nil { - return svr.sendPacket(statusFromError(p.ID, err)) - } - - return svr.sendPacket(sshFxpStatResponse{ - ID: p.ID, - info: info, - }) -} - -func (p sshFxpFstatPacket) readonly() bool { return true } - -func (p sshFxpFstatPacket) respond(svr *Server) error { - f, ok := svr.getHandle(p.Handle) - if !ok { - return svr.sendPacket(statusFromError(p.ID, syscall.EBADF)) - } - - info, err := f.Stat() - if err != nil { - return svr.sendPacket(statusFromError(p.ID, err)) - } - - return svr.sendPacket(sshFxpStatResponse{ - ID: p.ID, - info: info, - }) -} - -func (p sshFxpMkdirPacket) readonly() bool { return false } - -func (p sshFxpMkdirPacket) respond(svr *Server) error { - // TODO FIXME: ignore flags field - err := os.Mkdir(p.Path, 0755) - return svr.sendPacket(statusFromError(p.ID, err)) -} - -func (p sshFxpRmdirPacket) readonly() bool { return false } - -func (p sshFxpRmdirPacket) respond(svr *Server) error { - err := os.Remove(p.Path) - return svr.sendPacket(statusFromError(p.ID, err)) -} - -func (p sshFxpRemovePacket) readonly() bool { return false } - -func (p sshFxpRemovePacket) respond(svr *Server) error { - err := os.Remove(p.Filename) - return svr.sendPacket(statusFromError(p.ID, err)) -} - -func (p sshFxpRenamePacket) readonly() bool { return false } - -func (p sshFxpRenamePacket) respond(svr *Server) error { - err := os.Rename(p.Oldpath, p.Newpath) - return svr.sendPacket(statusFromError(p.ID, err)) -} - -func (p sshFxpSymlinkPacket) readonly() bool { return false } - -func (p sshFxpSymlinkPacket) respond(svr *Server) error { - err := os.Symlink(p.Targetpath, p.Linkpath) - return svr.sendPacket(statusFromError(p.ID, err)) -} - var emptyFileStat = []interface{}{uint32(0)} -func (p sshFxpReadlinkPacket) readonly() bool { return true } - -func (p sshFxpReadlinkPacket) respond(svr *Server) error { - f, err := os.Readlink(p.Path) - if err != nil { - return svr.sendPacket(statusFromError(p.ID, err)) - } - - return svr.sendPacket(sshFxpNamePacket{ - ID: p.ID, - NameAttrs: []sshFxpNameAttr{{ - Name: f, - LongName: f, - Attrs: emptyFileStat, - }}, - }) -} - -func (p sshFxpRealpathPacket) readonly() bool { return true } - -func (p sshFxpRealpathPacket) respond(svr *Server) error { - f, err := filepath.Abs(p.Path) - if err != nil { - return svr.sendPacket(statusFromError(p.ID, err)) - } - - f = filepath.Clean(f) - - return svr.sendPacket(sshFxpNamePacket{ - ID: p.ID, - NameAttrs: []sshFxpNameAttr{{ - Name: f, - LongName: f, - Attrs: emptyFileStat, - }}, - }) -} - -func (p sshFxpOpendirPacket) readonly() bool { return true } - -func (p sshFxpOpendirPacket) respond(svr *Server) error { - return sshFxpOpenPacket{ - ID: p.ID, - Path: p.Path, - Pflags: ssh_FXF_READ, - }.respond(svr) -} - func (p sshFxpOpenPacket) readonly() bool { return !p.hasPflags(ssh_FXF_WRITE) } @@ -398,7 +401,6 @@ func (p sshFxpOpenPacket) hasPflags(flags ...uint32) bool { return false } } - return true } @@ -412,7 +414,7 @@ func (p sshFxpOpenPacket) respond(svr *Server) error { osFlags |= os.O_RDONLY } else { // how are they opening? - return svr.sendPacket(statusFromError(p.ID, syscall.EINVAL)) + return svr.sendError(p, syscall.EINVAL) } if p.hasPflags(ssh_FXF_APPEND) { @@ -430,69 +432,23 @@ func (p sshFxpOpenPacket) respond(svr *Server) error { f, err := os.OpenFile(p.Path, osFlags, 0644) if err != nil { - return svr.sendPacket(statusFromError(p.ID, err)) + return svr.sendError(p, err) } handle := svr.nextHandle(f) return svr.sendPacket(sshFxpHandlePacket{p.ID, handle}) } -func (p sshFxpClosePacket) readonly() bool { return true } - -func (p sshFxpClosePacket) respond(svr *Server) error { - return svr.sendPacket(statusFromError(p.ID, svr.closeHandle(p.Handle))) -} - -func (p sshFxpReadPacket) readonly() bool { return true } - -func (p sshFxpReadPacket) respond(svr *Server) error { - f, ok := svr.getHandle(p.Handle) - if !ok { - return svr.sendPacket(statusFromError(p.ID, syscall.EBADF)) - } - - if p.Len > svr.maxTxPacket { - p.Len = svr.maxTxPacket - } - ret := sshFxpDataPacket{ - ID: p.ID, - Length: p.Len, - Data: make([]byte, p.Len), - } - - n, err := f.ReadAt(ret.Data, int64(p.Offset)) - if err != nil && (err != io.EOF || n == 0) { - return svr.sendPacket(statusFromError(p.ID, err)) - } - - ret.Length = uint32(n) - return svr.sendPacket(ret) -} - -func (p sshFxpWritePacket) readonly() bool { return false } - -func (p sshFxpWritePacket) respond(svr *Server) error { - f, ok := svr.getHandle(p.Handle) - if !ok { - return svr.sendPacket(statusFromError(p.ID, syscall.EBADF)) - } - - _, err := f.WriteAt(p.Data, int64(p.Offset)) - return svr.sendPacket(statusFromError(p.ID, err)) -} - -func (p sshFxpReaddirPacket) readonly() bool { return true } - func (p sshFxpReaddirPacket) respond(svr *Server) error { f, ok := svr.getHandle(p.Handle) if !ok { - return svr.sendPacket(statusFromError(p.ID, syscall.EBADF)) + return svr.sendError(p, syscall.EBADF) } dirname := f.Name() dirents, err := f.Readdir(128) if err != nil { - return svr.sendPacket(statusFromError(p.ID, err)) + return svr.sendError(p, err) } ret := sshFxpNamePacket{ID: p.ID} @@ -506,8 +462,6 @@ func (p sshFxpReaddirPacket) respond(svr *Server) error { return svr.sendPacket(ret) } -func (p sshFxpSetstatPacket) readonly() bool { return false } - func (p sshFxpSetstatPacket) respond(svr *Server) error { // additional unmarshalling is required for each possibility here b := p.Attrs.([]byte) @@ -547,15 +501,13 @@ func (p sshFxpSetstatPacket) respond(svr *Server) error { } } - return svr.sendPacket(statusFromError(p.ID, err)) + return svr.sendError(p, err) } -func (p sshFxpFsetstatPacket) readonly() bool { return false } - func (p sshFxpFsetstatPacket) respond(svr *Server) error { f, ok := svr.getHandle(p.Handle) if !ok { - return svr.sendPacket(statusFromError(p.ID, syscall.EBADF)) + return svr.sendError(p, syscall.EBADF) } // additional unmarshalling is required for each possibility here @@ -596,7 +548,7 @@ func (p sshFxpFsetstatPacket) respond(svr *Server) error { } } - return svr.sendPacket(statusFromError(p.ID, err)) + return svr.sendError(p, err) } // translateErrno translates a syscall error number to a SFTP error code. @@ -613,9 +565,9 @@ func translateErrno(errno syscall.Errno) uint32 { return ssh_FX_FAILURE } -func statusFromError(id uint32, err error) sshFxpStatusPacket { +func statusFromError(p id, err error) sshFxpStatusPacket { ret := sshFxpStatusPacket{ - ID: id, + ID: p.id(), StatusError: StatusError{ // ssh_FX_OK = 0 // ssh_FX_EOF = 1 @@ -646,3 +598,10 @@ func statusFromError(id uint32, err error) sshFxpStatusPacket { } return ret } + +func clamp(v, max uint32) uint32 { + if v > max { + return max + } + return v +} diff --git a/vendor/src/github.com/pkg/sftp/server_integration_test.go b/vendor/src/github.com/pkg/sftp/server_integration_test.go index 4d70829e5..c5786074a 100644 --- a/vendor/src/github.com/pkg/sftp/server_integration_test.go +++ b/vendor/src/github.com/pkg/sftp/server_integration_test.go @@ -295,7 +295,6 @@ func (chsvr *sshSessionChannelServer) handleSubsystem(req *ssh.Request) error { } sftpServer, err := NewServer( - chsvr.ch, chsvr.ch, WithDebug(sftpServerDebugStream), ) diff --git a/vendor/src/github.com/pkg/sftp/server_standalone/main.go b/vendor/src/github.com/pkg/sftp/server_standalone/main.go index f7fcccdee..646e99dfc 100644 --- a/vendor/src/github.com/pkg/sftp/server_standalone/main.go +++ b/vendor/src/github.com/pkg/sftp/server_standalone/main.go @@ -6,6 +6,7 @@ package main import ( "flag" "fmt" + "io" "io/ioutil" "os" @@ -16,10 +17,12 @@ func main() { var ( readOnly bool debugStderr bool + debugLevel string ) flag.BoolVar(&readOnly, "R", false, "read-only server") flag.BoolVar(&debugStderr, "e", false, "debug to stderr") + flag.StringVar(&debugLevel, "l", "none", "debug level (ignored)") flag.Parse() debugStream := ioutil.Discard @@ -28,8 +31,12 @@ func main() { } svr, _ := sftp.NewServer( - os.Stdin, - os.Stdout, + struct { + io.Reader + io.WriteCloser + }{os.Stdin, + os.Stdout, + }, sftp.WithDebug(debugStream), sftp.ReadOnly(), ) diff --git a/vendor/src/github.com/pkg/sftp/server_statvfs_darwin.go b/vendor/src/github.com/pkg/sftp/server_statvfs_darwin.go new file mode 100644 index 000000000..8c01dac52 --- /dev/null +++ b/vendor/src/github.com/pkg/sftp/server_statvfs_darwin.go @@ -0,0 +1,21 @@ +package sftp + +import ( + "syscall" +) + +func statvfsFromStatfst(stat *syscall.Statfs_t) (*StatVFS, error) { + return &StatVFS{ + Bsize: uint64(stat.Bsize), + Frsize: uint64(stat.Bsize), // fragment size is a linux thing; use block size here + Blocks: stat.Blocks, + Bfree: stat.Bfree, + Bavail: stat.Bavail, + Files: stat.Files, + Ffree: stat.Ffree, + Favail: stat.Ffree, // not sure how to calculate Favail + Fsid: uint64(uint64(stat.Fsid.Val[1])<<32 | uint64(stat.Fsid.Val[0])), // endianness? + Flag: uint64(stat.Flags), // assuming POSIX? + Namemax: 1024, // man 2 statfs shows: #define MAXPATHLEN 1024 + }, nil +} diff --git a/vendor/src/github.com/pkg/sftp/server_statvfs_impl.go b/vendor/src/github.com/pkg/sftp/server_statvfs_impl.go new file mode 100644 index 000000000..c26870ebe --- /dev/null +++ b/vendor/src/github.com/pkg/sftp/server_statvfs_impl.go @@ -0,0 +1,25 @@ +// +build darwin linux,!gccgo + +// fill in statvfs structure with OS specific values +// Statfs_t is different per-kernel, and only exists on some unixes (not Solaris for instance) + +package sftp + +import ( + "syscall" +) + +func (p sshFxpExtendedPacketStatVFS) respond(svr *Server) error { + stat := &syscall.Statfs_t{} + if err := syscall.Statfs(p.Path, stat); err != nil { + return svr.sendPacket(statusFromError(p, err)) + } + + retPkt, err := statvfsFromStatfst(stat) + if err != nil { + return svr.sendPacket(statusFromError(p, err)) + } + retPkt.ID = p.ID + + return svr.sendPacket(retPkt) +} diff --git a/vendor/src/github.com/pkg/sftp/server_statvfs_linux.go b/vendor/src/github.com/pkg/sftp/server_statvfs_linux.go new file mode 100644 index 000000000..77fd1bf43 --- /dev/null +++ b/vendor/src/github.com/pkg/sftp/server_statvfs_linux.go @@ -0,0 +1,23 @@ +// +build !gccgo,linux + +package sftp + +import ( + "syscall" +) + +func statvfsFromStatfst(stat *syscall.Statfs_t) (*StatVFS, error) { + return &StatVFS{ + Bsize: uint64(stat.Bsize), + Frsize: uint64(stat.Frsize), + Blocks: stat.Blocks, + Bfree: stat.Bfree, + Bavail: stat.Bavail, + Files: stat.Files, + Ffree: stat.Ffree, + Favail: stat.Ffree, // not sure how to calculate Favail + Fsid: uint64(uint64(stat.Fsid.X__val[1])<<32 | uint64(stat.Fsid.X__val[0])), // endianness? + Flag: uint64(stat.Flags), // assuming POSIX? + Namemax: uint64(stat.Namelen), + }, nil +} diff --git a/vendor/src/github.com/pkg/sftp/server_statvfs_stubs.go b/vendor/src/github.com/pkg/sftp/server_statvfs_stubs.go new file mode 100644 index 000000000..1512a132b --- /dev/null +++ b/vendor/src/github.com/pkg/sftp/server_statvfs_stubs.go @@ -0,0 +1,11 @@ +// +build !darwin,!linux gccgo + +package sftp + +import ( + "syscall" +) + +func (p sshFxpExtendedPacketStatVFS) respond(svr *Server) error { + return syscall.ENOTSUP +} diff --git a/vendor/src/github.com/pkg/sftp/server_test.go b/vendor/src/github.com/pkg/sftp/server_test.go new file mode 100644 index 000000000..2ff02ea22 --- /dev/null +++ b/vendor/src/github.com/pkg/sftp/server_test.go @@ -0,0 +1,64 @@ +package sftp + +import ( + "io" + "testing" +) + +func clientServerPair(t *testing.T) (*Client, *Server) { + cr, sw := io.Pipe() + sr, cw := io.Pipe() + server, err := NewServer(struct { + io.Reader + io.WriteCloser + }{sr, sw}) + if err != nil { + t.Fatal(err) + } + go server.Serve() + client, err := NewClientPipe(cr, cw) + if err != nil { + t.Fatalf("%+v\n", err) + } + return client, server +} + +type sshFxpTestBadExtendedPacket struct { + ID uint32 + Extension string + Data string +} + +func (p sshFxpTestBadExtendedPacket) id() uint32 { return p.ID } + +func (p sshFxpTestBadExtendedPacket) MarshalBinary() ([]byte, error) { + l := 1 + 4 + 4 + // type(byte) + uint32 + uint32 + len(p.Extension) + + len(p.Data) + + b := make([]byte, 0, l) + b = append(b, ssh_FXP_EXTENDED) + b = marshalUint32(b, p.ID) + b = marshalString(b, p.Extension) + b = marshalString(b, p.Data) + return b, nil +} + +// test that errors are sent back when we request an invalid extended packet operation +func TestInvalidExtendedPacket(t *testing.T) { + client, _ := clientServerPair(t) + defer client.Close() + badPacket := sshFxpTestBadExtendedPacket{client.nextID(), "thisDoesn'tExist", "foobar"} + _, _, err := client.clientConn.sendPacket(badPacket) + if err == nil { + t.Fatal("expected error from bad packet") + } + + // try to stat a file; the client should have shut down. + filePath := "/etc/passwd" + _, err = client.Stat(filePath) + if err == nil { + t.Fatal("expected error from closed connection") + } + +} diff --git a/vendor/src/github.com/pkg/sftp/sftp.go b/vendor/src/github.com/pkg/sftp/sftp.go index 7d96660f8..22184afe0 100644 --- a/vendor/src/github.com/pkg/sftp/sftp.go +++ b/vendor/src/github.com/pkg/sftp/sftp.go @@ -4,6 +4,8 @@ package sftp import ( "fmt" + + "github.com/pkg/errors" ) const ( @@ -182,7 +184,7 @@ func (u *unexpectedPacketErr) Error() string { } func unimplementedPacketErr(u uint8) error { - return fmt.Errorf("sftp: unimplemented packet type: got %v", fxp(u)) + return errors.Errorf("sftp: unimplemented packet type: got %v", fxp(u)) } type unexpectedIDErr struct{ want, got uint32 } @@ -192,11 +194,11 @@ func (u *unexpectedIDErr) Error() string { } func unimplementedSeekWhence(whence int) error { - return fmt.Errorf("sftp: unimplemented seek whence %v", whence) + return errors.Errorf("sftp: unimplemented seek whence %v", whence) } func unexpectedCount(want, got uint32) error { - return fmt.Errorf("sftp: unexpected count: want %v, got %v", want, got) + return errors.Errorf("sftp: unexpected count: want %v, got %v", want, got) } type unexpectedVersionErr struct{ want, got uint32 }