2011-05-07 01:29:10 +02:00
|
|
|
package ftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2013-02-17 10:03:46 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2011-09-05 23:36:14 +02:00
|
|
|
"io"
|
2011-05-07 01:29:10 +02:00
|
|
|
"net"
|
2011-05-07 13:56:42 +02:00
|
|
|
"net/textproto"
|
2011-05-07 01:29:10 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2011-09-05 23:36:14 +02:00
|
|
|
type EntryType int
|
|
|
|
|
2011-05-07 01:29:10 +02:00
|
|
|
const (
|
2011-09-05 23:36:14 +02:00
|
|
|
EntryTypeFile EntryType = iota
|
2011-05-07 01:29:10 +02:00
|
|
|
EntryTypeFolder
|
|
|
|
EntryTypeLink
|
|
|
|
)
|
|
|
|
|
2011-05-07 13:13:51 +02:00
|
|
|
type ServerConn struct {
|
2011-05-07 13:56:42 +02:00
|
|
|
conn *textproto.Conn
|
|
|
|
host string
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Entry struct {
|
|
|
|
Name string
|
2011-09-05 23:36:14 +02:00
|
|
|
Type EntryType
|
2011-05-07 01:29:10 +02:00
|
|
|
Size uint64
|
|
|
|
}
|
|
|
|
|
2011-09-05 23:36:14 +02:00
|
|
|
type response struct {
|
|
|
|
conn net.Conn
|
|
|
|
c *ServerConn
|
|
|
|
}
|
|
|
|
|
2011-05-07 13:13:51 +02:00
|
|
|
// Connect to a ftp server and returns a ServerConn handler.
|
2011-12-27 22:50:50 +01:00
|
|
|
func Connect(addr string) (*ServerConn, error) {
|
2011-09-06 18:27:30 +02:00
|
|
|
conn, err := textproto.Dial("tcp", addr)
|
2011-05-07 01:29:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2011-09-06 18:27:30 +02:00
|
|
|
a := strings.SplitN(addr, ":", 2)
|
2011-05-07 13:56:42 +02:00
|
|
|
c := &ServerConn{conn, a[0]}
|
2011-05-07 01:29:10 +02:00
|
|
|
|
2011-05-07 13:56:42 +02:00
|
|
|
_, _, err = c.conn.ReadCodeLine(StatusReady)
|
2011-05-07 01:29:10 +02:00
|
|
|
if err != nil {
|
2011-09-05 23:36:14 +02:00
|
|
|
c.Quit()
|
2011-05-07 01:29:10 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2011-09-06 18:27:30 +02:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) Login(user, password string) error {
|
2011-09-06 19:12:22 +02:00
|
|
|
_, _, err := c.cmd(StatusUserOK, "USER %s", user)
|
2011-05-07 01:29:10 +02:00
|
|
|
if err != nil {
|
2011-09-06 18:27:30 +02:00
|
|
|
return err
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2011-09-06 19:12:22 +02:00
|
|
|
_, _, err = c.cmd(StatusLoggedIn, "PASS %s", password)
|
2013-05-08 17:39:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch to binary mode
|
|
|
|
_, _, err = c.cmd(StatusCommandOK, "TYPE I")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enter extended passive mode
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) epsv() (port int, err error) {
|
2011-05-07 13:56:42 +02:00
|
|
|
c.conn.Cmd("EPSV")
|
|
|
|
_, line, err := c.conn.ReadCodeLine(StatusExtendedPassiveMode)
|
2011-05-07 01:29:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
start := strings.Index(line, "|||")
|
|
|
|
end := strings.LastIndex(line, "|")
|
|
|
|
if start == -1 || end == -1 {
|
2011-12-27 22:50:50 +01:00
|
|
|
err = errors.New("Invalid EPSV response format")
|
2011-05-07 01:29:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
port, err = strconv.Atoi(line[start+3 : end])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a new data connection using extended passive mode
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) openDataConn() (net.Conn, error) {
|
2011-05-07 01:29:10 +02:00
|
|
|
port, err := c.epsv()
|
|
|
|
if err != nil {
|
2011-09-05 23:36:14 +02:00
|
|
|
return nil, err
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the new net address string
|
2011-05-07 13:56:42 +02:00
|
|
|
addr := fmt.Sprintf("%s:%d", c.host, port)
|
2011-05-07 01:29:10 +02:00
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", addr)
|
|
|
|
if err != nil {
|
2011-09-05 23:36:14 +02:00
|
|
|
return nil, err
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2011-09-05 23:36:14 +02:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2011-09-06 19:12:22 +02:00
|
|
|
// Helper function to execute a command and check for the expected code
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, error) {
|
2011-09-06 19:12:22 +02:00
|
|
|
_, err := c.conn.Cmd(format, args...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
code, line, err := c.conn.ReadCodeLine(expected)
|
|
|
|
return code, line, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to execute commands which require a data connection
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) cmdDataConn(format string, args ...interface{}) (net.Conn, error) {
|
2011-09-06 19:12:22 +02:00
|
|
|
conn, err := c.openDataConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.conn.Cmd(format, args...)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2011-09-05 23:36:14 +02:00
|
|
|
code, msg, err := c.conn.ReadCodeLine(-1)
|
|
|
|
if err != nil {
|
2011-09-06 19:12:22 +02:00
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
2011-09-05 23:36:14 +02:00
|
|
|
}
|
|
|
|
if code != StatusAlreadyOpen && code != StatusAboutToSend {
|
2011-09-06 19:12:22 +02:00
|
|
|
conn.Close()
|
2011-09-06 19:15:39 +02:00
|
|
|
return nil, &textproto.Error{code, msg}
|
2011-09-05 23:36:14 +02:00
|
|
|
}
|
2011-09-06 19:12:22 +02:00
|
|
|
|
|
|
|
return conn, nil
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
func parseListLine(line string) (*Entry, error) {
|
2011-05-07 01:29:10 +02:00
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < 9 {
|
2011-12-27 22:50:50 +01:00
|
|
|
return nil, errors.New("Unsupported LIST line")
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
e := &Entry{}
|
|
|
|
switch fields[0][0] {
|
2011-09-05 23:36:14 +02:00
|
|
|
case '-':
|
|
|
|
e.Type = EntryTypeFile
|
|
|
|
case 'd':
|
|
|
|
e.Type = EntryTypeFolder
|
|
|
|
case 'l':
|
|
|
|
e.Type = EntryTypeLink
|
|
|
|
default:
|
2011-12-27 22:50:50 +01:00
|
|
|
return nil, errors.New("Unknown entry type")
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2013-02-17 10:03:46 +01:00
|
|
|
if e.Type == EntryTypeFile {
|
|
|
|
size, err := strconv.ParseUint(fields[4], 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.Size = size
|
|
|
|
}
|
|
|
|
|
2011-05-07 01:29:10 +02:00
|
|
|
e.Name = strings.Join(fields[8:], " ")
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) List(path string) (entries []*Entry, err error) {
|
2011-09-06 19:12:22 +02:00
|
|
|
conn, err := c.cmdDataConn("LIST %s", path)
|
2011-05-07 01:29:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-09-05 23:36:14 +02:00
|
|
|
|
|
|
|
r := &response{conn, c}
|
2011-05-07 01:29:10 +02:00
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
bio := bufio.NewReader(r)
|
|
|
|
for {
|
|
|
|
line, e := bio.ReadString('\n')
|
2011-12-27 22:50:50 +01:00
|
|
|
if e == io.EOF {
|
2011-05-07 01:29:10 +02:00
|
|
|
break
|
|
|
|
} else if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
entry, err := parseListLine(line)
|
|
|
|
if err == nil {
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Changes the current directory to the specified path.
|
|
|
|
func (c *ServerConn) ChangeDir(path string) error {
|
2011-09-06 19:12:22 +02:00
|
|
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
|
|
|
|
return err
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Changes the current directory to the parent directory.
|
|
|
|
// ChangeDir("..")
|
|
|
|
func (c *ServerConn) ChangeDirToParent() error {
|
2011-09-07 16:52:52 +02:00
|
|
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Returns the path of the current directory.
|
|
|
|
func (c *ServerConn) CurrentDir() (string, error) {
|
2011-09-07 16:52:52 +02:00
|
|
|
_, msg, err := c.cmd(StatusPathCreated, "PWD")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
start := strings.Index(msg, "\"")
|
|
|
|
end := strings.LastIndex(msg, "\"")
|
|
|
|
|
|
|
|
if start == -1 || end == -1 {
|
2011-12-27 22:50:50 +01:00
|
|
|
return "", errors.New("Unsuported PWD response format")
|
2011-09-07 16:52:52 +02:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
return msg[start+1 : end], nil
|
2011-09-07 16:52:52 +02:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Retrieves a file from the remote FTP server.
|
|
|
|
// The ReadCloser must be closed at the end of the operation.
|
|
|
|
func (c *ServerConn) Retr(path string) (io.ReadCloser, error) {
|
2011-09-06 19:12:22 +02:00
|
|
|
conn, err := c.cmdDataConn("RETR %s", path)
|
2011-09-05 23:36:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &response{conn, c}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Uploads a file to the remote FTP server.
|
|
|
|
// 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)
|
2011-09-05 23:36:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(conn, r)
|
|
|
|
conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = c.conn.ReadCodeLine(StatusClosingDataConnection)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Renames a file on the remote FTP server.
|
|
|
|
func (c *ServerConn) Rename(from, to string) error {
|
2011-09-06 19:12:22 +02:00
|
|
|
_, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
|
2011-09-05 23:36:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2011-09-06 19:12:22 +02:00
|
|
|
_, _, err = c.cmd(StatusRequestedFileActionOK, "RNTO %s", to)
|
2011-09-05 23:36:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Deletes a file on the remote FTP server.
|
|
|
|
func (c *ServerConn) Delete(path string) error {
|
|
|
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", path)
|
2011-09-06 00:02:01 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Creates a new directory on the remote FTP server.
|
|
|
|
func (c *ServerConn) MakeDir(path string) error {
|
|
|
|
_, _, err := c.cmd(StatusPathCreated, "MKD %s", path)
|
2011-09-06 00:02:01 +02:00
|
|
|
return err
|
2011-09-05 23:36:14 +02:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// Removes a directory from the remote FTP server.
|
|
|
|
func (c *ServerConn) RemoveDir(path string) error {
|
|
|
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", path)
|
2011-09-06 00:02:01 +02:00
|
|
|
return err
|
2011-09-05 23:36:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sends a NOOP command. Usualy used to prevent timeouts.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) NoOp() error {
|
2011-09-06 19:12:22 +02:00
|
|
|
_, _, err := c.cmd(StatusCommandOK, "NOOP")
|
2011-09-05 23:36:14 +02:00
|
|
|
return err
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
// 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 {
|
2011-05-07 13:56:42 +02:00
|
|
|
c.conn.Cmd("QUIT")
|
2011-09-05 23:36:14 +02:00
|
|
|
return c.conn.Close()
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
func (r *response) Read(buf []byte) (int, error) {
|
2011-05-07 01:29:10 +02:00
|
|
|
n, err := r.conn.Read(buf)
|
2011-12-27 22:50:50 +01:00
|
|
|
if err == io.EOF {
|
2011-05-07 13:56:42 +02:00
|
|
|
_, _, err2 := r.c.conn.ReadCodeLine(StatusClosingDataConnection)
|
2011-05-07 01:29:10 +02:00
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2011-12-27 22:50:50 +01:00
|
|
|
func (r *response) Close() error {
|
2011-05-07 01:29:10 +02:00
|
|
|
return r.conn.Close()
|
|
|
|
}
|