2021-01-03 00:05:52 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2021-11-04 10:12:57 +00:00
|
|
|
"fmt"
|
2024-08-15 18:29:24 +00:00
|
|
|
"math"
|
2021-01-03 00:05:52 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// applyOptions configures volume from request options.
|
|
|
|
//
|
|
|
|
// There are 5 special options:
|
2022-08-05 15:35:41 +00:00
|
|
|
// - "remote" aka "fs" determines existing remote from config file
|
|
|
|
// with a path or on-the-fly remote using the ":backend:" syntax.
|
|
|
|
// It is usually named "remote" in documentation but can be aliased as
|
|
|
|
// "fs" to avoid confusion with the "remote" option of some backends.
|
|
|
|
// - "type" is equivalent to the ":backend:" syntax (optional).
|
|
|
|
// - "path" provides explicit on-remote path for "type" (optional).
|
|
|
|
// - "mount-type" can be "mount", "cmount" or "mount2", defaults to
|
|
|
|
// first found (optional).
|
|
|
|
// - "persist" is reserved for future to create remotes persisted
|
|
|
|
// in rclone.conf similar to rcd (optional).
|
2021-01-03 00:05:52 +00:00
|
|
|
//
|
|
|
|
// Unlike rcd we use the flat naming scheme for mount, vfs and backend
|
|
|
|
// options without substructures. Dashes, underscores and mixed case
|
|
|
|
// in option names can be used interchangeably. Option name conflicts
|
|
|
|
// can be resolved in a manner similar to rclone CLI by adding prefixes:
|
|
|
|
// "vfs-", primary mount backend type like "sftp-", and so on.
|
|
|
|
//
|
|
|
|
// After triaging the options are put in MountOpt, VFSOpt or connect
|
|
|
|
// string for actual filesystem setup and in volume.Options for saving
|
|
|
|
// the state.
|
|
|
|
func (vol *Volume) applyOptions(volOpt VolOpts) error {
|
|
|
|
// copy options to override later
|
|
|
|
mntOpt := &vol.mnt.MountOpt
|
|
|
|
vfsOpt := &vol.mnt.VFSOpt
|
|
|
|
*mntOpt = vol.drv.mntOpt
|
|
|
|
*vfsOpt = vol.drv.vfsOpt
|
|
|
|
|
|
|
|
// vol.Options has all options except "remote" and "type"
|
|
|
|
vol.Options = VolOpts{}
|
|
|
|
vol.fsString = ""
|
|
|
|
|
|
|
|
var fsName, fsPath, fsType string
|
|
|
|
var explicitPath string
|
|
|
|
var fsOpt configmap.Simple
|
|
|
|
|
|
|
|
// parse "remote" or "type"
|
|
|
|
for key, str := range volOpt {
|
|
|
|
switch key {
|
|
|
|
case "":
|
|
|
|
continue
|
|
|
|
case "remote", "fs":
|
2023-02-28 11:29:07 +00:00
|
|
|
if str != "" {
|
|
|
|
p, err := fspath.Parse(str)
|
|
|
|
if err != nil || p.Name == ":" {
|
|
|
|
return fmt.Errorf("cannot parse path %q: %w", str, err)
|
|
|
|
}
|
|
|
|
fsName, fsPath, fsOpt = p.Name, p.Path, p.Config
|
|
|
|
vol.Fs = str
|
2021-01-03 00:05:52 +00:00
|
|
|
}
|
|
|
|
case "type":
|
|
|
|
fsType = str
|
|
|
|
vol.Type = str
|
|
|
|
case "path":
|
|
|
|
explicitPath = str
|
|
|
|
vol.Path = str
|
|
|
|
default:
|
|
|
|
vol.Options[key] = str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find options supported by backend
|
|
|
|
if strings.HasPrefix(fsName, ":") {
|
|
|
|
fsType = fsName[1:]
|
|
|
|
fsName = ""
|
|
|
|
}
|
|
|
|
if fsType == "" {
|
|
|
|
fsType = "local"
|
|
|
|
if fsName != "" {
|
|
|
|
var ok bool
|
2024-07-01 17:06:49 +00:00
|
|
|
fsType, ok = fs.ConfigMap("", nil, fsName, nil).Get("type")
|
2021-01-03 00:05:52 +00:00
|
|
|
if !ok {
|
|
|
|
return fs.ErrorNotFoundInConfigFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if explicitPath != "" {
|
|
|
|
if fsPath != "" {
|
|
|
|
fs.Logf(nil, "Explicit path will override connection string")
|
|
|
|
}
|
|
|
|
fsPath = explicitPath
|
|
|
|
}
|
|
|
|
fsInfo, err := fs.Find(fsType)
|
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("unknown filesystem type %q", fsType)
|
2021-01-03 00:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle remaining options, override fsOpt
|
|
|
|
if fsOpt == nil {
|
|
|
|
fsOpt = configmap.Simple{}
|
|
|
|
}
|
|
|
|
opt := rc.Params{}
|
|
|
|
for key, val := range vol.Options {
|
|
|
|
opt[key] = val
|
|
|
|
}
|
|
|
|
for key := range opt {
|
|
|
|
var ok bool
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch normalOptName(key) {
|
|
|
|
case "persist":
|
|
|
|
vol.persist, err = opt.GetBool(key)
|
|
|
|
ok = true
|
|
|
|
case "mount-type":
|
|
|
|
vol.mountType, err = opt.GetString(key)
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("cannot parse option %q: %w", key, err)
|
2021-01-03 00:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// try to use as a mount option in mntOpt
|
|
|
|
ok, err = getMountOption(mntOpt, opt, key)
|
|
|
|
if ok && err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("cannot parse mount option %q: %w", key, err)
|
2021-01-03 00:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
// try as a vfs option in vfsOpt
|
|
|
|
ok, err = getVFSOption(vfsOpt, opt, key)
|
|
|
|
if ok && err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("cannot parse vfs option %q: %w", key, err)
|
2021-01-03 00:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// try as a backend option in fsOpt (backends use "_" instead of "-")
|
|
|
|
optWithPrefix := strings.ReplaceAll(normalOptName(key), "-", "_")
|
|
|
|
fsOptName := strings.TrimPrefix(optWithPrefix, fsType+"_")
|
|
|
|
hasFsPrefix := optWithPrefix != fsOptName
|
|
|
|
if !hasFsPrefix || fsInfo.Options.Get(fsOptName) == nil {
|
|
|
|
fs.Logf(nil, "Option %q is not supported by backend %q", key, fsType)
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("unsupported backend option %q", key)
|
2021-01-03 00:05:52 +00:00
|
|
|
}
|
|
|
|
fsOpt[fsOptName], err = opt.GetString(key)
|
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("cannot parse backend option %q: %w", key, err)
|
2021-01-03 00:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// build remote string from fsName, fsType, fsOpt, fsPath
|
|
|
|
colon := ":"
|
|
|
|
comma := ","
|
|
|
|
if fsName == "" {
|
|
|
|
fsName = ":" + fsType
|
|
|
|
}
|
|
|
|
connString := fsOpt.String()
|
|
|
|
if fsName == "" && fsType == "" {
|
|
|
|
colon = ""
|
|
|
|
connString = ""
|
|
|
|
}
|
|
|
|
if connString == "" {
|
|
|
|
comma = ""
|
|
|
|
}
|
|
|
|
vol.fsString = fsName + comma + connString + colon + fsPath
|
|
|
|
|
|
|
|
return vol.validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMountOption(mntOpt *mountlib.Options, opt rc.Params, key string) (ok bool, err error) {
|
|
|
|
ok = true
|
|
|
|
switch normalOptName(key) {
|
|
|
|
case "debug-fuse":
|
|
|
|
mntOpt.DebugFUSE, err = opt.GetBool(key)
|
|
|
|
case "attr-timeout":
|
2024-07-02 09:26:15 +00:00
|
|
|
mntOpt.AttrTimeout, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "option":
|
|
|
|
mntOpt.ExtraOptions, err = getStringArray(opt, key)
|
|
|
|
case "fuse-flag":
|
|
|
|
mntOpt.ExtraFlags, err = getStringArray(opt, key)
|
|
|
|
case "daemon":
|
|
|
|
mntOpt.Daemon, err = opt.GetBool(key)
|
|
|
|
case "daemon-timeout":
|
2024-07-02 09:26:15 +00:00
|
|
|
mntOpt.DaemonTimeout, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "default-permissions":
|
|
|
|
mntOpt.DefaultPermissions, err = opt.GetBool(key)
|
|
|
|
case "allow-non-empty":
|
|
|
|
mntOpt.AllowNonEmpty, err = opt.GetBool(key)
|
|
|
|
case "allow-root":
|
|
|
|
mntOpt.AllowRoot, err = opt.GetBool(key)
|
|
|
|
case "allow-other":
|
|
|
|
mntOpt.AllowOther, err = opt.GetBool(key)
|
|
|
|
case "async-read":
|
|
|
|
mntOpt.AsyncRead, err = opt.GetBool(key)
|
|
|
|
case "max-read-ahead":
|
|
|
|
err = getFVarP(&mntOpt.MaxReadAhead, opt, key)
|
|
|
|
case "write-back-cache":
|
|
|
|
mntOpt.WritebackCache, err = opt.GetBool(key)
|
|
|
|
case "volname":
|
|
|
|
mntOpt.VolumeName, err = opt.GetString(key)
|
|
|
|
case "noappledouble":
|
|
|
|
mntOpt.NoAppleDouble, err = opt.GetBool(key)
|
|
|
|
case "noapplexattr":
|
|
|
|
mntOpt.NoAppleXattr, err = opt.GetBool(key)
|
|
|
|
case "network-mode":
|
|
|
|
mntOpt.NetworkMode, err = opt.GetBool(key)
|
|
|
|
default:
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVFSOption(vfsOpt *vfscommon.Options, opt rc.Params, key string) (ok bool, err error) {
|
|
|
|
var intVal int64
|
|
|
|
ok = true
|
|
|
|
switch normalOptName(key) {
|
|
|
|
|
|
|
|
// options prefixed with "vfs-"
|
|
|
|
case "vfs-cache-mode":
|
|
|
|
err = getFVarP(&vfsOpt.CacheMode, opt, key)
|
|
|
|
case "vfs-cache-poll-interval":
|
2024-07-02 16:31:51 +00:00
|
|
|
vfsOpt.CachePollInterval, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "vfs-cache-max-age":
|
2024-07-02 16:31:51 +00:00
|
|
|
vfsOpt.CacheMaxAge, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "vfs-cache-max-size":
|
|
|
|
err = getFVarP(&vfsOpt.CacheMaxSize, opt, key)
|
|
|
|
case "vfs-read-chunk-size":
|
|
|
|
err = getFVarP(&vfsOpt.ChunkSize, opt, key)
|
|
|
|
case "vfs-read-chunk-size-limit":
|
|
|
|
err = getFVarP(&vfsOpt.ChunkSizeLimit, opt, key)
|
|
|
|
case "vfs-case-insensitive":
|
|
|
|
vfsOpt.CaseInsensitive, err = opt.GetBool(key)
|
|
|
|
case "vfs-write-wait":
|
2024-07-02 16:31:51 +00:00
|
|
|
vfsOpt.WriteWait, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "vfs-read-wait":
|
2024-07-02 16:31:51 +00:00
|
|
|
vfsOpt.ReadWait, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "vfs-write-back":
|
2024-07-02 16:31:51 +00:00
|
|
|
vfsOpt.WriteBack, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "vfs-read-ahead":
|
|
|
|
err = getFVarP(&vfsOpt.ReadAhead, opt, key)
|
|
|
|
case "vfs-used-is-size":
|
|
|
|
vfsOpt.UsedIsSize, err = opt.GetBool(key)
|
2024-09-09 09:07:25 +00:00
|
|
|
case "vfs-read-chunk-streams":
|
|
|
|
intVal, err = opt.GetInt64(key)
|
|
|
|
if err == nil {
|
|
|
|
if intVal >= 0 && intVal <= math.MaxInt {
|
|
|
|
vfsOpt.ChunkStreams = int(intVal)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("key %q (%v) overflows int", key, intVal)
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 00:05:52 +00:00
|
|
|
|
|
|
|
// unprefixed vfs options
|
|
|
|
case "no-modtime":
|
|
|
|
vfsOpt.NoModTime, err = opt.GetBool(key)
|
|
|
|
case "no-checksum":
|
|
|
|
vfsOpt.NoChecksum, err = opt.GetBool(key)
|
|
|
|
case "dir-cache-time":
|
2024-07-02 16:31:51 +00:00
|
|
|
vfsOpt.DirCacheTime, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "poll-interval":
|
2024-07-02 16:31:51 +00:00
|
|
|
vfsOpt.PollInterval, err = opt.GetFsDuration(key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "read-only":
|
|
|
|
vfsOpt.ReadOnly, err = opt.GetBool(key)
|
|
|
|
case "dir-perms":
|
2024-07-03 10:34:29 +00:00
|
|
|
err = getFVarP(&vfsOpt.DirPerms, opt, key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "file-perms":
|
2024-07-03 10:34:29 +00:00
|
|
|
err = getFVarP(&vfsOpt.FilePerms, opt, key)
|
2021-01-03 00:05:52 +00:00
|
|
|
|
|
|
|
// unprefixed unix-only vfs options
|
|
|
|
case "umask":
|
2024-07-03 10:34:29 +00:00
|
|
|
err = getFVarP(&vfsOpt.Umask, opt, key)
|
2021-01-03 00:05:52 +00:00
|
|
|
case "uid":
|
|
|
|
intVal, err = opt.GetInt64(key)
|
2024-08-15 18:29:24 +00:00
|
|
|
if err == nil {
|
|
|
|
if intVal >= 0 && intVal <= math.MaxUint32 {
|
|
|
|
vfsOpt.UID = uint32(intVal)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("key %q (%v) overflows uint32", key, intVal)
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 00:05:52 +00:00
|
|
|
case "gid":
|
|
|
|
intVal, err = opt.GetInt64(key)
|
2024-08-15 18:29:24 +00:00
|
|
|
if err == nil {
|
|
|
|
if intVal >= 0 && intVal <= math.MaxUint32 {
|
|
|
|
vfsOpt.UID = uint32(intVal)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("key %q (%v) overflows uint32", key, intVal)
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 00:05:52 +00:00
|
|
|
|
|
|
|
// non-vfs options
|
|
|
|
default:
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFVarP(pvalue pflag.Value, opt rc.Params, key string) error {
|
|
|
|
str, err := opt.GetString(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return pvalue.Set(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStringArray(opt rc.Params, key string) ([]string, error) {
|
|
|
|
str, err := opt.GetString(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return strings.Split(str, ","), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalOptName(key string) string {
|
|
|
|
return strings.ReplaceAll(strings.TrimPrefix(strings.ToLower(key), "--"), "_", "-")
|
|
|
|
}
|