2021-09-09 14:25:25 +02:00
|
|
|
//go:build !plan9
|
2018-09-13 19:21:20 +02:00
|
|
|
|
2022-08-28 13:21:57 +02:00
|
|
|
// Package ftp implements an FTP server for rclone
|
2018-03-25 17:03:37 +02:00
|
|
|
package ftp
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:18:51 +01:00
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2018-03-25 17:03:37 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-08-22 01:36:17 +02:00
|
|
|
iofs "io/fs"
|
2018-03-25 17:03:37 +02:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
2021-11-12 13:48:14 +01:00
|
|
|
"regexp"
|
2018-03-25 17:03:37 +02:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
2021-09-04 13:47:18 +02:00
|
|
|
"time"
|
2018-03-25 17:03:37 +02:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2019-08-01 16:02:31 +02:00
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2019-08-01 12:43:06 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2023-08-23 13:21:26 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2024-07-03 12:34:29 +02:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
2018-03-25 17:03:37 +02:00
|
|
|
"github.com/spf13/cobra"
|
2019-08-01 12:43:06 +02:00
|
|
|
"github.com/spf13/pflag"
|
2023-08-22 01:36:17 +02:00
|
|
|
ftp "goftp.io/server/v2"
|
2018-03-25 17:03:37 +02:00
|
|
|
)
|
|
|
|
|
2024-07-03 20:14:56 +02:00
|
|
|
// OptionsInfo descripts the Options in use
|
|
|
|
var OptionsInfo = fs.Options{{
|
|
|
|
Name: "addr",
|
|
|
|
Default: "localhost:2121",
|
|
|
|
Help: "IPaddress:Port or :Port to bind server to",
|
|
|
|
}, {
|
|
|
|
Name: "public_ip",
|
|
|
|
Default: "",
|
|
|
|
Help: "Public IP address to advertise for passive connections",
|
|
|
|
}, {
|
|
|
|
Name: "passive_port",
|
|
|
|
Default: "30000-32000",
|
|
|
|
Help: "Passive port range to use",
|
|
|
|
}, {
|
|
|
|
Name: "user",
|
|
|
|
Default: "anonymous",
|
|
|
|
Help: "User name for authentication",
|
|
|
|
}, {
|
|
|
|
Name: "pass",
|
|
|
|
Default: "",
|
|
|
|
Help: "Password for authentication (empty value allow every password)",
|
|
|
|
}, {
|
|
|
|
Name: "cert",
|
|
|
|
Default: "",
|
|
|
|
Help: "TLS PEM key (concatenation of certificate and CA certificate)",
|
|
|
|
}, {
|
|
|
|
Name: "key",
|
|
|
|
Default: "",
|
|
|
|
Help: "TLS PEM Private key",
|
|
|
|
}}
|
|
|
|
|
2019-08-01 12:43:06 +02:00
|
|
|
// Options contains options for the http Server
|
|
|
|
type Options struct {
|
|
|
|
//TODO add more options
|
2024-07-03 20:14:56 +02:00
|
|
|
ListenAddr string `config:"addr"` // Port to listen on
|
|
|
|
PublicIP string `config:"public_ip"` // Passive ports range
|
|
|
|
PassivePorts string `config:"passive_port"` // Passive ports range
|
|
|
|
BasicUser string `config:"user"` // single username for basic auth if not using Htpasswd
|
|
|
|
BasicPass string `config:"pass"` // password for BasicUser
|
|
|
|
TLSCert string `config:"cert"` // TLS PEM key (concatenation of certificate and CA certificate)
|
|
|
|
TLSKey string `config:"key"` // TLS PEM Private key
|
2019-08-01 12:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Opt is options set by command line flags
|
2024-07-03 20:14:56 +02:00
|
|
|
var Opt Options
|
2019-08-01 12:43:06 +02:00
|
|
|
|
|
|
|
// AddFlags adds flags for ftp
|
|
|
|
func AddFlags(flagSet *pflag.FlagSet) {
|
2024-07-03 20:14:56 +02:00
|
|
|
flags.AddFlagsFromOptions(flagSet, "", OptionsInfo)
|
2019-08-01 12:43:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-25 17:03:37 +02:00
|
|
|
func init() {
|
|
|
|
vfsflags.AddFlags(Command.Flags())
|
2019-08-01 16:02:31 +02:00
|
|
|
proxyflags.AddFlags(Command.Flags())
|
2019-08-01 12:43:06 +02:00
|
|
|
AddFlags(Command.Flags())
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "ftp remote:path",
|
|
|
|
Short: `Serve remote:path over FTP.`,
|
2024-04-05 13:27:33 +02:00
|
|
|
Long: `Run a basic FTP server to serve a remote over FTP protocol.
|
2022-06-19 19:18:14 +02:00
|
|
|
This can be viewed with a FTP client or you can make a remote of
|
|
|
|
type FTP to read and write it.
|
2019-08-01 12:43:06 +02:00
|
|
|
|
|
|
|
### Server options
|
|
|
|
|
|
|
|
Use --addr to specify which IP address and port the server should
|
2020-10-13 23:49:58 +02:00
|
|
|
listen on, e.g. --addr 1.2.3.4:8000 or --addr :8080 to listen to all
|
2019-08-01 12:43:06 +02:00
|
|
|
IPs. By default it only listens on localhost. You can use port
|
|
|
|
:0 to let the OS choose an available port.
|
|
|
|
|
|
|
|
If you set --addr to listen on a public or LAN accessible IP address
|
|
|
|
then using Authentication is advised - see the next section for info.
|
|
|
|
|
|
|
|
#### Authentication
|
|
|
|
|
|
|
|
By default this will serve files without needing a login.
|
|
|
|
|
|
|
|
You can set a single username and password with the --user and --pass flags.
|
2024-04-05 13:27:33 +02:00
|
|
|
|
|
|
|
` + vfs.Help() + proxy.Help,
|
2022-11-26 23:40:49 +01:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.44",
|
2023-07-10 19:34:10 +02:00
|
|
|
"groups": "Filter",
|
2022-11-26 23:40:49 +01:00
|
|
|
},
|
2018-03-25 17:03:37 +02:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
2019-08-01 16:02:31 +02:00
|
|
|
var f fs.Fs
|
|
|
|
if proxyflags.Opt.AuthProxy == "" {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
f = cmd.NewFsSrc(args)
|
|
|
|
} else {
|
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
|
|
|
}
|
2018-03-25 17:03:37 +02:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2020-11-05 16:18:51 +01:00
|
|
|
s, err := newServer(context.Background(), f, &Opt)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.serve()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:36:17 +02:00
|
|
|
// driver contains everything to run the driver for the FTP server
|
|
|
|
type driver struct {
|
2023-08-23 13:21:26 +02:00
|
|
|
f fs.Fs
|
|
|
|
srv *ftp.Server
|
|
|
|
ctx context.Context // for global config
|
|
|
|
opt Options
|
|
|
|
globalVFS *vfs.VFS // the VFS if not using auth proxy
|
|
|
|
proxy *proxy.Proxy // may be nil if not in use
|
|
|
|
useTLS bool
|
|
|
|
userPassMu sync.Mutex // to protect userPass
|
|
|
|
userPass map[string]string // cache of username => password when using vfs proxy
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
2021-11-12 13:48:14 +01:00
|
|
|
var passivePortsRe = regexp.MustCompile(`^\s*\d+\s*-\s*\d+\s*$`)
|
|
|
|
|
2018-03-25 17:03:37 +02:00
|
|
|
// Make a new FTP to serve the remote
|
2023-08-22 01:36:17 +02:00
|
|
|
func newServer(ctx context.Context, f fs.Fs, opt *Options) (*driver, error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
host, port, err := net.SplitHostPort(opt.ListenAddr)
|
|
|
|
if err != nil {
|
2022-06-08 22:54:39 +02:00
|
|
|
return nil, errors.New("failed to parse host:port")
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
portNum, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
2022-06-08 22:54:39 +02:00
|
|
|
return nil, errors.New("failed to parse host:port")
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-22 01:36:17 +02:00
|
|
|
d := &driver{
|
2020-06-29 15:09:45 +02:00
|
|
|
f: f,
|
2020-11-05 16:18:51 +01:00
|
|
|
ctx: ctx,
|
2020-06-29 15:09:45 +02:00
|
|
|
opt: *opt,
|
2019-08-01 12:43:06 +02:00
|
|
|
}
|
2019-08-01 16:02:31 +02:00
|
|
|
if proxyflags.Opt.AuthProxy != "" {
|
2023-08-22 01:36:17 +02:00
|
|
|
d.proxy = proxy.New(ctx, &proxyflags.Opt)
|
2023-08-23 13:21:26 +02:00
|
|
|
d.userPass = make(map[string]string, 16)
|
2019-08-01 16:02:31 +02:00
|
|
|
} else {
|
2024-07-03 12:34:29 +02:00
|
|
|
d.globalVFS = vfs.New(f, &vfscommon.Opt)
|
2019-08-01 16:02:31 +02:00
|
|
|
}
|
2023-08-22 01:36:17 +02:00
|
|
|
d.useTLS = d.opt.TLSKey != ""
|
2019-08-01 16:02:31 +02:00
|
|
|
|
2022-08-14 04:56:32 +02:00
|
|
|
// Check PassivePorts format since the server library doesn't!
|
2021-11-12 13:48:14 +01:00
|
|
|
if !passivePortsRe.MatchString(opt.PassivePorts) {
|
|
|
|
return nil, fmt.Errorf("invalid format for passive ports %q", opt.PassivePorts)
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:36:17 +02:00
|
|
|
ftpopt := &ftp.Options{
|
2018-03-25 17:03:37 +02:00
|
|
|
Name: "Rclone FTP Server",
|
2019-08-01 16:02:31 +02:00
|
|
|
WelcomeMessage: "Welcome to Rclone " + fs.Version + " FTP Server",
|
2023-08-22 01:36:17 +02:00
|
|
|
Driver: d,
|
2019-08-01 16:02:31 +02:00
|
|
|
Hostname: host,
|
|
|
|
Port: portNum,
|
2020-06-29 15:09:45 +02:00
|
|
|
PublicIP: opt.PublicIP,
|
2019-08-01 16:02:31 +02:00
|
|
|
PassivePorts: opt.PassivePorts,
|
2023-08-22 01:36:17 +02:00
|
|
|
Auth: d,
|
|
|
|
Perm: ftp.NewSimplePerm("ftp", "ftp"), // fake user and group
|
2019-08-01 16:02:31 +02:00
|
|
|
Logger: &Logger{},
|
2023-08-22 01:36:17 +02:00
|
|
|
TLS: d.useTLS,
|
|
|
|
CertFile: d.opt.TLSCert,
|
|
|
|
KeyFile: d.opt.TLSKey,
|
2019-10-29 18:41:56 +01:00
|
|
|
//TODO implement a maximum of https://godoc.org/goftp.io/server#ServerOpts
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
2023-08-22 01:36:17 +02:00
|
|
|
d.srv, err = ftp.NewServer(ftpopt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create new FTP server: %w", err)
|
|
|
|
}
|
|
|
|
return d, nil
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// serve runs the ftp server
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) serve() error {
|
|
|
|
fs.Logf(d.f, "Serving FTP on %s", d.srv.Hostname+":"+strconv.Itoa(d.srv.Port))
|
|
|
|
return d.srv.ListenAndServe()
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
2022-06-24 18:34:37 +02:00
|
|
|
// close stops the ftp server
|
2022-08-05 17:35:41 +02:00
|
|
|
//
|
2022-06-24 18:34:37 +02:00
|
|
|
//lint:ignore U1000 unused when not building linux
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) close() error {
|
|
|
|
fs.Logf(d.f, "Stopping FTP on %s", d.srv.Hostname+":"+strconv.Itoa(d.srv.Port))
|
|
|
|
return d.srv.Shutdown()
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Logger ftp logger output formatted message
|
2018-03-25 17:03:37 +02:00
|
|
|
type Logger struct{}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Print log simple text message
|
2018-03-25 17:03:37 +02:00
|
|
|
func (l *Logger) Print(sessionID string, message interface{}) {
|
|
|
|
fs.Infof(sessionID, "%s", message)
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Printf log formatted text message
|
2018-03-25 17:03:37 +02:00
|
|
|
func (l *Logger) Printf(sessionID string, format string, v ...interface{}) {
|
|
|
|
fs.Infof(sessionID, format, v...)
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// PrintCommand log formatted command execution
|
2018-03-25 17:03:37 +02:00
|
|
|
func (l *Logger) PrintCommand(sessionID string, command string, params string) {
|
|
|
|
if command == "PASS" {
|
|
|
|
fs.Infof(sessionID, "> PASS ****")
|
|
|
|
} else {
|
|
|
|
fs.Infof(sessionID, "> %s %s", command, params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// PrintResponse log responses
|
2018-03-25 17:03:37 +02:00
|
|
|
func (l *Logger) PrintResponse(sessionID string, code int, message string) {
|
|
|
|
fs.Infof(sessionID, "< %d %s", code, message)
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:02:31 +02:00
|
|
|
// CheckPasswd handle auth based on configuration
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) CheckPasswd(sctx *ftp.Context, user, pass string) (ok bool, err error) {
|
|
|
|
if d.proxy != nil {
|
2023-08-23 13:21:26 +02:00
|
|
|
_, _, err = d.proxy.Call(user, pass, false)
|
2020-06-29 15:09:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fs.Infof(nil, "proxy login failed: %v", err)
|
|
|
|
return false, nil
|
|
|
|
}
|
2023-08-23 13:21:26 +02:00
|
|
|
// Cache obscured password for later lookup.
|
|
|
|
//
|
|
|
|
// We don't cache the VFS directly in the driver as we want them
|
|
|
|
// to be expired and the auth proxy does that for us.
|
|
|
|
oPass, err := obscure.Obscure(pass)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
d.userPassMu.Lock()
|
|
|
|
d.userPass[user] = oPass
|
|
|
|
d.userPassMu.Unlock()
|
2020-06-29 15:09:45 +02:00
|
|
|
} else {
|
2023-08-22 01:36:17 +02:00
|
|
|
ok = d.opt.BasicUser == user && (d.opt.BasicPass == "" || d.opt.BasicPass == pass)
|
2020-06-29 15:09:45 +02:00
|
|
|
if !ok {
|
|
|
|
fs.Infof(nil, "login failed: bad credentials")
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-08-01 16:02:31 +02:00
|
|
|
}
|
2020-06-29 15:09:45 +02:00
|
|
|
return true, nil
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 13:21:26 +02:00
|
|
|
// Get the VFS for this connection
|
|
|
|
func (d *driver) getVFS(sctx *ftp.Context) (VFS *vfs.VFS, err error) {
|
|
|
|
if d.proxy == nil {
|
|
|
|
// If no proxy always use the same VFS
|
|
|
|
return d.globalVFS, nil
|
|
|
|
}
|
|
|
|
user := sctx.Sess.LoginUser()
|
|
|
|
d.userPassMu.Lock()
|
|
|
|
oPass, ok := d.userPass[user]
|
|
|
|
d.userPassMu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("proxy user not logged in")
|
|
|
|
}
|
|
|
|
pass, err := obscure.Reveal(oPass)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
VFS, _, err = d.proxy.Call(user, pass, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("proxy login failed: %w", err)
|
|
|
|
}
|
|
|
|
return VFS, nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Stat get information on file or folder
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) Stat(sctx *ftp.Context, path string) (fi iofs.FileInfo, err error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
defer log.Trace(path, "")("fi=%+v, err = %v", &fi, &err)
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-23 13:21:26 +02:00
|
|
|
n, err := VFS.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &FileInfo{n, n.Mode(), VFS.Opt.UID, VFS.Opt.GID}, err
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// ChangeDir move current folder
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) ChangeDir(sctx *ftp.Context, path string) (err error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
defer log.Trace(path, "")("err = %v", &err)
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err := VFS.Stat(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !n.IsDir() {
|
2022-06-08 22:54:39 +02:00
|
|
|
return errors.New("not a directory")
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// ListDir list content of a folder
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) ListDir(sctx *ftp.Context, path string, callback func(iofs.FileInfo) error) (err error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
defer log.Trace(path, "")("err = %v", &err)
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node, err := VFS.Stat(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err == vfs.ENOENT {
|
2022-06-08 22:54:39 +02:00
|
|
|
return errors.New("directory not found")
|
2018-03-25 17:03:37 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !node.IsDir() {
|
2022-06-08 22:54:39 +02:00
|
|
|
return errors.New("not a directory")
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dir := node.(*vfs.Dir)
|
|
|
|
dirEntries, err := dir.ReadDirAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account the transfer
|
2024-01-18 17:44:13 +01:00
|
|
|
tr := accounting.GlobalStats().NewTransferRemoteSize(path, node.Size(), d.f, nil)
|
2019-07-16 13:56:20 +02:00
|
|
|
defer func() {
|
2023-08-22 01:36:17 +02:00
|
|
|
tr.Done(d.ctx, err)
|
2019-07-16 13:56:20 +02:00
|
|
|
}()
|
2018-03-25 17:03:37 +02:00
|
|
|
|
|
|
|
for _, file := range dirEntries {
|
2023-08-23 13:21:26 +02:00
|
|
|
err = callback(&FileInfo{file, file.Mode(), VFS.Opt.UID, VFS.Opt.GID})
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// DeleteDir delete a folder and his content
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) DeleteDir(sctx *ftp.Context, path string) (err error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
defer log.Trace(path, "")("err = %v", &err)
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node, err := VFS.Stat(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !node.IsDir() {
|
2022-06-08 22:54:39 +02:00
|
|
|
return errors.New("not a directory")
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
err = node.Remove()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// DeleteFile delete a file
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) DeleteFile(sctx *ftp.Context, path string) (err error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
defer log.Trace(path, "")("err = %v", &err)
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node, err := VFS.Stat(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !node.IsFile() {
|
2022-06-08 22:54:39 +02:00
|
|
|
return errors.New("not a file")
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
err = node.Remove()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Rename rename a file or folder
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) Rename(sctx *ftp.Context, oldName, newName string) (err error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
defer log.Trace(oldName, "newName=%q", newName)("err = %v", &err)
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return VFS.Rename(oldName, newName)
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// MakeDir create a folder
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) MakeDir(sctx *ftp.Context, path string) (err error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
defer log.Trace(path, "")("err = %v", &err)
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir, leaf, err := VFS.StatParent(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = dir.Mkdir(leaf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// GetFile download a file
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) GetFile(sctx *ftp.Context, path string, offset int64) (size int64, fr io.ReadCloser, err error) {
|
2018-03-25 17:03:37 +02:00
|
|
|
defer log.Trace(path, "offset=%v", offset)("err = %v", &err)
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
node, err := VFS.Stat(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err == vfs.ENOENT {
|
|
|
|
fs.Infof(path, "File not found")
|
2022-06-08 22:54:39 +02:00
|
|
|
return 0, nil, errors.New("file not found")
|
2018-03-25 17:03:37 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
if !node.IsFile() {
|
2022-06-08 22:54:39 +02:00
|
|
|
return 0, nil, errors.New("not a file")
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handle, err := node.Open(os.O_RDONLY)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2020-02-05 18:58:29 +01:00
|
|
|
_, err = handle.Seek(offset, io.SeekStart)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account the transfer
|
2024-01-18 17:44:13 +01:00
|
|
|
tr := accounting.GlobalStats().NewTransferRemoteSize(path, node.Size(), d.f, nil)
|
2023-08-22 01:36:17 +02:00
|
|
|
defer tr.Done(d.ctx, nil)
|
2018-03-25 17:03:37 +02:00
|
|
|
|
|
|
|
return node.Size(), handle, nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// PutFile upload a file
|
2023-08-22 01:36:17 +02:00
|
|
|
func (d *driver) PutFile(sctx *ftp.Context, path string, data io.Reader, offset int64) (n int64, err error) {
|
|
|
|
defer log.Trace(path, "offset=%d", offset)("err = %v", &err)
|
|
|
|
|
2018-03-25 17:03:37 +02:00
|
|
|
var isExist bool
|
2023-08-23 13:21:26 +02:00
|
|
|
VFS, err := d.getVFS(sctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
fi, err := VFS.Stat(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err == nil {
|
|
|
|
isExist = true
|
2023-08-22 01:36:17 +02:00
|
|
|
if fi.IsDir() {
|
|
|
|
return 0, errors.New("can't create file - directory exists")
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
isExist = false
|
|
|
|
} else {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:36:17 +02:00
|
|
|
if offset > -1 && !isExist {
|
|
|
|
offset = -1
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-22 01:36:17 +02:00
|
|
|
var f vfs.Handle
|
|
|
|
|
|
|
|
if offset == -1 {
|
2018-03-25 17:03:37 +02:00
|
|
|
if isExist {
|
2023-08-23 13:21:26 +02:00
|
|
|
err = VFS.Remove(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 13:21:26 +02:00
|
|
|
f, err = VFS.Create(path)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-08-22 01:36:17 +02:00
|
|
|
defer fs.CheckClose(f, &err)
|
|
|
|
n, err = io.Copy(f, data)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-08-22 01:36:17 +02:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2023-08-23 13:21:26 +02:00
|
|
|
f, err = VFS.OpenFile(path, os.O_APPEND|os.O_RDWR, 0660)
|
2023-08-22 01:36:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
2023-08-22 01:36:17 +02:00
|
|
|
defer fs.CheckClose(f, &err)
|
2018-03-25 17:03:37 +02:00
|
|
|
|
2023-08-22 01:36:17 +02:00
|
|
|
info, err := f.Stat()
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-08-22 01:36:17 +02:00
|
|
|
if offset > info.Size() {
|
|
|
|
return 0, fmt.Errorf("offset %d is beyond file size %d", offset, info.Size())
|
|
|
|
}
|
2018-03-25 17:03:37 +02:00
|
|
|
|
2023-08-22 01:36:17 +02:00
|
|
|
_, err = f.Seek(offset, io.SeekStart)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:36:17 +02:00
|
|
|
bytes, err := io.Copy(f, data)
|
2018-03-25 17:03:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes, nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// FileInfo struct to hold file info for ftp server
|
2018-03-25 17:03:37 +02:00
|
|
|
type FileInfo struct {
|
|
|
|
os.FileInfo
|
|
|
|
|
|
|
|
mode os.FileMode
|
|
|
|
owner uint32
|
|
|
|
group uint32
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Mode return mode of file.
|
2018-03-25 17:03:37 +02:00
|
|
|
func (f *FileInfo) Mode() os.FileMode {
|
|
|
|
return f.mode
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Owner return owner of file. Try to find the username if possible
|
2018-03-25 17:03:37 +02:00
|
|
|
func (f *FileInfo) Owner() string {
|
|
|
|
str := fmt.Sprint(f.owner)
|
|
|
|
u, err := user.LookupId(str)
|
|
|
|
if err != nil {
|
|
|
|
return str //User not found
|
|
|
|
}
|
|
|
|
return u.Username
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Group return group of file. Try to find the group name if possible
|
2018-03-25 17:03:37 +02:00
|
|
|
func (f *FileInfo) Group() string {
|
|
|
|
str := fmt.Sprint(f.group)
|
|
|
|
g, err := user.LookupGroupId(str)
|
|
|
|
if err != nil {
|
2019-04-30 14:06:24 +02:00
|
|
|
return str //Group not found default to numerical value
|
2018-03-25 17:03:37 +02:00
|
|
|
}
|
|
|
|
return g.Name
|
|
|
|
}
|
|
|
|
|
2021-09-04 13:47:18 +02:00
|
|
|
// ModTime returns the time in UTC
|
|
|
|
func (f *FileInfo) ModTime() time.Time {
|
|
|
|
return f.FileInfo.ModTime().UTC()
|
|
|
|
}
|