/
fs_sftp.go
338 lines (290 loc) · 7.76 KB
/
fs_sftp.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
package filesys
import (
"bufio"
"context"
"io"
"net/url"
"os"
"path"
"strings"
"github.com/flarco/g"
"github.com/pkg/sftp"
"github.com/slingdata-io/sling-cli/core/dbio"
"github.com/slingdata-io/sling-cli/core/dbio/iop"
"github.com/spf13/cast"
)
// SftpFileSysClient is for SFTP / SSH file ops
type SftpFileSysClient struct {
BaseFileSysClient
context g.Context
client *sftp.Client
sshClient *iop.SSHClient
}
// Init initializes the fs client
func (fs *SftpFileSysClient) Init(ctx context.Context) (err error) {
var instance FileSysClient
instance = fs
fs.BaseFileSysClient.instance = &instance
fs.BaseFileSysClient.context = g.NewContext(ctx)
return fs.Connect()
}
// Prefix returns the url prefix
func (fs *SftpFileSysClient) Prefix(suffix ...string) string {
return g.F("%s://%s:%s", fs.FsType().String(), fs.GetProp("host"), fs.GetProp("port")) + strings.Join(suffix, "")
}
// GetPath returns the path of url
func (fs *SftpFileSysClient) GetPath(uri string) (path string, err error) {
// normalize, in case url is provided without prefix
uri = NormalizeURI(fs, uri)
_, path, err = ParseURL(uri)
if err != nil {
return
}
return path, err
}
// Connect initiates the Google Cloud Storage client
func (fs *SftpFileSysClient) Connect() (err error) {
if fs.GetProp("PRIVATE_KEY") == "" {
if os.Getenv("SSH_PRIVATE_KEY") != "" {
fs.SetProp("PRIVATE_KEY", os.Getenv("SSH_PRIVATE_KEY"))
} else {
defPrivKey := path.Join(g.UserHomeDir(), ".ssh", "id_rsa")
if g.PathExists(defPrivKey) {
g.Debug("adding default private key (%s) as auth method for SFTP", defPrivKey)
fs.SetProp("PRIVATE_KEY", defPrivKey)
}
}
}
if fs.GetProp("URL") != "" {
u, err := url.Parse(fs.GetProp("URL"))
if err != nil {
return g.Error(err, "could not parse SFTP URL")
}
host := u.Hostname()
user := u.User.Username()
password, _ := u.User.Password()
sshPort := cast.ToInt(u.Port())
if sshPort == 0 {
sshPort = 22
}
if user != "" {
fs.SetProp("USER", user)
}
if password != "" {
fs.SetProp("PASSWORD", password)
}
if host != "" {
fs.SetProp("HOST", host)
}
if sshPort != 0 {
fs.SetProp("PORT", cast.ToString(sshPort))
}
}
fs.sshClient = &iop.SSHClient{
Host: fs.GetProp("HOST"),
Port: cast.ToInt(fs.GetProp("PORT")),
User: fs.GetProp("USER"),
Password: fs.GetProp("PASSWORD"),
PrivateKey: fs.GetProp("PRIVATE_KEY"), // raw value or path to ssh key file
Passphrase: fs.GetProp("PASSPHRASE"),
}
err = fs.sshClient.Connect()
if err != nil {
return g.Error(err, "unable to connect to ssh server ")
}
fs.client, err = fs.sshClient.SftpClient()
if err != nil {
return g.Error(err, "unable to start SFTP client")
}
return nil
}
// List list objects in path
func (fs *SftpFileSysClient) List(url string) (nodes dbio.FileNodes, err error) {
path, err := fs.GetPath(url)
if err != nil {
err = g.Error(err, "Error Parsing url: "+url)
return
}
var files []os.FileInfo
stat, err := fs.client.Stat(strings.TrimSuffix(path, "/"))
if err == nil && (!stat.IsDir() || !strings.HasSuffix(path, "/")) {
node := dbio.FileNode{
URI: g.F("%s%s", fs.Prefix("/"), path),
Updated: stat.ModTime().Unix(),
Size: cast.ToUint64(stat.Size()),
IsDir: stat.IsDir(),
}
nodes.Add(node)
return
}
if path == "" {
path = "/"
} else if path != "/" {
path = strings.TrimSuffix(path, "/")
}
files, err = fs.client.ReadDir(path)
if err != nil {
return nodes, g.Error(err, "error listing path: %#v", path)
}
if path == "/" {
path = ""
}
for _, file := range files {
node := dbio.FileNode{
URI: g.F("%s%s%s", fs.Prefix("/"), path+"/", file.Name()),
Updated: file.ModTime().Unix(),
Size: cast.ToUint64(file.Size()),
IsDir: file.IsDir(),
}
nodes.Add(node)
}
return
}
// ListRecursive list objects in path recursively
func (fs *SftpFileSysClient) ListRecursive(uri string) (nodes dbio.FileNodes, err error) {
path, err := fs.GetPath(uri)
if err != nil {
err = g.Error(err, "Error Parsing url: "+uri)
return
}
pattern, err := makeGlob(NormalizeURI(fs, uri))
if err != nil {
err = g.Error(err, "Error Parsing url pattern: "+uri)
return
}
ts := fs.GetRefTs().Unix()
var files []os.FileInfo
stat, err := fs.client.Stat(strings.TrimSuffix(path, "/"))
if err == nil {
node := dbio.FileNode{
URI: g.F("%s%s", fs.Prefix("/"), path),
Updated: stat.ModTime().Unix(),
Size: cast.ToUint64(stat.Size()),
IsDir: stat.IsDir(),
}
nodes.Add(node)
if !stat.IsDir() {
return
}
}
path = strings.TrimSuffix(path, "/")
files, err = fs.client.ReadDir(path)
if err != nil {
return nodes, g.Error(err, "error listing path: %#v", path)
}
for _, file := range files {
node := dbio.FileNode{
URI: g.F("%s%s%s", fs.Prefix("/"), path+"/", file.Name()),
Updated: file.ModTime().Unix(),
Size: cast.ToUint64(file.Size()),
IsDir: file.IsDir(),
}
if file.IsDir() {
subNodes, err := fs.ListRecursive(node.Path() + "/")
if err != nil {
return nil, g.Error(err, "error listing sub path")
}
nodes.AddWhere(pattern, ts, subNodes...)
} else {
nodes.AddWhere(pattern, ts, node)
}
}
return
}
// Delete list objects in path
func (fs *SftpFileSysClient) delete(urlStr string) (err error) {
path, err := fs.GetPath(urlStr)
if err != nil {
err = g.Error(err, "Error Parsing url: "+urlStr)
return
}
nodes, err := fs.ListRecursive(urlStr)
if err != nil {
return g.Error(err, "error listing path")
}
for _, sNode := range nodes {
sNode.URI, _ = fs.GetPath(sNode.URI)
err = fs.client.Remove(sNode.URI)
if err != nil {
return g.Error(err, "error deleting path "+sNode.URI)
}
}
err = fs.client.Remove(path)
if err != nil && !strings.Contains(err.Error(), "not exist") {
return g.Error(err, "error deleting path")
}
return nil
}
// MkdirAll creates child directories
func (fs *SftpFileSysClient) MkdirAll(path string) (err error) {
return fs.client.MkdirAll(path)
}
func (fs *SftpFileSysClient) Write(urlStr string, reader io.Reader) (bw int64, err error) {
path, err := fs.GetPath(urlStr)
if err != nil {
err = g.Error(err, "Error Parsing url: "+urlStr)
return
}
// manage concurrency
defer fs.Context().Wg.Write.Done()
fs.Context().Wg.Write.Add()
filePathArr := strings.Split(path, "/")
if len(filePathArr) > 1 {
folderPath := strings.Join(filePathArr[:len(filePathArr)-1], "/")
err = fs.client.MkdirAll(folderPath)
if err != nil {
err = g.Error(err, "Unable to create directory "+folderPath)
return
}
}
file, err := fs.client.Create(path)
if err != nil {
err = g.Error(err, "Unable to open "+path)
return
}
bw, err = io.Copy(io.Writer(file), reader)
if err != nil {
err = g.Error(err, "Error writing from reader")
}
return
}
// GetReader return a reader for the given path
func (fs *SftpFileSysClient) GetReader(urlStr string) (reader io.Reader, err error) {
path, err := fs.GetPath(urlStr)
if err != nil {
err = g.Error(err, "Error Parsing url: "+urlStr)
return
}
file, err := fs.client.Open(path)
if err != nil {
err = g.Error(err, "Unable to open "+path)
return
}
pipeR, pipeW := io.Pipe()
go func() {
defer pipeW.Close()
reader = bufio.NewReader(file)
_, err = io.Copy(pipeW, reader)
if err != nil {
fs.Context().CaptureErr(g.Error(err, "Error writing from reader"))
fs.Context().Cancel()
g.LogError(fs.Context().Err())
}
}()
return pipeR, err
}
// GetWriter creates the file if non-existent and return a writer
func (fs *SftpFileSysClient) GetWriter(urlStr string) (writer io.Writer, err error) {
path, err := fs.GetPath(urlStr)
if err != nil {
err = g.Error(err, "Error Parsing url: "+urlStr)
return
}
file, err := fs.client.Create(path)
if err != nil {
err = g.Error(err, "Unable to open "+path)
return
}
writer = io.Writer(file)
return
}