2013-05-19 13:09:37 +02:00
|
|
|
// Package ftp implements a FTP client as described in RFC 959.
|
2011-05-07 01:29:10 +02:00
|
|
|
package ftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2013-02-17 10:03:46 +01:00
|
|
|
"errors"
|
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"
|
2013-07-08 07:21:43 +02:00
|
|
|
"time"
|
2011-05-07 01:29:10 +02:00
|
|
|
)
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// EntryType describes the different types of an Entry.
|
2011-09-05 23:36:14 +02:00
|
|
|
type EntryType int
|
|
|
|
|
2015-03-05 11:57:38 +01:00
|
|
|
// The differents types of an Entry
|
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
|
|
|
|
)
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// ServerConn represents the connection to a remote FTP server.
|
2011-05-07 13:13:51 +02:00
|
|
|
type ServerConn struct {
|
2013-05-19 21:54:57 +02:00
|
|
|
conn *textproto.Conn
|
|
|
|
host string
|
2015-03-16 23:45:56 +01:00
|
|
|
timeout time.Duration
|
2013-05-19 21:54:57 +02:00
|
|
|
features map[string]string
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Entry describes a file and is returned by List().
|
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
|
2013-07-08 07:21:43 +02:00
|
|
|
Time time.Time
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 21:54:57 +02:00
|
|
|
// response represent a data-connection
|
2011-09-05 23:36:14 +02:00
|
|
|
type response struct {
|
|
|
|
conn net.Conn
|
|
|
|
c *ServerConn
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:45:56 +01:00
|
|
|
// Connect is an alias to Dial, for backward compatibility
|
|
|
|
func Connect(addr string) (*ServerConn, error) {
|
|
|
|
return Dial(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial is like DialTimeout with no timeout
|
|
|
|
func Dial(addr string) (*ServerConn, error) {
|
|
|
|
return DialTimeout(addr, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialTimeout initializes the connection to the specified ftp server address.
|
2013-05-19 13:09:37 +02:00
|
|
|
//
|
|
|
|
// It is generally followed by a call to Login() as most FTP commands require
|
|
|
|
// an authenticated user.
|
2015-03-16 23:45:56 +01:00
|
|
|
func DialTimeout(addr string, timeout time.Duration) (*ServerConn, error) {
|
2015-08-18 23:38:53 +02:00
|
|
|
tconn, err := net.DialTimeout("tcp", addr, timeout)
|
2011-05-07 01:29:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-18 23:38:53 +02:00
|
|
|
// Use the resolved IP address in case addr contains a domain name
|
|
|
|
// If we use the domain name, we might not resolve to the same IP.
|
|
|
|
remoteAddr := tconn.RemoteAddr().String()
|
|
|
|
host, _, err := net.SplitHostPort(remoteAddr)
|
2013-12-04 23:42:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-18 19:00:56 +02:00
|
|
|
|
|
|
|
conn := textproto.NewConn(tconn)
|
|
|
|
|
2013-05-19 21:54:57 +02:00
|
|
|
c := &ServerConn{
|
|
|
|
conn: conn,
|
2013-12-04 23:42:17 +01:00
|
|
|
host: host,
|
2015-03-16 23:45:56 +01:00
|
|
|
timeout: timeout,
|
2013-05-19 21:54:57 +02:00
|
|
|
features: make(map[string]string),
|
|
|
|
}
|
2011-05-07 01:29:10 +02:00
|
|
|
|
2013-07-08 07:48:11 +02:00
|
|
|
_, _, err = c.conn.ReadResponse(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
|
|
|
|
}
|
|
|
|
|
2013-05-19 21:54:57 +02:00
|
|
|
err = c.feat()
|
|
|
|
if err != nil {
|
|
|
|
c.Quit()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2011-09-06 18:27:30 +02:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Login authenticates the client with specified user and password.
|
|
|
|
//
|
|
|
|
// "anonymous"/"anonymous" is a common user/password scheme for FTP servers
|
|
|
|
// that allows anonymous read-only accounts.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) Login(user, password string) error {
|
2014-02-18 12:57:01 +01:00
|
|
|
code, message, err := c.cmd(-1, "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
|
|
|
}
|
|
|
|
|
2014-02-18 12:57:01 +01:00
|
|
|
switch code {
|
|
|
|
case StatusLoggedIn:
|
|
|
|
case StatusUserOK:
|
|
|
|
_, _, err = c.cmd(StatusLoggedIn, "PASS %s", password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New(message)
|
2013-05-08 17:39:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-05-19 21:54:57 +02:00
|
|
|
// feat issues a FEAT FTP command to list the additional commands supported by
|
|
|
|
// the remote FTP server.
|
|
|
|
// FEAT is described in RFC 2389
|
|
|
|
func (c *ServerConn) feat() error {
|
|
|
|
code, message, err := c.cmd(-1, "FEAT")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if code != StatusSystem {
|
|
|
|
// The server does not support the FEAT command. This is not an
|
2013-05-20 01:20:29 +02:00
|
|
|
// error: we consider that there is no additional feature.
|
2013-05-19 21:54:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(message, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
if !strings.HasPrefix(line, " ") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-05-20 01:30:03 +02:00
|
|
|
line = strings.TrimSpace(line)
|
2013-05-19 21:54:57 +02:00
|
|
|
featureElements := strings.SplitN(line, " ", 2)
|
|
|
|
|
|
|
|
command := featureElements[0]
|
|
|
|
|
|
|
|
var commandDesc string
|
|
|
|
if len(featureElements) == 2 {
|
|
|
|
commandDesc = featureElements[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
c.features[command] = commandDesc
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// epsv issues an "EPSV" command to get a port number for a data connection.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) epsv() (port int, err error) {
|
2013-05-19 21:23:29 +02:00
|
|
|
_, line, err := c.cmd(StatusExtendedPassiveMode, "EPSV")
|
2011-05-07 01:29:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-05-19 21:23:29 +02:00
|
|
|
|
2011-05-07 01:29:10 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-09-07 18:48:40 +02:00
|
|
|
// pasv issues a "PASV" command to get a port number for a data connection.
|
2013-09-04 14:54:57 +02:00
|
|
|
func (c *ServerConn) pasv() (port int, err error) {
|
2013-09-07 18:48:40 +02:00
|
|
|
_, line, err := c.cmd(StatusPassiveMode, "PASV")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// PASV response format : 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
|
|
|
|
start := strings.Index(line, "(")
|
|
|
|
end := strings.LastIndex(line, ")")
|
|
|
|
if start == -1 || end == -1 {
|
2016-02-07 22:57:06 +01:00
|
|
|
return 0, errors.New("Invalid PASV response format")
|
2013-09-07 18:48:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have to split the response string
|
|
|
|
pasvData := strings.Split(line[start+1:end], ",")
|
2016-02-07 22:57:06 +01:00
|
|
|
|
|
|
|
if len(pasvData) < 6 {
|
|
|
|
return 0, errors.New("Invalid PASV response format")
|
|
|
|
}
|
|
|
|
|
2013-09-07 18:48:40 +02:00
|
|
|
// Let's compute the port number
|
|
|
|
portPart1, err1 := strconv.Atoi(pasvData[4])
|
|
|
|
if err1 != nil {
|
|
|
|
err = err1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
portPart2, err2 := strconv.Atoi(pasvData[5])
|
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recompose port
|
|
|
|
port = portPart1*256 + portPart2
|
|
|
|
return
|
2013-09-04 14:54:57 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// openDataConn creates a new FTP data connection.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) openDataConn() (net.Conn, error) {
|
2015-12-02 14:06:05 +01:00
|
|
|
var (
|
|
|
|
port int
|
|
|
|
err error
|
|
|
|
)
|
2013-09-04 14:54:57 +02:00
|
|
|
|
2015-12-02 14:06:05 +01:00
|
|
|
if port, err = c.epsv(); err != nil {
|
|
|
|
if port, err = c.pasv(); err != nil {
|
2013-09-07 18:48:40 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2015-12-02 14:06:05 +01:00
|
|
|
return net.DialTimeout("tcp", net.JoinHostPort(c.host, strconv.Itoa(port)), c.timeout)
|
2011-09-05 23:36:14 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// cmd is a helper function to execute a command and check for the expected FTP
|
|
|
|
// return 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
|
|
|
|
}
|
|
|
|
|
2015-08-18 23:16:40 +02:00
|
|
|
return c.conn.ReadResponse(expected)
|
2011-09-06 19:12:22 +02:00
|
|
|
}
|
|
|
|
|
2013-12-04 23:30:50 +01:00
|
|
|
// cmdDataConnFrom executes a command which require a FTP data connection.
|
|
|
|
// Issues a REST FTP command to specify the number of bytes to skip for the transfer.
|
|
|
|
func (c *ServerConn) cmdDataConnFrom(offset uint64, 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
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:30:50 +01:00
|
|
|
if offset != 0 {
|
|
|
|
_, _, err := c.cmd(StatusRequestFilePending, "REST %d", offset)
|
|
|
|
if err != nil {
|
2016-11-18 01:20:19 +01:00
|
|
|
conn.Close()
|
2013-12-04 23:30:50 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-06 19:12:22 +02:00
|
|
|
_, err = c.conn.Cmd(format, args...)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-30 11:55:46 +01:00
|
|
|
code, msg, err := c.conn.ReadResponse(-1)
|
2011-09-05 23:36:14 +02:00
|
|
|
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()
|
2015-03-05 11:49:26 +01:00
|
|
|
return nil, &textproto.Error{Code: code, Msg: 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
|
|
|
}
|
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
var errUnsupportedListLine = errors.New("Unsupported LIST line")
|
2015-08-15 13:28:07 +02:00
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
// parseRFC3659ListLine parses the style of directory line defined in RFC 3659.
|
|
|
|
func parseRFC3659ListLine(line string) (*Entry, error) {
|
2015-08-29 13:20:47 +02:00
|
|
|
iSemicolon := strings.Index(line, ";")
|
|
|
|
iWhitespace := strings.Index(line, " ")
|
|
|
|
|
|
|
|
if iSemicolon < 0 || iSemicolon > iWhitespace {
|
2015-08-25 01:03:52 +02:00
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
2015-08-15 13:28:07 +02:00
|
|
|
|
2015-08-29 13:20:47 +02:00
|
|
|
e := &Entry{
|
2015-11-30 12:22:16 +01:00
|
|
|
Name: line[iWhitespace+1:],
|
2015-08-29 13:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range strings.Split(line[:iWhitespace-1], ";") {
|
2015-08-25 01:03:52 +02:00
|
|
|
i := strings.Index(field, "=")
|
|
|
|
if i < 1 {
|
|
|
|
return nil, errUnsupportedListLine
|
2015-08-15 13:28:07 +02:00
|
|
|
}
|
2011-05-07 01:29:10 +02:00
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
key := field[:i]
|
|
|
|
value := field[i+1:]
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "modify":
|
|
|
|
var err error
|
|
|
|
e.Time, err = time.Parse("20060102150405", value)
|
|
|
|
if err != nil {
|
2015-08-15 13:28:47 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-25 01:03:52 +02:00
|
|
|
case "type":
|
|
|
|
switch value {
|
|
|
|
case "dir", "cdir", "pdir":
|
|
|
|
e.Type = EntryTypeFolder
|
|
|
|
case "file":
|
|
|
|
e.Type = EntryTypeFile
|
|
|
|
}
|
|
|
|
case "size":
|
|
|
|
e.setSize(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
2015-08-15 13:28:47 +02:00
|
|
|
|
2016-03-10 08:56:31 +01:00
|
|
|
// parse file or folder name with multiple spaces
|
|
|
|
func parseLsListLineName(line string, fields []string, offset int) string {
|
|
|
|
if offset < 1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
match := fields[offset-1]
|
|
|
|
index := strings.Index(line, match)
|
|
|
|
if index == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
index += len(match)
|
|
|
|
return strings.TrimSpace(line[index:])
|
|
|
|
}
|
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
// parseLsListLine parses a directory line in a format based on the output of
|
|
|
|
// the UNIX ls command.
|
|
|
|
func parseLsListLine(line string) (*Entry, error) {
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) >= 7 && fields[1] == "folder" && fields[2] == "0" {
|
|
|
|
e := &Entry{
|
|
|
|
Type: EntryTypeFolder,
|
|
|
|
Name: strings.Join(fields[6:], " "),
|
|
|
|
}
|
|
|
|
if err := e.setTime(fields[3:6]); err != nil {
|
|
|
|
return nil, err
|
2015-08-15 13:28:47 +02:00
|
|
|
}
|
2011-05-07 01:29:10 +02:00
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
return e, nil
|
|
|
|
}
|
2015-08-15 13:28:47 +02:00
|
|
|
|
2015-12-28 12:17:09 +01:00
|
|
|
if len(fields) < 8 {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
if fields[1] == "0" {
|
|
|
|
e := &Entry{
|
|
|
|
Type: EntryTypeFile,
|
|
|
|
Name: strings.Join(fields[7:], " "),
|
|
|
|
}
|
2015-08-15 13:28:47 +02:00
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
if err := e.setSize(fields[2]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := e.setTime(fields[4:7]); err != nil {
|
|
|
|
return nil, err
|
2015-08-15 13:28:47 +02:00
|
|
|
}
|
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fields) < 9 {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
|
|
|
|
e := &Entry{}
|
|
|
|
switch fields[0][0] {
|
|
|
|
case '-':
|
|
|
|
e.Type = EntryTypeFile
|
|
|
|
if err := e.setSize(fields[4]); err != nil {
|
|
|
|
return nil, err
|
2015-08-15 13:28:07 +02:00
|
|
|
}
|
2015-08-25 01:03:52 +02:00
|
|
|
case 'd':
|
|
|
|
e.Type = EntryTypeFolder
|
|
|
|
case 'l':
|
|
|
|
e.Type = EntryTypeLink
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Unknown entry type")
|
|
|
|
}
|
2015-08-15 13:28:07 +02:00
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
if err := e.setTime(fields[5:8]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-10 08:56:31 +01:00
|
|
|
e.Name = parseLsListLineName(line, fields, 8)
|
|
|
|
if len(e.Name) == 0 {
|
|
|
|
e.Name = strings.Join(fields[8:], " ")
|
|
|
|
}
|
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var dirTimeFormats = []string{
|
|
|
|
"01-02-06 03:04PM",
|
|
|
|
"2006-01-02 15:04",
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseDirListLine parses a directory line in a format based on the output of
|
|
|
|
// the MS-DOS DIR command.
|
|
|
|
func parseDirListLine(line string) (*Entry, error) {
|
|
|
|
e := &Entry{}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Try various time formats that DIR might use, and stop when one works.
|
|
|
|
for _, format := range dirTimeFormats {
|
2015-12-28 00:09:00 +01:00
|
|
|
if len(line) > len(format) {
|
|
|
|
e.Time, err = time.Parse(format, line[:len(format)])
|
|
|
|
if err == nil {
|
|
|
|
line = line[len(format):]
|
|
|
|
break
|
|
|
|
}
|
2015-08-15 13:28:07 +02:00
|
|
|
}
|
2015-08-25 01:03:52 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// None of the time formats worked.
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
2011-05-07 01:29:10 +02:00
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
line = strings.TrimLeft(line, " ")
|
|
|
|
if strings.HasPrefix(line, "<DIR>") {
|
|
|
|
e.Type = EntryTypeFolder
|
|
|
|
line = strings.TrimPrefix(line, "<DIR>")
|
|
|
|
} else {
|
|
|
|
space := strings.Index(line, " ")
|
|
|
|
if space == -1 {
|
|
|
|
return nil, errUnsupportedListLine
|
2013-02-17 10:03:46 +01:00
|
|
|
}
|
2015-08-25 01:03:52 +02:00
|
|
|
e.Size, err = strconv.ParseUint(line[:space], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errUnsupportedListLine
|
|
|
|
}
|
|
|
|
e.Type = EntryTypeFile
|
|
|
|
line = line[space:]
|
|
|
|
}
|
2013-02-17 10:03:46 +01:00
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
e.Name = strings.TrimLeft(line, " ")
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var listLineParsers = []func(line string) (*Entry, error){
|
|
|
|
parseRFC3659ListLine,
|
|
|
|
parseLsListLine,
|
|
|
|
parseDirListLine,
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseListLine parses the various non-standard format returned by the LIST
|
|
|
|
// FTP command.
|
|
|
|
func parseListLine(line string) (*Entry, error) {
|
|
|
|
for _, f := range listLineParsers {
|
|
|
|
e, err := f(line)
|
|
|
|
if err == errUnsupportedListLine {
|
|
|
|
// Try another format.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return e, err
|
2013-02-17 10:03:46 +01:00
|
|
|
}
|
2015-08-25 01:03:52 +02:00
|
|
|
return nil, errUnsupportedListLine
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 18:54:22 +02:00
|
|
|
func (e *Entry) setSize(str string) (err error) {
|
2015-08-18 23:16:40 +02:00
|
|
|
e.Size, err = strconv.ParseUint(str, 0, 64)
|
2015-08-15 13:28:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-18 18:54:22 +02:00
|
|
|
func (e *Entry) setTime(fields []string) (err error) {
|
2013-07-08 07:21:43 +02:00
|
|
|
var timeStr string
|
2015-08-15 13:28:47 +02:00
|
|
|
if strings.Contains(fields[2], ":") { // this year
|
2013-07-08 07:21:43 +02:00
|
|
|
thisYear, _, _ := time.Now().Date()
|
2015-08-15 13:28:47 +02:00
|
|
|
timeStr = fields[1] + " " + fields[0] + " " + strconv.Itoa(thisYear)[2:4] + " " + fields[2] + " GMT"
|
2013-07-08 07:21:43 +02:00
|
|
|
} else { // not this year
|
2015-08-15 13:28:47 +02:00
|
|
|
if len(fields[2]) != 4 {
|
|
|
|
return errors.New("Invalid year format in time string")
|
|
|
|
}
|
2015-08-18 23:16:40 +02:00
|
|
|
timeStr = fields[1] + " " + fields[0] + " " + fields[2][2:4] + " 00:00 GMT"
|
2013-07-08 07:21:43 +02:00
|
|
|
}
|
2015-08-15 13:28:47 +02:00
|
|
|
e.Time, err = time.Parse("_2 Jan 06 15:04 MST", timeStr)
|
|
|
|
return
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2013-12-04 01:09:55 +01:00
|
|
|
// NameList issues an NLST FTP command.
|
|
|
|
func (c *ServerConn) NameList(path string) (entries []string, err error) {
|
2013-12-04 23:30:50 +01:00
|
|
|
conn, err := c.cmdDataConnFrom(0, "NLST %s", path)
|
2013-12-04 01:09:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &response{conn, c}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
entries = append(entries, scanner.Text())
|
|
|
|
}
|
|
|
|
if err = scanner.Err(); err != nil {
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// List issues a LIST FTP command.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) List(path string) (entries []*Entry, err error) {
|
2013-12-04 23:30:50 +01:00
|
|
|
conn, err := c.cmdDataConnFrom(0, "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()
|
|
|
|
|
2015-08-25 01:03:52 +02:00
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2011-05-07 01:29:10 +02:00
|
|
|
entry, err := parseListLine(line)
|
|
|
|
if err == nil {
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
}
|
2015-08-25 01:03:52 +02:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-05-07 01:29:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// ChangeDir issues a CWD FTP command, which changes the current directory to
|
|
|
|
// the specified path.
|
2011-12-27 22:50:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// ChangeDirToParent issues a CDUP FTP command, which changes the current
|
|
|
|
// directory to the parent directory. This is similar to a call to ChangeDir
|
|
|
|
// with a path set to "..".
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) ChangeDirToParent() error {
|
2011-09-07 16:52:52 +02:00
|
|
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// CurrentDir issues a PWD FTP command, which Returns the path of the current
|
|
|
|
// directory.
|
2011-12-27 22:50:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Retr issues a RETR FTP command to fetch the specified file from the remote
|
|
|
|
// FTP server.
|
|
|
|
//
|
|
|
|
// The returned ReadCloser must be closed to cleanup the FTP data connection.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) Retr(path string) (io.ReadCloser, error) {
|
2013-12-04 23:30:50 +01:00
|
|
|
return c.RetrFrom(path, 0)
|
|
|
|
}
|
|
|
|
|
2015-11-30 12:22:16 +01:00
|
|
|
// RetrFrom issues a RETR FTP command to fetch the specified file from the remote
|
2013-12-04 23:30:50 +01:00
|
|
|
// FTP server, the server will not send the offset first bytes of the file.
|
|
|
|
//
|
|
|
|
// The returned ReadCloser must be closed to cleanup the FTP data connection.
|
|
|
|
func (c *ServerConn) RetrFrom(path string, offset uint64) (io.ReadCloser, error) {
|
|
|
|
conn, err := c.cmdDataConnFrom(offset, "RETR %s", path)
|
2011-09-05 23:36:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-18 23:16:40 +02:00
|
|
|
return &response{conn, c}, nil
|
2011-09-05 23:36:14 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Stor issues a STOR FTP command to store a file to the remote FTP server.
|
|
|
|
// Stor creates the specified file with the content of the io.Reader.
|
|
|
|
//
|
|
|
|
// Hint: io.Pipe() can be used if an io.Writer is required.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) Stor(path string, r io.Reader) error {
|
2013-12-04 23:30:50 +01:00
|
|
|
return c.StorFrom(path, r, 0)
|
|
|
|
}
|
|
|
|
|
2015-11-30 12:22:16 +01:00
|
|
|
// StorFrom issues a STOR FTP command to store a file to the remote FTP server.
|
2013-12-04 23:30:50 +01:00
|
|
|
// Stor creates the specified file with the content of the io.Reader, writing
|
|
|
|
// on the server will start at the given file offset.
|
|
|
|
//
|
|
|
|
// Hint: io.Pipe() can be used if an io.Writer is required.
|
|
|
|
func (c *ServerConn) StorFrom(path string, r io.Reader, offset uint64) error {
|
|
|
|
conn, err := c.cmdDataConnFrom(offset, "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
|
|
|
|
}
|
|
|
|
|
2014-03-25 09:35:34 +01:00
|
|
|
_, _, err = c.conn.ReadResponse(StatusClosingDataConnection)
|
2011-09-05 23:36:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Rename renames a file on the remote FTP server.
|
2011-12-27 22:50:50 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Delete issues a DELE FTP command to delete the specified file from the
|
|
|
|
// remote FTP server.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (c *ServerConn) Delete(path string) error {
|
|
|
|
_, _, err := c.cmd(StatusRequestedFileActionOK, "DELE %s", path)
|
2011-09-06 00:02:01 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// MakeDir issues a MKD FTP command to create the specified directory on the
|
|
|
|
// remote FTP server.
|
2011-12-27 22:50:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// RemoveDir issues a RMD FTP command to remove the specified directory from
|
|
|
|
// the remote FTP server.
|
2011-12-27 22:50:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// NoOp issues a NOOP FTP command.
|
|
|
|
// NOOP has no effects and is usually used to prevent the remote FTP server to
|
|
|
|
// close the otherwise idle connection.
|
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
|
|
|
}
|
|
|
|
|
2013-05-19 21:15:23 +02:00
|
|
|
// Logout issues a REIN FTP command to logout the current user.
|
|
|
|
func (c *ServerConn) Logout() error {
|
2013-10-18 18:30:28 +02:00
|
|
|
_, _, err := c.cmd(StatusReady, "REIN")
|
2013-05-19 21:15:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Quit issues a QUIT FTP command to properly close the connection from the
|
|
|
|
// remote FTP server.
|
2011-12-27 22:50:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Read implements the io.Reader interface on a FTP data connection.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (r *response) Read(buf []byte) (int, error) {
|
2015-08-18 23:16:40 +02:00
|
|
|
return r.conn.Read(buf)
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 13:09:37 +02:00
|
|
|
// Close implements the io.Closer interface on a FTP data connection.
|
2011-12-27 22:50:50 +01:00
|
|
|
func (r *response) Close() error {
|
2013-07-10 12:18:32 +02:00
|
|
|
err := r.conn.Close()
|
2014-02-18 16:51:07 +01:00
|
|
|
_, _, err2 := r.c.conn.ReadResponse(StatusClosingDataConnection)
|
2013-07-10 12:18:32 +02:00
|
|
|
if err2 != nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
return err
|
2011-05-07 01:29:10 +02:00
|
|
|
}
|