Commits

Ross Light  committed 0c51a10

gofix for weekly.2012-02-22

  • Participants
  • Parent commits 95790c2

Comments (0)

Files changed (3)

 package ftp
 
 import (
+	"errors"
 	"io"
-	"os"
 	"regexp"
 	"strconv"
 	"strings"
 }
 
 // Dial connects to an FTP server.
-func Dial(network, addr string) (*Client, os.Error) {
+func Dial(network, addr string) (*Client, error) {
 	c, err := net.Dial(network, addr)
 	if err != nil {
 		return nil, err
 }
 
 // NewClient creates an FTP client from an existing connection.
-func NewClient(c net.Conn) (*Client, os.Error) {
-	var err os.Error
+func NewClient(c net.Conn) (*Client, error) {
+	var err error
 	client := &Client{
 		c:     c,
 		proto: textproto.NewConn(c),
 }
 
 // Quit sends the QUIT command and closes the connection.
-func (client *Client) Quit() os.Error {
+func (client *Client) Quit() error {
 	_, err := client.Do("QUIT")
 	if err != nil {
 		return err
 }
 
 // Close closes the connection.
-func (client *Client) Close() os.Error {
+func (client *Client) Close() error {
 	return client.proto.Close()
 }
 
 // Login sends credentials to the server.
-func (client *Client) Login(username, password string) os.Error {
+func (client *Client) Login(username, password string) error {
 	reply, err := client.Do("USER " + username)
 	if err != nil {
 		return err
 }
 
 // response reads a reply from the server.
-func (client *Client) response() (Reply, os.Error) {
+func (client *Client) response() (Reply, error) {
 	line, err := client.proto.ReadLine()
 	if err != nil {
 		return Reply{}, err
 	} else if len(line) < 4 {
-		return Reply{}, os.NewError("Short response line in FTP")
+		return Reply{}, errors.New("Short response line in FTP")
 	}
 
 	code, err := strconv.Atoi(line[:3])
 	case ' ':
 		reply.Msg = line[4:]
 	default:
-		return Reply{}, os.NewError("Expected space after FTP response code")
+		return Reply{}, errors.New("Expected space after FTP response code")
 	}
 	return reply, nil
 }
 
 // Do sends a command over the control connection and waits for the response.  It returns any
 // protocol error encountered while performing the command.
-func (client *Client) Do(command string) (Reply, os.Error) {
+func (client *Client) Do(command string) (Reply, error) {
 	err := client.proto.PrintfLine("%s", command)
 	if err != nil {
 		return Reply{}, err
 	}
-	return client.response()
+	r, err := client.response()
+	return r, err
 }
 
 // Passive opens a new passive data port.
-func (client *Client) Passive() (io.ReadWriteCloser, os.Error) {
+func (client *Client) Passive() (io.ReadWriteCloser, error) {
 	var reply Reply
 	var addr *net.TCPAddr
-	var err os.Error
+	var err error
 
 	switch client.c.RemoteAddr().Network() {
 	case "tcp6":
 
 var pasvRegexp = regexp.MustCompile(`([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)`)
 
-func parsePasvReply(msg string) (*net.TCPAddr, os.Error) {
+func parsePasvReply(msg string) (*net.TCPAddr, error) {
 	numberStrings := pasvRegexp.FindStringSubmatch(msg)
 	if numberStrings == nil {
-		return nil, os.NewError("PASV reply provided no port")
+		return nil, errors.New("PASV reply provided no port")
 	}
 	numbers := make([]byte, len(numberStrings))
 	for i, s := range numberStrings {
 	epsvEnd   = "|)"
 )
 
-func parseEpsvReply(msg string) (port int, err os.Error) {
+func parseEpsvReply(msg string) (port int, err error) {
 	start := strings.LastIndex(msg, epsvStart)
 	if start == -1 {
-		return 0, os.NewError("EPSV reply provided no port")
+		return 0, errors.New("EPSV reply provided no port")
 	}
 	start += len(epsvStart)
 
 	end := strings.LastIndex(msg, epsvEnd)
 	if end == -1 || end <= start {
-		return 0, os.NewError("EPSV reply provided no port")
+		return 0, errors.New("EPSV reply provided no port")
 	}
 
 	return strconv.Atoi(msg[start:end])
 }
 
+type transferConn struct {
+	io.ReadWriteCloser
+	client *Client
+}
+
+func (conn transferConn) Close() error {
+	err := conn.ReadWriteCloser.Close()
+	if err != nil {
+		return err
+	}
+	reply, err := conn.client.response()
+	if err != nil {
+		return err
+	} else if !reply.PositiveComplete() {
+		return reply
+	}
+	return nil
+}
+
 // transfer sends a command and opens a new passive data connection.
-func (client *Client) transfer(command, dataType string) (io.ReadWriteCloser, os.Error) {
+func (client *Client) transfer(command, dataType string) (io.ReadWriteCloser, error) {
 	// Set type
 	reply, err := client.Do("TYPE " + dataType)
 	switch {
 		return nil, reply
 	}
 
-	return conn, nil
+	return transferConn{conn, client}, nil
 }
 
 // Text sends a command and opens a new passive data connection in ASCII mode.
-func (client *Client) Text(command string) (io.ReadWriteCloser, os.Error) {
+func (client *Client) Text(command string) (io.ReadWriteCloser, error) {
 	return client.transfer(command, "A")
 }
 
 // Binary sends a command and opens a new passive data connection in image mode.
-func (client *Client) Binary(command string) (io.ReadWriteCloser, os.Error) {
+func (client *Client) Binary(command string) (io.ReadWriteCloser, error) {
 	return client.transfer(command, "I")
 }

File client_test.go

 	"bytes"
 	"net"
 	"net/textproto"
-	"os"
 	"reflect"
 	"testing"
 )
 	R, W *bytes.Buffer
 }
 
-func (conn MockRWC) Read(p []byte) (n int, err os.Error) {
+func (conn MockRWC) Read(p []byte) (n int, err error) {
 	return conn.R.Read(p)
 }
 
-func (conn MockRWC) Write(p []byte) (n int, err os.Error) {
+func (conn MockRWC) Write(p []byte) (n int, err error) {
 	return conn.W.Write(p)
 }
 
-func (conn MockRWC) Close() os.Error {
+func (conn MockRWC) Close() error {
 	return nil
 }
 
 	}
 	return r.Code.String() + " " + r.Msg
 }
+
+func (r Reply) Error() string {
+	return r.String()
+}