2019-05-23 13:26:16 +02:00
|
|
|
// Package cache implements the Fs cache
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:18:51 +01:00
|
|
|
"context"
|
2020-08-31 18:46:58 +02:00
|
|
|
"runtime"
|
2020-05-01 13:49:34 +02:00
|
|
|
"sync"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-02-19 14:13:54 +01:00
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2019-07-31 23:19:07 +02:00
|
|
|
"github.com/rclone/rclone/lib/cache"
|
2019-05-23 13:26:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-03-29 18:18:49 +02:00
|
|
|
once sync.Once // creation
|
|
|
|
c *cache.Cache
|
2020-05-01 13:49:34 +02:00
|
|
|
mu sync.Mutex // mutex to protect remap
|
|
|
|
remap = map[string]string{} // map user supplied names to canonical names
|
2019-05-23 13:26:16 +02:00
|
|
|
)
|
|
|
|
|
2021-03-29 18:18:49 +02: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 15:02:04 +02: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 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2020-05-01 13:49:34 +02: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 12:39:20 +02:00
|
|
|
// GetFn gets an fs.Fs named fsString either from the cache or creates
|
2019-07-31 23:19:07 +02:00
|
|
|
// it afresh with the create function
|
2020-11-05 16:18:51 +01: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 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2020-06-15 15:02:04 +02:00
|
|
|
fsString = Canonicalize(fsString)
|
2020-05-01 13:49:34 +02:00
|
|
|
created := false
|
2019-08-09 23:19:41 +02:00
|
|
|
value, err := c.Get(fsString, func(fsString string) (f interface{}, ok bool, err error) {
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err = create(ctx, fsString)
|
2019-07-31 23:19:07 +02:00
|
|
|
ok = err == nil || err == fs.ErrorIsFile
|
2020-05-01 13:49:34 +02:00
|
|
|
created = ok
|
2019-07-31 23:19:07 +02:00
|
|
|
return f, ok, err
|
|
|
|
})
|
2019-08-09 23:19:41 +02:00
|
|
|
if err != nil && err != fs.ErrorIsFile {
|
2019-07-31 23:19:07 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 13:49:34 +02:00
|
|
|
f = value.(fs.Fs)
|
|
|
|
// Check we stored the Fs at the canonical name
|
|
|
|
if created {
|
|
|
|
canonicalName := fs.ConfigString(f)
|
|
|
|
if canonicalName != fsString {
|
2020-07-02 11:15:47 +02: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 {
|
|
|
|
fs.Debugf(nil, "fs cache: renaming cache item %q to be canonical %q", fsString, canonicalName)
|
|
|
|
value, found := c.Rename(fsString, canonicalName)
|
|
|
|
if found {
|
|
|
|
f = value.(fs.Fs)
|
|
|
|
}
|
|
|
|
addMapping(fsString, canonicalName)
|
|
|
|
} else {
|
|
|
|
fs.Debugf(nil, "fs cache: adding new entry for parent of %q, %q", fsString, canonicalName)
|
|
|
|
Put(canonicalName, f)
|
2020-05-01 13:49:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pin f into the cache until Unpin is called
|
|
|
|
func Pin(f fs.Fs) {
|
2021-03-29 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2020-05-01 13:49:34 +02:00
|
|
|
c.Pin(fs.ConfigString(f))
|
|
|
|
}
|
|
|
|
|
2020-08-31 18:46:58 +02: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 13:49:34 +02:00
|
|
|
// Unpin f from the cache
|
|
|
|
func Unpin(f fs.Fs) {
|
2021-03-29 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2020-05-01 13:49:34 +02:00
|
|
|
c.Pin(fs.ConfigString(f))
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// Get gets an fs.Fs named fsString either from the cache or creates it afresh
|
2020-11-05 16:18:51 +01:00
|
|
|
func Get(ctx context.Context, fsString string) (f fs.Fs, err error) {
|
2021-02-19 14:13:54 +01: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 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-16 04:04:29 +01: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 13:26:16 +02:00
|
|
|
// Put puts an fs.Fs named fsString into the cache
|
|
|
|
func Put(fsString string, f fs.Fs) {
|
2021-03-29 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2020-05-01 13:49:34 +02:00
|
|
|
canonicalName := fs.ConfigString(f)
|
|
|
|
c.Put(canonicalName, f)
|
|
|
|
addMapping(fsString, canonicalName)
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 12:52:25 +01: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 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2020-11-30 12:52:25 +01:00
|
|
|
return c.DeletePrefix(name + ":")
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:39:20 +02:00
|
|
|
// Clear removes everything from the cache
|
2019-05-23 13:26:16 +02:00
|
|
|
func Clear() {
|
2021-03-29 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2019-07-31 23:19:07 +02:00
|
|
|
c.Clear()
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
2020-11-29 12:25:51 +01:00
|
|
|
|
|
|
|
// Entries returns the number of entries in the cache
|
|
|
|
func Entries() int {
|
2021-03-29 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2020-11-29 12:25:51 +01:00
|
|
|
return c.Entries()
|
|
|
|
}
|