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)
|
2022-06-28 13:51:59 +02:00
|
|
|
c.SetFinalizer(func(value interface{}) {
|
|
|
|
if s, ok := value.(fs.Shutdowner); ok {
|
|
|
|
_ = fs.CountError(s.Shutdown(context.Background()))
|
|
|
|
}
|
|
|
|
})
|
2021-03-29 18:18:49 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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()
|
2021-03-30 18:47:44 +02:00
|
|
|
canonicalFsString := Canonicalize(fsString)
|
2020-05-01 13:49:34 +02:00
|
|
|
created := false
|
2021-03-30 18:47:44 +02: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 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)
|
2021-03-30 18:47:44 +02:00
|
|
|
if canonicalName != canonicalFsString {
|
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 {
|
2021-03-30 18:47:44 +02: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 11:15:47 +02:00
|
|
|
if found {
|
|
|
|
f = value.(fs.Fs)
|
|
|
|
}
|
2021-03-30 18:47:44 +02:00
|
|
|
addMapping(canonicalFsString, canonicalName)
|
2020-07-02 11:15:47 +02:00
|
|
|
} else {
|
2021-03-30 18:47:44 +02:00
|
|
|
fs.Debugf(nil, "fs cache: adding new entry for parent of %q, %q", canonicalFsString, canonicalName)
|
2020-07-02 11:15:47 +02:00
|
|
|
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()
|
2022-06-20 13:14:58 +02:00
|
|
|
c.Unpin(fs.ConfigString(f))
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
2023-06-11 16:25:14 +02:00
|
|
|
// To avoid circular dependencies these are filled in by fs/rc/jobs/job.go
|
|
|
|
var (
|
|
|
|
// JobGetJobID for internal use only
|
|
|
|
JobGetJobID func(context.Context) (int64, bool)
|
|
|
|
// JobOnFinish for internal use only
|
|
|
|
JobOnFinish func(int64, func()) (func(), error)
|
|
|
|
)
|
|
|
|
|
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)
|
2023-06-11 16:25:14 +02:00
|
|
|
f, err = GetFn(newCtx, fsString, fs.NewFs)
|
|
|
|
if f == nil || (err != nil && err != fs.ErrorIsFile) {
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
// If this is part of an rc job then pin the backend until it finishes
|
|
|
|
if JobOnFinish != nil && JobGetJobID != nil {
|
|
|
|
if jobID, ok := JobGetJobID(ctx); ok {
|
|
|
|
// fs.Debugf(f, "Pin for job %d", jobID)
|
|
|
|
Pin(f)
|
|
|
|
_, _ = JobOnFinish(jobID, func() {
|
|
|
|
// fs.Debugf(f, "Unpin for job %d", jobID)
|
|
|
|
Unpin(f)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, err
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-19 11:34:03 +01:00
|
|
|
// PutErr puts an fs.Fs named fsString into the cache with err
|
|
|
|
func PutErr(fsString string, f fs.Fs, err error) {
|
2021-03-29 18:18:49 +02:00
|
|
|
createOnFirstUse()
|
2020-05-01 13:49:34 +02:00
|
|
|
canonicalName := fs.ConfigString(f)
|
2024-01-19 11:34:03 +01:00
|
|
|
c.PutErr(canonicalName, f, err)
|
2020-05-01 13:49:34 +02:00
|
|
|
addMapping(fsString, canonicalName)
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
2024-01-19 11:34:03 +01:00
|
|
|
// Put puts an fs.Fs named fsString into the cache
|
|
|
|
func Put(fsString string, f fs.Fs) {
|
|
|
|
PutErr(fsString, f, nil)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|