-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.go
567 lines (493 loc) · 14.6 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// Package ftpfs implements a FTP(S) client file system.
package ftpfs
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
iofs "io/fs"
"net/url"
"path"
"strings"
"time"
"github.com/jlaffaye/ftp"
"github.com/ungerik/go-fs"
"github.com/ungerik/go-fs/fsimpl"
)
const (
Prefix = "ftp://"
PrefixTLS = "ftps://"
Separator = "/"
)
func init() {
// Register with prefix ftp:// and ftps:// for URLs with
// ftp(s)://username:password@host:port schema.
fs.Register(&fileSystem{secure: false, prefix: Prefix})
fs.Register(&fileSystem{secure: true, prefix: PrefixTLS})
}
// CredentialsCallback is called by Dial to get the username and password for a SFTP connection.
type CredentialsCallback func(*url.URL) (username, password string, err error)
// Password returns a CredentialsCallback that always returns
// the provided password together with the username
// from the URL that is passed to the callback.
func Password(password string) CredentialsCallback {
return func(u *url.URL) (string, string, error) {
return u.User.String(), password, nil
}
}
// UsernameAndPassword returns a CredentialsCallback that always returns
// the provided username and password.
func UsernameAndPassword(username, password string) CredentialsCallback {
return func(u *url.URL) (string, string, error) {
return username, password, nil
}
}
// // UsernameAndPasswordFromURL is a CredentialsCallback that returns
// // the username and password encoded in the passed URL.
// func UsernameAndPasswordFromURL(u *url.URL) (username, password string, err error) {
// password, ok := u.User.Password()
// if !ok {
// return "", "", fmt.Errorf("no password in URL: %s", u.String())
// }
// return u.User.Username(), password, nil
// }
type fileSystem struct {
conn *ftp.ServerConn
prefix string
secure bool
}
// Dial a new FTP or FTPS connection and registers it as file system.
//
// The passed address can be a URL with scheme `ftp:` or `ftps:`.
func Dial(ctx context.Context, address string, credentialsCallback CredentialsCallback, debugOut io.Writer) (fs.FileSystem, error) {
u, username, password, prefix, secure, err := prepareDial(address, credentialsCallback)
if err != nil {
return nil, err
}
conn, err := dial(ctx, u.Host, username, password, secure, debugOut)
if err != nil {
return nil, err
}
return &fileSystem{
conn: conn,
prefix: prefix,
secure: secure,
}, nil
}
func dial(ctx context.Context, host, username, password string, secure bool, debugOut io.Writer) (conn *ftp.ServerConn, err error) {
dialOptions := []ftp.DialOption{
ftp.DialWithContext(ctx),
ftp.DialWithDebugOutput(debugOut),
}
if secure {
if !strings.ContainsRune(host, ':') {
host += ":990"
}
dialOptions = append(dialOptions, ftp.DialWithExplicitTLS(&tls.Config{InsecureSkipVerify: true}))
} else {
if !strings.ContainsRune(host, ':') {
host += ":21"
}
}
conn, err = ftp.Dial(host, dialOptions...)
if err != nil {
return nil, err
}
err = conn.Login(username, password)
if err != nil {
return nil, errors.Join(err, conn.Quit())
}
return conn, nil
}
// DialAndRegister dials a new FTP or FTPS connection and register it as file system.
//
// The passed address can be a URL with scheme `ftp:` or `ftps:`.
func DialAndRegister(ctx context.Context, address string, credentialsCallback CredentialsCallback, debugOut io.Writer) (fs.FileSystem, error) {
fileSystem, err := Dial(ctx, address, credentialsCallback, debugOut)
if err != nil {
return nil, err
}
fs.Register(fileSystem)
return fileSystem, nil
}
// EnsureRegistered first checks if a FTP(S) file system with the passed address
// is already registered. If not, then a new connection is dialed and registered.
// The returned free function has to be called to decrease the file system's
// reference count and close it when the reference count reaches 0.
// The returned free function will never be nil.
func EnsureRegistered(ctx context.Context, address string, credentialsCallback CredentialsCallback, debugOut io.Writer) (free func() error, err error) {
u, username, password, prefix, secure, err := prepareDial(address, credentialsCallback)
if err != nil {
return nop, err
}
f := fs.GetFileSystemByPrefixOrNil(prefix)
if f != nil {
fs.Register(f) // Increase ref count
return func() error { fs.Unregister(f); return nil }, nil
}
conn, err := dial(ctx, u.Host, username, password, secure, debugOut)
if err != nil {
return nop, err
}
f = &fileSystem{
conn: conn,
prefix: prefix,
secure: secure,
}
fs.Register(f) // TODO somone else might have registered, so free should not close it
return func() error { return f.Close() }, nil
}
func prepareDial(address string, credentialsCallback CredentialsCallback) (u *url.URL, username, password, prefix string, secure bool, err error) {
if !strings.HasPrefix(address, "ftp://") && !strings.HasPrefix(address, "ftps://") {
if strings.Contains(address, "://") {
return nil, "", "", "", false, fmt.Errorf("not an FTP or FTPS URL scheme: %s", address)
}
address = "ftp://" + address
}
if credentialsCallback == nil {
return nil, "", "", "", false, errors.New("nil credentialsCall")
}
u, err = url.Parse(address)
if err != nil {
return nil, "", "", "", false, err
}
if u.Scheme != "ftp" && u.Scheme != "ftps" {
return nil, "", "", "", false, fmt.Errorf("not an FTP or FTPS URL scheme: %s", address)
}
// Trim default port number
switch u.Scheme {
case "ftp":
u.Host = strings.TrimSuffix(u.Host, ":21")
case "ftps":
u.Host = strings.TrimSuffix(u.Host, ":990")
}
username, password, err = credentialsCallback(u)
if err != nil {
return nil, "", "", "", false, err
}
if username == "" {
return nil, "", "", "", false, fmt.Errorf("missing FTP username for: %s", address)
}
if password == "" {
return nil, "", "", "", false, fmt.Errorf("missing FTP password for: %s", address)
}
prefix = fmt.Sprintf("%s://%s@%s", u.Scheme, url.User(username), u.Host)
secure = u.Scheme == "ftps"
return u, username, password, prefix, secure, nil
}
func nop() error { return nil }
func (f *fileSystem) getConn(ctx context.Context, filePath string) (conn *ftp.ServerConn, clientPath string, release func() error, err error) {
if err = ctx.Err(); err != nil {
return nil, "", nop, err
}
if f.conn != nil {
return f.conn, filePath, nop, nil
}
// fmt.Printf("%s file system not registered, trying to dial with credentials from URL: %s", f.Name(), f.URL(filePath))
u, err := url.Parse(f.URL(filePath))
if err != nil {
return nil, "", nop, err
}
username := u.User.Username()
if username == "" {
return nil, "", nop, fmt.Errorf("no username in %s URL: %s", f.Name(), f.URL(filePath))
}
password, ok := u.User.Password()
if !ok {
return nil, "", nop, fmt.Errorf("no password in %s URL: %s", f.Name(), f.URL(filePath))
}
conn, err = dial(ctx, u.Host, username, password, f.secure, nil)
if err != nil {
return nil, "", nop, err
}
return conn, u.Path, func() error { return conn.Quit() }, nil
}
func (f *fileSystem) ReadableWritable() (readable, writable bool) {
return true, true
}
func (f *fileSystem) RootDir() fs.File {
return fs.File(f.prefix + Separator)
}
func (f *fileSystem) ID() (string, error) {
return f.prefix, nil
}
func (f *fileSystem) Prefix() string {
return f.prefix
}
func (f *fileSystem) Separator() string { return Separator }
func (f *fileSystem) Name() string {
if f.secure {
return "FTPS"
}
return "FTP"
}
func (f *fileSystem) String() string {
return f.prefix + " file system"
}
func (f *fileSystem) URL(cleanPath string) string {
return f.prefix + cleanPath
}
func (f *fileSystem) CleanPathFromURI(uri string) string {
port := ":21"
if f.secure {
port = ":990"
}
return path.Clean(
strings.TrimPrefix(
strings.TrimPrefix(uri, f.prefix),
port, // In case f.prefix has no port number and url has the default port number
),
)
}
func (f *fileSystem) JoinCleanPath(uriParts ...string) string {
if f.secure {
return fsimpl.JoinCleanPath(uriParts, PrefixTLS, Separator)
}
return fsimpl.JoinCleanPath(uriParts, Prefix, Separator)
}
func (f *fileSystem) JoinCleanFile(uriParts ...string) fs.File {
path := f.JoinCleanPath(uriParts...)
if strings.HasSuffix(f.prefix, Separator) && strings.HasPrefix(path, Separator) {
// For example: "sftp://" + "/example.com/absolute/path"
// should not result in 3 slashes: "sftp:///example.com/absolute/path"
path = path[len(Separator):]
}
return fs.File(f.prefix + path)
}
func (f *fileSystem) SplitPath(filePath string) []string {
return fsimpl.SplitPath(filePath, f.prefix, Separator)
}
func (f *fileSystem) IsAbsPath(filePath string) bool {
if f.secure {
return strings.HasPrefix(filePath, PrefixTLS)
}
return strings.HasPrefix(filePath, Prefix)
}
func (f *fileSystem) AbsPath(filePath string) string {
if f.IsAbsPath(filePath) {
return filePath
}
return Prefix + strings.TrimPrefix(filePath, Separator)
}
func (*fileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fsimpl.SplitDirAndName(filePath, 0, Separator)
}
type fileInfo struct {
entry *ftp.Entry
}
func (i fileInfo) Name() string { return i.entry.Name }
func (i fileInfo) Size() int64 { return int64(i.entry.Size) }
func (i fileInfo) Mode() iofs.FileMode { return 0666 }
func (i fileInfo) ModTime() time.Time { return i.entry.Time }
func (i fileInfo) IsDir() bool { return i.entry.Type == ftp.EntryTypeFolder }
func (i fileInfo) Sys() any { return nil }
func entryToFileInfo(entry *ftp.Entry, file fs.File) *fs.FileInfo {
return &fs.FileInfo{
File: file,
Name: entry.Name,
Exists: true,
IsDir: entry.Type == ftp.EntryTypeFolder,
IsRegular: entry.Type != ftp.EntryTypeLink,
IsHidden: false,
Size: int64(entry.Size),
Modified: entry.Time,
Permissions: 0666,
}
}
func (f *fileSystem) Stat(filePath string) (iofs.FileInfo, error) {
conn, filePath, release, err := f.getConn(context.Background(), filePath)
if err != nil {
return nil, err
}
defer release()
entry, err := conn.GetEntry(filePath)
if err != nil {
return nil, err
}
return fileInfo{entry}, nil
}
func (f *fileSystem) IsHidden(filePath string) bool { return false }
func (f *fileSystem) IsSymbolicLink(filePath string) bool {
conn, filePath, release, err := f.getConn(context.Background(), filePath)
if err != nil {
return false
}
defer release()
entry, err := conn.GetEntry(filePath)
if err != nil {
return false
}
return entry.Type == ftp.EntryTypeLink
}
func (f *fileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) error {
conn, dirPath, release, err := f.getConn(ctx, dirPath)
if err != nil {
return err
}
defer release()
if ctx.Err() != nil {
return ctx.Err()
}
entries, err := conn.List(dirPath)
if err != nil {
return err
}
for _, entry := range entries {
if ctx.Err() != nil {
return ctx.Err()
}
match, err := fsimpl.MatchAnyPattern(entry.Name, patterns)
if err != nil {
return err
}
if !match {
continue
}
err = callback(entryToFileInfo(entry, f.JoinCleanFile(dirPath, entry.Name)))
if err != nil {
return err
}
}
return nil
}
func (f *fileSystem) MatchAnyPattern(name string, patterns []string) (bool, error) {
return fsimpl.MatchAnyPattern(name, patterns)
}
func (f *fileSystem) MakeDir(dirPath string, perm []fs.Permissions) error {
conn, dirPath, release, err := f.getConn(context.Background(), dirPath)
if err != nil {
return err
}
defer release()
return conn.MakeDir(dirPath)
}
type fileReader struct {
path string
conn *ftp.ServerConn
response *ftp.Response
release func() error
}
func (f *fileReader) Stat() (iofs.FileInfo, error) {
entry, err := f.conn.GetEntry(f.path)
if err != nil {
return nil, err
}
return fileInfo{entry}, nil
}
func (f *fileReader) Read(buf []byte) (int, error) {
return f.response.Read(buf)
}
func (f *fileReader) Close() error {
return errors.Join(f.response.Close(), f.release())
}
func (f *fileSystem) OpenReader(filePath string) (reader iofs.File, err error) {
conn, filePath, release, err := f.getConn(context.Background(), filePath)
if err != nil {
return nil, err
}
response, err := conn.Retr(filePath)
if err != nil {
return nil, errors.Join(err, release())
}
return &fileReader{
path: filePath,
conn: conn,
response: response,
release: release,
}, nil
}
func (f *fileSystem) OpenWriter(filePath string, perm []fs.Permissions) (fs.WriteCloser, error) {
return f.OpenReadWriter(filePath, perm)
}
// func (f *FTPFileSystem) OpenAppendWriter(filePath string, perm []fs.Permissions) (fs.WriteCloser, error) {
// }
type file struct {
path string
offset int64
conn *ftp.ServerConn
release func() error
}
func (f *file) Read(p []byte) (n int, err error) {
return f.ReadAt(p, f.offset)
}
func (f *file) ReadAt(p []byte, offset int64) (n int, err error) {
response, err := f.conn.RetrFrom(f.path, uint64(offset))
if err != nil {
return 0, err
}
return response.Read(p)
}
func (f *file) Write(p []byte) (n int, err error) {
return f.WriteAt(p, f.offset)
}
func (f *file) WriteAt(p []byte, offset int64) (n int, err error) {
r := bytes.NewReader(p)
err = f.conn.StorFrom(f.path, r, uint64(offset))
return len(p) - r.Len(), err
}
func (f *file) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekStart:
f.offset = offset
case io.SeekCurrent:
f.offset += offset
case io.SeekEnd:
size, err := f.conn.FileSize(f.path)
if err != nil {
return 0, err
}
f.offset = size + offset
}
return f.offset, nil
}
func (f *file) Close() error {
return f.release()
}
func (f *fileSystem) OpenReadWriter(filePath string, perm []fs.Permissions) (fs.ReadWriteSeekCloser, error) {
conn, filePath, release, err := f.getConn(context.Background(), filePath)
if err != nil {
return nil, err
}
return &file{
path: filePath,
conn: conn,
release: release,
}, nil
}
func (f *fileSystem) Move(filePath string, destPath string) error {
conn, filePath, release, err := f.getConn(context.Background(), filePath)
if err != nil {
return err
}
defer release()
return conn.Rename(filePath, destPath)
}
func (f *fileSystem) Remove(filePath string) error {
conn, filePath, release, err := f.getConn(context.Background(), filePath)
if err != nil {
return err
}
defer release()
entry, err := conn.GetEntry(filePath)
if err != nil {
return err
}
if entry.Type == ftp.EntryTypeFolder {
return conn.RemoveDir(filePath)
}
return conn.Delete(filePath)
}
func (f *fileSystem) Close() error {
if f.conn == nil {
return nil // already closed
}
count := fs.Unregister(f)
if count > 1 {
return nil // still referenced
}
err := f.conn.Quit()
f.conn = nil
return err
}