Fix issues reported by errcheck

This commit is contained in:
Julien Laffaye 2021-03-06 19:36:26 -05:00
parent 52feea7744
commit 05cd33e2ad
3 changed files with 53 additions and 22 deletions

65
ftp.go
View File

@ -8,6 +8,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt"
"io" "io"
"net" "net"
"net/textproto" "net/textproto"
@ -124,7 +125,7 @@ func Dial(addr string, options ...DialOption) (*ServerConn, error) {
_, _, err := c.conn.ReadResponse(StatusReady) _, _, err := c.conn.ReadResponse(StatusReady)
if err != nil { if err != nil {
c.Quit() _ = c.Quit()
return nil, err return nil, err
} }
@ -308,8 +309,12 @@ func (c *ServerConn) Login(user, password string) error {
// If using implicit TLS, make data connections also use TLS // If using implicit TLS, make data connections also use TLS
if c.options.tlsConfig != nil { if c.options.tlsConfig != nil {
c.cmd(StatusCommandOK, "PBSZ 0") if _, _, err := c.cmd(StatusCommandOK, "PBSZ 0"); err != nil {
c.cmd(StatusCommandOK, "PROT P") return err
}
if _, _, err := c.cmd(StatusCommandOK, "PROT P"); err != nil {
return err
}
} }
return err return err
@ -513,24 +518,24 @@ func (c *ServerConn) cmdDataConnFrom(offset uint64, format string, args ...inter
if offset != 0 { if offset != 0 {
_, _, err := c.cmd(StatusRequestFilePending, "REST %d", offset) _, _, err := c.cmd(StatusRequestFilePending, "REST %d", offset)
if err != nil { if err != nil {
conn.Close() _ = conn.Close()
return nil, err return nil, err
} }
} }
_, err = c.conn.Cmd(format, args...) _, err = c.conn.Cmd(format, args...)
if err != nil { if err != nil {
conn.Close() _ = conn.Close()
return nil, err return nil, err
} }
code, msg, err := c.conn.ReadResponse(-1) code, msg, err := c.conn.ReadResponse(-1)
if err != nil { if err != nil {
conn.Close() _ = conn.Close()
return nil, err return nil, err
} }
if code != StatusAlreadyOpen && code != StatusAboutToSend { if code != StatusAlreadyOpen && code != StatusAboutToSend {
conn.Close() _ = conn.Close()
return nil, &textproto.Error{Code: code, Msg: msg} return nil, &textproto.Error{Code: code, Msg: msg}
} }
@ -549,16 +554,20 @@ func (c *ServerConn) NameList(path string) (entries []string, err error) {
} }
r := &Response{conn: conn, c: c} r := &Response{conn: conn, c: c}
defer r.Close() defer func() {
errClose := r.Close()
if err == nil {
err = errClose
}
}()
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
for scanner.Scan() { for scanner.Scan() {
entries = append(entries, scanner.Text()) entries = append(entries, scanner.Text())
} }
if err = scanner.Err(); err != nil {
err = scanner.Err()
return entries, err return entries, err
}
return entries, nil
} }
// List issues a LIST FTP command. // List issues a LIST FTP command.
@ -584,7 +593,12 @@ func (c *ServerConn) List(path string) (entries []*Entry, err error) {
} }
r := &Response{conn: conn, c: c} r := &Response{conn: conn, c: c}
defer r.Close() defer func() {
errClose := r.Close()
if err == nil {
err = errClose
}
}()
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
now := time.Now() now := time.Now()
@ -594,10 +608,9 @@ func (c *ServerConn) List(path string) (entries []*Entry, err error) {
entries = append(entries, entry) entries = append(entries, entry)
} }
} }
if err := scanner.Err(); err != nil {
return nil, err err = scanner.Err()
} return entries, err
return entries, nil
} }
// ChangeDir issues a CWD FTP command, which changes the current directory to // ChangeDir issues a CWD FTP command, which changes the current directory to
@ -733,12 +746,17 @@ func (c *ServerConn) Append(path string, r io.Reader) error {
// see the comment for StorFrom above // see the comment for StorFrom above
_, err = io.Copy(conn, r) _, err = io.Copy(conn, r)
conn.Close() errClose := conn.Close()
_, _, respErr := c.conn.ReadResponse(StatusClosingDataConnection) _, _, respErr := c.conn.ReadResponse(StatusClosingDataConnection)
if respErr != nil { if respErr != nil {
err = respErr err = respErr
} }
if err == nil {
err = errClose
}
return err return err
} }
@ -846,8 +864,17 @@ func (c *ServerConn) Logout() error {
// Quit issues a QUIT FTP command to properly close the connection from the // Quit issues a QUIT FTP command to properly close the connection from the
// remote FTP server. // remote FTP server.
func (c *ServerConn) Quit() error { func (c *ServerConn) Quit() error {
c.conn.Cmd("QUIT") _, errQuit := c.conn.Cmd("QUIT")
return c.conn.Close() err := c.conn.Close()
if errQuit != nil {
if err != nil {
return fmt.Errorf("error while quitting: %s: %w", errQuit, err)
}
return errQuit
}
return err
} }
// Read implements the io.Reader interface on a FTP data connection. // Read implements the io.Reader interface on a FTP data connection.

View File

@ -63,7 +63,9 @@ func parseRFC3659ListLine(line string, now time.Time, loc *time.Location) (*Entr
e.Type = EntryTypeFile e.Type = EntryTypeFile
} }
case "size": case "size":
e.setSize(value) if err := e.setSize(value); err != nil {
return nil, err
}
} }
} }
return e, nil return e, nil

View File

@ -160,7 +160,9 @@ func TestSettime(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run(test.line, func(t *testing.T) { t.Run(test.line, func(t *testing.T) {
entry := &Entry{} entry := &Entry{}
entry.setTime(strings.Fields(test.line), now, time.UTC) if err := entry.setTime(strings.Fields(test.line), now, time.UTC); err != nil {
t.Fatal(err)
}
assert.Equal(t, test.expected, entry.Time) assert.Equal(t, test.expected, entry.Time)
}) })