Merge pull request #35 from digineo/code_optimizations

Some code optimizations
This commit is contained in:
Julien Laffaye
2015-08-18 23:43:34 +02:00
2 changed files with 11 additions and 22 deletions

31
ftp.go
View File

@@ -238,12 +238,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
@@ -254,8 +249,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.
@@ -323,7 +317,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
@@ -366,6 +360,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':
@@ -374,12 +371,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
}
@@ -390,7 +381,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
}
@@ -403,7 +394,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
@@ -506,8 +497,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.
@@ -594,8 +584,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.