Browse Source

Set the size of the entry in List()

Julien Laffaye 13 years ago
parent
commit
d69e9326f4
2 changed files with 23 additions and 11 deletions
  1. 10 2
      ftp.go
  2. 13 9
      parse_test.go

+ 10 - 2
ftp.go

@@ -2,13 +2,13 @@ package ftp
 
 
 import (
 import (
 	"bufio"
 	"bufio"
+	"errors"
+	"fmt"
 	"io"
 	"io"
 	"net"
 	"net"
 	"net/textproto"
 	"net/textproto"
-	"fmt"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
-	"errors"
 )
 )
 
 
 type EntryType int
 type EntryType int
@@ -154,6 +154,14 @@ func parseListLine(line string) (*Entry, error) {
 		return nil, errors.New("Unknown entry type")
 		return nil, errors.New("Unknown entry type")
 	}
 	}
 
 
+	if e.Type == EntryTypeFile {
+		size, err := strconv.ParseUint(fields[4], 10, 0)
+		if err != nil {
+			return nil, err
+		}
+		e.Size = size
+	}
+
 	e.Name = strings.Join(fields[8:], " ")
 	e.Name = strings.Join(fields[8:], " ")
 	return e, nil
 	return e, nil
 }
 }

+ 13 - 9
parse_test.go

@@ -5,26 +5,27 @@ import "testing"
 type line struct {
 type line struct {
 	line string
 	line string
 	name string
 	name string
+	size uint64
 	entryType EntryType
 	entryType EntryType
 }
 }
 
 
 var listTests = []line {
 var listTests = []line {
 	// UNIX ls -l style
 	// UNIX ls -l style
-	line{"drwxr-xr-x    3 110      1002            3 Dec 02  2009 pub", "pub", EntryTypeFolder},
-	line{"drwxr-xr-x    3 110      1002            3 Dec 02  2009 p u b", "p u b", EntryTypeFolder},
-	line{"-rwxr-xr-x    3 110      1002            1234567 Dec 02  2009 fileName", "fileName", EntryTypeFile},
-	line{"lrwxrwxrwx   1 root     other          7 Jan 25 00:17 bin -> usr/bin", "bin -> usr/bin", EntryTypeLink},
+	line{"drwxr-xr-x    3 110      1002            3 Dec 02  2009 pub", "pub", 0, EntryTypeFolder},
+	line{"drwxr-xr-x    3 110      1002            3 Dec 02  2009 p u b", "p u b", 0, EntryTypeFolder},
+	line{"-rwxr-xr-x    3 110      1002            1234567 Dec 02  2009 fileName", "fileName", 1234567, EntryTypeFile},
+	line{"lrwxrwxrwx   1 root     other          7 Jan 25 00:17 bin -> usr/bin", "bin -> usr/bin", 0, EntryTypeLink},
 	// Microsoft's FTP servers for Windows
 	// Microsoft's FTP servers for Windows
-	line{"----------   1 owner    group         1803128 Jul 10 10:18 ls-lR.Z", "ls-lR.Z", EntryTypeFile},
-	line{"d---------   1 owner    group               0 May  9 19:45 Softlib", "Softlib", EntryTypeFolder},
+	line{"----------   1 owner    group         1803128 Jul 10 10:18 ls-lR.Z", "ls-lR.Z", 1803128, EntryTypeFile},
+	line{"d---------   1 owner    group               0 May  9 19:45 Softlib", "Softlib", 0, EntryTypeFolder},
 	// WFTPD for MSDOS
 	// WFTPD for MSDOS
-	line{"-rwxrwxrwx   1 noone    nogroup      322 Aug 19  1996 message.ftp", "message.ftp", EntryTypeFile},
+	line{"-rwxrwxrwx   1 noone    nogroup      322 Aug 19  1996 message.ftp", "message.ftp", 322, EntryTypeFile},
 }
 }
 
 
 // Not supported, at least we should properly return failure
 // Not supported, at least we should properly return failure
 var listTestsFail = []line {
 var listTestsFail = []line {
-	line{"d [R----F--] supervisor            512       Jan 16 18:53    login", "login", EntryTypeFolder},
-	line{"- [R----F--] rhesus             214059       Oct 20 15:27    cx.exe", "cx.exe", EntryTypeFile},
+	line{"d [R----F--] supervisor            512       Jan 16 18:53 login", "login", 0, EntryTypeFolder},
+	line{"- [R----F--] rhesus             214059       Oct 20 15:27 cx.exe", "cx.exe", 0, EntryTypeFile},
 }
 }
 
 
 func TestParseListLine(t *testing.T) {
 func TestParseListLine(t *testing.T) {
@@ -40,6 +41,9 @@ func TestParseListLine(t *testing.T) {
 		if entry.Type != lt.entryType {
 		if entry.Type != lt.entryType {
 			t.Errorf("parseListLine(%v).EntryType = %v, want %v", lt.line, entry.Type, lt.entryType,)
 			t.Errorf("parseListLine(%v).EntryType = %v, want %v", lt.line, entry.Type, lt.entryType,)
 		}
 		}
+		if entry.Size != lt.size {
+			t.Errorf("parseListLine(%v).Size = %v, want %v", lt.line, entry.Size, lt.size)
+		}
 	}
 	}
 	for _, lt := range listTestsFail {
 	for _, lt := range listTestsFail {
 		_, err := parseListLine(lt.line)
 		_, err := parseListLine(lt.line)