2020-05-20 18:39:20 +08:00
|
|
|
// Package cmount implements a FUSE mounting system for rclone remotes.
|
2017-05-03 05:36:11 +08:00
|
|
|
//
|
|
|
|
// This uses the cgo based cgofuse library
|
|
|
|
|
2017-05-19 22:46:13 +08:00
|
|
|
// +build cmount
|
2017-05-03 05:36:11 +08:00
|
|
|
// +build cgo
|
|
|
|
// +build linux darwin freebsd windows
|
|
|
|
|
|
|
|
package cmount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2020-03-08 00:32:37 +08:00
|
|
|
"strings"
|
2017-05-03 05:36:11 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/billziss-gh/cgofuse/fuse"
|
|
|
|
"github.com/pkg/errors"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2017-05-03 05:36:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-06-19 20:44:49 +08:00
|
|
|
name := "cmount"
|
2020-10-29 20:19:48 +08:00
|
|
|
cmountOnly := runtime.GOOS == "windows" || runtime.GOOS == "darwin"
|
|
|
|
if cmountOnly {
|
2017-06-19 20:44:49 +08:00
|
|
|
name = "mount"
|
|
|
|
}
|
2020-10-29 20:19:48 +08:00
|
|
|
cmd := mountlib.NewMountCommand(name, false, mount)
|
|
|
|
if cmountOnly {
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "cmount")
|
|
|
|
}
|
2020-04-25 13:03:07 +08:00
|
|
|
mountlib.AddRc("cmount", mount)
|
2017-05-03 05:36:11 +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, mountpoint string, opt *mountlib.Options) (options []string) {
|
2017-05-03 05:36:11 +08:00
|
|
|
// Options
|
|
|
|
options = []string{
|
|
|
|
"-o", "fsname=" + device,
|
|
|
|
"-o", "subtype=rclone",
|
2020-07-24 00:17:01 +08:00
|
|
|
"-o", fmt.Sprintf("max_readahead=%d", opt.MaxReadAhead),
|
|
|
|
"-o", fmt.Sprintf("attr_timeout=%g", opt.AttrTimeout.Seconds()),
|
2017-11-14 01:18:47 +08:00
|
|
|
// This causes FUSE to supply O_TRUNC with the Open
|
|
|
|
// call which is more efficient for cmount. However
|
|
|
|
// it does not work with cgofuse on Windows with
|
|
|
|
// WinFSP so cmount must work with or without it.
|
|
|
|
"-o", "atomic_o_trunc",
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.DebugFUSE {
|
2017-05-03 05:36:11 +08:00
|
|
|
options = append(options, "-o", "debug")
|
|
|
|
}
|
|
|
|
|
|
|
|
// OSX options
|
|
|
|
if runtime.GOOS == "darwin" {
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.NoAppleDouble {
|
2018-05-03 16:45:24 +08:00
|
|
|
options = append(options, "-o", "noappledouble")
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.NoAppleXattr {
|
2018-05-03 16:45:24 +08:00
|
|
|
options = append(options, "-o", "noapplexattr")
|
|
|
|
}
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
2020-03-08 00:32:37 +08:00
|
|
|
// determine if ExtraOptions already has an opt in
|
|
|
|
hasOption := func(optionName string) bool {
|
|
|
|
optionName += "="
|
|
|
|
for _, option := range opt.ExtraOptions {
|
|
|
|
if strings.HasPrefix(option, optionName) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-09 21:03:37 +08:00
|
|
|
// Windows options
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// These cause WinFsp to mean the current user
|
2020-03-08 00:32:37 +08:00
|
|
|
if !hasOption("uid") {
|
|
|
|
options = append(options, "-o", "uid=-1")
|
|
|
|
}
|
|
|
|
if !hasOption("gid") {
|
|
|
|
options = append(options, "-o", "gid=-1")
|
|
|
|
}
|
2017-05-10 16:20:09 +08:00
|
|
|
options = append(options, "--FileSystemName=rclone")
|
2017-05-09 21:03:37 +08:00
|
|
|
}
|
|
|
|
|
2018-11-23 04:41:05 +08:00
|
|
|
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.VolumeName != "" {
|
|
|
|
options = append(options, "-o", "volname="+opt.VolumeName)
|
2018-11-23 04:41:05 +08:00
|
|
|
}
|
2018-10-21 18:05:20 +08:00
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.AllowNonEmpty {
|
2017-05-03 05:36:11 +08:00
|
|
|
options = append(options, "-o", "nonempty")
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.AllowOther {
|
2017-05-03 05:36:11 +08:00
|
|
|
options = append(options, "-o", "allow_other")
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.AllowRoot {
|
2017-05-03 05:36:11 +08:00
|
|
|
options = append(options, "-o", "allow_root")
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.DefaultPermissions {
|
2017-05-03 05:36:11 +08:00
|
|
|
options = append(options, "-o", "default_permissions")
|
|
|
|
}
|
2020-07-23 00:58:49 +08:00
|
|
|
if VFS.Opt.ReadOnly {
|
2017-05-03 05:36:11 +08:00
|
|
|
options = append(options, "-o", "ro")
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.WritebackCache {
|
2017-05-03 05:36:11 +08:00
|
|
|
// FIXME? options = append(options, "-o", WritebackCache())
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
if opt.DaemonTimeout != 0 {
|
|
|
|
options = append(options, "-o", fmt.Sprintf("daemon_timeout=%d", int(opt.DaemonTimeout.Seconds())))
|
2018-07-18 23:21:35 +08:00
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
for _, option := range opt.ExtraOptions {
|
2017-05-09 21:24:07 +08:00
|
|
|
options = append(options, "-o", option)
|
|
|
|
}
|
2020-07-24 00:17:01 +08:00
|
|
|
for _, option := range opt.ExtraFlags {
|
2017-05-10 16:13:46 +08:00
|
|
|
options = append(options, option)
|
|
|
|
}
|
2017-05-03 05:36:11 +08:00
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2017-11-17 18:31:23 +08:00
|
|
|
// waitFor runs fn() until it returns true or the timeout expires
|
|
|
|
func waitFor(fn func() bool) (ok bool) {
|
|
|
|
const totalWait = 10 * time.Second
|
|
|
|
const individualWait = 10 * time.Millisecond
|
|
|
|
for i := 0; i < int(totalWait/individualWait); i++ {
|
|
|
|
ok = fn()
|
|
|
|
if ok {
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
time.Sleep(individualWait)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:36:11 +08:00
|
|
|
// 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) {
|
2020-07-23 00:58:49 +08:00
|
|
|
f := VFS.Fs()
|
2017-05-03 05:36:11 +08:00
|
|
|
fs.Debugf(f, "Mounting on %q", mountpoint)
|
|
|
|
|
2019-04-30 20:06:24 +08:00
|
|
|
// Check the mountpoint - in Windows the mountpoint mustn't exist before the mount
|
2017-05-08 03:48:17 +08:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
fi, err := os.Stat(mountpoint)
|
|
|
|
if err != nil {
|
2020-07-23 00:58:49 +08:00
|
|
|
return nil, nil, errors.Wrap(err, "mountpoint")
|
2017-05-08 03:48:17 +08:00
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
2020-07-23 00:58:49 +08:00
|
|
|
return nil, nil, errors.New("mountpoint is not a directory")
|
2017-05-08 03:48:17 +08:00
|
|
|
}
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create underlying FS
|
2020-07-23 00:58:49 +08:00
|
|
|
fsys := NewFS(VFS)
|
2017-05-03 05:36:11 +08:00
|
|
|
host := fuse.NewFileSystemHost(fsys)
|
2020-07-05 18:28:30 +08:00
|
|
|
host.SetCapReaddirPlus(true) // only works on Windows
|
2020-05-02 01:35:32 +08:00
|
|
|
host.SetCapCaseInsensitive(f.Features().CaseInsensitive)
|
2017-05-03 05:36:11 +08:00
|
|
|
|
|
|
|
// Create options
|
2020-07-24 00:17:01 +08:00
|
|
|
options := mountOptions(VFS, f.Name()+":"+f.Root(), mountpoint, opt)
|
2017-05-03 05:36:11 +08:00
|
|
|
fs.Debugf(f, "Mounting with options: %q", options)
|
|
|
|
|
|
|
|
// Serve the mount point in the background returning error to errChan
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
go func() {
|
2020-07-05 01:54:21 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
errChan <- errors.Errorf("mount failed: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
2017-05-03 05:36:11 +08:00
|
|
|
var err error
|
|
|
|
ok := host.Mount(mountpoint, options)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New("mount failed")
|
|
|
|
fs.Errorf(f, "Mount failed")
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// unmount
|
|
|
|
unmount := func() error {
|
2017-11-08 02:03:23 +08:00
|
|
|
// Shutdown the VFS
|
|
|
|
fsys.VFS.Shutdown()
|
2017-05-03 05:36:11 +08:00
|
|
|
fs.Debugf(nil, "Calling host.Unmount")
|
|
|
|
if host.Unmount() {
|
|
|
|
fs.Debugf(nil, "host.Unmount succeeded")
|
2017-11-17 18:31:23 +08:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if !waitFor(func() bool {
|
|
|
|
_, err := os.Stat(mountpoint)
|
|
|
|
return err != nil
|
|
|
|
}) {
|
|
|
|
fs.Errorf(nil, "mountpoint %q didn't disappear after unmount - continuing anyway", mountpoint)
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 05:36:11 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fs.Debugf(nil, "host.Unmount failed")
|
|
|
|
return errors.New("host unmount failed")
|
|
|
|
}
|
|
|
|
|
2017-05-10 16:34:01 +08:00
|
|
|
// Wait for the filesystem to become ready, checking the file
|
|
|
|
// system didn't blow up before starting
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
err = errors.Wrap(err, "mount stopped before calling Init")
|
2020-07-23 00:58:49 +08:00
|
|
|
return nil, nil, err
|
2017-05-10 16:34:01 +08:00
|
|
|
case <-fsys.ready:
|
|
|
|
}
|
|
|
|
|
2017-05-10 18:16:53 +08:00
|
|
|
// Wait for the mount point to be available on Windows
|
|
|
|
// On Windows the Init signal comes slightly before the mount is ready
|
|
|
|
if runtime.GOOS == "windows" {
|
2017-11-17 18:31:23 +08:00
|
|
|
if !waitFor(func() bool {
|
2017-05-10 18:16:53 +08:00
|
|
|
_, err := os.Stat(mountpoint)
|
2017-11-17 18:31:23 +08:00
|
|
|
return err == nil
|
|
|
|
}) {
|
|
|
|
fs.Errorf(nil, "mountpoint %q didn't became available on mount - continuing anyway", mountpoint)
|
2017-05-10 18:16:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 00:58:49 +08:00
|
|
|
return errChan, unmount, nil
|
2017-05-03 05:36:11 +08:00
|
|
|
}
|