|
@@ -2,6 +2,7 @@ package ftp
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"bufio"
|
|
"bufio"
|
|
|
|
|
+ "io"
|
|
|
"net"
|
|
"net"
|
|
|
"net/textproto"
|
|
"net/textproto"
|
|
|
"os"
|
|
"os"
|
|
@@ -10,8 +11,10 @@ import (
|
|
|
"strings"
|
|
"strings"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+type EntryType int
|
|
|
|
|
+
|
|
|
const (
|
|
const (
|
|
|
- EntryTypeFile = iota
|
|
|
|
|
|
|
+ EntryTypeFile EntryType = iota
|
|
|
EntryTypeFolder
|
|
EntryTypeFolder
|
|
|
EntryTypeLink
|
|
EntryTypeLink
|
|
|
)
|
|
)
|
|
@@ -21,17 +24,17 @@ type ServerConn struct {
|
|
|
host string
|
|
host string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-type Response struct {
|
|
|
|
|
- conn net.Conn
|
|
|
|
|
- c *ServerConn
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
type Entry struct {
|
|
type Entry struct {
|
|
|
Name string
|
|
Name string
|
|
|
- EntryType int
|
|
|
|
|
|
|
+ Type EntryType
|
|
|
Size uint64
|
|
Size uint64
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+type response struct {
|
|
|
|
|
+ conn net.Conn
|
|
|
|
|
+ c *ServerConn
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// Connect to a ftp server and returns a ServerConn handler.
|
|
// Connect to a ftp server and returns a ServerConn handler.
|
|
|
func Connect(host, user, password string) (*ServerConn, os.Error) {
|
|
func Connect(host, user, password string) (*ServerConn, os.Error) {
|
|
|
conn, err := textproto.Dial("tcp", host)
|
|
conn, err := textproto.Dial("tcp", host)
|
|
@@ -39,26 +42,26 @@ func Connect(host, user, password string) (*ServerConn, os.Error) {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- a := strings.Split(host, ":", 2)
|
|
|
|
|
|
|
+ a := strings.SplitN(host, ":", 2)
|
|
|
c := &ServerConn{conn, a[0]}
|
|
c := &ServerConn{conn, a[0]}
|
|
|
|
|
|
|
|
_, _, err = c.conn.ReadCodeLine(StatusReady)
|
|
_, _, err = c.conn.ReadCodeLine(StatusReady)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- c.Close()
|
|
|
|
|
|
|
+ c.Quit()
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
c.conn.Cmd("USER %s", user)
|
|
c.conn.Cmd("USER %s", user)
|
|
|
_, _, err = c.conn.ReadCodeLine(StatusUserOK)
|
|
_, _, err = c.conn.ReadCodeLine(StatusUserOK)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- c.Close()
|
|
|
|
|
|
|
+ c.Quit()
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
c.conn.Cmd("PASS %s", password)
|
|
c.conn.Cmd("PASS %s", password)
|
|
|
_, _, err = c.conn.ReadCodeLine(StatusLoggedIn)
|
|
_, _, err = c.conn.ReadCodeLine(StatusLoggedIn)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- c.Close()
|
|
|
|
|
|
|
+ c.Quit()
|
|
|
return nil, err
|
|
return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -88,10 +91,10 @@ 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() (r *Response, err os.Error) {
|
|
|
|
|
|
|
+func (c *ServerConn) openDataConnection() (net.Conn, os.Error) {
|
|
|
port, err := c.epsv()
|
|
port, err := c.epsv()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return
|
|
|
|
|
|
|
+ return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Build the new net address string
|
|
// Build the new net address string
|
|
@@ -99,11 +102,25 @@ func (c *ServerConn) openDataConnection() (r *Response, err os.Error) {
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", addr)
|
|
conn, err := net.Dial("tcp", addr)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return
|
|
|
|
|
|
|
+ return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- r = &Response{conn, c}
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ return conn, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Helper function to check if the last command succeeded and if it will
|
|
|
|
|
+// send the data to the data connection.
|
|
|
|
|
+// This is needed because some servers return StatusAboutToSend (150)
|
|
|
|
|
+// and some StatusAlreadyOpen (125)
|
|
|
|
|
+func (c *ServerConn) checkDataConn() os.Error {
|
|
|
|
|
+ code, msg, err := c.conn.ReadCodeLine(-1)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ if code != StatusAlreadyOpen && code != StatusAboutToSend {
|
|
|
|
|
+ return os.NewError(fmt.Sprintf("%d %s", code, msg))
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func parseListLine(line string) (*Entry, os.Error) {
|
|
func parseListLine(line string) (*Entry, os.Error) {
|
|
@@ -114,14 +131,14 @@ func parseListLine(line string) (*Entry, os.Error) {
|
|
|
|
|
|
|
|
e := &Entry{}
|
|
e := &Entry{}
|
|
|
switch fields[0][0] {
|
|
switch fields[0][0] {
|
|
|
- case '-':
|
|
|
|
|
- e.EntryType = EntryTypeFile
|
|
|
|
|
- case 'd':
|
|
|
|
|
- e.EntryType = EntryTypeFolder
|
|
|
|
|
- case 'l':
|
|
|
|
|
- e.EntryType = EntryTypeLink
|
|
|
|
|
- default:
|
|
|
|
|
- return nil, os.NewError("Unknown entry type")
|
|
|
|
|
|
|
+ case '-':
|
|
|
|
|
+ e.Type = EntryTypeFile
|
|
|
|
|
+ case 'd':
|
|
|
|
|
+ e.Type = EntryTypeFolder
|
|
|
|
|
+ case 'l':
|
|
|
|
|
+ e.Type = EntryTypeLink
|
|
|
|
|
+ default:
|
|
|
|
|
+ return nil, os.NewError("Unknown entry type")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
e.Name = strings.Join(fields[8:], " ")
|
|
e.Name = strings.Join(fields[8:], " ")
|
|
@@ -129,14 +146,20 @@ 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) {
|
|
|
- r, err := c.openDataConnection()
|
|
|
|
|
|
|
+ conn, err := c.openDataConnection()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ r := &response{conn, c}
|
|
|
defer r.Close()
|
|
defer r.Close()
|
|
|
|
|
|
|
|
- c.conn.Cmd("LIST %s", path)
|
|
|
|
|
- _, _, err = c.conn.ReadCodeLine(StatusAboutToSend)
|
|
|
|
|
|
|
+ _, err = c.conn.Cmd("LIST %s", path)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ err = c.checkDataConn()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -158,28 +181,109 @@ func (c *ServerConn) List(path string) (entries []*Entry, err os.Error) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (c *ServerConn) ChangeDir(path string) (err os.Error) {
|
|
func (c *ServerConn) ChangeDir(path string) (err os.Error) {
|
|
|
- c.conn.Cmd("CWD %s", path);
|
|
|
|
|
- _, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
|
|
|
|
|
|
|
+ _, err = c.conn.Cmd("CWD %s", path)
|
|
|
|
|
+ if err == nil {
|
|
|
|
|
+ _, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
|
|
|
|
|
+ }
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *ServerConn) Get(path string) (r *Response, err os.Error) {
|
|
|
|
|
- r, err = c.openDataConnection()
|
|
|
|
|
|
|
+// Retrieves a remote file
|
|
|
|
|
+func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) {
|
|
|
|
|
+ conn, err := c.openDataConnection()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- return
|
|
|
|
|
|
|
+ return nil, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- c.conn.Cmd("RETR %s", path)
|
|
|
|
|
- _, _, err = c.conn.ReadCodeLine(StatusAboutToSend)
|
|
|
|
|
- return
|
|
|
|
|
|
|
+ _, 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}
|
|
|
|
|
+ return r, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (c *ServerConn) Stor(name string, r io.Reader) os.Error {
|
|
|
|
|
+ conn, err := c.openDataConnection()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ 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)
|
|
|
|
|
+ conn.Close()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _, _, err = c.conn.ReadCodeLine(StatusClosingDataConnection)
|
|
|
|
|
+ return err
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (c *ServerConn) Rename(from, to string) os.Error {
|
|
|
|
|
+ _, err := c.conn.Cmd("RNFR %s", from)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _, _, err = c.conn.ReadCodeLine(StatusRequestFilePending)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _, err = c.conn.Cmd("RNTO %s", to)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
|
|
|
|
|
+ return err
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (c *ServerConn) MakeDir(name string) os.Error {
|
|
|
|
|
+ // todo
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (c *ServerConn) RemoveDir(name string) os.Error {
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Sends a NOOP command. Usualy used to prevent timeouts.
|
|
|
|
|
+func (c *ServerConn) NoOp() os.Error {
|
|
|
|
|
+ _, err := c.conn.Cmd("NOOP")
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ _, _, err = c.conn.ReadCodeLine(StatusCommandOK)
|
|
|
|
|
+ return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *ServerConn) Close() {
|
|
|
|
|
|
|
+func (c *ServerConn) Quit() os.Error {
|
|
|
c.conn.Cmd("QUIT")
|
|
c.conn.Cmd("QUIT")
|
|
|
- c.conn.Close()
|
|
|
|
|
|
|
+ return c.conn.Close()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (r *Response) Read(buf []byte) (int, os.Error) {
|
|
|
|
|
|
|
+func (r *response) Read(buf []byte) (int, os.Error) {
|
|
|
n, err := r.conn.Read(buf)
|
|
n, err := r.conn.Read(buf)
|
|
|
if err == os.EOF {
|
|
if err == os.EOF {
|
|
|
_, _, err2 := r.c.conn.ReadCodeLine(StatusClosingDataConnection)
|
|
_, _, err2 := r.c.conn.ReadCodeLine(StatusClosingDataConnection)
|
|
@@ -190,6 +294,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() os.Error {
|
|
|
return r.conn.Close()
|
|
return r.conn.Close()
|
|
|
}
|
|
}
|