2019-05-23 11:26:16 +00:00
|
|
|
// Package cache implements the Fs cache
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2020-11-05 15:18:51 +00:00
|
|
|
"context"
|
2020-08-31 16:46:58 +00:00
|
|
|
"runtime"
|
2020-05-01 11:49:34 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-02-19 13:13:54 +00:00
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2019-07-31 21:19:07 +00:00
|
|
|
"github.com/rclone/rclone/lib/cache"
|
2019-05-23 11:26:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-03-29 16:18:49 +00:00
|
|
|
once sync.Once // creation
|
|
|
|
c *cache.Cache
|
2020-05-01 11:49:34 +00:00
|
|
|
mu sync.Mutex // mutex to protect remap
|
|
|
|
remap = map[string]string{} // map user supplied names to canonical names
|
2019-05-23 11:26:16 +00:00
|
|
|
)
|
|
|
|
|
2021-03-29 16:18:49 +00:00
|
|
|
// Create the cache just once
|
|
|
|
func createOnFirstUse() {
|
|
|
|
once.Do(func() {
|
|
|
|
ci := fs.GetConfig(context.Background())
|
|
|
|
c = cache.New()
|
|
|
|
c.SetExpireDuration(ci.FsCacheExpireDuration)
|
|
|
|
c.SetExpireInterval(ci.FsCacheExpireInterval)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-15 13:02:04 +00:00
|
|
|
// Canonicalize looks up fsString in the mapping from user supplied
|
|
|
|
// names to canonical names and return the canonical form
|
|
|
|
func Canonicalize(fsString string) string {
|
2021-03-29 16:18:49 +00:00
|
|
|
createOnFirstUse()
|
2020-05-01 11:49:34 +00:00
|
|
|
mu.Lock()
|
|
|
|
canonicalName, ok := remap[fsString]
|
|
|
|
mu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return fsString
|
|
|
|
}
|
|
|
|
fs.Debugf(nil, "fs cache: switching user supplied name %q for canonical name %q", fsString, canonicalName)
|
|
|
|
return canonicalName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put in a mapping from fsString => canonicalName if they are different
|
|
|
|
func addMapping(fsString, canonicalName string) {
|
|
|
|
if canonicalName == fsString {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mu.Lock()
|
|
|
|
remap[fsString] = canonicalName
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:39:20 +00:00
|
|
|
// GetFn gets an fs.Fs named fsString either from the cache or creates
|
2019-07-31 21:19:07 +00:00
|
|
|
// it afresh with the create function
|
2020-11-05 15:18:51 +00:00
|
|
|
func GetFn(ctx context.Context, fsString string, create func(ctx context.Context, fsString string) (fs.Fs, error)) (f fs.Fs, err error) {
|
2021-03-29 16:18:49 +00:00
|
|
|
createOnFirstUse()
|
2021-03-30 16:47:44 +00:00
|
|
|
canonicalFsString := Canonicalize(fsString)
|
2020-05-01 11:49:34 +00:00
|
|
|
created := false
|
2021-03-30 16:47:44 +00:00
|
|
|
value, err := c.Get(canonicalFsString, func(canonicalFsString string) (f interface{}, ok bool, err error) {
|
|
|
|
f, err = create(ctx, fsString) // always create the backend with the original non-canonicalised string
|
2019-07-31 21:19:07 +00:00
|
|
|
ok = err == nil || err == fs.ErrorIsFile
|
2020-05-01 11:49:34 +00:00
|
|
|
created = ok
|
2019-07-31 21:19:07 +00:00
|
|
|
return f, ok, err
|
|
|
|
})
|
2019-08-09 21:19:41 +00:00
|
|
|
if err != nil && err != fs.ErrorIsFile {
|
2019-07-31 21:19:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 11:49:34 +00:00
|
|
|
f = value.(fs.Fs)
|
|
|
|
// Check we stored the Fs at the canonical name
|
|
|
|
if created {
|
|
|
|
canonicalName := fs.ConfigString(f)
|
2021-03-30 16:47:44 +00:00
|
|
|
if canonicalName != canonicalFsString {
|
2020-07-02 09:15:47 +00:00
|
|
|
// Note that if err == fs.ErrorIsFile at this moment
|
|
|
|
// then we can't rename the remote as it will have the
|
|
|
|
// wrong error status, we need to add a new one.
|
|
|
|
if err == nil {
|
2021-03-30 16:47:44 +00:00
|
|
|
fs.Debugf(nil, "fs cache: renaming cache item %q to be canonical %q", canonicalFsString, canonicalName)
|
|
|
|
value, found := c.Rename(canonicalFsString, canonicalName)
|
2020-07-02 09:15:47 +00:00
|
|
|
if found {
|
|
|
|
f = value.(fs.Fs)
|
|
|
|
}
|
2021-03-30 16:47:44 +00:00
|
|
|
addMapping(canonicalFsString, canonicalName)
|
2020-07-02 09:15:47 +00:00
|
|
|
} else {
|
2021-03-30 16:47:44 +00:00
|
|
|
fs.Debugf(nil, "fs cache: adding new entry for parent of %q, %q", canonicalFsString, canonicalName)
|
2020-07-02 09:15:47 +00:00
|
|
|
Put(canonicalName, f)
|
2020-05-01 11:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pin f into the cache until Unpin is called
|
|
|
|
func Pin(f fs.Fs) {
|
2021-03-29 16:18:49 +00:00
|
|
|
createOnFirstUse()
|
2020-05-01 11:49:34 +00:00
|
|
|
c.Pin(fs.ConfigString(f))
|
|
|
|
}
|
|
|
|
|
2020-08-31 16:46:58 +00:00
|
|
|
// PinUntilFinalized pins f into the cache until x is garbage collected
|
|
|
|
//
|
|
|
|
// This calls runtime.SetFinalizer on x so it shouldn't have a
|
|
|
|
// finalizer already.
|
|
|
|
func PinUntilFinalized(f fs.Fs, x interface{}) {
|
|
|
|
Pin(f)
|
|
|
|
runtime.SetFinalizer(x, func(_ interface{}) {
|
|
|
|
Unpin(f)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-01 11:49:34 +00:00
|
|
|
// Unpin f from the cache
|
|
|
|
func Unpin(f fs.Fs) {
|
2021-03-29 16:18:49 +00:00
|
|
|
createOnFirstUse()
|
2020-05-01 11:49:34 +00:00
|
|
|
c.Pin(fs.ConfigString(f))
|
2019-05-23 11:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 10:39:20 +00:00
|
|
|
// Get gets an fs.Fs named fsString either from the cache or creates it afresh
|
2020-11-05 15:18:51 +00:00
|
|
|
func Get(ctx context.Context, fsString string) (f fs.Fs, err error) {
|
2021-02-19 13:13:54 +00:00
|
|
|
// If we are making a long lived backend which lives longer
|
|
|
|
// than this request, we want to disconnect it from the
|
|
|
|
// current context and in particular any WithCancel contexts,
|
|
|
|
// but we want to preserve the config embedded in the context.
|
|
|
|
newCtx := context.Background()
|
|
|
|
newCtx = fs.CopyConfig(newCtx, ctx)
|
|
|
|
newCtx = filter.CopyConfig(newCtx, ctx)
|
|
|
|
return GetFn(newCtx, fsString, fs.NewFs)
|
2019-05-23 11:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 03:04:29 +00:00
|
|
|
// GetArr gets []fs.Fs from []fsStrings either from the cache or creates it afresh
|
|
|
|
func GetArr(ctx context.Context, fsStrings []string) (f []fs.Fs, err error) {
|
|
|
|
var fArr []fs.Fs
|
|
|
|
for _, fsString := range fsStrings {
|
|
|
|
f1, err1 := GetFn(ctx, fsString, fs.NewFs)
|
|
|
|
if err1 != nil {
|
|
|
|
return fArr, err1
|
|
|
|
}
|
|
|
|
fArr = append(fArr, f1)
|
|
|
|
}
|
|
|
|
return fArr, nil
|
|
|
|
}
|
|
|
|
|
2019-05-23 11:26:16 +00:00
|
|
|
// Put puts an fs.Fs named fsString into the cache
|
|
|
|
func Put(fsString string, f fs.Fs) {
|
2021-03-29 16:18:49 +00:00
|
|
|
createOnFirstUse()
|
2020-05-01 11:49:34 +00:00
|
|
|
canonicalName := fs.ConfigString(f)
|
|
|
|
c.Put(canonicalName, f)
|
|
|
|
addMapping(fsString, canonicalName)
|
2019-05-23 11:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 11:52:25 +00:00
|
|
|
// ClearConfig deletes all entries which were based on the config name passed in
|
|
|
|
//
|
|
|
|
// Returns number of entries deleted
|
|
|
|
func ClearConfig(name string) (deleted int) {
|
2021-03-29 16:18:49 +00:00
|
|
|
createOnFirstUse()
|
2020-11-30 11:52:25 +00:00
|
|
|
return c.DeletePrefix(name + ":")
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:39:20 +00:00
|
|
|
// Clear removes everything from the cache
|
2019-05-23 11:26:16 +00:00
|
|
|
func Clear() {
|
2021-03-29 16:18:49 +00:00
|
|
|
createOnFirstUse()
|
2019-07-31 21:19:07 +00:00
|
|
|
c.Clear()
|
2019-05-23 11:26:16 +00:00
|
|
|
}
|
2020-11-29 11:25:51 +00:00
|
|
|
|
|
|
|
// Entries returns the number of entries in the cache
|
|
|
|
func Entries() int {
|
2021-03-29 16:18:49 +00:00
|
|
|
createOnFirstUse()
|
2020-11-29 11:25:51 +00:00
|
|
|
return c.Entries()
|
|
|
|
}
|