This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
/
ftp.go
197 lines (163 loc) · 4.26 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
// Package ftp contains an FTP client implementation that supports health checks and tracing.
package ftp
import (
"context"
"errors"
"io"
"os"
"path/filepath"
"time"
"github.com/jlaffaye/ftp"
"github.com/opentracing/opentracing-go"
"pkg.dsb.dev/closers"
"pkg.dsb.dev/health"
"pkg.dsb.dev/multierror"
"pkg.dsb.dev/tracing"
)
type (
// The Conn type represents a connection to an FTP server.
Conn struct {
inner *ftp.ServerConn
}
// FileInfo is an os.FileInfo implementation that represents FTP file information.
FileInfo struct {
name string
size int64
isDir bool
modTime time.Time
}
config struct {
username string
password string
}
)
// Open a connection to the specified FTP server, applying any provided options. See the Option type for configuration
// options.
func Open(ctx context.Context, addr string, opts ...Option) (*Conn, error) {
c := &config{}
for _, opt := range opts {
opt(c)
}
conn, err := ftp.Dial(addr,
ftp.DialWithContext(ctx),
ftp.DialWithTimeout(time.Minute),
)
if err != nil {
return nil, err
}
out := &Conn{inner: conn}
health.AddCheck(addr, func() error {
return ping(ctx, addr, c)
})
if c.username == "" {
return out, nil
}
if err = conn.Login(c.username, c.password); err != nil {
return nil, multierror.Append(err, out.Close())
}
return out, nil
}
// Close the connection to the FTP server.
func (c *Conn) Close() error {
return c.inner.Quit()
}
// NewReader returns a new io.ReadCloser implementation that reads the content of the file at the specified path.
func (c *Conn) NewReader(path string) (io.ReadCloser, error) {
return c.inner.Retr(path)
}
// ListDir returns a slice of os.FileInfo implementations for all files/directories found at the given path of the
// FTP server.
func (c *Conn) ListDir(ctx context.Context, path string) ([]os.FileInfo, error) {
span, _ := opentracing.StartSpanFromContext(ctx, "ftp-list-dir")
span.SetTag("path", path)
defer span.Finish()
entries, err := c.inner.List(path)
if err != nil {
return nil, tracing.WithError(span, err)
}
span.SetTag("count", len(entries))
out := make([]os.FileInfo, len(entries))
for i, entry := range entries {
out[i] = &FileInfo{
name: entry.Name,
size: int64(entry.Size),
isDir: entry.Type == ftp.EntryTypeFolder,
modTime: entry.Time,
}
}
return out, nil
}
// Walk walks the file tree rooted at root, calling fn for each file or
// directory in the tree. All errors that arise visiting files
// and directories are filtered by fn.
func (c *Conn) Walk(ctx context.Context, path string, fn filepath.WalkFunc) error {
span, _ := opentracing.StartSpanFromContext(ctx, "ftp-walk")
defer span.Finish()
span.SetTag("path", path)
walker := c.inner.Walk(path)
for walker.Next() {
entry := walker.Stat()
err := fn(walker.Path(), &FileInfo{
name: entry.Name,
size: int64(entry.Size),
isDir: entry.Type == ftp.EntryTypeFolder,
modTime: entry.Time,
}, walker.Err())
switch {
case errors.Is(err, filepath.SkipDir):
walker.SkipDir()
case err != nil:
return err
}
}
return walker.Err()
}
func ping(ctx context.Context, addr string, c *config) error {
conn, err := ftp.Dial(addr,
ftp.DialWithContext(ctx),
ftp.DialWithTimeout(time.Minute),
)
if err != nil {
return err
}
defer closers.CloseFunc(conn.Quit)
if c.username == "" {
return conn.NoOp()
}
if err = conn.Login(c.username, c.password); err != nil {
return err
}
return conn.NoOp()
}
// Ping asserts that the connection to the FTP server is alive and healthy.
func (c *Conn) Ping() error {
return c.inner.NoOp()
}
// Name returns the file name.
func (f *FileInfo) Name() string {
return f.name
}
// Size returns the file size in bytes.
func (f *FileInfo) Size() int64 {
return f.size
}
// Mode returns os.ModeDir if the os.FileMode implementation is a directory. Otherwise it returns
// os.ModeIrregular.
func (f *FileInfo) Mode() os.FileMode {
if f.isDir {
return os.ModeDir
}
return os.ModeIrregular
}
// ModTime returns the last modified time of the file.
func (f *FileInfo) ModTime() time.Time {
return f.modTime
}
// IsDir returns true if the current file is a directory.
func (f *FileInfo) IsDir() bool {
return f.isDir
}
// Sys returns nil.
func (f *FileInfo) Sys() interface{} {
return nil
}