-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.go
340 lines (305 loc) · 6.62 KB
/
ftp.go
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package joint
import (
"bytes"
"errors"
"io"
"io/fs"
"net/url"
"path"
"strings"
"time"
"github.com/jlaffaye/ftp"
)
var (
ErrFtpWhence = errors.New("invalid whence at FTP seeker")
ErrFtpNegPos = errors.New("negative position at FTP seeker")
)
// FtpEscapeBrackets escapes square brackets at FTP-path.
// FTP-server does not recognize path with square brackets
// as is to get a list of files, so such path should be escaped.
func FtpEscapeBrackets(s string) string {
var n = 0
for _, c := range s {
if c == '[' || c == ']' {
n++
}
}
if n == 0 {
return s
}
var esc = make([]rune, 0, len(s)+n*2)
for _, c := range s {
if c == '[' {
esc = append(esc, '[', '[', ']')
} else if c == ']' {
esc = append(esc, '[', ']', ']')
} else {
esc = append(esc, c)
}
}
return string(esc)
}
// FtpJoint create connection to FTP-server, login with provided by
// given URL credentials, and gets a once current directory.
// Key is address of FTP-service, i.e. ftp://user:pass@example.com.
type FtpJoint struct {
conn *ftp.ServerConn
path string // path inside of FTP-service
list []*ftp.Entry
resp *ftp.Response
pos int64
end int64
rdn int
}
func (j *FtpJoint) Make(base Joint, urladdr string) (err error) {
var u *url.URL
if u, err = url.Parse(urladdr); err != nil {
return
}
if j.conn, err = ftp.Dial(u.Host, ftp.DialWithTimeout(Cfg.DialTimeout)); err != nil {
return
}
var pass, _ = u.User.Password()
if err = j.conn.Login(u.User.Username(), pass); err != nil {
return
}
if u.Path != "" && u.Path != "/" { // skip empty path
var fpath = strings.Trim(u.Path, "/")
if err = j.conn.ChangeDir(fpath); err != nil {
return
}
}
return
}
func (j *FtpJoint) Cleanup() error {
var err1, err2 error
if j.Busy() {
err1 = j.Close()
}
if j.conn != nil {
err2 = j.conn.Quit()
j.conn = nil
}
return errors.Join(err1, err2)
}
func (j *FtpJoint) Busy() bool {
return j.path != ""
}
func (j *FtpJoint) Open(fpath string) (file fs.File, err error) {
if j.Busy() {
return nil, fs.ErrExist
}
j.path = fpath
j.list = nil // delete previous readdir result
j.rdn = 0 // start new sequence
return j, nil
}
func (j *FtpJoint) Close() (err error) {
j.path = ""
if j.resp != nil {
err = j.resp.Close()
j.resp = nil
}
j.pos = 0
j.end = 0
return
}
func (j *FtpJoint) ReadDir(n int) (list []fs.DirEntry, err error) {
if j.resp != nil {
j.resp.Close()
j.resp = nil
}
if j.list == nil {
var fpath = FtpEscapeBrackets(j.path)
var list2 []*ftp.Entry
if list2, err = j.conn.List(fpath); err != nil {
return
}
// skip "." and ".." directories
var i int
for i < len(list2) {
if list2[i].Name == "." || list2[i].Name == ".." {
copy(list2[i:], list2[i+1:])
list2 = list2[:len(list2)-1]
} else {
i++
}
}
j.list = list2
}
if n < 0 {
n = len(j.list) - j.rdn
} else if n > len(j.list)-j.rdn {
n = len(j.list) - j.rdn
err = io.EOF
}
if n <= 0 { // on case all files readed or some deleted
return
}
list = make([]fs.DirEntry, n)
for i := 0; i < n; i++ {
list[i] = FtpFileInfo{j.list[j.rdn+i]}
}
j.rdn += n
return
}
func (j *FtpJoint) Stat() (fs.FileInfo, error) {
if j.resp != nil {
j.resp.Close()
j.resp = nil
}
var ent, err = j.conn.GetEntry(j.path)
if err != nil {
return nil, err
}
return FtpFileInfo{ent}, nil
}
func (j *FtpJoint) Info(fpath string) (fs.FileInfo, error) {
if j.resp != nil {
j.resp.Close()
j.resp = nil
}
var ent, err = j.conn.GetEntry(fpath)
if err != nil {
return nil, err
}
return FtpFileInfo{ent}, nil
}
func (j *FtpJoint) Size() (int64, error) {
if j.resp != nil {
j.resp.Close()
j.resp = nil
}
var err error
if j.end == 0 {
time.Sleep(500 * time.Millisecond)
j.end, err = j.conn.FileSize(j.path)
}
return j.end, err
}
func (j *FtpJoint) ModTime() (time.Time, error) {
if j.resp != nil {
j.resp.Close()
j.resp = nil
}
return j.conn.GetTime(j.path)
}
func (j *FtpJoint) Read(b []byte) (n int, err error) {
if j.resp == nil {
if j.resp, err = j.conn.RetrFrom(j.path, uint64(j.pos)); err != nil {
return
}
}
n, err = j.resp.Read(b)
j.pos += int64(n)
return
}
func (j *FtpJoint) Write(p []byte) (n int, err error) {
var buf = bytes.NewReader(p)
err = j.conn.StorFrom(j.path, buf, uint64(j.pos))
var n64, _ = buf.Seek(0, io.SeekCurrent)
j.pos += n64
n = int(n64)
return
}
func (j *FtpJoint) Seek(offset int64, whence int) (abs int64, err error) {
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = j.pos + offset
case io.SeekEnd:
if j.end == 0 {
if j.end, err = j.conn.FileSize(j.path); err != nil {
return
}
}
abs = j.end + offset
default:
err = ErrFtpWhence
return
}
if abs < 0 {
err = ErrFtpNegPos
return
}
if abs != j.pos && j.resp != nil {
j.resp.Close()
j.resp = nil
}
j.pos = abs
return
}
func (j *FtpJoint) ReadAt(b []byte, off int64) (n int, err error) {
if off < 0 {
err = ErrFtpNegPos
return
}
if off != j.pos && j.resp != nil {
j.resp.Close()
j.resp = nil
}
j.pos = off
return j.Read(b)
}
func (j *FtpJoint) CurrentDir() (wd string, err error) {
return j.conn.CurrentDir()
}
func (j *FtpJoint) ChangeDir(wd string) (err error) {
return j.conn.ChangeDir(wd)
}
func (j *FtpJoint) ChangeDirToParent() (err error) {
return j.conn.ChangeDirToParent()
}
// FtpFileInfo encapsulates ftp.Entry structure and provides fs.FileInfo implementation.
type FtpFileInfo struct {
*ftp.Entry
}
// fs.FileInfo implementation.
func (fi FtpFileInfo) Name() string {
return path.Base(fi.Entry.Name)
}
// fs.FileInfo implementation.
func (fi FtpFileInfo) Size() int64 {
return int64(fi.Entry.Size)
}
// fs.FileInfo implementation.
func (fi FtpFileInfo) Mode() fs.FileMode {
var mode fs.FileMode = 0444
switch fi.Entry.Type {
case ftp.EntryTypeFile:
if IsTypeIso(fi.Entry.Name) {
mode |= fs.ModeDir
}
case ftp.EntryTypeFolder:
mode = fs.ModeDir
case ftp.EntryTypeLink:
mode = fs.ModeSymlink
}
return mode
}
// fs.FileInfo implementation.
func (fi FtpFileInfo) ModTime() time.Time {
return fi.Entry.Time
}
// fs.FileInfo implementation.
func (fi FtpFileInfo) IsDir() bool {
return fi.Entry.Type == ftp.EntryTypeFolder || IsTypeIso(fi.Entry.Name)
}
func (fi FtpFileInfo) IsRealDir() bool {
return fi.Entry.Type == ftp.EntryTypeFolder
}
func (fi FtpFileInfo) Type() fs.FileMode {
return fi.Mode().Type()
}
// Info provided for fs.DirEntry compatibility and returns object itself.
func (fi FtpFileInfo) Info() (fs.FileInfo, error) {
return fi, nil
}
// fs.FileInfo implementation. Returns structure pointer itself.
func (fi FtpFileInfo) Sys() interface{} {
return fi
}
func (fi FtpFileInfo) String() string {
return fs.FormatDirEntry(fi)
}