2017-05-02 23:36:11 +02:00
|
|
|
// Package cmount implents a FUSE mounting system for rclone remotes.
|
|
|
|
//
|
|
|
|
// This uses the cgo based cgofuse library
|
|
|
|
|
2017-05-19 16:46:13 +02:00
|
|
|
// +build cmount
|
2017-05-02 23:36:11 +02:00
|
|
|
// +build cgo
|
|
|
|
// +build linux darwin freebsd windows
|
|
|
|
|
|
|
|
package cmount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-05-07 14:04:20 +02:00
|
|
|
"os/signal"
|
2017-05-02 23:36:11 +02:00
|
|
|
"runtime"
|
2017-05-07 14:04:20 +02:00
|
|
|
"syscall"
|
2017-05-02 23:36:11 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/billziss-gh/cgofuse/fuse"
|
2017-05-07 14:04:20 +02:00
|
|
|
"github.com/ncw/rclone/cmd/mountlib"
|
2017-05-02 23:36:11 +02:00
|
|
|
"github.com/ncw/rclone/fs"
|
2017-10-28 21:01:34 +02:00
|
|
|
"github.com/ncw/rclone/vfs"
|
2017-10-29 12:00:56 +01:00
|
|
|
"github.com/ncw/rclone/vfs/vfsflags"
|
2017-11-19 23:03:49 +01:00
|
|
|
"github.com/okzk/sdnotify"
|
2017-05-02 23:36:11 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-06-19 14:44:49 +02:00
|
|
|
name := "cmount"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
name = "mount"
|
|
|
|
}
|
|
|
|
mountlib.NewMountCommand(name, Mount)
|
2017-05-02 23:36:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// mountOptions configures the options from the command line flags
|
|
|
|
func mountOptions(device string, mountpoint string) (options []string) {
|
|
|
|
// Options
|
|
|
|
options = []string{
|
|
|
|
"-o", "fsname=" + device,
|
|
|
|
"-o", "subtype=rclone",
|
2017-06-19 14:44:49 +02:00
|
|
|
"-o", fmt.Sprintf("max_readahead=%d", mountlib.MaxReadAhead),
|
2018-03-02 17:39:42 +01:00
|
|
|
"-o", fmt.Sprintf("attr_timeout=%g", mountlib.AttrTimeout.Seconds()),
|
2017-11-13 18:18:47 +01: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-02 23:36:11 +02:00
|
|
|
}
|
2017-06-19 14:44:49 +02:00
|
|
|
if mountlib.DebugFUSE {
|
2017-05-02 23:36:11 +02:00
|
|
|
options = append(options, "-o", "debug")
|
|
|
|
}
|
|
|
|
|
|
|
|
// OSX options
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
options = append(options, "-o", "volname="+device)
|
|
|
|
options = append(options, "-o", "noappledouble")
|
|
|
|
options = append(options, "-o", "noapplexattr")
|
|
|
|
}
|
|
|
|
|
2017-05-09 15:03:37 +02:00
|
|
|
// Windows options
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// These cause WinFsp to mean the current user
|
|
|
|
options = append(options, "-o", "uid=-1")
|
|
|
|
options = append(options, "-o", "gid=-1")
|
2017-05-10 10:20:09 +02:00
|
|
|
options = append(options, "--FileSystemName=rclone")
|
2017-05-09 15:03:37 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 14:44:49 +02:00
|
|
|
if mountlib.AllowNonEmpty {
|
2017-05-02 23:36:11 +02:00
|
|
|
options = append(options, "-o", "nonempty")
|
|
|
|
}
|
2017-06-19 14:44:49 +02:00
|
|
|
if mountlib.AllowOther {
|
2017-05-02 23:36:11 +02:00
|
|
|
options = append(options, "-o", "allow_other")
|
|
|
|
}
|
2017-06-19 14:44:49 +02:00
|
|
|
if mountlib.AllowRoot {
|
2017-05-02 23:36:11 +02:00
|
|
|
options = append(options, "-o", "allow_root")
|
|
|
|
}
|
2017-06-19 14:44:49 +02:00
|
|
|
if mountlib.DefaultPermissions {
|
2017-05-02 23:36:11 +02:00
|
|
|
options = append(options, "-o", "default_permissions")
|
|
|
|
}
|
2017-10-29 12:00:56 +01:00
|
|
|
if vfsflags.Opt.ReadOnly {
|
2017-05-02 23:36:11 +02:00
|
|
|
options = append(options, "-o", "ro")
|
|
|
|
}
|
2017-06-19 14:44:49 +02:00
|
|
|
if mountlib.WritebackCache {
|
2017-05-02 23:36:11 +02:00
|
|
|
// FIXME? options = append(options, "-o", WritebackCache())
|
|
|
|
}
|
2017-11-07 18:09:08 +01:00
|
|
|
for _, option := range mountlib.ExtraOptions {
|
2017-05-09 15:24:07 +02:00
|
|
|
options = append(options, "-o", option)
|
|
|
|
}
|
2017-11-07 18:09:08 +01:00
|
|
|
for _, option := range mountlib.ExtraFlags {
|
2017-05-10 10:13:46 +02:00
|
|
|
options = append(options, option)
|
|
|
|
}
|
2017-05-02 23:36:11 +02:00
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2017-11-17 11:31:23 +01: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-02 23:36:11 +02: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.
|
2017-10-28 21:01:34 +02:00
|
|
|
func mount(f fs.Fs, mountpoint string) (*vfs.VFS, <-chan error, func() error, error) {
|
2017-05-02 23:36:11 +02:00
|
|
|
fs.Debugf(f, "Mounting on %q", mountpoint)
|
|
|
|
|
2017-05-10 12:16:53 +02:00
|
|
|
// Check the mountpoint - in Windows the mountpoint musn't exist before the mount
|
2017-05-07 21:48:17 +02:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
fi, err := os.Stat(mountpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, errors.Wrap(err, "mountpoint")
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil, nil, nil, errors.New("mountpoint is not a directory")
|
|
|
|
}
|
2017-05-02 23:36:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create underlying FS
|
|
|
|
fsys := NewFS(f)
|
|
|
|
host := fuse.NewFileSystemHost(fsys)
|
|
|
|
|
|
|
|
// Create options
|
|
|
|
options := mountOptions(f.Name()+":"+f.Root(), mountpoint)
|
|
|
|
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() {
|
|
|
|
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-07 19:03:23 +01:00
|
|
|
// Shutdown the VFS
|
|
|
|
fsys.VFS.Shutdown()
|
2017-05-02 23:36:11 +02:00
|
|
|
fs.Debugf(nil, "Calling host.Unmount")
|
|
|
|
if host.Unmount() {
|
|
|
|
fs.Debugf(nil, "host.Unmount succeeded")
|
2017-11-17 11:31:23 +01: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-02 23:36:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fs.Debugf(nil, "host.Unmount failed")
|
|
|
|
return errors.New("host unmount failed")
|
|
|
|
}
|
|
|
|
|
2017-05-10 10:34:01 +02: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")
|
|
|
|
return nil, nil, nil, err
|
|
|
|
case <-fsys.ready:
|
|
|
|
}
|
|
|
|
|
2017-05-10 12:16:53 +02: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 11:31:23 +01:00
|
|
|
if !waitFor(func() bool {
|
2017-05-10 12:16:53 +02:00
|
|
|
_, err := os.Stat(mountpoint)
|
2017-11-17 11:31:23 +01:00
|
|
|
return err == nil
|
|
|
|
}) {
|
|
|
|
fs.Errorf(nil, "mountpoint %q didn't became available on mount - continuing anyway", mountpoint)
|
2017-05-10 12:16:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 21:01:34 +02:00
|
|
|
return fsys.VFS, errChan, unmount, nil
|
2017-05-02 23:36:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount mounts the remote at mountpoint.
|
|
|
|
//
|
|
|
|
// If noModTime is set then it
|
|
|
|
func Mount(f fs.Fs, mountpoint string) error {
|
|
|
|
// Mount it
|
2017-05-07 14:04:20 +02:00
|
|
|
FS, errChan, _, err := mount(f, mountpoint)
|
2017-05-02 23:36:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to mount FUSE fs")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note cgofuse unmounts the fs on SIGINT etc
|
|
|
|
|
2017-05-07 14:04:20 +02:00
|
|
|
sigHup := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigHup, syscall.SIGHUP)
|
|
|
|
|
2017-11-19 23:03:49 +01:00
|
|
|
if err := sdnotify.SdNotifyReady(); err != nil && err != sdnotify.SdNotifyNoSocket {
|
|
|
|
return errors.Wrap(err, "failed to notify systemd")
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:04:20 +02:00
|
|
|
waitloop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// umount triggered outside the app
|
|
|
|
case err = <-errChan:
|
|
|
|
break waitloop
|
|
|
|
// user sent SIGHUP to clear the cache
|
|
|
|
case <-sigHup:
|
|
|
|
root, err := FS.Root()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(f, "Error reading root: %v", err)
|
|
|
|
} else {
|
|
|
|
root.ForgetAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-19 23:03:49 +01:00
|
|
|
_ = sdnotify.SdNotifyStopping()
|
2017-05-02 23:36:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to umount FUSE fs")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|