forked from dutchcoders/goftp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathftp_list.go
207 lines (183 loc) · 5.89 KB
/
ftp_list.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
package goftp
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"path"
"regexp"
"strings"
)
//getServerFeatures use FEAT to retrieve server features.
func (ftp *FTP) getServerFeatures() (features uint32) {
features = 0
code, str := ftp.RawCmd("FEAT")
if code < 0 || code > 299 {
return BAD
}
lines := strings.Split(str, "\n")
for _, f := range lines {
// Only MLSD/MLST is supported
if (features&MLSD == 0) && strings.Contains(f, "MLST") { // not a bug, see command reference.
features = features | MLSD
// Check of NLST and EPLF are disabled since not yet supported
/*} else if (features&NLST == 0) && strings.Contains(f, "NLST") {
features = features | NLST
} else if (features&EPLF == 0) && strings.Contains(f, "EPLF") {
features = features | EPLF*/
}
}
return features
}
//GetFilesList list the path (or current directory) and parse it.
//Return an array with the files, one with the directories and one with the links
func (ftp *FTP) GetFilesList(path string) (files []string, directories []string, links []string, err error) {
if ftp.supportedfeatures == 0 {
ftp.supportedfeatures = ftp.getServerFeatures()
}
if len(path) < 2 {
path = "./"
}
if ftp.supportedfeatures&MLSD > 0 {
//fmt.Println("Using MLSD")
code, response := ftp.RawPassiveCmd("MLSD " + path)
if code == 550 {
return nil, nil, nil, errors.New("550 Requested action not taken. File unavailable")
}
if code < 0 || code > 299 {
return nil, nil, nil, fmt.Errorf("%v MLSD did not work ", code)
}
return ftp.parseMLSD(response, path)
} else if ftp.supportedfeatures&EPLF > 0 {
//fmt.Println("Using EPLF")
code, response := ftp.RawPassiveCmd("EPLF " + path)
if code == 550 {
return nil, nil, nil, errors.New("550 Requested action not taken. File unavailable")
}
if code < 0 || code > 299 {
return nil, nil, nil, fmt.Errorf("%v EPLF did not work ", code)
}
return ftp.parseEPLF(response, path)
} else if ftp.supportedfeatures&NLST > 0 {
//fmt.Println("Using NLST")
code, response := ftp.RawPassiveCmd("NLST " + path)
if code == 550 {
return nil, nil, nil, errors.New("550 Requested action not taken. File unavailable")
}
if code < 0 || code > 299 {
return nil, nil, nil, fmt.Errorf("%v NLST did not work ", code)
}
return ftp.parseNLST(response, path)
}
//fmt.Println("Using LIST")
code, response := ftp.RawPassiveCmd("LIST " + path)
//fmt.Println(response)
if code == 550 {
return nil, nil, nil, errors.New("550 Requested action not taken. File unavailable")
}
if code < 0 || code > 299 {
return nil, nil, nil, errors.New("LIST did not work")
}
return ftp.parseUnixLIST(response, path)
}
//parseMLSD parse the response of a MLSD
//Return an array with the files, one with the directories and one with the links
func (ftp *FTP) parseMLSD(data []string, basePath string) (files []string, directories []string, links []string, err error) {
for _, line := range data {
_, t, subpath := parseLineMLST(line)
switch t {
case "dir":
if subpath == "." {
} else if subpath == ".." {
} else {
directories = append(directories, path.Join(basePath, subpath))
}
case "file":
files = append(files, path.Join(basePath, subpath))
case "OS.unix=symlink":
links = append(links, path.Join(basePath, subpath))
}
}
return files, directories, links, err
}
//parseLineMLST Parse a single MLST line
func parseLineMLST(line string) (perm string, t string, filename string) {
for _, v := range strings.Split(line, ";") {
v2 := strings.Split(v, "=")
switch v2[0] {
case "perm":
perm = v2[1]
case "type":
t = v2[1]
if t == "OS.unix" {
t = t + "=" + v2[2]
//fmt.Println("Found a link -> " + t)
}
default:
filename = v[1 : len(v)-2]
}
}
return
}
//parseEPLF parse the response of a EPLF
//Return an array with the files, one with the directories and one with the links
func (ftp *FTP) parseEPLF(data []string, basePath string) (files []string, directories []string, links []string, err error) {
return nil, nil, nil, errors.New("Not implemented! (EPLF)")
}
//parseNLST parse the response of a NLST
//Return an array with the files, one with the directories and one with the links
func (ftp *FTP) parseNLST(data []string, basePath string) (files []string, directories []string, links []string, err error) {
return nil, nil, nil, errors.New("Not implemented! (NLST)")
}
//parseUnixLIST parse the response of a LIST
//Return an array with the files, one with the directories and one with the links
func (ftp *FTP) parseUnixLIST(data []string, basePath string) (files []string, directories []string, links []string, err error) {
var pattern = regexp.MustCompile(`([-ld])` + //dir,link flags -dbclps
`([-rwxs]+)\s+` + //permissions
`\d+\s+` + //items count
`([\d\w\-]+)\s+` + //owner
`(\(\?\)|[\d\w\-]+)\s+` + //group
`(\d+)\s*` + //size
`(\w+\s*\d+\s*\d+\:*\d*)\s+` + //date|time
`(\S+)` /*name*/)
for _, line := range data {
match := pattern.FindStringSubmatch(line)
/*for i, val := range match {
fmt.Printf("entry %d:%s\n", i, val)
}*/
if len(match) > 6 {
if match[1] == "-" { // a file
files = append(files, path.Join(basePath, match[7]))
} else if match[1] == "d" { // a directory
directories = append(directories, path.Join(basePath, match[7]))
} else if match[1] == "l" { // a link
token := strings.Trim(strings.Split(line, "->")[1], " ")
links = append(links, token)
}
}
}
sum := len(files) + len(directories) + len(links)
if sum > 0 {
return files, directories, links, nil
}
//empty folder
if ftp.debug {
log.Printf("Empty folder")
}
return nil, nil, nil, nil
}
//splitLines extract single lines from a buffered reader
func (ftp *FTP) splitLines(reader *bufio.Reader) (files []string, err error) {
var line string
for {
line, err = reader.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
files = append(files, string(line))
}
return files, nil
}