Update to latest Go weekly.
This commit is contained in:
parent
6110501482
commit
47b339b57b
80
ftp.go
80
ftp.go
@ -5,10 +5,10 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"os"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EntryType int
|
type EntryType int
|
||||||
@ -36,7 +36,7 @@ type response struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect to a ftp server and returns a ServerConn handler.
|
// Connect to a ftp server and returns a ServerConn handler.
|
||||||
func Connect(addr string) (*ServerConn, os.Error) {
|
func Connect(addr string) (*ServerConn, error) {
|
||||||
conn, err := textproto.Dial("tcp", addr)
|
conn, err := textproto.Dial("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -54,7 +54,7 @@ func Connect(addr string) (*ServerConn, os.Error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) Login(user, password string) os.Error {
|
func (c *ServerConn) Login(user, password string) error {
|
||||||
_, _, err := c.cmd(StatusUserOK, "USER %s", user)
|
_, _, err := c.cmd(StatusUserOK, "USER %s", user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -65,7 +65,7 @@ func (c *ServerConn) Login(user, password string) os.Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Enter extended passive mode
|
// Enter extended passive mode
|
||||||
func (c *ServerConn) epsv() (port int, err os.Error) {
|
func (c *ServerConn) epsv() (port int, err error) {
|
||||||
c.conn.Cmd("EPSV")
|
c.conn.Cmd("EPSV")
|
||||||
_, line, err := c.conn.ReadCodeLine(StatusExtendedPassiveMode)
|
_, line, err := c.conn.ReadCodeLine(StatusExtendedPassiveMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -74,7 +74,7 @@ func (c *ServerConn) epsv() (port int, err os.Error) {
|
|||||||
start := strings.Index(line, "|||")
|
start := strings.Index(line, "|||")
|
||||||
end := strings.LastIndex(line, "|")
|
end := strings.LastIndex(line, "|")
|
||||||
if start == -1 || end == -1 {
|
if start == -1 || end == -1 {
|
||||||
err = os.NewError("Invalid EPSV response format")
|
err = errors.New("Invalid EPSV response format")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
port, err = strconv.Atoi(line[start+3 : end])
|
port, err = strconv.Atoi(line[start+3 : end])
|
||||||
@ -82,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) openDataConn() (net.Conn, os.Error) {
|
func (c *ServerConn) openDataConn() (net.Conn, error) {
|
||||||
port, err := c.epsv()
|
port, err := c.epsv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -100,7 +100,7 @@ func (c *ServerConn) openDataConn() (net.Conn, os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to execute a command and check for the expected code
|
// Helper function to execute a command and check for the expected code
|
||||||
func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, os.Error) {
|
func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, error) {
|
||||||
_, err := c.conn.Cmd(format, args...)
|
_, err := c.conn.Cmd(format, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, "", err
|
return 0, "", err
|
||||||
@ -111,7 +111,7 @@ func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to execute commands which require a data connection
|
// Helper function to execute commands which require a data connection
|
||||||
func (c *ServerConn) cmdDataConn(format string, args ...interface{}) (net.Conn, os.Error) {
|
func (c *ServerConn) cmdDataConn(format string, args ...interface{}) (net.Conn, error) {
|
||||||
conn, err := c.openDataConn()
|
conn, err := c.openDataConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -136,10 +136,10 @@ func (c *ServerConn) cmdDataConn(format string, args ...interface{}) (net.Conn,
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseListLine(line string) (*Entry, os.Error) {
|
func parseListLine(line string) (*Entry, error) {
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
if len(fields) < 9 {
|
if len(fields) < 9 {
|
||||||
return nil, os.NewError("Unsupported LIST line")
|
return nil, errors.New("Unsupported LIST line")
|
||||||
}
|
}
|
||||||
|
|
||||||
e := &Entry{}
|
e := &Entry{}
|
||||||
@ -151,14 +151,14 @@ func parseListLine(line string) (*Entry, os.Error) {
|
|||||||
case 'l':
|
case 'l':
|
||||||
e.Type = EntryTypeLink
|
e.Type = EntryTypeLink
|
||||||
default:
|
default:
|
||||||
return nil, os.NewError("Unknown entry type")
|
return nil, errors.New("Unknown entry type")
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Name = strings.Join(fields[8:], " ")
|
e.Name = strings.Join(fields[8:], " ")
|
||||||
return e, nil
|
return e, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) List(path string) (entries []*Entry, err os.Error) {
|
func (c *ServerConn) List(path string) (entries []*Entry, err error) {
|
||||||
conn, err := c.cmdDataConn("LIST %s", path)
|
conn, err := c.cmdDataConn("LIST %s", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -170,7 +170,7 @@ func (c *ServerConn) List(path string) (entries []*Entry, err os.Error) {
|
|||||||
bio := bufio.NewReader(r)
|
bio := bufio.NewReader(r)
|
||||||
for {
|
for {
|
||||||
line, e := bio.ReadString('\n')
|
line, e := bio.ReadString('\n')
|
||||||
if e == os.EOF {
|
if e == io.EOF {
|
||||||
break
|
break
|
||||||
} else if e != nil {
|
} else if e != nil {
|
||||||
return nil, e
|
return nil, e
|
||||||
@ -183,17 +183,21 @@ func (c *ServerConn) List(path string) (entries []*Entry, err os.Error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) ChangeDir(path string) os.Error {
|
// Changes the current directory to the specified path.
|
||||||
|
func (c *ServerConn) ChangeDir(path string) error {
|
||||||
_, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) ChangeDirToParent() os.Error {
|
// Changes the current directory to the parent directory.
|
||||||
|
// ChangeDir("..")
|
||||||
|
func (c *ServerConn) ChangeDirToParent() error {
|
||||||
_, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) CurrentDir() (string, os.Error) {
|
// Returns the path of the current directory.
|
||||||
|
func (c *ServerConn) CurrentDir() (string, error) {
|
||||||
_, msg, err := c.cmd(StatusPathCreated, "PWD")
|
_, msg, err := c.cmd(StatusPathCreated, "PWD")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -203,14 +207,15 @@ func (c *ServerConn) CurrentDir() (string, os.Error) {
|
|||||||
end := strings.LastIndex(msg, "\"")
|
end := strings.LastIndex(msg, "\"")
|
||||||
|
|
||||||
if start == -1 || end == -1 {
|
if start == -1 || end == -1 {
|
||||||
return "", os.NewError("Unsuported PWD response format")
|
return "", errors.New("Unsuported PWD response format")
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg[start+1 : end], nil
|
return msg[start+1 : end], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieves a remote file
|
// Retrieves a file from the remote FTP server.
|
||||||
func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) {
|
// The ReadCloser must be closed at the end of the operation.
|
||||||
|
func (c *ServerConn) Retr(path string) (io.ReadCloser, error) {
|
||||||
conn, err := c.cmdDataConn("RETR %s", path)
|
conn, err := c.cmdDataConn("RETR %s", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -220,8 +225,10 @@ func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) {
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) Stor(name string, r io.Reader) os.Error {
|
// Uploads a file to the remote FTP server.
|
||||||
conn, err := c.cmdDataConn("STOR %s", name)
|
// This function gets the data from the io.Reader. Hint: io.Pipe()
|
||||||
|
func (c *ServerConn) Stor(path string, r io.Reader) error {
|
||||||
|
conn, err := c.cmdDataConn("STOR %s", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -236,7 +243,8 @@ func (c *ServerConn) Stor(name string, r io.Reader) os.Error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) Rename(from, to string) os.Error {
|
// Renames a file on the remote FTP server.
|
||||||
|
func (c *ServerConn) Rename(from, to string) error {
|
||||||
_, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
|
_, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -246,35 +254,41 @@ func (c *ServerConn) Rename(from, to string) os.Error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) Delete(name string) os.Error {
|
// Deletes a file on the remote FTP server.
|
||||||
_, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", name)
|
func (c *ServerConn) Delete(path string) error {
|
||||||
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", path)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) MakeDir(name string) os.Error {
|
// Creates a new directory on the remote FTP server.
|
||||||
_, _, err := c.cmd(StatusPathCreated, "MKD %s", name)
|
func (c *ServerConn) MakeDir(path string) error {
|
||||||
|
_, _, err := c.cmd(StatusPathCreated, "MKD %s", path)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) RemoveDir(name string) os.Error {
|
// Removes a directory from the remote FTP server.
|
||||||
_, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", name)
|
func (c *ServerConn) RemoveDir(path string) error {
|
||||||
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", path)
|
||||||
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() error {
|
||||||
_, _, err := c.cmd(StatusCommandOK, "NOOP")
|
_, _, err := c.cmd(StatusCommandOK, "NOOP")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ServerConn) Quit() os.Error {
|
// Properly close the connection from the remote FTP server.
|
||||||
|
// It notifies the remote server that we are about to close the connection,
|
||||||
|
// then it really closes it.
|
||||||
|
func (c *ServerConn) Quit() error {
|
||||||
c.conn.Cmd("QUIT")
|
c.conn.Cmd("QUIT")
|
||||||
return c.conn.Close()
|
return c.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *response) Read(buf []byte) (int, os.Error) {
|
func (r *response) Read(buf []byte) (int, error) {
|
||||||
n, err := r.conn.Read(buf)
|
n, err := r.conn.Read(buf)
|
||||||
if err == os.EOF {
|
if err == io.EOF {
|
||||||
_, _, err2 := r.c.conn.ReadCodeLine(StatusClosingDataConnection)
|
_, _, err2 := r.c.conn.ReadCodeLine(StatusClosingDataConnection)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
err = err2
|
err = err2
|
||||||
@ -283,6 +297,6 @@ func (r *response) Read(buf []byte) (int, os.Error) {
|
|||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *response) Close() os.Error {
|
func (r *response) Close() error {
|
||||||
return r.conn.Close()
|
return r.conn.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user