rclone/cmd/mountlib/rc.go

262 lines
6.1 KiB
Go
Raw Normal View History

2020-04-25 13:03:07 +08:00
package mountlib
import (
"context"
"log"
"sort"
"sync"
"time"
2020-04-25 13:03:07 +08:00
"github.com/pkg/errors"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/rc"
)
// MountInfo defines the configuration for a mount
type MountInfo struct {
unmountFn UnmountFn
MountPoint string `json:"MountPoint"`
MountedOn time.Time `json:"MountedOn"`
Fs string `json:"Fs"`
}
2020-04-25 13:03:07 +08:00
var (
// mutex to protect all the variables in this block
mountMu sync.Mutex
2020-04-25 13:03:07 +08:00
// Mount functions available
mountFns = map[string]MountFn{}
// Map of mounted path => MountInfo
liveMounts = map[string]MountInfo{}
2020-04-25 13:03:07 +08:00
)
// AddRc adds mount and unmount functionality to rc
func AddRc(mountUtilName string, mountFunction MountFn) {
mountMu.Lock()
defer mountMu.Unlock()
// rcMount allows the mount command to be run from rc
mountFns[mountUtilName] = mountFunction
}
2020-04-25 13:03:07 +08:00
func init() {
rc.Add(rc.Call{
Path: "mount/mount",
AuthRequired: true,
Fn: mountRc,
Title: "Create a new mount point",
Help: `rclone allows Linux, FreeBSD, macOS and Windows to mount any of
Rclone's cloud storage systems as a file system with FUSE.
If no mountType is provided, the priority is given as follows: 1. mount 2.cmount 3.mount2
2020-04-25 13:03:07 +08:00
This takes the following parameters
- fs - a remote path to be mounted (required)
- mountPoint: valid path on the local machine (required)
- mountType: One of the values (mount, cmount, mount2) specifies the mount implementation to use
2020-04-25 13:03:07 +08:00
Eg
rclone rc mount/mount fs=mydrive: mountPoint=/home/<user>/mountPoint
rclone rc mount/mount fs=mydrive: mountPoint=/home/<user>/mountPoint mountType=mount
2020-04-25 13:03:07 +08:00
`,
})
}
// mountRc allows the mount command to be run from rc
func mountRc(_ context.Context, in rc.Params) (out rc.Params, err error) {
mountPoint, err := in.GetString("mountPoint")
if err != nil {
return nil, err
}
mountType, err := in.GetString("mountType")
mountMu.Lock()
defer mountMu.Unlock()
if err != nil || mountType == "" {
if mountFns["mount"] != nil {
mountType = "mount"
} else if mountFns["cmount"] != nil {
mountType = "cmount"
} else if mountFns["mount2"] != nil {
mountType = "mount2"
}
}
// Get Fs.fs to be mounted from fs parameter in the params
fdst, err := rc.GetFs(in)
if err != nil {
return nil, err
}
2020-04-25 13:03:07 +08:00
if mountFns[mountType] != nil {
_, _, unmountFn, err := mountFns[mountType](fdst, mountPoint)
liveMounts[mountPoint] = MountInfo{
unmountFn: unmountFn,
MountedOn: time.Now(),
Fs: fdst.Name(),
MountPoint: mountPoint,
}
if err != nil {
log.Printf("mount FAILED: %v", err)
return nil, err
}
fs.Debugf(nil, "Mount for %s created at %s using %s", fdst.String(), mountPoint, mountType)
return nil, nil
}
return nil, errors.New("Mount Option specified is not registered, or is invalid")
}
func init() {
2020-04-25 13:03:07 +08:00
rc.Add(rc.Call{
Path: "mount/unmount",
AuthRequired: true,
Fn: unMountRc,
Title: "Unmount selected active mount",
2020-04-25 13:03:07 +08:00
Help: `
rclone allows Linux, FreeBSD, macOS and Windows to
mount any of Rclone's cloud storage systems as a file system with
FUSE.
This takes the following parameters
- mountPoint: valid path on the local machine where the mount was created (required)
Eg
rclone rc mount/unmount mountPoint=/home/<user>/mountPoint
`,
})
}
// unMountRc allows the umount command to be run from rc
2020-04-25 13:03:07 +08:00
func unMountRc(_ context.Context, in rc.Params) (out rc.Params, err error) {
mountPoint, err := in.GetString("mountPoint")
if err != nil {
return nil, err
}
mountMu.Lock()
defer mountMu.Unlock()
err = performUnMount(mountPoint)
if err != nil {
return nil, err
}
return nil, nil
}
2020-04-25 13:03:07 +08:00
func init() {
rc.Add(rc.Call{
Path: "mount/types",
AuthRequired: true,
Fn: mountTypesRc,
Title: "Show all possible mount types",
Help: `This shows all possible mount types and returns them as a list.
2020-04-25 13:03:07 +08:00
This takes no parameters and returns
2020-04-25 13:03:07 +08:00
- mountTypes: list of mount types
2020-04-25 13:03:07 +08:00
The mount types are strings like "mount", "mount2", "cmount" and can
be passed to mount/mount as the mountType parameter.
2020-04-25 13:03:07 +08:00
Eg
rclone rc mount/types
`,
})
}
// mountTypesRc returns a list of available mount types.
func mountTypesRc(_ context.Context, in rc.Params) (out rc.Params, err error) {
var mountTypes = []string{}
mountMu.Lock()
defer mountMu.Unlock()
for mountType := range mountFns {
mountTypes = append(mountTypes, mountType)
2020-04-25 13:03:07 +08:00
}
sort.Strings(mountTypes)
return rc.Params{
"mountTypes": mountTypes,
}, nil
2020-04-25 13:03:07 +08:00
}
func init() {
rc.Add(rc.Call{
Path: "mount/listmounts",
AuthRequired: true,
Fn: listMountsRc,
Title: "Show current mount points",
Help: `This shows currently mounted points, which can be used for performing an unmount
This takes no parameters and returns
- mountPoints: list of current mount points
Eg
rclone rc mount/listmounts
`,
})
}
// listMountsRc returns a list of current mounts
func listMountsRc(_ context.Context, in rc.Params) (out rc.Params, err error) {
var mountTypes = []MountInfo{}
mountMu.Lock()
defer mountMu.Unlock()
for _, a := range liveMounts {
mountTypes = append(mountTypes, a)
}
return rc.Params{
"mountPoints": mountTypes,
}, nil
}
func init() {
rc.Add(rc.Call{
Path: "mount/unmountall",
AuthRequired: true,
Fn: unmountAll,
Title: "Show current mount points",
Help: `This shows currently mounted points, which can be used for performing an unmount
This takes no parameters and returns error if unmount does not succeed.
Eg
rclone rc mount/unmountall
`,
})
}
// unmountAll unmounts all the created mounts
func unmountAll(_ context.Context, in rc.Params) (out rc.Params, err error) {
mountMu.Lock()
defer mountMu.Unlock()
for key, mountInfo := range liveMounts {
err = performUnMount(mountInfo.MountPoint)
if err != nil {
fs.Debugf(nil, "Couldn't unmount : %s", key)
return nil, err
}
}
return nil, nil
}
// performUnMount unmounts the specified mountPoint
func performUnMount(mountPoint string) (err error) {
mountInfo, ok := liveMounts[mountPoint]
if ok {
err := mountInfo.unmountFn()
if err != nil {
return err
}
delete(liveMounts, mountPoint)
} else {
return errors.New("mount not found")
}
return nil
}