Some code optimizations

This commit is contained in:
Julian Kornberger 2015-08-18 23:16:40 +02:00
parent 23b395751d
commit 3a78f26034
2 changed files with 11 additions and 22 deletions

31
ftp.go
View File

@ -235,12 +235,7 @@ func (c *ServerConn) openDataConn() (net.Conn, error) {
// Build the new net address string // Build the new net address string
addr := net.JoinHostPort(c.host, strconv.Itoa(port)) addr := net.JoinHostPort(c.host, strconv.Itoa(port))
conn, err := net.DialTimeout("tcp", addr, c.timeout) return net.DialTimeout("tcp", addr, c.timeout)
if err != nil {
return nil, err
}
return conn, nil
} }
// cmd is a helper function to execute a command and check for the expected FTP // cmd is a helper function to execute a command and check for the expected FTP
@ -251,8 +246,7 @@ func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int,
return 0, "", err return 0, "", err
} }
code, line, err := c.conn.ReadResponse(expected) return c.conn.ReadResponse(expected)
return code, line, err
} }
// cmdDataConnFrom executes a command which require a FTP data connection. // cmdDataConnFrom executes a command which require a FTP data connection.
@ -320,7 +314,7 @@ func parseListLine(line string) (*Entry, error) {
e.Type = EntryTypeFile e.Type = EntryTypeFile
} }
case "size": case "size":
e.Size, _ = strconv.ParseUint(value, 0, 64) e.setSize(value)
} }
} }
return e, nil return e, nil
@ -363,6 +357,9 @@ func parseListLine(line string) (*Entry, error) {
switch fields[0][0] { switch fields[0][0] {
case '-': case '-':
e.Type = EntryTypeFile e.Type = EntryTypeFile
if err = e.setSize(fields[4]); err != nil {
return nil, err
}
case 'd': case 'd':
e.Type = EntryTypeFolder e.Type = EntryTypeFolder
case 'l': case 'l':
@ -371,12 +368,6 @@ func parseListLine(line string) (*Entry, error) {
return nil, errors.New("Unknown entry type") return nil, errors.New("Unknown entry type")
} }
if e.Type == EntryTypeFile {
if err = e.setSize(fields[4]); err != nil {
return nil, err
}
}
if err = e.setTime(fields[5:8]); err != nil { if err = e.setTime(fields[5:8]); err != nil {
return nil, err return nil, err
} }
@ -387,7 +378,7 @@ func parseListLine(line string) (*Entry, error) {
} }
func (e *Entry) setSize(str string) (err error) { func (e *Entry) setSize(str string) (err error) {
e.Size, err = strconv.ParseUint(str, 10, 0) e.Size, err = strconv.ParseUint(str, 0, 64)
return return
} }
@ -400,7 +391,7 @@ func (e *Entry) setTime(fields []string) (err error) {
if len(fields[2]) != 4 { if len(fields[2]) != 4 {
return errors.New("Invalid year format in time string") return errors.New("Invalid year format in time string")
} }
timeStr = fields[1] + " " + fields[0] + " " + fields[2][2:4] + " " + "00:00" + " GMT" timeStr = fields[1] + " " + fields[0] + " " + fields[2][2:4] + " 00:00 GMT"
} }
e.Time, err = time.Parse("_2 Jan 06 15:04 MST", timeStr) e.Time, err = time.Parse("_2 Jan 06 15:04 MST", timeStr)
return return
@ -503,8 +494,7 @@ func (c *ServerConn) RetrFrom(path string, offset uint64) (io.ReadCloser, error)
return nil, err return nil, err
} }
r := &response{conn, c} return &response{conn, c}, nil
return r, nil
} }
// Stor issues a STOR FTP command to store a file to the remote FTP server. // Stor issues a STOR FTP command to store a file to the remote FTP server.
@ -591,8 +581,7 @@ func (c *ServerConn) Quit() error {
// Read implements the io.Reader interface on a FTP data connection. // Read implements the io.Reader interface on a FTP data connection.
func (r *response) Read(buf []byte) (int, error) { func (r *response) Read(buf []byte) (int, error) {
n, err := r.conn.Read(buf) return r.conn.Read(buf)
return n, err
} }
// Close implements the io.Closer interface on a FTP data connection. // Close implements the io.Closer interface on a FTP data connection.

View File

@ -25,7 +25,7 @@ var listTests = []line{
// Another ls style // 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)}, {"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)}, {"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)}, {"-rw-r--r-- 0 18446744073709551615 18446744073709551615 Nov 16 2006 VIDEO_TS.VOB", "VIDEO_TS.VOB", 18446744073709551615, EntryTypeFile, time.Date(2006, time.November, 16, 0, 0, 0, 0, time.UTC)},
// Microsoft's FTP servers for Windows // 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)}, {"---------- 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)},