2017-05-03 12:51:03 +00:00
|
|
|
// Package ftp interfaces with FTP servers
|
2016-12-27 20:52:30 +00:00
|
|
|
package ftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-05-03 20:45:01 +00:00
|
|
|
"net/textproto"
|
2017-05-03 15:07:40 +00:00
|
|
|
"net/url"
|
2017-05-24 22:24:46 +00:00
|
|
|
"os"
|
2017-05-03 15:42:04 +00:00
|
|
|
"path"
|
2017-05-25 09:16:51 +00:00
|
|
|
"strings"
|
2017-05-03 12:18:17 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jlaffaye/ftp"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2017-05-03 12:51:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-12-27 20:52:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
|
|
|
fs.Register(&fs.RegInfo{
|
2017-05-03 15:07:40 +00:00
|
|
|
Name: "ftp",
|
2017-05-24 22:24:46 +00:00
|
|
|
Description: "FTP Connection",
|
2017-05-03 12:18:17 +00:00
|
|
|
NewFs: NewFs,
|
2016-12-27 20:52:30 +00:00
|
|
|
Options: []fs.Option{
|
|
|
|
{
|
2017-05-24 22:24:46 +00:00
|
|
|
Name: "host",
|
|
|
|
Help: "FTP host to connect to",
|
|
|
|
Optional: false,
|
|
|
|
Examples: []fs.OptionExample{{
|
|
|
|
Value: "ftp.example.com",
|
|
|
|
Help: "Connect to ftp.example.com",
|
|
|
|
}},
|
2016-12-27 20:52:30 +00:00
|
|
|
}, {
|
2017-05-24 22:24:46 +00:00
|
|
|
Name: "user",
|
|
|
|
Help: "FTP username, leave blank for current username, " + os.Getenv("USER"),
|
|
|
|
Optional: true,
|
|
|
|
}, {
|
|
|
|
Name: "port",
|
|
|
|
Help: "FTP port, leave blank to use default (21) ",
|
|
|
|
Optional: true,
|
2016-12-27 20:52:30 +00:00
|
|
|
}, {
|
2017-05-24 22:24:46 +00:00
|
|
|
Name: "pass",
|
|
|
|
Help: "FTP password",
|
|
|
|
IsPassword: true,
|
|
|
|
Optional: false,
|
2016-12-27 20:52:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Fs represents a remote FTP server
|
2016-12-27 20:52:30 +00:00
|
|
|
type Fs struct {
|
2017-05-18 14:51:35 +00:00
|
|
|
name string // name of this remote
|
|
|
|
root string // the path we are working on if any
|
|
|
|
features *fs.Features // optional features
|
2017-05-25 09:16:51 +00:00
|
|
|
url string
|
2017-05-03 20:45:01 +00:00
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
dialAddr string
|
2017-05-18 14:51:35 +00:00
|
|
|
poolMu sync.Mutex
|
|
|
|
pool []*ftp.ServerConn
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Object describes an FTP file
|
2016-12-27 20:52:30 +00:00
|
|
|
type Object struct {
|
2017-05-03 12:18:17 +00:00
|
|
|
fs *Fs
|
|
|
|
remote string
|
|
|
|
info *FileInfo
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// FileInfo is the metadata known about an FTP file
|
2016-12-27 20:52:30 +00:00
|
|
|
type FileInfo struct {
|
|
|
|
Name string
|
|
|
|
Size uint64
|
|
|
|
ModTime time.Time
|
|
|
|
IsDir bool
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// ------------------------------------------------------------
|
2016-12-27 20:52:30 +00:00
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Name of this fs
|
2016-12-27 20:52:30 +00:00
|
|
|
func (f *Fs) Name() string {
|
2017-05-03 12:18:17 +00:00
|
|
|
return f.name
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Root of the remote (as passed into NewFs)
|
|
|
|
func (f *Fs) Root() string {
|
|
|
|
return f.root
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// String returns a description of the FS
|
2016-12-27 20:52:30 +00:00
|
|
|
func (f *Fs) String() string {
|
2017-05-25 09:16:51 +00:00
|
|
|
return f.url
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:18:17 +00:00
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
return f.features
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Open a new connection to the FTP server.
|
2017-05-03 20:45:01 +00:00
|
|
|
func (f *Fs) ftpConnection() (*ftp.ServerConn, error) {
|
|
|
|
fs.Debugf(f, "Connecting to FTP server")
|
2017-05-18 15:20:26 +00:00
|
|
|
c, err := ftp.DialTimeout(f.dialAddr, fs.Config.ConnectTimeout)
|
2017-05-03 20:45:01 +00:00
|
|
|
if err != nil {
|
2017-05-18 14:51:35 +00:00
|
|
|
fs.Errorf(f, "Error while Dialing %s: %s", f.dialAddr, err)
|
|
|
|
return nil, errors.Wrap(err, "ftpConnection Dial")
|
2017-05-03 20:45:01 +00:00
|
|
|
}
|
|
|
|
err = c.Login(f.user, f.pass)
|
|
|
|
if err != nil {
|
2017-05-18 14:51:35 +00:00
|
|
|
_ = c.Quit()
|
|
|
|
fs.Errorf(f, "Error while Logging in into %s: %s", f.dialAddr, err)
|
|
|
|
return nil, errors.Wrap(err, "ftpConnection Login")
|
2017-05-03 20:45:01 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:51:35 +00:00
|
|
|
// Get an FTP connection from the pool, or open a new one
|
|
|
|
func (f *Fs) getFtpConnection() (c *ftp.ServerConn, err error) {
|
|
|
|
f.poolMu.Lock()
|
|
|
|
if len(f.pool) > 0 {
|
|
|
|
c = f.pool[0]
|
|
|
|
f.pool = f.pool[1:]
|
|
|
|
}
|
|
|
|
f.poolMu.Unlock()
|
|
|
|
if c != nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
return f.ftpConnection()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FTP connection to the pool
|
|
|
|
//
|
|
|
|
// It nils the pointed to connection out so it can't be reused
|
2017-05-24 13:47:13 +00:00
|
|
|
//
|
|
|
|
// if err is not nil then it checks the connection is alive using a
|
|
|
|
// NOOP request
|
|
|
|
func (f *Fs) putFtpConnection(pc **ftp.ServerConn, err error) {
|
|
|
|
c := *pc
|
|
|
|
*pc = nil
|
|
|
|
if err != nil {
|
|
|
|
// If not a regular FTP error code then check the connection
|
|
|
|
_, isRegularError := errors.Cause(err).(*textproto.Error)
|
|
|
|
if !isRegularError {
|
|
|
|
nopErr := c.NoOp()
|
|
|
|
if nopErr != nil {
|
|
|
|
fs.Debugf(f, "Connection failed, closing: %v", nopErr)
|
|
|
|
_ = c.Quit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 14:51:35 +00:00
|
|
|
f.poolMu.Lock()
|
2017-05-24 13:47:13 +00:00
|
|
|
f.pool = append(f.pool, c)
|
2017-05-18 14:51:35 +00:00
|
|
|
f.poolMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-05-03 20:45:01 +00:00
|
|
|
// NewFs contstructs an Fs from the path, container:path
|
|
|
|
func NewFs(name, root string) (ff fs.Fs, err error) {
|
|
|
|
// defer fs.Trace(nil, "name=%q, root=%q", name, root)("fs=%v, err=%v", &ff, &err)
|
2017-05-25 09:16:51 +00:00
|
|
|
// FIXME Convert the old scheme used for the first beta - remove after release
|
|
|
|
if ftpURL := fs.ConfigFileGet(name, "url"); ftpURL != "" {
|
|
|
|
fs.Infof(name, "Converting old configuration")
|
|
|
|
u, err := url.Parse(ftpURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "Failed to parse old url %q", ftpURL)
|
|
|
|
}
|
|
|
|
parts := strings.Split(u.Host, ":")
|
|
|
|
fs.ConfigFileSet(name, "host", parts[0])
|
|
|
|
if len(parts) > 1 {
|
|
|
|
fs.ConfigFileSet(name, "port", parts[1])
|
|
|
|
}
|
|
|
|
fs.ConfigFileSet(name, "host", u.Host)
|
|
|
|
fs.ConfigFileSet(name, "user", fs.ConfigFileGet(name, "username"))
|
|
|
|
fs.ConfigFileSet(name, "pass", fs.ConfigFileGet(name, "password"))
|
|
|
|
fs.ConfigFileDeleteKey(name, "username")
|
|
|
|
fs.ConfigFileDeleteKey(name, "password")
|
|
|
|
fs.ConfigFileDeleteKey(name, "url")
|
|
|
|
fs.SaveConfig()
|
|
|
|
if u.Path != "" && u.Path != "/" {
|
|
|
|
fs.Errorf(name, "Path %q in FTP URL no longer supported - put it on the end of the remote %s:%s", u.Path, name, u.Path)
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 22:24:46 +00:00
|
|
|
host := fs.ConfigFileGet(name, "host")
|
|
|
|
user := fs.ConfigFileGet(name, "user")
|
|
|
|
pass := fs.ConfigFileGet(name, "pass")
|
|
|
|
port := fs.ConfigFileGet(name, "port")
|
2017-05-03 20:45:01 +00:00
|
|
|
pass, err = fs.Reveal(pass)
|
2017-05-03 15:07:40 +00:00
|
|
|
if err != nil {
|
2017-05-03 20:45:01 +00:00
|
|
|
return nil, errors.Wrap(err, "NewFS decrypt password")
|
2017-05-03 15:07:40 +00:00
|
|
|
}
|
2017-05-24 22:24:46 +00:00
|
|
|
if user == "" {
|
|
|
|
user = os.Getenv("USER")
|
2017-05-03 15:07:40 +00:00
|
|
|
}
|
2017-05-24 22:24:46 +00:00
|
|
|
if port == "" {
|
|
|
|
port = "21"
|
2017-05-03 20:45:01 +00:00
|
|
|
}
|
2017-05-24 22:24:46 +00:00
|
|
|
|
|
|
|
dialAddr := host + ":" + port
|
2017-05-25 09:16:51 +00:00
|
|
|
u := "ftp://" + path.Join(dialAddr+"/", root)
|
2017-05-03 20:45:01 +00:00
|
|
|
f := &Fs{
|
|
|
|
name: name,
|
|
|
|
root: root,
|
|
|
|
url: u,
|
|
|
|
user: user,
|
|
|
|
pass: pass,
|
|
|
|
dialAddr: dialAddr,
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
2017-05-03 20:45:01 +00:00
|
|
|
f.features = (&fs.Features{}).Fill(f)
|
2017-05-18 14:51:35 +00:00
|
|
|
// Make a connection and pool it to return errors early
|
|
|
|
c, err := f.getFtpConnection()
|
2017-05-03 12:51:03 +00:00
|
|
|
if err != nil {
|
2017-05-18 14:51:35 +00:00
|
|
|
return nil, errors.Wrap(err, "NewFs")
|
2017-05-03 12:51:03 +00:00
|
|
|
}
|
2017-05-24 13:47:13 +00:00
|
|
|
f.putFtpConnection(&c, nil)
|
2017-05-03 20:45:01 +00:00
|
|
|
if root != "" {
|
|
|
|
// Check to see if the root actually an existing file
|
|
|
|
remote := path.Base(root)
|
|
|
|
f.root = path.Dir(root)
|
|
|
|
if f.root == "." {
|
|
|
|
f.root = ""
|
|
|
|
}
|
|
|
|
_, err := f.NewObject(remote)
|
|
|
|
if err != nil {
|
|
|
|
if err == fs.ErrorObjectNotFound || errors.Cause(err) == fs.ErrorNotAFile {
|
|
|
|
// File doesn't exist so return old f
|
|
|
|
f.root = root
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// return an error with an fs which points to the parent
|
|
|
|
return f, fs.ErrorIsFile
|
|
|
|
}
|
|
|
|
return f, err
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 20:45:01 +00:00
|
|
|
// translateErrorFile turns FTP errors into rclone errors if possible for a file
|
|
|
|
func translateErrorFile(err error) error {
|
|
|
|
switch errX := err.(type) {
|
|
|
|
case *textproto.Error:
|
|
|
|
switch errX.Code {
|
|
|
|
case ftp.StatusFileUnavailable:
|
|
|
|
err = fs.ErrorObjectNotFound
|
|
|
|
}
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
2017-05-03 20:45:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// translateErrorDir turns FTP errors into rclone errors if possible for a directory
|
|
|
|
func translateErrorDir(err error) error {
|
|
|
|
switch errX := err.(type) {
|
|
|
|
case *textproto.Error:
|
|
|
|
switch errX.Code {
|
|
|
|
case ftp.StatusFileUnavailable:
|
|
|
|
err = fs.ErrorDirNotFound
|
|
|
|
}
|
2017-05-03 12:51:03 +00:00
|
|
|
}
|
2017-05-03 20:45:01 +00:00
|
|
|
return err
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// NewObject finds the Object at remote. If it can't be found
|
|
|
|
// it returns the error fs.ErrorObjectNotFound.
|
2017-05-03 20:45:01 +00:00
|
|
|
func (f *Fs) NewObject(remote string) (o fs.Object, err error) {
|
|
|
|
// defer fs.Trace(remote, "")("o=%v, err=%v", &o, &err)
|
|
|
|
fullPath := path.Join(f.root, remote)
|
|
|
|
dir := path.Dir(fullPath)
|
|
|
|
base := path.Base(fullPath)
|
2016-12-27 20:52:30 +00:00
|
|
|
|
2017-05-18 14:51:35 +00:00
|
|
|
c, err := f.getFtpConnection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "NewObject")
|
|
|
|
}
|
|
|
|
files, err := c.List(dir)
|
2017-05-24 13:47:13 +00:00
|
|
|
f.putFtpConnection(&c, err)
|
2017-05-03 15:42:04 +00:00
|
|
|
if err != nil {
|
2017-05-03 20:45:01 +00:00
|
|
|
return nil, translateErrorFile(err)
|
2017-05-03 15:42:04 +00:00
|
|
|
}
|
2017-05-03 20:45:01 +00:00
|
|
|
for i, file := range files {
|
|
|
|
if file.Type != ftp.EntryTypeFolder && file.Name == base {
|
2016-12-27 20:52:30 +00:00
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
info := &FileInfo{
|
2017-05-03 12:18:17 +00:00
|
|
|
Name: remote,
|
|
|
|
Size: files[i].Size,
|
2016-12-27 20:52:30 +00:00
|
|
|
ModTime: files[i].Time,
|
|
|
|
}
|
|
|
|
o.info = info
|
2017-05-03 12:18:17 +00:00
|
|
|
|
2016-12-27 20:52:30 +00:00
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:18:17 +00:00
|
|
|
func (f *Fs) list(out fs.ListOpts, dir string, curlevel int) {
|
2017-05-03 20:45:01 +00:00
|
|
|
// defer fs.Trace(dir, "curlevel=%d", curlevel)("")
|
2017-05-18 14:51:35 +00:00
|
|
|
c, err := f.getFtpConnection()
|
|
|
|
if err != nil {
|
|
|
|
out.SetError(errors.Wrap(err, "list"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
files, err := c.List(path.Join(f.root, dir))
|
2017-05-24 13:47:13 +00:00
|
|
|
f.putFtpConnection(&c, err)
|
2017-05-03 15:42:04 +00:00
|
|
|
if err != nil {
|
2017-05-03 20:45:01 +00:00
|
|
|
out.SetError(translateErrorDir(err))
|
2017-05-03 15:42:04 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-03 12:18:17 +00:00
|
|
|
for i := range files {
|
2016-12-27 20:52:30 +00:00
|
|
|
object := files[i]
|
2017-05-03 15:42:04 +00:00
|
|
|
newremote := path.Join(dir, object.Name)
|
2016-12-27 20:52:30 +00:00
|
|
|
switch object.Type {
|
|
|
|
case ftp.EntryTypeFolder:
|
2017-05-03 20:45:01 +00:00
|
|
|
if object.Name == "." || object.Name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
2017-05-03 12:18:17 +00:00
|
|
|
if out.IncludeDirectory(newremote) {
|
2016-12-27 20:52:30 +00:00
|
|
|
d := &fs.Dir{
|
|
|
|
Name: newremote,
|
|
|
|
When: object.Time,
|
|
|
|
Bytes: 0,
|
|
|
|
Count: -1,
|
|
|
|
}
|
2017-05-03 12:18:17 +00:00
|
|
|
if curlevel < out.Level() {
|
2017-05-03 15:42:04 +00:00
|
|
|
f.list(out, path.Join(dir, object.Name), curlevel+1)
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
if out.AddDir(d) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: newremote,
|
|
|
|
}
|
|
|
|
info := &FileInfo{
|
2017-05-03 12:18:17 +00:00
|
|
|
Name: newremote,
|
|
|
|
Size: object.Size,
|
2016-12-27 20:52:30 +00:00
|
|
|
ModTime: object.Time,
|
|
|
|
}
|
|
|
|
o.info = info
|
|
|
|
if out.Add(o) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// List the objects and directories of the Fs starting from dir
|
|
|
|
//
|
|
|
|
// dir should be "" to start from the root, and should not
|
|
|
|
// have trailing slashes.
|
|
|
|
//
|
|
|
|
// This should return ErrDirNotFound (using out.SetError())
|
|
|
|
// if the directory isn't found.
|
|
|
|
//
|
|
|
|
// Fses must support recursion levels of fs.MaxLevel and 1.
|
|
|
|
// They may return ErrorLevelNotSupported otherwise.
|
2016-12-27 20:52:30 +00:00
|
|
|
func (f *Fs) List(out fs.ListOpts, dir string) {
|
2017-05-03 20:45:01 +00:00
|
|
|
// defer fs.Trace(dir, "")("")
|
2016-12-27 20:52:30 +00:00
|
|
|
f.list(out, dir, 1)
|
|
|
|
out.Finished()
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Hashes are not supported
|
|
|
|
func (f *Fs) Hashes() fs.HashSet {
|
|
|
|
return 0
|
|
|
|
}
|
2016-12-27 20:52:30 +00:00
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Precision shows Modified Time not supported
|
|
|
|
func (f *Fs) Precision() time.Duration {
|
|
|
|
return fs.ModTimeNotSupported
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Put in to the remote path with the modTime given of the given size
|
|
|
|
//
|
|
|
|
// May create the object even if it returns an error - if so
|
|
|
|
// will return the object and the error, otherwise will return
|
|
|
|
// nil and the error
|
2017-05-28 11:44:22 +00:00
|
|
|
func (f *Fs) Put(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
2017-05-03 20:45:01 +00:00
|
|
|
// fs.Debugf(f, "Trying to put file %s", src.Remote())
|
2017-05-18 16:23:02 +00:00
|
|
|
err := f.mkParentDir(src.Remote())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Put mkParentDir failed")
|
|
|
|
}
|
2017-05-03 12:51:03 +00:00
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: src.Remote(),
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
2017-05-28 11:44:22 +00:00
|
|
|
err = o.Update(in, src, options...)
|
2017-05-03 12:51:03 +00:00
|
|
|
return o, err
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// getInfo reads the FileInfo for a path
|
2017-05-03 20:45:01 +00:00
|
|
|
func (f *Fs) getInfo(remote string) (fi *FileInfo, err error) {
|
|
|
|
// defer fs.Trace(remote, "")("fi=%v, err=%v", &fi, &err)
|
2017-05-03 15:42:04 +00:00
|
|
|
dir := path.Dir(remote)
|
|
|
|
base := path.Base(remote)
|
2017-05-03 12:51:03 +00:00
|
|
|
|
2017-05-18 14:51:35 +00:00
|
|
|
c, err := f.getFtpConnection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "getInfo")
|
|
|
|
}
|
|
|
|
files, err := c.List(dir)
|
2017-05-24 13:47:13 +00:00
|
|
|
f.putFtpConnection(&c, err)
|
2017-05-03 15:42:04 +00:00
|
|
|
if err != nil {
|
2017-05-03 20:45:01 +00:00
|
|
|
return nil, translateErrorFile(err)
|
2017-05-03 15:42:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
for i := range files {
|
|
|
|
if files[i].Name == base {
|
|
|
|
info := &FileInfo{
|
|
|
|
Name: remote,
|
|
|
|
Size: files[i].Size,
|
|
|
|
ModTime: files[i].Time,
|
|
|
|
IsDir: files[i].Type == ftp.EntryTypeFolder,
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 16:23:02 +00:00
|
|
|
// mkdir makes the directory and parents using unrooted paths
|
2017-05-03 12:51:03 +00:00
|
|
|
func (f *Fs) mkdir(abspath string) error {
|
2017-05-18 16:23:02 +00:00
|
|
|
if abspath == "." || abspath == "/" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fi, err := f.getInfo(abspath)
|
|
|
|
if err == nil {
|
|
|
|
if fi.IsDir {
|
|
|
|
return nil
|
2017-05-18 14:51:35 +00:00
|
|
|
}
|
2017-05-18 16:23:02 +00:00
|
|
|
return fs.ErrorIsFile
|
|
|
|
} else if err != fs.ErrorObjectNotFound {
|
|
|
|
return errors.Wrapf(err, "mkdir %q failed", abspath)
|
|
|
|
}
|
|
|
|
parent := path.Dir(abspath)
|
|
|
|
err = f.mkdir(parent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c, connErr := f.getFtpConnection()
|
|
|
|
if connErr != nil {
|
|
|
|
return errors.Wrap(connErr, "mkdir")
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
2017-05-18 16:23:02 +00:00
|
|
|
err = c.MakeDir(abspath)
|
2017-05-24 13:47:13 +00:00
|
|
|
f.putFtpConnection(&c, err)
|
2016-12-27 20:52:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-18 16:23:02 +00:00
|
|
|
// mkParentDir makes the parent of remote if necessary and any
|
|
|
|
// directories above that
|
|
|
|
func (f *Fs) mkParentDir(remote string) error {
|
|
|
|
parent := path.Dir(remote)
|
|
|
|
return f.mkdir(path.Join(f.root, parent))
|
|
|
|
}
|
|
|
|
|
2017-05-03 15:42:04 +00:00
|
|
|
// Mkdir creates the directory if it doesn't exist
|
2017-05-03 20:45:01 +00:00
|
|
|
func (f *Fs) Mkdir(dir string) (err error) {
|
|
|
|
// defer fs.Trace(dir, "")("err=%v", &err)
|
2017-05-18 16:23:02 +00:00
|
|
|
root := path.Join(f.root, dir)
|
|
|
|
return f.mkdir(root)
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Rmdir removes the directory (container, bucket) if empty
|
|
|
|
//
|
|
|
|
// Return an error if it doesn't exist or isn't empty
|
|
|
|
func (f *Fs) Rmdir(dir string) error {
|
2017-05-18 14:51:35 +00:00
|
|
|
c, err := f.getFtpConnection()
|
|
|
|
if err != nil {
|
2017-05-18 14:52:28 +00:00
|
|
|
return errors.Wrap(translateErrorFile(err), "Rmdir")
|
2017-05-18 14:51:35 +00:00
|
|
|
}
|
|
|
|
err = c.RemoveDir(path.Join(f.root, dir))
|
2017-05-24 13:47:13 +00:00
|
|
|
f.putFtpConnection(&c, err)
|
2017-05-18 14:51:35 +00:00
|
|
|
return translateErrorDir(err)
|
2017-05-03 12:51:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 16:32:45 +00:00
|
|
|
// Move renames a remote file object
|
|
|
|
func (f *Fs) Move(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*Object)
|
|
|
|
if !ok {
|
|
|
|
fs.Debugf(src, "Can't move - not same remote type")
|
|
|
|
return nil, fs.ErrorCantMove
|
|
|
|
}
|
|
|
|
err := f.mkParentDir(remote)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Move mkParentDir failed")
|
|
|
|
}
|
|
|
|
c, err := f.getFtpConnection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Move")
|
|
|
|
}
|
|
|
|
err = c.Rename(
|
|
|
|
path.Join(srcObj.fs.root, srcObj.remote),
|
|
|
|
path.Join(f.root, remote),
|
|
|
|
)
|
2017-05-24 13:47:13 +00:00
|
|
|
f.putFtpConnection(&c, err)
|
2017-05-18 16:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Move Rename failed")
|
|
|
|
}
|
|
|
|
dstObj, err := f.NewObject(remote)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Move NewObject failed")
|
|
|
|
}
|
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DirMove moves src, srcRemote to this remote at dstRemote
|
|
|
|
// using server side move operations.
|
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantDirMove
|
|
|
|
//
|
|
|
|
// If destination exists then return fs.ErrorDirExists
|
|
|
|
func (f *Fs) DirMove(src fs.Fs, srcRemote, dstRemote string) error {
|
|
|
|
srcFs, ok := src.(*Fs)
|
|
|
|
if !ok {
|
|
|
|
fs.Debugf(srcFs, "Can't move directory - not same remote type")
|
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
}
|
|
|
|
srcPath := path.Join(srcFs.root, srcRemote)
|
|
|
|
dstPath := path.Join(f.root, dstRemote)
|
|
|
|
|
|
|
|
// Check if destination exists
|
|
|
|
fi, err := f.getInfo(dstPath)
|
|
|
|
if err == nil {
|
|
|
|
if fi.IsDir {
|
|
|
|
return fs.ErrorDirExists
|
|
|
|
}
|
|
|
|
return fs.ErrorIsFile
|
|
|
|
} else if err != fs.ErrorObjectNotFound {
|
|
|
|
return errors.Wrapf(err, "DirMove getInfo failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the parent directory exists
|
|
|
|
err = f.mkdir(path.Dir(dstPath))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "DirMove mkParentDir dst failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the move
|
|
|
|
c, err := f.getFtpConnection()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "DirMove")
|
|
|
|
}
|
|
|
|
err = c.Rename(
|
|
|
|
srcPath,
|
|
|
|
dstPath,
|
|
|
|
)
|
2017-05-24 13:47:13 +00:00
|
|
|
f.putFtpConnection(&c, err)
|
2017-05-18 16:32:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "DirMove Rename(%q,%q) failed", srcPath, dstPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Fs returns the parent Fs
|
2016-12-27 20:52:30 +00:00
|
|
|
func (o *Object) Fs() fs.Info {
|
|
|
|
return o.fs
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// String version of o
|
|
|
|
func (o *Object) String() string {
|
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Remote returns the remote path
|
|
|
|
func (o *Object) Remote() string {
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash returns the hash of an object returning a lowercase hex string
|
|
|
|
func (o *Object) Hash(t fs.HashType) (string, error) {
|
|
|
|
return "", fs.ErrHashUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
2016-12-27 20:52:30 +00:00
|
|
|
func (o *Object) Size() int64 {
|
|
|
|
return int64(o.info.Size)
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// ModTime returns the modification time of the object
|
|
|
|
func (o *Object) ModTime() time.Time {
|
|
|
|
return o.info.ModTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetModTime sets the modification time of the object
|
|
|
|
func (o *Object) SetModTime(modTime time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storable returns a boolean as to whether this object is storable
|
2016-12-27 20:52:30 +00:00
|
|
|
func (o *Object) Storable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// ftpReadCloser implements io.ReadCloser for FTP objects.
|
|
|
|
type ftpReadCloser struct {
|
2017-05-19 16:45:22 +00:00
|
|
|
rc io.ReadCloser
|
|
|
|
c *ftp.ServerConn
|
|
|
|
f *Fs
|
|
|
|
err error // errors found during read
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes into p
|
|
|
|
func (f *ftpReadCloser) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = f.rc.Read(p)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
f.err = err // store any errors for Close to examine
|
|
|
|
}
|
|
|
|
return
|
2017-05-03 12:51:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:51:35 +00:00
|
|
|
// Close the FTP reader and return the connection to the pool
|
2017-05-03 12:51:03 +00:00
|
|
|
func (f *ftpReadCloser) Close() error {
|
2017-05-19 16:45:22 +00:00
|
|
|
err := f.rc.Close()
|
|
|
|
// if errors while reading or closing, dump the connection
|
|
|
|
if err != nil || f.err != nil {
|
|
|
|
_ = f.c.Quit()
|
|
|
|
} else {
|
2017-05-24 13:47:13 +00:00
|
|
|
f.f.putFtpConnection(&f.c, nil)
|
2017-05-19 16:45:22 +00:00
|
|
|
}
|
|
|
|
// mask the error if it was caused by a premature close
|
2017-05-19 11:21:24 +00:00
|
|
|
switch errX := err.(type) {
|
|
|
|
case *textproto.Error:
|
|
|
|
switch errX.Code {
|
|
|
|
case ftp.StatusTransfertAborted, ftp.StatusFileUnavailable:
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 12:51:03 +00:00
|
|
|
return err
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Open an object for read
|
2017-05-03 20:45:01 +00:00
|
|
|
func (o *Object) Open(options ...fs.OpenOption) (rc io.ReadCloser, err error) {
|
|
|
|
// defer fs.Trace(o, "")("rc=%v, err=%v", &rc, &err)
|
2017-05-03 15:42:04 +00:00
|
|
|
path := path.Join(o.fs.root, o.remote)
|
2017-05-03 20:45:01 +00:00
|
|
|
var offset int64
|
|
|
|
for _, option := range options {
|
|
|
|
switch x := option.(type) {
|
|
|
|
case *fs.SeekOption:
|
|
|
|
offset = x.Offset
|
|
|
|
default:
|
|
|
|
if option.Mandatory() {
|
|
|
|
fs.Logf(o, "Unsupported mandatory option: %v", option)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 14:51:35 +00:00
|
|
|
c, err := o.fs.getFtpConnection()
|
2017-05-03 12:51:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "open")
|
|
|
|
}
|
2017-05-18 14:51:35 +00:00
|
|
|
fd, err := c.RetrFrom(path, uint64(offset))
|
2017-05-03 12:51:03 +00:00
|
|
|
if err != nil {
|
2017-05-24 13:47:13 +00:00
|
|
|
o.fs.putFtpConnection(&c, err)
|
2017-05-03 12:51:03 +00:00
|
|
|
return nil, errors.Wrap(err, "open")
|
|
|
|
}
|
2017-05-19 16:45:22 +00:00
|
|
|
rc = &ftpReadCloser{rc: fd, c: c, f: o.fs}
|
2017-05-03 20:45:01 +00:00
|
|
|
return rc, nil
|
2017-05-03 12:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the already existing object
|
|
|
|
//
|
|
|
|
// Copy the reader into the object updating modTime and size
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
2017-05-28 11:44:22 +00:00
|
|
|
func (o *Object) Update(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (err error) {
|
2017-05-18 14:51:35 +00:00
|
|
|
// defer fs.Trace(o, "src=%v", src)("err=%v", &err)
|
2017-05-03 15:42:04 +00:00
|
|
|
path := path.Join(o.fs.root, o.remote)
|
2017-05-03 20:45:01 +00:00
|
|
|
// remove the file if upload failed
|
|
|
|
remove := func() {
|
|
|
|
removeErr := o.Remove()
|
|
|
|
if removeErr != nil {
|
|
|
|
fs.Debugf(o, "Failed to remove: %v", removeErr)
|
|
|
|
} else {
|
|
|
|
fs.Debugf(o, "Removed after failed upload: %v", err)
|
|
|
|
}
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
2017-05-18 14:51:35 +00:00
|
|
|
c, err := o.fs.getFtpConnection()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Update")
|
|
|
|
}
|
2017-05-03 12:51:03 +00:00
|
|
|
err = c.Stor(path, in)
|
2016-12-27 20:52:30 +00:00
|
|
|
if err != nil {
|
2017-05-18 14:51:35 +00:00
|
|
|
_ = c.Quit()
|
2017-05-03 20:45:01 +00:00
|
|
|
remove()
|
|
|
|
return errors.Wrap(err, "update stor")
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
2017-05-24 13:47:13 +00:00
|
|
|
o.fs.putFtpConnection(&c, nil)
|
2017-05-03 12:51:03 +00:00
|
|
|
o.info, err = o.fs.getInfo(path)
|
2016-12-27 20:52:30 +00:00
|
|
|
if err != nil {
|
2017-05-03 20:45:01 +00:00
|
|
|
return errors.Wrap(err, "update getinfo")
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
2017-05-03 12:51:03 +00:00
|
|
|
return nil
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:51:03 +00:00
|
|
|
// Remove an object
|
2017-05-03 20:45:01 +00:00
|
|
|
func (o *Object) Remove() (err error) {
|
|
|
|
// defer fs.Trace(o, "")("err=%v", &err)
|
2017-05-03 15:42:04 +00:00
|
|
|
path := path.Join(o.fs.root, o.remote)
|
2017-05-03 12:51:03 +00:00
|
|
|
// Check if it's a directory or a file
|
2017-05-03 15:42:04 +00:00
|
|
|
info, err := o.fs.getInfo(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-03 12:51:03 +00:00
|
|
|
if info.IsDir {
|
|
|
|
err = o.fs.Rmdir(o.remote)
|
|
|
|
} else {
|
2017-05-18 14:51:35 +00:00
|
|
|
c, err := o.fs.getFtpConnection()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Remove")
|
|
|
|
}
|
|
|
|
err = c.Delete(path)
|
2017-05-24 13:47:13 +00:00
|
|
|
o.fs.putFtpConnection(&c, err)
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
2017-05-03 12:51:03 +00:00
|
|
|
return err
|
2016-12-27 20:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 12:18:17 +00:00
|
|
|
// Check the interfaces are satisfied
|
2016-12-27 20:52:30 +00:00
|
|
|
var (
|
2017-05-18 16:32:45 +00:00
|
|
|
_ fs.Fs = &Fs{}
|
|
|
|
_ fs.Mover = &Fs{}
|
|
|
|
_ fs.DirMover = &Fs{}
|
|
|
|
_ fs.Object = &Object{}
|
2016-12-27 20:52:30 +00:00
|
|
|
)
|