2021-09-09 14:25:25 +02:00
|
|
|
//go:build linux || freebsd
|
2021-02-03 18:46:08 +01:00
|
|
|
// +build linux freebsd
|
2016-07-18 00:03:23 +02:00
|
|
|
|
2022-08-28 13:21:57 +02:00
|
|
|
// Package mount implements a FUSE mounting system for rclone remotes.
|
2016-07-18 00:03:23 +02:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2018-07-18 17:21:35 +02:00
|
|
|
"fmt"
|
2020-07-24 16:41:31 +02:00
|
|
|
"runtime"
|
2016-09-09 09:39:19 +02:00
|
|
|
|
2016-07-18 00:03:23 +02:00
|
|
|
"bazil.org/fuse"
|
2017-05-02 23:35:07 +02:00
|
|
|
fusefs "bazil.org/fuse/fs"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2016-07-18 00:03:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-07-23 14:08:38 +02:00
|
|
|
mountlib.NewMountCommand("mount", false, mount)
|
2020-04-25 07:03:07 +02:00
|
|
|
mountlib.AddRc("mount", mount)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// mountOptions configures the options from the command line flags
|
2020-07-23 18:17:01 +02:00
|
|
|
func mountOptions(VFS *vfs.VFS, device string, opt *mountlib.Options) (options []fuse.MountOption) {
|
2017-05-02 23:35:07 +02:00
|
|
|
options = []fuse.MountOption{
|
2020-07-23 18:17:01 +02:00
|
|
|
fuse.MaxReadahead(uint32(opt.MaxReadAhead)),
|
2017-05-02 23:35:07 +02:00
|
|
|
fuse.Subtype("rclone"),
|
2018-05-03 10:45:24 +02:00
|
|
|
fuse.FSName(device),
|
2017-05-02 23:35:07 +02:00
|
|
|
|
|
|
|
// Options from benchmarking in the fuse module
|
|
|
|
//fuse.MaxReadahead(64 * 1024 * 1024),
|
|
|
|
//fuse.WritebackCache(),
|
|
|
|
}
|
2020-07-23 18:17:01 +02:00
|
|
|
if opt.AsyncRead {
|
2020-02-04 11:21:07 +01:00
|
|
|
options = append(options, fuse.AsyncRead())
|
|
|
|
}
|
2020-07-23 18:17:01 +02:00
|
|
|
if opt.AllowOther {
|
2017-05-02 23:35:07 +02:00
|
|
|
options = append(options, fuse.AllowOther())
|
|
|
|
}
|
2020-07-23 18:17:01 +02:00
|
|
|
if opt.AllowRoot {
|
2020-02-25 15:21:03 +01: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-02 23:35:07 +02:00
|
|
|
}
|
2020-07-23 18:17:01 +02:00
|
|
|
if opt.DefaultPermissions {
|
2017-05-02 23:35:07 +02:00
|
|
|
options = append(options, fuse.DefaultPermissions())
|
|
|
|
}
|
2020-07-22 18:58:49 +02:00
|
|
|
if VFS.Opt.ReadOnly {
|
2017-05-02 23:35:07 +02:00
|
|
|
options = append(options, fuse.ReadOnly())
|
|
|
|
}
|
2020-07-23 18:17:01 +02:00
|
|
|
if opt.WritebackCache {
|
2017-05-02 23:35:07 +02:00
|
|
|
options = append(options, fuse.WritebackCache())
|
|
|
|
}
|
2020-07-23 18:17:01 +02:00
|
|
|
if opt.DaemonTimeout != 0 {
|
|
|
|
options = append(options, fuse.DaemonTimeout(fmt.Sprint(int(opt.DaemonTimeout.Seconds()))))
|
2018-07-18 17:21:35 +02:00
|
|
|
}
|
2020-07-23 18:17:01 +02:00
|
|
|
if len(opt.ExtraOptions) > 0 {
|
2017-06-19 14:44:49 +02:00
|
|
|
fs.Errorf(nil, "-o/--option not supported with this FUSE backend")
|
|
|
|
}
|
2020-07-23 18:17:01 +02:00
|
|
|
if len(opt.ExtraFlags) > 0 {
|
2017-06-19 14:44:49 +02:00
|
|
|
fs.Errorf(nil, "--fuse-flag not supported with this FUSE backend")
|
|
|
|
}
|
2017-05-02 23:35:07 +02: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-23 18:17:01 +02:00
|
|
|
func mount(VFS *vfs.VFS, mountpoint string, opt *mountlib.Options) (<-chan error, func() error, error) {
|
2022-06-11 16:18:48 +02:00
|
|
|
f := VFS.Fs()
|
2020-07-24 16:41:31 +02: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 16:18:48 +02: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 16:41:31 +02:00
|
|
|
|
2020-07-23 18:17:01 +02:00
|
|
|
if opt.DebugFUSE {
|
2020-07-23 14:08:38 +02:00
|
|
|
fuse.Debug = func(msg interface{}) {
|
|
|
|
fs.Debugf("fuse", "%v", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 12:56:43 +01:00
|
|
|
c, err := fuse.Mount(mountpoint, mountOptions(VFS, opt.DeviceName, opt)...)
|
2017-05-02 23:35:07 +02:00
|
|
|
if err != nil {
|
2020-07-22 18:58:49 +02:00
|
|
|
return nil, nil, err
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 18:17:01 +02:00
|
|
|
filesys := NewFS(VFS, opt)
|
2021-03-16 14:23:36 +01:00
|
|
|
filesys.server = fusefs.New(c, nil)
|
2017-05-02 23:35:07 +02:00
|
|
|
|
|
|
|
// Serve the mount point in the background returning error to errChan
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
go func() {
|
2021-03-16 14:23:36 +01:00
|
|
|
err := filesys.server.Serve(filesys)
|
2017-05-02 23:35:07 +02:00
|
|
|
closeErr := c.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = closeErr
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
unmount := func() error {
|
2017-11-07 19:03:23 +01:00
|
|
|
// Shutdown the VFS
|
|
|
|
filesys.VFS.Shutdown()
|
2017-05-02 23:35:07 +02:00
|
|
|
return fuse.Unmount(mountpoint)
|
|
|
|
}
|
|
|
|
|
2020-07-22 18:58:49 +02:00
|
|
|
return errChan, unmount, nil
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|