8000 Merge pull request #310 from pkg/issue-305-client-write · etherscan-io/sftp@463bb93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 463bb93

Browse files
authored
Merge pull request pkg#310 from pkg/issue-305-client-write
Issue 305 client write Fixes pkg#305
2 parents 84e6527 + c732bf9 commit 463bb93

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ type Client struct {
167167
// already exists. If successful, methods on the returned File can be used for
168168
// I/O; the associated file descriptor has mode O_RDWR. If you need more
169169
// control over the flags/mode used to open the file see client.OpenFile.
170+
//
171+
// Note that some SFTP servers (eg. AWS Transfer) do not support opening files
172+
// read/write at the same time. For those services you will need to use
173+
// `client.OpenFile(os.O_WRONLY|os.O_CREATE|os.O_TRUNC)`.
170174
func (c *Client) Create(path string) (*File, error) {
171175
return c.open(path, flags(os.O_RDWR|os.O_CREATE|os.O_TRUNC))
172176
}

sftp.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,11 @@ type StatusError struct {
219219
msg, lang string
220220
}
221221

222-
func (s *StatusError) Error() string { return fmt.Sprintf("sftp: %q (%v)", s.msg, fx(s.Code)) }
222+
func (s *StatusError) Error() string {
223+
return fmt.Sprintf("sftp: %q (%v)", s.msg, fx(s.Code))
224+
}
225+
226+
// FxCode returns the error code typed to match against the exported codes
227+
func (s *StatusError) FxCode() fxerr {
228+
return fxerr(s.Code)
229+
}

sftp_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sftp
2+
3+
import (
4+
"io"
5+
"syscall"
6+
"testing"
7+
8+
"github.com/pkg/errors"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestErrFxCode(t *testing.T) {
13+
ider := sshFxpStatusPacket{ID: 1}
14+
table := []struct {
15+
err error
16+
fx fxerr
17+
}{
18+
{err: errors.New("random error"), fx: ErrSSHFxFailure},
19+
{err: syscall.EBADF, fx: ErrSSHFxFailure},
20+
{err: syscall.ENOENT, fx: ErrSSHFxNoSuchFile},
21+
{err: syscall.EPERM, fx: ErrSSHFxPermissionDenied},
22+
{err: io.EOF, fx: ErrSSHFxEOF},
23+
}
24+
for _, tt := range table {
25+
statusErr := statusFromError(ider, tt.err).StatusError
26+
assert.Equal(t, statusErr.FxCode(), tt.fx)
27+
}
28+
}

0 commit comments

Comments
 (0)
0