-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsftp.go
166 lines (149 loc) · 3.38 KB
/
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
package joint
import (
"errors"
"io"
"io/fs"
"net/url"
"strings"
"sync"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
type SftpFileStat = sftp.FileStat
var (
pwdmap = map[string]string{}
pwdmux sync.RWMutex
)
// SftpPwd return SFTP current directory. It's used cache to avoid
// extra calls to SFTP-server to get current directory for every call.
func SftpPwd(ftpaddr string, client *sftp.Client) (pwd string, err error) {
pwdmux.RLock()
pwd, ok := pwdmap[ftpaddr]
pwdmux.RUnlock()
if ok {
return
}
if pwd, err = client.Getwd(); err != nil {
return
}
pwdmux.Lock()
pwdmap[ftpaddr] = pwd
pwdmux.Unlock()
return
}
// SftpJoint create SSH-connection to SFTP-server, login with provided by
// given URL credentials, and gets a once current directory.
// Key is address of SFTP-service, i.e. sftp://user:[email protected].
type SftpJoint struct {
conn *ssh.Client
client *sftp.Client
pwd string
path string // path inside of SFTP-service without PWD
files []fs.FileInfo
*sftp.File
rdn int
}
func (j *SftpJoint) Make(base Joint, urladdr string) (err error) {
var u *url.URL
if u, err = url.Parse(urladdr); err != nil {
return
}
var pass, _ = u.User.Password()
var config = &ssh.ClientConfig{
User: u.User.Username(),
Auth: []ssh.AuthMethod{
ssh.Password(pass),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
if j.conn, err = ssh.Dial("tcp", u.Host, config); err != nil {
return
}
if j.client, err = sftp.NewClient(j.conn); err != nil {
return
}
if j.pwd, err = SftpPwd(u.Host, j.client); err != nil {
return
}
if u.Path != "" && u.Path != "/" { // skip empty path
var fpath = strings.Trim(u.Path, "/")
j.pwd = JoinPath(j.pwd, fpath)
}
return
}
func (j *SftpJoint) Cleanup() error {
var err1, err2, err3 error
if j.Busy() {
err1 = j.Close()
}
if j.client != nil {
err2 = j.client.Close()
j.client = nil
}
if j.conn != nil {
err3 = j.conn.Close()
j.conn = nil
}
return errors.Join(err1, err2, err3)
}
func (j *SftpJoint) Busy() bool {
return j.File != nil
}
// Opens new connection for any some one file with given full SFTP URL.
func (j *SftpJoint) Open(fpath string) (file fs.File, err error) {
if j.Busy() {
return nil, fs.ErrExist
}
j.path = fpath
if j.File, err = j.client.Open(JoinPath(j.pwd, fpath)); err != nil {
return
}
j.files = nil // delete previous readdir result
j.rdn = 0 // start new sequence
return j, nil
}
func (j *SftpJoint) Close() (err error) {
j.path = ""
if j.File != nil {
err = j.File.Close()
j.File = nil
}
return
}
func (j *SftpJoint) Size() (int64, error) {
var fi, err = j.File.Stat()
if err != nil {
return 0, err
}
return fi.Size(), nil
}
func (j *SftpJoint) ReadDir(n int) (list []fs.DirEntry, err error) {
if j.files == nil {
if j.files, err = j.client.ReadDir(JoinPath(j.pwd, j.path)); err != nil {
return
}
}
if n < 0 {
n = len(j.files) - j.rdn
} else if n > len(j.files)-j.rdn {
n = len(j.files) - 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] = ToDirEntry(j.files[j.rdn+i])
}
j.rdn += n
return
}
func (j *SftpJoint) Stat() (fs.FileInfo, error) {
var fi, err = j.File.Stat()
return ToFileInfo(fi), err
}
func (j *SftpJoint) Info(fpath string) (fs.FileInfo, error) {
var fi, err = j.client.Stat(JoinPath(j.pwd, fpath))
return ToFileInfo(fi), err
}