aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/pkg/sftp/sftp.go
blob: 22184afe0c6401afbb76fe862d10a328b770c8b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Package sftp implements the SSH File Transfer Protocol as described in
// https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
package sftp

import (
	"fmt"

	"github.com/pkg/errors"
)

const (
	ssh_FXP_INIT           = 1
	ssh_FXP_VERSION        = 2
	ssh_FXP_OPEN           = 3
	ssh_FXP_CLOSE          = 4
	ssh_FXP_READ           = 5
	ssh_FXP_WRITE          = 6
	ssh_FXP_LSTAT          = 7
	ssh_FXP_FSTAT          = 8
	ssh_FXP_SETSTAT        = 9
	ssh_FXP_FSETSTAT       = 10
	ssh_FXP_OPENDIR        = 11
	ssh_FXP_READDIR        = 12
	ssh_FXP_REMOVE         = 13
	ssh_FXP_MKDIR          = 14
	ssh_FXP_RMDIR          = 15
	ssh_FXP_REALPATH       = 16
	ssh_FXP_STAT           = 17
	ssh_FXP_RENAME         = 18
	ssh_FXP_READLINK       = 19
	ssh_FXP_SYMLINK        = 20
	ssh_FXP_STATUS         = 101
	ssh_FXP_HANDLE         = 102
	ssh_FXP_DATA           = 103
	ssh_FXP_NAME           = 104
	ssh_FXP_ATTRS          = 105
	ssh_FXP_EXTENDED       = 200
	ssh_FXP_EXTENDED_REPLY = 201
)

const (
	ssh_FX_OK                = 0
	ssh_FX_EOF               = 1
	ssh_FX_NO_SUCH_FILE      = 2
	ssh_FX_PERMISSION_DENIED = 3
	ssh_FX_FAILURE           = 4
	ssh_FX_BAD_MESSAGE       = 5
	ssh_FX_NO_CONNECTION     = 6
	ssh_FX_CONNECTION_LOST   = 7
	ssh_FX_OP_UNSUPPORTED    = 8

	// see draft-ietf-secsh-filexfer-13
	// https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-9.1
	ssh_FX_INVALID_HANDLE              = 9
	ssh_FX_NO_SUCH_PATH                = 10
	ssh_FX_FILE_ALREADY_EXISTS         = 11
	ssh_FX_WRITE_PROTECT               = 12
	ssh_FX_NO_MEDIA                    = 13
	ssh_FX_NO_SPACE_ON_FILESYSTEM      = 14
	ssh_FX_QUOTA_EXCEEDED              = 15
	ssh_FX_UNKNOWN_PRINCIPAL           = 16
	ssh_FX_LOCK_CONFLICT               = 17
	ssh_FX_DIR_NOT_EMPTY               = 18
	ssh_FX_NOT_A_DIRECTORY             = 19
	ssh_FX_INVALID_FILENAME            = 20
	ssh_FX_LINK_LOOP                   = 21
	ssh_FX_CANNOT_DELETE               = 22
	ssh_FX_INVALID_PARAMETER           = 23
	ssh_FX_FILE_IS_A_DIRECTORY         = 24
	ssh_FX_BYTE_RANGE_LOCK_CONFLICT    = 25
	ssh_FX_BYTE_RANGE_LOCK_REFUSED     = 26
	ssh_FX_DELETE_PENDING              = 27
	ssh_FX_FILE_CORRUPT                = 28
	ssh_FX_OWNER_INVALID               = 29
	ssh_FX_GROUP_INVALID               = 30
	ssh_FX_NO_MATCHING_BYTE_RANGE_LOCK = 31
)

const (
	ssh_FXF_READ   = 0x00000001
	ssh_FXF_WRITE  = 0x00000002
	ssh_FXF_APPEND = 0x00000004
	ssh_FXF_CREAT  = 0x00000008
	ssh_FXF_TRUNC  = 0x00000010
	ssh_FXF_EXCL   = 0x00000020
)

type fxp uint8

func (f fxp) String() string {
	switch f {
	case ssh_FXP_INIT:
		return "SSH_FXP_INIT"
	case ssh_FXP_VERSION:
		return "SSH_FXP_VERSION"
	case ssh_FXP_OPEN:
		return "SSH_FXP_OPEN"
	case ssh_FXP_CLOSE:
		return "SSH_FXP_CLOSE"
	case ssh_FXP_READ:
		return "SSH_FXP_READ"
	case ssh_FXP_WRITE:
		return "SSH_FXP_WRITE"
	case ssh_FXP_LSTAT:
		return "SSH_FXP_LSTAT"
	case ssh_FXP_FSTAT:
		return "SSH_FXP_FSTAT"
	case ssh_FXP_SETSTAT:
		return "SSH_FXP_SETSTAT"
	case ssh_FXP_FSETSTAT:
		return "SSH_FXP_FSETSTAT"
	case ssh_FXP_OPENDIR:
		return "SSH_FXP_OPENDIR"
	case ssh_FXP_READDIR:
		return "SSH_FXP_READDIR"
	case ssh_FXP_REMOVE:
		return "SSH_FXP_REMOVE"
	case ssh_FXP_MKDIR:
		return "SSH_FXP_MKDIR"
	case ssh_FXP_RMDIR:
		return "SSH_FXP_RMDIR"
	case ssh_FXP_REALPATH:
		return "SSH_FXP_REALPATH"
	case ssh_FXP_STAT:
		return "SSH_FXP_STAT"
	case ssh_FXP_RENAME:
		return "SSH_FXP_RENAME"
	case ssh_FXP_READLINK:
		return "SSH_FXP_READLINK"
	case ssh_FXP_SYMLINK:
		return "SSH_FXP_SYMLINK"
	case ssh_FXP_STATUS:
		return "SSH_FXP_STATUS"
	case ssh_FXP_HANDLE:
		return "SSH_FXP_HANDLE"
	case ssh_FXP_DATA:
		return "SSH_FXP_DATA"
	case ssh_FXP_NAME:
		return "SSH_FXP_NAME"
	case ssh_FXP_ATTRS:
		return "SSH_FXP_ATTRS"
	case ssh_FXP_EXTENDED:
		return "SSH_FXP_EXTENDED"
	case ssh_FXP_EXTENDED_REPLY:
		return "SSH_FXP_EXTENDED_REPLY"
	default:
		return "unknown"
	}
}

type fx uint8

func (f fx) String() string {
	switch f {
	case ssh_FX_OK:
		return "SSH_FX_OK"
	case ssh_FX_EOF:
		return "SSH_FX_EOF"
	case ssh_FX_NO_SUCH_FILE:
		return "SSH_FX_NO_SUCH_FILE"
	case ssh_FX_PERMISSION_DENIED:
		return "SSH_FX_PERMISSION_DENIED"
	case ssh_FX_FAILURE:
		return "SSH_FX_FAILURE"
	case ssh_FX_BAD_MESSAGE:
		return "SSH_FX_BAD_MESSAGE"
	case ssh_FX_NO_CONNECTION:
		return "SSH_FX_NO_CONNECTION"
	case ssh_FX_CONNECTION_LOST:
		return "SSH_FX_CONNECTION_LOST"
	case ssh_FX_OP_UNSUPPORTED:
		return "SSH_FX_OP_UNSUPPORTED"
	default:
		return "unknown"
	}
}

type unexpectedPacketErr struct {
	want, got uint8
}

func (u *unexpectedPacketErr) Error() string {
	return fmt.Sprintf("sftp: unexpected packet: want %v, got %v", fxp(u.want), fxp(u.got))
}

func unimplementedPacketErr(u uint8) error {
	return errors.Errorf("sftp: unimplemented packet type: got %v", fxp(u))
}

type unexpectedIDErr struct{ want, got uint32 }

func (u *unexpectedIDErr) Error() string {
	return fmt.Sprintf("sftp: unexpected id: want %v, got %v", u.want, u.got)
}

func unimplementedSeekWhence(whence int) error {
	return errors.Errorf("sftp: unimplemented seek whence %v", whence)
}

func unexpectedCount(want, got uint32) error {
	return errors.Errorf("sftp: unexpected count: want %v, got %v", want, got)
}

type unexpectedVersionErr struct{ want, got uint32 }

func (u *unexpectedVersionErr) Error() string {
	return fmt.Sprintf("sftp: unexpected server version: want %v, got %v", u.want, u.got)
}

// A StatusError is returned when an SFTP operation fails, and provides
// additional information about the failure.
type StatusError struct {
	Code      uint32
	msg, lang string
}

func (s *StatusError) Error() string { return fmt.Sprintf("sftp: %q (%v)", s.msg, fx(s.Code)) }