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