From 3a78f26034c87701fd5ede9175e3cfe688f2a099 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Tue, 18 Aug 2015 23:16:40 +0200 Subject: [PATCH] Some code optimizations --- ftp.go | 31 ++++++++++--------------------- parse_test.go | 2 +- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/ftp.go b/ftp.go index 3658f23..e644611 100644 --- a/ftp.go +++ b/ftp.go @@ -235,12 +235,7 @@ func (c *ServerConn) openDataConn() (net.Conn, error) { // Build the new net address string addr := net.JoinHostPort(c.host, strconv.Itoa(port)) - conn, err := net.DialTimeout("tcp", addr, c.timeout) - if err != nil { - return nil, err - } - - return conn, nil + return net.DialTimeout("tcp", addr, c.timeout) } // 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 } - code, line, err := c.conn.ReadResponse(expected) - return code, line, err + return c.conn.ReadResponse(expected) } // cmdDataConnFrom executes a command which require a FTP data connection. @@ -320,7 +314,7 @@ func parseListLine(line string) (*Entry, error) { e.Type = EntryTypeFile } case "size": - e.Size, _ = strconv.ParseUint(value, 0, 64) + e.setSize(value) } } return e, nil @@ -363,6 +357,9 @@ func parseListLine(line string) (*Entry, error) { 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': @@ -371,12 +368,6 @@ func parseListLine(line string) (*Entry, error) { 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 { return nil, err } @@ -387,7 +378,7 @@ func parseListLine(line string) (*Entry, 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 } @@ -400,7 +391,7 @@ func (e *Entry) setTime(fields []string) (err error) { if len(fields[2]) != 4 { 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) return @@ -503,8 +494,7 @@ func (c *ServerConn) RetrFrom(path string, offset uint64) (io.ReadCloser, error) return nil, err } - r := &response{conn, c} - return r, nil + return &response{conn, c}, nil } // 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. func (r *response) Read(buf []byte) (int, error) { - n, err := r.conn.Read(buf) - return n, err + return r.conn.Read(buf) } // Close implements the io.Closer interface on a FTP data connection. diff --git a/parse_test.go b/parse_test.go index fa8cd85..78ef26e 100644 --- a/parse_test.go +++ b/parse_test.go @@ -25,7 +25,7 @@ var listTests = []line{ // 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)}, + {"-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 {"---------- 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)},