From 72db6844f1ce2dc6c58923fceefeee18999874da Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Tue, 18 Aug 2015 22:33:04 +0200 Subject: [PATCH] Improve line parsing --- ftp.go | 6 +++--- parse_test.go | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ftp.go b/ftp.go index eb38052..3658f23 100644 --- a/ftp.go +++ b/ftp.go @@ -294,7 +294,8 @@ func (c *ServerConn) cmdDataConnFrom(offset uint64, format string, args ...inter func parseListLine(line string) (*Entry, error) { var err error - if strings.HasPrefix(line, "modify=") { + // RFC3659 style + if i := strings.Index(line, ";"); i > 0 && i < strings.Index(line, " ") { e := &Entry{} arr := strings.Split(line, "; ") e.Name = arr[1] @@ -326,7 +327,7 @@ func parseListLine(line string) (*Entry, error) { } else { fields := strings.Fields(line) - if len(fields) >= 7 && fields[1] == "folder" { + if len(fields) >= 7 && fields[1] == "folder" && fields[2] == "0" { e := &Entry{ Type: EntryTypeFolder, Name: strings.Join(fields[6:], " "), @@ -355,7 +356,6 @@ func parseListLine(line string) (*Entry, error) { } if len(fields) < 9 { - //panic(fmt.Sprintf("%d %v", len(fields), fields[6])) return nil, errors.New("Unsupported LIST line") } diff --git a/parse_test.go b/parse_test.go index 375f1f5..fa8cd85 100644 --- a/parse_test.go +++ b/parse_test.go @@ -21,16 +21,20 @@ var listTests = []line{ {"drwxr-xr-x 3 110 1002 3 Dec 02 2009 p u b", "p u b", 0, EntryTypeFolder, time.Date(2009, time.December, 2, 0, 0, 0, 0, time.UTC)}, {"-rwxr-xr-x 3 110 1002 1234567 Dec 02 2009 fileName", "fileName", 1234567, EntryTypeFile, time.Date(2009, time.December, 2, 0, 0, 0, 0, time.UTC)}, {"lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin", "bin -> usr/bin", 0, EntryTypeLink, time.Date(thisYear, time.January, 25, 0, 17, 0, 0, time.UTC)}, + // Another ls style {"drwxr-xr-x folder 0 Aug 15 05:49 !!!-Tipp des Haus!", "!!!-Tipp des Haus!", 0, EntryTypeFolder, time.Date(thisYear, time.August, 15, 5, 49, 0, 0, time.UTC)}, {"drwxrwxrwx folder 0 Aug 11 20:32 P0RN", "P0RN", 0, EntryTypeFolder, time.Date(thisYear, time.August, 11, 20, 32, 0, 0, time.UTC)}, {"-rw-r--r-- 0 195291136 195291136 Nov 16 2006 VIDEO_TS.VOB", "VIDEO_TS.VOB", 195291136, EntryTypeFile, time.Date(2006, time.November, 16, 0, 0, 0, 0, time.UTC)}, + // Microsoft's FTP servers for Windows {"---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z", "ls-lR.Z", 1803128, EntryTypeFile, time.Date(thisYear, time.July, 10, 10, 18, 0, 0, time.UTC)}, {"d--------- 1 owner group 0 May 9 19:45 Softlib", "Softlib", 0, EntryTypeFolder, time.Date(thisYear, time.May, 9, 19, 45, 0, 0, time.UTC)}, + // WFTPD for MSDOS {"-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp", "message.ftp", 322, EntryTypeFile, time.Date(1996, time.August, 19, 0, 0, 0, 0, time.UTC)}, - // Some other + + // RFC3659 format: https://tools.ietf.org/html/rfc3659#section-7 {"modify=20150813224845;perm=fle;type=cdir;unique=119FBB87U4;UNIX.group=0;UNIX.mode=0755;UNIX.owner=0; .", ".", 0, EntryTypeFolder, time.Date(2015, time.August, 13, 22, 48, 45, 0, time.UTC)}, {"modify=20150813224845;perm=fle;type=pdir;unique=119FBB87U4;UNIX.group=0;UNIX.mode=0755;UNIX.owner=0; ..", "..", 0, EntryTypeFolder, time.Date(2015, time.August, 13, 22, 48, 45, 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)},