2018-10-26 13:48:22 +00:00
|
|
|
// This implements the Fs cache
|
|
|
|
|
|
|
|
package rc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fsCacheMu sync.Mutex
|
|
|
|
fsCache = map[string]fs.Fs{}
|
|
|
|
fsNewFs = fs.NewFs // for tests
|
|
|
|
)
|
|
|
|
|
2018-10-27 17:29:20 +00:00
|
|
|
// GetCachedFs gets a fs.Fs named fsString either from the cache or creates it afresh
|
|
|
|
func GetCachedFs(fsString string) (f fs.Fs, err error) {
|
2018-10-26 13:48:22 +00:00
|
|
|
fsCacheMu.Lock()
|
|
|
|
defer fsCacheMu.Unlock()
|
|
|
|
|
|
|
|
f = fsCache[fsString]
|
|
|
|
if f == nil {
|
|
|
|
f, err = fsNewFs(fsString)
|
|
|
|
if err == nil {
|
|
|
|
fsCache[fsString] = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
2018-10-27 17:29:20 +00:00
|
|
|
// GetFsNamed gets a fs.Fs named fsName either from the cache or creates it afresh
|
|
|
|
func GetFsNamed(in Params, fsName string) (f fs.Fs, err error) {
|
|
|
|
fsString, err := in.GetString(fsName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetCachedFs(fsString)
|
|
|
|
}
|
|
|
|
|
2018-10-26 13:48:22 +00:00
|
|
|
// GetFs gets a fs.Fs named "fs" either from the cache or creates it afresh
|
|
|
|
func GetFs(in Params) (f fs.Fs, err error) {
|
|
|
|
return GetFsNamed(in, "fs")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFsAndRemoteNamed gets the fsName parameter from in, makes a
|
|
|
|
// remote or fetches it from the cache then gets the remoteName
|
|
|
|
// parameter from in too.
|
|
|
|
func GetFsAndRemoteNamed(in Params, fsName, remoteName string) (f fs.Fs, remote string, err error) {
|
|
|
|
remote, err = in.GetString(remoteName)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, err = GetFsNamed(in, fsName)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFsAndRemote gets the `fs` parameter from in, makes a remote or
|
|
|
|
// fetches it from the cache then gets the `remote` parameter from in
|
|
|
|
// too.
|
|
|
|
func GetFsAndRemote(in Params) (f fs.Fs, remote string, err error) {
|
|
|
|
return GetFsAndRemoteNamed(in, "fs", "remote")
|
|
|
|
}
|