ftp/ftp.go

268 lines
5.2 KiB
Go
Raw Normal View History

2011-05-07 01:29:10 +02:00
package ftp
import (
"bufio"
"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
"os"
"fmt"
"strconv"
"strings"
)
type EntryType int
2011-05-07 01:29:10 +02:00
const (
EntryTypeFile EntryType = iota
2011-05-07 01:29:10 +02:00
EntryTypeFolder
EntryTypeLink
)
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
Type EntryType
2011-05-07 01:29:10 +02:00
Size uint64
}
type response struct {
conn net.Conn
c *ServerConn
}
// Connect to a ftp server and returns a ServerConn handler.
func Connect(addr string) (*ServerConn, os.Error) {
conn, err := textproto.Dial("tcp", addr)
2011-05-07 01:29:10 +02:00
if err != nil {
return nil, err
}
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 {
c.Quit()
2011-05-07 01:29:10 +02:00
return nil, err
}
return c, nil
}
func (c *ServerConn) Login(user, password string) os.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 {
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)
return err
2011-05-07 01:29:10 +02:00
}
// Enter extended passive mode
func (c *ServerConn) epsv() (port int, err os.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 {
err = os.NewError("Invalid EPSV response format")
return
}
port, err = strconv.Atoi(line[start+3 : end])
return
}
// Open a new data connection using extended passive mode
2011-09-06 19:12:22 +02:00
func (c *ServerConn) openDataConn() (net.Conn, os.Error) {
2011-05-07 01:29:10 +02:00
port, err := c.epsv()
if err != nil {
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 {
return nil, err
2011-05-07 01:29:10 +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
func (c *ServerConn) cmd(expected int, format string, args ...interface{}) (int, string, os.Error) {
_, 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
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)
if err != nil {
2011-09-06 19:12:22 +02:00
conn.Close()
return nil, err
}
if code != StatusAlreadyOpen && code != StatusAboutToSend {
2011-09-06 19:12:22 +02:00
conn.Close()
return nil, &textproto.Error{code, msg}
}
2011-09-06 19:12:22 +02:00
return conn, nil
2011-05-07 01:29:10 +02:00
}
func parseListLine(line string) (*Entry, os.Error) {
fields := strings.Fields(line)
if len(fields) < 9 {
return nil, os.NewError("Unsupported LIST line")
}
e := &Entry{}
switch fields[0][0] {
case '-':
e.Type = EntryTypeFile
case 'd':
e.Type = EntryTypeFolder
case 'l':
e.Type = EntryTypeLink
default:
return nil, os.NewError("Unknown entry type")
2011-05-07 01:29:10 +02:00
}
e.Name = strings.Join(fields[8:], " ")
return e, nil
}
2011-05-08 14:08:31 +02:00
func (c *ServerConn) List(path string) (entries []*Entry, err os.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
}
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')
if e == os.EOF {
break
} else if e != nil {
return nil, e
}
entry, err := parseListLine(line)
if err == nil {
entries = append(entries, entry)
}
}
return
}
2011-09-06 19:12:22 +02:00
func (c *ServerConn) ChangeDir(path string) os.Error {
_, _, err := c.cmd(StatusRequestedFileActionOK, "CWD %s", path)
return err
2011-05-07 01:29:10 +02:00
}
// Retrieves a remote file
func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) {
2011-09-06 19:12:22 +02:00
conn, err := c.cmdDataConn("RETR %s", path)
if err != nil {
return nil, err
}
r := &response{conn, c}
return r, nil
}
func (c *ServerConn) Stor(name string, r io.Reader) os.Error {
2011-09-06 19:12:22 +02:00
conn, err := c.cmdDataConn("STOR %s", name)
if err != nil {
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 {
2011-09-06 19:12:22 +02:00
_, _, err := c.cmd(StatusRequestFilePending, "RNFR %s", from)
if err != nil {
return err
}
2011-09-06 19:12:22 +02:00
_, _, err = c.cmd(StatusRequestedFileActionOK, "RNTO %s", to)
return err
}
func (c *ServerConn) Delete(name string) os.Error {
2011-09-06 19:12:22 +02:00
_, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", name)
return err
}
func (c *ServerConn) MakeDir(name string) os.Error {
2011-09-06 19:12:22 +02:00
_, _, err := c.cmd(StatusPathCreated, "MKD %s", name)
return err
}
func (c *ServerConn) RemoveDir(name string) os.Error {
2011-09-06 19:12:22 +02:00
_, _, err := c.cmd(StatusRequestedFileActionOK, "RMD %s", name)
return err
}
// Sends a NOOP command. Usualy used to prevent timeouts.
func (c *ServerConn) NoOp() os.Error {
2011-09-06 19:12:22 +02:00
_, _, err := c.cmd(StatusCommandOK, "NOOP")
return err
2011-05-07 01:29:10 +02:00
}
func (c *ServerConn) Quit() os.Error {
2011-05-07 13:56:42 +02:00
c.conn.Cmd("QUIT")
return c.conn.Close()
2011-05-07 01:29:10 +02:00
}
func (r *response) Read(buf []byte) (int, os.Error) {
2011-05-07 01:29:10 +02:00
n, err := r.conn.Read(buf)
if err == os.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
}
func (r *response) Close() os.Error {
2011-05-07 01:29:10 +02:00
return r.conn.Close()
}