ftp/ftp.go

196 lines
3.6 KiB
Go
Raw Normal View History

2011-05-07 01:29:10 +02:00
package ftp
import (
"bufio"
"net"
2011-05-07 13:56:42 +02:00
"net/textproto"
2011-05-07 01:29:10 +02:00
"os"
"fmt"
"strconv"
"strings"
)
const (
EntryTypeFile = iota
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 Response struct {
conn net.Conn
c *ServerConn
2011-05-07 01:29:10 +02:00
}
type Entry struct {
Name string
EntryType int
Size uint64
}
// Connect to a ftp server and returns a ServerConn handler.
func Connect(host, user, password string) (*ServerConn, os.Error) {
2011-05-07 13:56:42 +02:00
conn, err := textproto.Dial("tcp", host)
2011-05-07 01:29:10 +02:00
if err != nil {
return nil, err
}
2011-05-07 13:56:42 +02:00
a := strings.Split(host, ":", 2)
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.Close()
return nil, err
}
2011-05-07 13:56:42 +02:00
c.conn.Cmd("USER %s", user)
_, _, err = c.conn.ReadCodeLine(StatusUserOK)
2011-05-07 01:29:10 +02:00
if err != nil {
c.Close()
return nil, err
}
2011-05-07 13:56:42 +02:00
c.conn.Cmd("PASS %s", password)
_, _, err = c.conn.ReadCodeLine(StatusLoggedIn)
2011-05-07 01:29:10 +02:00
if err != nil {
c.Close()
return nil, err
}
return c, nil
}
// Like Connect() but with anonymous credentials.
func ConnectAnonymous(host string) (*ServerConn, os.Error) {
2011-05-07 01:29:10 +02:00
return Connect(host, "anonymous", "anonymous")
}
// 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
func (c *ServerConn) openDataConnection() (r *Response, err os.Error) {
2011-05-07 01:29:10 +02:00
port, err := c.epsv()
if err != nil {
return
}
// 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
}
r = &Response{conn, c}
return
}
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.EntryType = EntryTypeFile
case 'd':
e.EntryType = EntryTypeFolder
case 'l':
e.EntryType = EntryTypeLink
default:
return nil, os.NewError("Unknown entry type")
}
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-05-07 01:29:10 +02:00
r, err := c.openDataConnection()
if err != nil {
return
}
defer r.Close()
2011-05-08 14:08:31 +02:00
c.conn.Cmd("LIST %s", path)
2011-05-07 13:56:42 +02:00
_, _, err = c.conn.ReadCodeLine(StatusAboutToSend)
2011-05-07 01:29:10 +02:00
if err != nil {
return
}
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
}
func (c *ServerConn) ChangeDir(path string) (err os.Error) {
2011-05-07 13:56:42 +02:00
c.conn.Cmd("CWD %s", path);
_, _, err = c.conn.ReadCodeLine(StatusRequestedFileActionOK)
2011-05-07 01:29:10 +02:00
return
}
func (c *ServerConn) Get(path string) (r *Response, err os.Error) {
2011-05-07 01:29:10 +02:00
r, err = c.openDataConnection()
if err != nil {
return
}
2011-05-07 13:56:42 +02:00
c.conn.Cmd("RETR %s", path)
_, _, err = c.conn.ReadCodeLine(StatusAboutToSend)
2011-05-07 01:29:10 +02:00
return
}
func (c *ServerConn) Close() {
2011-05-07 13:56:42 +02:00
c.conn.Cmd("QUIT")
2011-05-07 01:29:10 +02:00
c.conn.Close()
}
func (r *Response) Read(buf []byte) (int, os.Error) {
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 {
return r.conn.Close()
}