Add helpers functions to cleanup code.

This commit is contained in:
jlaffaye 2011-09-06 19:12:22 +02:00
parent 6aaa275d08
commit af6f5aac6a

136
ftp.go
View File

@ -55,14 +55,12 @@ func Connect(addr string) (*ServerConn, os.Error) {
} }
func (c *ServerConn) Login(user, password string) os.Error { func (c *ServerConn) Login(user, password string) os.Error {
c.conn.Cmd("USER %s", user) _, _, err := c.cmd(StatusUserOK, "USER %s", user)
_, _, err := c.conn.ReadCodeLine(StatusUserOK)
if err != nil { if err != nil {
return err return err
} }
c.conn.Cmd("PASS %s", password) _, _, err = c.cmd(StatusLoggedIn, "PASS %s", password)
_, _, err = c.conn.ReadCodeLine(StatusLoggedIn)
return err return err
} }
@ -84,7 +82,7 @@ func (c *ServerConn) epsv() (port int, err os.Error) {
} }
// Open a new data connection using extended passive mode // Open a new data connection using extended passive mode
func (c *ServerConn) openDataConnection() (net.Conn, os.Error) { func (c *ServerConn) openDataConn() (net.Conn, os.Error) {
port, err := c.epsv() port, err := c.epsv()
if err != nil { if err != nil {
return nil, err return nil, err
@ -101,19 +99,41 @@ func (c *ServerConn) openDataConnection() (net.Conn, os.Error) {
return conn, nil return conn, nil
} }
// Helper function to check if the last command succeeded and if it will // Helper function to execute a command and check for the expected code
// send the data to the data connection. func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, os.Error) {
// This is needed because some servers return StatusAboutToSend (150) _, err := c.conn.Cmd(format, args...)
// and some StatusAlreadyOpen (125) if err != nil {
func (c *ServerConn) checkDataConn() os.Error { return 0, "", err
}
code, line, err := c.conn.ReadCodeLine(expected)
return code, line, err
}
// Helper function to execute commands which require a data connection
func (c *ServerConn) cmdDataConn(format string, args ...interface{}) (net.Conn, os.Error) {
conn, err := c.openDataConn()
if err != nil {
return nil, err
}
_, err = c.conn.Cmd(format, args...)
if err != nil {
conn.Close()
return nil, err
}
code, msg, err := c.conn.ReadCodeLine(-1) code, msg, err := c.conn.ReadCodeLine(-1)
if err != nil { if err != nil {
return err conn.Close()
return nil, err
} }
if code != StatusAlreadyOpen && code != StatusAboutToSend { if code != StatusAlreadyOpen && code != StatusAboutToSend {
return os.NewError(fmt.Sprintf("%d %s", code, msg)) conn.Close()
return nil, os.NewError(fmt.Sprintf("%d %s", code, msg))
} }
return nil
return conn, nil
} }
func parseListLine(line string) (*Entry, os.Error) { func parseListLine(line string) (*Entry, os.Error) {
@ -139,7 +159,7 @@ func parseListLine(line string) (*Entry, os.Error) {
} }
func (c *ServerConn) List(path string) (entries []*Entry, err os.Error) { func (c *ServerConn) List(path string) (entries []*Entry, err os.Error) {
conn, err := c.openDataConnection() conn, err := c.cmdDataConn("LIST %s", path)
if err != nil { if err != nil {
return return
} }
@ -147,16 +167,6 @@ func (c *ServerConn) List(path string) (entries []*Entry, err os.Error) {
r := &response{conn, c} r := &response{conn, c}
defer r.Close() defer r.Close()
_, err = c.conn.Cmd("LIST %s", path)
if err != nil {
return
}
err = c.checkDataConn()
if err != nil {
return
}
bio := bufio.NewReader(r) bio := bufio.NewReader(r)
for { for {
line, e := bio.ReadString('\n') line, e := bio.ReadString('\n')
@ -173,55 +183,28 @@ func (c *ServerConn) List(path string) (entries []*Entry, err os.Error) {
return return
} }
func (c *ServerConn) ChangeDir(path string) (err os.Error) { func (c *ServerConn) ChangeDir(path string) os.Error {
_, err = c.conn.Cmd("CWD %s", path) _, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
if err == nil { return err
_, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
}
return
} }
// Retrieves a remote file // Retrieves a remote file
func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) { func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) {
conn, err := c.openDataConnection() conn, err := c.cmdDataConn("RETR %s", path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, err = c.conn.Cmd("RETR %s", path)
if err != nil {
conn.Close()
return nil, err
}
err = c.checkDataConn()
if err != nil {
conn.Close()
return nil, err
}
r := &response{conn, c} r := &response{conn, c}
return r, nil return r, nil
} }
func (c *ServerConn) Stor(name string, r io.Reader) os.Error { func (c *ServerConn) Stor(name string, r io.Reader) os.Error {
conn, err := c.openDataConnection() conn, err := c.cmdDataConn("STOR %s", name)
if err != nil { if err != nil {
return err return err
} }
_, err = c.conn.Cmd("STOR %s", name)
if err != nil {
conn.Close()
return err
}
err = c.checkDataConn()
if err != nil {
conn.Close()
return err
}
_, err = io.Copy(conn, r) _, err = io.Copy(conn, r)
conn.Close() conn.Close()
if err != nil { if err != nil {
@ -233,62 +216,33 @@ func (c *ServerConn) Stor(name string, r io.Reader) os.Error {
} }
func (c *ServerConn) Rename(from, to string) os.Error { func (c *ServerConn) Rename(from, to string) os.Error {
_, err := c.conn.Cmd("RNFR %s", from) _, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
if err != nil { if err != nil {
return err return err
} }
_, _, err = c.conn.ReadCodeLine(StatusRequestFilePending) _, _, err = c.cmd(StatusRequestedFileActionOK, "RNTO %s", to)
if err != nil {
return err
}
_, err = c.conn.Cmd("RNTO %s", to)
if err != nil {
return err
}
_, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
return err return err
} }
func (c *ServerConn) Delete(name string) os.Error { func (c *ServerConn) Delete(name string) os.Error {
_, err := c.conn.Cmd("DELE %s", name) _, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", name)
if err != nil {
return err
}
_, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
return err return err
} }
func (c *ServerConn) MakeDir(name string) os.Error { func (c *ServerConn) MakeDir(name string) os.Error {
_, err := c.conn.Cmd("MKD %s", name) _, _, err := c.cmd(StatusPathCreated, "MKD %s", name)
if err != nil {
return err
}
_, _, err = c.conn.ReadCodeLine(StatusPathCreated)
return err return err
} }
func (c *ServerConn) RemoveDir(name string) os.Error { func (c *ServerConn) RemoveDir(name string) os.Error {
_, err := c.conn.Cmd("RMD %s", name) _, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", name)
if err != nil {
return err
}
_, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
return err return err
} }
// Sends a NOOP command. Usualy used to prevent timeouts. // Sends a NOOP command. Usualy used to prevent timeouts.
func (c *ServerConn) NoOp() os.Error { func (c *ServerConn) NoOp() os.Error {
_, err := c.conn.Cmd("NOOP") _, _, err := c.cmd(StatusCommandOK, "NOOP")
if err != nil {
return err
}
_, _, err = c.conn.ReadCodeLine(StatusCommandOK)
return err return err
} }