Add support for directory listings in MS-DOS DIR format.
This commit is contained in:
parent
7acded32b2
commit
95346071de
115
ftp.go
115
ftp.go
@ -286,13 +286,13 @@ func (c *ServerConn) cmdDataConnFrom(offset uint64, format string, args ...inter
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseListLine parses the various non-standard format returned by the LIST
|
var errUnsupportedListLine = errors.New("Unsupported LIST line")
|
||||||
// FTP command.
|
|
||||||
func parseListLine(line string) (*Entry, error) {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
// RFC3659 style
|
// parseRFC3659ListLine parses the style of directory line defined in RFC 3659.
|
||||||
if i := strings.Index(line, ";"); i > 0 && i < strings.Index(line, " ") {
|
func parseRFC3659ListLine(line string) (*Entry, error) {
|
||||||
|
if i := strings.Index(line, ";"); i < 0 || i > strings.Index(line, " ") {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
e := &Entry{}
|
e := &Entry{}
|
||||||
arr := strings.Split(line, "; ")
|
arr := strings.Split(line, "; ")
|
||||||
e.Name = arr[1]
|
e.Name = arr[1]
|
||||||
@ -300,7 +300,7 @@ func parseListLine(line string) (*Entry, error) {
|
|||||||
for _, field := range strings.Split(arr[0], ";") {
|
for _, field := range strings.Split(arr[0], ";") {
|
||||||
i := strings.Index(field, "=")
|
i := strings.Index(field, "=")
|
||||||
if i < 1 {
|
if i < 1 {
|
||||||
return nil, errors.New("Unsupported LIST line")
|
return nil, errUnsupportedListLine
|
||||||
}
|
}
|
||||||
|
|
||||||
key := field[:i]
|
key := field[:i]
|
||||||
@ -308,7 +308,11 @@ func parseListLine(line string) (*Entry, error) {
|
|||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
case "modify":
|
case "modify":
|
||||||
e.Time, _ = time.Parse("20060102150405", value)
|
var err error
|
||||||
|
e.Time, err = time.Parse("20060102150405", value)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case "type":
|
case "type":
|
||||||
switch value {
|
switch value {
|
||||||
case "dir", "cdir", "pdir":
|
case "dir", "cdir", "pdir":
|
||||||
@ -321,15 +325,18 @@ func parseListLine(line string) (*Entry, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return e, nil
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
// parseLsListLine parses a directory line in a format based on the output of
|
||||||
|
// the UNIX ls command.
|
||||||
|
func parseLsListLine(line string) (*Entry, error) {
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
if len(fields) >= 7 && fields[1] == "folder" && fields[2] == "0" {
|
if len(fields) >= 7 && fields[1] == "folder" && fields[2] == "0" {
|
||||||
e := &Entry{
|
e := &Entry{
|
||||||
Type: EntryTypeFolder,
|
Type: EntryTypeFolder,
|
||||||
Name: strings.Join(fields[6:], " "),
|
Name: strings.Join(fields[6:], " "),
|
||||||
}
|
}
|
||||||
if err = e.setTime(fields[3:6]); err != nil {
|
if err := e.setTime(fields[3:6]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -342,10 +349,10 @@ func parseListLine(line string) (*Entry, error) {
|
|||||||
Name: strings.Join(fields[7:], " "),
|
Name: strings.Join(fields[7:], " "),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = e.setSize(fields[2]); err != nil {
|
if err := e.setSize(fields[2]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.setTime(fields[4:7]); err != nil {
|
if err := e.setTime(fields[4:7]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -353,14 +360,14 @@ func parseListLine(line string) (*Entry, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(fields) < 9 {
|
if len(fields) < 9 {
|
||||||
return nil, errors.New("Unsupported LIST line")
|
return nil, errUnsupportedListLine
|
||||||
}
|
}
|
||||||
|
|
||||||
e := &Entry{}
|
e := &Entry{}
|
||||||
switch fields[0][0] {
|
switch fields[0][0] {
|
||||||
case '-':
|
case '-':
|
||||||
e.Type = EntryTypeFile
|
e.Type = EntryTypeFile
|
||||||
if err = e.setSize(fields[4]); err != nil {
|
if err := e.setSize(fields[4]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case 'd':
|
case 'd':
|
||||||
@ -371,13 +378,77 @@ func parseListLine(line string) (*Entry, error) {
|
|||||||
return nil, errors.New("Unknown entry type")
|
return nil, errors.New("Unknown entry type")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = e.setTime(fields[5:8]); err != nil {
|
if err := e.setTime(fields[5:8]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Name = strings.Join(fields[8:], " ")
|
e.Name = strings.Join(fields[8:], " ")
|
||||||
return e, nil
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var dirTimeFormats = []string{
|
||||||
|
"01-02-06 03:04PM",
|
||||||
|
"2006-01-02 15:04",
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseDirListLine parses a directory line in a format based on the output of
|
||||||
|
// the MS-DOS DIR command.
|
||||||
|
func parseDirListLine(line string) (*Entry, error) {
|
||||||
|
e := &Entry{}
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// Try various time formats that DIR might use, and stop when one works.
|
||||||
|
for _, format := range dirTimeFormats {
|
||||||
|
e.Time, err = time.Parse(format, line[:len(format)])
|
||||||
|
if err == nil {
|
||||||
|
line = line[len(format):]
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// None of the time formats worked.
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
|
||||||
|
line = strings.TrimLeft(line, " ")
|
||||||
|
if strings.HasPrefix(line, "<DIR>") {
|
||||||
|
e.Type = EntryTypeFolder
|
||||||
|
line = strings.TrimPrefix(line, "<DIR>")
|
||||||
|
} else {
|
||||||
|
space := strings.Index(line, " ")
|
||||||
|
if space == -1 {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
e.Size, err = strconv.ParseUint(line[:space], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
|
}
|
||||||
|
e.Type = EntryTypeFile
|
||||||
|
line = line[space:]
|
||||||
|
}
|
||||||
|
|
||||||
|
e.Name = strings.TrimLeft(line, " ")
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var listLineParsers = []func(line string) (*Entry, error){
|
||||||
|
parseRFC3659ListLine,
|
||||||
|
parseLsListLine,
|
||||||
|
parseDirListLine,
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseListLine parses the various non-standard format returned by the LIST
|
||||||
|
// FTP command.
|
||||||
|
func parseListLine(line string) (*Entry, error) {
|
||||||
|
for _, f := range listLineParsers {
|
||||||
|
e, err := f(line)
|
||||||
|
if err == errUnsupportedListLine {
|
||||||
|
// Try another format.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return e, err
|
||||||
|
}
|
||||||
|
return nil, errUnsupportedListLine
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Entry) setSize(str string) (err error) {
|
func (e *Entry) setSize(str string) (err error) {
|
||||||
@ -430,19 +501,17 @@ func (c *ServerConn) List(path string) (entries []*Entry, err error) {
|
|||||||
r := &response{conn, c}
|
r := &response{conn, c}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
|
|
||||||
bio := bufio.NewReader(r)
|
scanner := bufio.NewScanner(r)
|
||||||
for {
|
for scanner.Scan() {
|
||||||
line, e := bio.ReadString('\n')
|
line := scanner.Text()
|
||||||
if e == io.EOF {
|
|
||||||
break
|
|
||||||
} else if e != nil {
|
|
||||||
return nil, e
|
|
||||||
}
|
|
||||||
entry, err := parseListLine(line)
|
entry, err := parseListLine(line)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
entries = append(entries, entry)
|
entries = append(entries, entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +45,10 @@ var listTests = []line{
|
|||||||
{"modify=20150806235817;perm=fle;type=dir;unique=1B20F360U4;UNIX.group=0;UNIX.mode=0755;UNIX.owner=0; movies", "movies", 0, EntryTypeFolder, time.Date(2015, time.August, 6, 23, 58, 17, 0, time.UTC)},
|
{"modify=20150806235817;perm=fle;type=dir;unique=1B20F360U4;UNIX.group=0;UNIX.mode=0755;UNIX.owner=0; movies", "movies", 0, EntryTypeFolder, time.Date(2015, time.August, 6, 23, 58, 17, 0, time.UTC)},
|
||||||
{"modify=20150814172949;perm=flcdmpe;type=dir;unique=85A0C168U4;UNIX.group=0;UNIX.mode=0777;UNIX.owner=0; _upload", "_upload", 0, EntryTypeFolder, time.Date(2015, time.August, 14, 17, 29, 49, 0, time.UTC)},
|
{"modify=20150814172949;perm=flcdmpe;type=dir;unique=85A0C168U4;UNIX.group=0;UNIX.mode=0777;UNIX.owner=0; _upload", "_upload", 0, EntryTypeFolder, time.Date(2015, time.August, 14, 17, 29, 49, 0, time.UTC)},
|
||||||
{"modify=20150813175250;perm=adfr;size=951;type=file;unique=119FBB87UE;UNIX.group=0;UNIX.mode=0644;UNIX.owner=0; welcome.msg", "welcome.msg", 951, EntryTypeFile, time.Date(2015, time.August, 13, 17, 52, 50, 0, time.UTC)},
|
{"modify=20150813175250;perm=adfr;size=951;type=file;unique=119FBB87UE;UNIX.group=0;UNIX.mode=0644;UNIX.owner=0; welcome.msg", "welcome.msg", 951, EntryTypeFile, time.Date(2015, time.August, 13, 17, 52, 50, 0, time.UTC)},
|
||||||
|
|
||||||
|
// DOS DIR command output
|
||||||
|
{"08-07-15 07:50PM 718 Post_PRR_20150901_1166_265118_13049.dat", "Post_PRR_20150901_1166_265118_13049.dat", 718, EntryTypeFile, time.Date(2015, time.August, 7, 19, 50, 0, 0, time.UTC)},
|
||||||
|
{"08-10-15 02:04PM <DIR> Billing", "Billing", 0, EntryTypeFolder, time.Date(2015, time.August, 10, 14, 4, 0, 0, time.UTC)},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not supported, we expect a specific error message
|
// Not supported, we expect a specific error message
|
||||||
|
Loading…
Reference in New Issue
Block a user