2017-05-16 17:01:02 +00:00
|
|
|
package ftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-01-16 13:20:59 +00:00
|
|
|
"fmt"
|
2017-05-16 17:01:02 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errUnsupportedListLine = errors.New("Unsupported LIST line")
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
type parseFunc func(string, time.Time) (*Entry, error)
|
|
|
|
|
|
|
|
var listLineParsers = []parseFunc{
|
2017-05-16 17:01:02 +00:00
|
|
|
parseRFC3659ListLine,
|
|
|
|
parseLsListLine,
|
|
|
|
parseDirListLine,
|
2017-07-23 07:51:42 +00:00
|
|
|
parseHostedFTPLine,
|
2017-05-16 17:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var dirTimeFormats = []string{
|
|
|
|
"01-02-06 03:04PM",
|
|
|
|
"2006-01-02 15:04",
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseRFC3659ListLine parses the style of directory line defined in RFC 3659.
|
2018-01-16 13:20:59 +00:00
|
|
|
func parseRFC3659ListLine(line string, now time.Time) (*Entry, error) {
|
2017-05-16 17:01:02 +00:00
|
|
|
iSemicolon := strings.Index(line, ";")
|
|
|
|
iWhitespace := strings.Index(line, " ")
|
|
|
|
|
|
|
|
if iSemicolon < 0 || iSemicolon > iWhitespace {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
|
|
|
e := &Entry{
|
|
|
|
Name: line[iWhitespace+1:],
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range strings.Split(line[:iWhitespace-1], ";") {
|
|
|
|
i := strings.Index(field, "=")
|
|
|
|
if i < 1 {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
2017-09-30 14:27:27 +00:00
|
|
|
key := strings.ToLower(field[:i])
|
2017-05-16 17:01:02 +00:00
|
|
|
value := field[i+1:]
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "modify":
|
|
|
|
var err error
|
|
|
|
e.Time, err = time.Parse("20060102150405", value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case "type":
|
|
|
|
switch value {
|
|
|
|
case "dir", "cdir", "pdir":
|
|
|
|
e.Type = EntryTypeFolder
|
|
|
|
case "file":
|
|
|
|
e.Type = EntryTypeFile
|
|
|
|
}
|
|
|
|
case "size":
|
|
|
|
e.setSize(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseLsListLine parses a directory line in a format based on the output of
|
|
|
|
// the UNIX ls command.
|
2018-01-16 13:20:59 +00:00
|
|
|
func parseLsListLine(line string, now time.Time) (*Entry, error) {
|
2017-05-16 17:01:02 +00:00
|
|
|
|
|
|
|
// Has the first field a length of 10 bytes?
|
|
|
|
if strings.IndexByte(line, ' ') != 10 {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := newScanner(line)
|
|
|
|
fields := scanner.NextFields(6)
|
|
|
|
|
|
|
|
if len(fields) < 6 {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
|
|
|
if fields[1] == "folder" && fields[2] == "0" {
|
|
|
|
e := &Entry{
|
|
|
|
Type: EntryTypeFolder,
|
|
|
|
Name: scanner.Remaining(),
|
|
|
|
}
|
2018-01-16 13:20:59 +00:00
|
|
|
if err := e.setTime(fields[3:6], now); err != nil {
|
2017-05-16 17:01:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fields[1] == "0" {
|
|
|
|
fields = append(fields, scanner.Next())
|
|
|
|
e := &Entry{
|
|
|
|
Type: EntryTypeFile,
|
|
|
|
Name: scanner.Remaining(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.setSize(fields[2]); err != nil {
|
2017-07-23 07:51:42 +00:00
|
|
|
return nil, errUnsupportedListLine
|
2017-05-16 17:01:02 +00:00
|
|
|
}
|
2018-01-16 13:20:59 +00:00
|
|
|
if err := e.setTime(fields[4:7], now); err != nil {
|
2017-05-16 17:01:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read two more fields
|
|
|
|
fields = append(fields, scanner.NextFields(2)...)
|
|
|
|
if len(fields) < 8 {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
|
|
|
e := &Entry{
|
|
|
|
Name: scanner.Remaining(),
|
|
|
|
}
|
|
|
|
switch fields[0][0] {
|
|
|
|
case '-':
|
|
|
|
e.Type = EntryTypeFile
|
|
|
|
if err := e.setSize(fields[4]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case 'd':
|
|
|
|
e.Type = EntryTypeFolder
|
|
|
|
case 'l':
|
|
|
|
e.Type = EntryTypeLink
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Unknown entry type")
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
if err := e.setTime(fields[5:8], now); err != nil {
|
2017-05-16 17:01:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseDirListLine parses a directory line in a format based on the output of
|
|
|
|
// the MS-DOS DIR command.
|
2018-01-16 13:20:59 +00:00
|
|
|
func parseDirListLine(line string, now time.Time) (*Entry, error) {
|
2017-05-16 17:01:02 +00:00
|
|
|
e := &Entry{}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Try various time formats that DIR might use, and stop when one works.
|
|
|
|
for _, format := range dirTimeFormats {
|
|
|
|
if len(line) > len(format) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-23 07:51:42 +00:00
|
|
|
// parseHostedFTPLine parses a directory line in the non-standard format used
|
|
|
|
// by hostedftp.com
|
|
|
|
// -r-------- 0 user group 65222236 Feb 24 00:39 UABlacklistingWeek8.csv
|
|
|
|
// (The link count is inexplicably 0)
|
2018-01-16 13:20:59 +00:00
|
|
|
func parseHostedFTPLine(line string, now time.Time) (*Entry, error) {
|
2017-07-23 07:51:42 +00:00
|
|
|
// Has the first field a length of 10 bytes?
|
|
|
|
if strings.IndexByte(line, ' ') != 10 {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := newScanner(line)
|
|
|
|
fields := scanner.NextFields(2)
|
|
|
|
|
|
|
|
if len(fields) < 2 || fields[1] != "0" {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set link count to 1 and attempt to parse as Unix.
|
2018-01-16 13:20:59 +00:00
|
|
|
return parseLsListLine(fields[0]+" 1 "+scanner.Remaining(), now)
|
2017-07-23 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:01:02 +00:00
|
|
|
// parseListLine parses the various non-standard format returned by the LIST
|
|
|
|
// FTP command.
|
2018-01-16 13:20:59 +00:00
|
|
|
func parseListLine(line string, now time.Time) (*Entry, error) {
|
2017-05-16 17:01:02 +00:00
|
|
|
for _, f := range listLineParsers {
|
2018-01-16 13:20:59 +00:00
|
|
|
e, err := f(line, now)
|
2017-05-16 17:01:02 +00:00
|
|
|
if err != errUnsupportedListLine {
|
|
|
|
return e, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Entry) setSize(str string) (err error) {
|
|
|
|
e.Size, err = strconv.ParseUint(str, 0, 64)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
func (e *Entry) setTime(fields []string, now time.Time) (err error) {
|
|
|
|
if strings.Contains(fields[2], ":") { // contains time
|
|
|
|
thisYear, _, _ := now.Date()
|
|
|
|
timeStr := fmt.Sprintf("%s %s %d %s GMT", fields[1], fields[0], thisYear, fields[2])
|
|
|
|
e.Time, err = time.Parse("_2 Jan 2006 15:04 MST", timeStr)
|
|
|
|
|
|
|
|
/*
|
|
|
|
On unix, `info ls` shows:
|
|
|
|
|
|
|
|
10.1.6 Formatting file timestamps
|
|
|
|
---------------------------------
|
|
|
|
|
|
|
|
A timestamp is considered to be “recent” if it is less than six
|
|
|
|
months old, and is not dated in the future. If a timestamp dated today
|
|
|
|
is not listed in recent form, the timestamp is in the future, which
|
|
|
|
means you probably have clock skew problems which may break programs
|
|
|
|
like ‘make’ that rely on file timestamps.
|
|
|
|
*/
|
|
|
|
if !e.Time.Before(now.AddDate(0, 6, 0)) {
|
|
|
|
e.Time = e.Time.AddDate(-1, 0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // only the date
|
2017-05-16 17:01:02 +00:00
|
|
|
if len(fields[2]) != 4 {
|
|
|
|
return errors.New("Invalid year format in time string")
|
|
|
|
}
|
2018-01-16 13:20:59 +00:00
|
|
|
timeStr := fmt.Sprintf("%s %s %s 00:00 GMT", fields[1], fields[0], fields[2])
|
|
|
|
e.Time, err = time.Parse("_2 Jan 2006 15:04 MST", timeStr)
|
2017-05-16 17:01:02 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|