2021-09-09 20:25:25 +08:00
|
|
|
//go:build linux || freebsd
|
2021-02-04 01:46:08 +08:00
|
|
|
// +build linux freebsd
|
2016-07-18 06:03:23 +08:00
|
|
|
|
2022-08-28 19:21:57 +08:00
|
|
|
// Package mount implements a FUSE mounting system for rclone remotes.
|
2016-07-18 06:03:23 +08:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2018-07-18 23:21:35 +08:00
|
|
|
"fmt"
|
2020-07-24 22:41:31 +08:00
|
|
|
"runtime"
|
2016-09-09 15:39:19 +08:00
|
|
|
|
2016-07-18 06:03:23 +08:00
|
|
|
"bazil.org/fuse"
|
2017-05-03 05:35:07 +08:00
|
|
|
fusefs "bazil.org/fuse/fs"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2016-07-18 06:03:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-07-23 20:08:38 +08:00
|
|
|
mountlib.NewMountCommand("mount", false, mount)
|
2020-04-25 13:03:07 +08:00
|
|
|
mountlib.AddRc("mount", mount)
|
2016-07-18 06:03:23 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// mountOptions configures the options from the command line flags
|
2020-07-24 00:17:01 +08:00
|
|
|
func mountOptions(VFS *vfs.VFS, device string, opt *mountlib.Options) (options []fuse.MountOption) {
|
2017-05-03 05:35:07 +08:00
|
|
|
options = []fuse.MountOption{
|
2020-07-24 00:17:01 +08:00
|
|
|
fuse.MaxReadahead(uint32(opt.MaxReadAhead)),
|
2017-05-03 05:35:07 +08:00
|
|
|
fuse.Subtype("rclone"),
|
2018-05-03 16:45:24 +08:00
|
|
|
fuse.FSName(device),
|
2017-05-03 05:35:07 +08:00
|
|
|
|
|
|
|
// Options from benchmarking in the fuse module
|
|
|
|
//fuse.MaxReadahead(64 * 1024 * 1024),
|
|
|
|
//fuse.WritebackCache(),
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.AsyncRead {
|
2020-02-04 18:21:07 +08:00
|
|
|
options = append(options, fuse.AsyncRead())
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.AllowOther {
|
2017-05-03 05:35:07 +08:00
|
|
|
options = append(options, fuse.AllowOther())
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.AllowRoot {
|
2020-02-25 22:21:03 +08:00
|
|
|
// options = append(options, fuse.AllowRoot())
|
|
|
|
fs.Errorf(nil, "Ignoring --allow-root. Support has been removed upstream - see https://github.com/bazil/fuse/issues/144 for more info")
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.DefaultPermissions {
|
2017-05-03 05:35:07 +08:00
|
|
|
options = append(options, fuse.DefaultPermissions())
|
|
|
|
}
|
2020-07-23 00:58:49 +08:00
|
|
|
if VFS.Opt.ReadOnly {
|
2017-05-03 05:35:07 +08:00
|
|
|
options = append(options, fuse.ReadOnly())
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.WritebackCache {
|
2017-05-03 05:35:07 +08:00
|
|
|
options = append(options, fuse.WritebackCache())
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.DaemonTimeout != 0 {
|
|
|
|
options = append(options, fuse.DaemonTimeout(fmt.Sprint(int(opt.DaemonTimeout.Seconds()))))
|
2018-07-18 23:21:35 +08:00
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if len(opt.ExtraOptions) > 0 {
|
2017-06-19 20:44:49 +08:00
|
|
|
fs.Errorf(nil, "-o/--option not supported with this FUSE backend")
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if len(opt.ExtraFlags) > 0 {
|
2017-06-19 20:44:49 +08:00
|
|
|
fs.Errorf(nil, "--fuse-flag not supported with this FUSE backend")
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
// mount the file system
|
|
|
|
//
|
|
|
|
// The mount point will be ready when this returns.
|
|
|
|
//
|
|
|
|
// returns an error, and an error channel for the serve process to
|
|
|
|
// report an error when fusermount is called.
|
2020-07-24 00:17:01 +08:00
|
|
|
func mount(VFS *vfs.VFS, mountpoint string, opt *mountlib.Options) (<-chan error, func() error, error) {
|
2022-06-11 22:18:48 +08:00
|
|
|
f := VFS.Fs()
|
2020-07-24 22:41:31 +08:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
fs.Logf(nil, "macOS users: please try \"rclone cmount\" as it will be the default in v1.54")
|
|
|
|
}
|
2022-06-11 22:18:48 +08:00
|
|
|
if err := mountlib.CheckOverlap(f, mountpoint); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := mountlib.CheckAllowNonEmpty(mountpoint, opt); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
fs.Debugf(f, "Mounting on %q", mountpoint)
|
2020-07-24 22:41:31 +08:00
|
|
|
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.DebugFUSE {
|
2020-07-23 20:08:38 +08:00
|
|
|
fuse.Debug = func(msg interface{}) {
|
|
|
|
fs.Debugf("fuse", "%v", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:56:43 +08:00
|
|
|
c, err := fuse.Mount(mountpoint, mountOptions(VFS, opt.DeviceName, opt)...)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
2020-07-23 00:58:49 +08:00
|
|
|
return nil, nil, err
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2020-07-24 00:17:01 +08:00
|
|
|
filesys := NewFS(VFS, opt)
|
2021-03-16 21:23:36 +08:00
|
|
|
filesys.server = fusefs.New(c, nil)
|
2017-05-03 05:35:07 +08:00
|
|
|
|
|
|
|
// Serve the mount point in the background returning error to errChan
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
go func() {
|
2021-03-16 21:23:36 +08:00
|
|
|
err := filesys.server.Serve(filesys)
|
2017-05-03 05:35:07 +08:00
|
|
|
closeErr := c.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = closeErr
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
unmount := func() error {
|
2017-11-08 02:03:23 +08:00
|
|
|
// Shutdown the VFS
|
|
|
|
filesys.VFS.Shutdown()
|
2017-05-03 05:35:07 +08:00
|
|
|
return fuse.Unmount(mountpoint)
|
|
|
|
}
|
|
|
|
|
2020-07-23 00:58:49 +08:00
|
|
|
return errChan, unmount, nil
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|