mountlib: refactor before adding serve docker (#5415)
Co-authored-by: Ivan Andreev <ivandeex@gmail.com>
This commit is contained in:
parent
aab29353d1
commit
221dfc3882
9 changed files with 611 additions and 511 deletions
|
@ -11,29 +11,33 @@ import (
|
|||
"github.com/rclone/rclone/fs"
|
||||
"github.com/rclone/rclone/fs/rc"
|
||||
"github.com/rclone/rclone/vfs"
|
||||
"github.com/rclone/rclone/vfs/vfscommon"
|
||||
"github.com/rclone/rclone/vfs/vfsflags"
|
||||
)
|
||||
|
||||
// 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"`
|
||||
MountOpt *Options
|
||||
VFSOpt *vfscommon.Options
|
||||
}
|
||||
|
||||
var (
|
||||
// mutex to protect all the variables in this block
|
||||
mountMu sync.Mutex
|
||||
// Mount functions available
|
||||
mountFns = map[string]MountFn{}
|
||||
// Map of mounted path => MountInfo
|
||||
liveMounts = map[string]MountInfo{}
|
||||
liveMounts = map[string]*MountPoint{}
|
||||
// Supported mount types
|
||||
supportedMountTypes = []string{"mount", "cmount", "mount2"}
|
||||
)
|
||||
|
||||
// ResolveMountMethod returns mount function by name
|
||||
func ResolveMountMethod(mountType string) (string, MountFn) {
|
||||
if mountType != "" {
|
||||
return mountType, mountFns[mountType]
|
||||
}
|
||||
for _, mountType := range supportedMountTypes {
|
||||
if mountFns[mountType] != nil {
|
||||
return mountType, mountFns[mountType]
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// AddRc adds mount and unmount functionality to rc
|
||||
func AddRc(mountUtilName string, mountFunction MountFn) {
|
||||
mountMu.Lock()
|
||||
|
@ -99,14 +103,12 @@ func mountRc(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
|||
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"
|
||||
}
|
||||
if err != nil {
|
||||
mountType = ""
|
||||
}
|
||||
mountType, mountFn := ResolveMountMethod(mountType)
|
||||
if mountFn == nil {
|
||||
return nil, errors.New("Mount Option specified is not registered, or is invalid")
|
||||
}
|
||||
|
||||
// Get Fs.fs to be mounted from fs parameter in the params
|
||||
|
@ -115,28 +117,26 @@ func mountRc(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if mountFns[mountType] != nil {
|
||||
VFS := vfs.New(fdst, &vfsOpt)
|
||||
_, unmountFn, err := mountFns[mountType](VFS, mountPoint, &mountOpt)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("mount FAILED: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
// Add mount to list if mount point was successfully created
|
||||
liveMounts[mountPoint] = MountInfo{
|
||||
unmountFn: unmountFn,
|
||||
MountedOn: time.Now(),
|
||||
Fs: fdst.Name(),
|
||||
MountPoint: mountPoint,
|
||||
VFSOpt: &vfsOpt,
|
||||
MountOpt: &mountOpt,
|
||||
}
|
||||
|
||||
fs.Debugf(nil, "Mount for %s created at %s using %s", fdst.String(), mountPoint, mountType)
|
||||
return nil, nil
|
||||
VFS := vfs.New(fdst, &vfsOpt)
|
||||
_, unmountFn, err := mountFn(VFS, mountPoint, &mountOpt)
|
||||
if err != nil {
|
||||
log.Printf("mount FAILED: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New("Mount Option specified is not registered, or is invalid")
|
||||
|
||||
// Add mount to list if mount point was successfully created
|
||||
liveMounts[mountPoint] = &MountPoint{
|
||||
MountPoint: mountPoint,
|
||||
MountedOn: time.Now(),
|
||||
MountFn: mountFn,
|
||||
UnmountFn: unmountFn,
|
||||
MountOpt: mountOpt,
|
||||
VFSOpt: vfsOpt,
|
||||
Fs: fdst,
|
||||
}
|
||||
|
||||
fs.Debugf(nil, "Mount for %s created at %s using %s", fdst.String(), mountPoint, mountType)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -169,10 +169,14 @@ func unMountRc(_ context.Context, in rc.Params) (out rc.Params, err error) {
|
|||
}
|
||||
mountMu.Lock()
|
||||
defer mountMu.Unlock()
|
||||
err = performUnMount(mountPoint)
|
||||
if err != nil {
|
||||
mountInfo, found := liveMounts[mountPoint]
|
||||
if !found {
|
||||
return nil, errors.New("mount not found")
|
||||
}
|
||||
if err = mountInfo.Unmount(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
delete(liveMounts, mountPoint)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
@ -231,16 +235,34 @@ Eg
|
|||
})
|
||||
}
|
||||
|
||||
// listMountsRc returns a list of current mounts
|
||||
// MountInfo is a transitional structure for json marshaling
|
||||
type MountInfo struct {
|
||||
Fs string `json:"Fs"`
|
||||
MountPoint string `json:"MountPoint"`
|
||||
MountedOn time.Time `json:"MountedOn"`
|
||||
}
|
||||
|
||||
// listMountsRc returns a list of current mounts sorted by mount path
|
||||
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)
|
||||
var keys []string
|
||||
for key := range liveMounts {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
mountPoints := []MountInfo{}
|
||||
for _, k := range keys {
|
||||
m := liveMounts[k]
|
||||
info := MountInfo{
|
||||
Fs: m.Fs.Name(),
|
||||
MountPoint: m.MountPoint,
|
||||
MountedOn: m.MountedOn,
|
||||
}
|
||||
mountPoints = append(mountPoints, info)
|
||||
}
|
||||
return rc.Params{
|
||||
"mountPoints": mountTypes,
|
||||
"mountPoints": mountPoints,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -265,27 +287,12 @@ Eg
|
|||
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)
|
||||
for mountPoint, mountInfo := range liveMounts {
|
||||
if err = mountInfo.Unmount(); err != nil {
|
||||
fs.Debugf(nil, "Couldn't unmount : %s", mountPoint)
|
||||
return nil, err
|
||||
}
|
||||
delete(liveMounts, mountPoint)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue