mirror of
https://github.com/rclone/rclone.git
synced 2024-12-25 00:19:13 +01:00
8d5bc7f28b
Before this change, if the cache was given a source `remote:file` it
stored `remote:` with the error `fs.ErrorIsFile` attached. This meant
that if it `remote:` was subsequently looked up it would return the
`fs.ErrorIsFile` error.
This broke `moveto remote:file remote:file2` as moveto would lookup
`remote:` from the second argument and erroneously get the
`fs.ErrorIsFile` error.
This likely broke other commands too.
This was broken in
4c9836035
fs/cache: Add Pin and Unpin and canonicalised lookup
Which was released in v1.52.0
The fix is to make a new cache entry for `remote:` with no error
attached in the case that the original call returned `fs.ErrorIsFile`.
104 lines
2.7 KiB
Go
104 lines
2.7 KiB
Go
// Package cache implements the Fs cache
|
|
package cache
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
"github.com/rclone/rclone/lib/cache"
|
|
)
|
|
|
|
var (
|
|
c = cache.New()
|
|
mu sync.Mutex // mutex to protect remap
|
|
remap = map[string]string{} // map user supplied names to canonical names
|
|
)
|
|
|
|
// Canonicalize looks up fsString in the mapping from user supplied
|
|
// names to canonical names and return the canonical form
|
|
func Canonicalize(fsString string) string {
|
|
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()
|
|
}
|
|
|
|
// GetFn gets an fs.Fs named fsString either from the cache or creates
|
|
// it afresh with the create function
|
|
func GetFn(fsString string, create func(fsString string) (fs.Fs, error)) (f fs.Fs, err error) {
|
|
fsString = Canonicalize(fsString)
|
|
created := false
|
|
value, err := c.Get(fsString, func(fsString string) (f interface{}, ok bool, err error) {
|
|
f, err = create(fsString)
|
|
ok = err == nil || err == fs.ErrorIsFile
|
|
created = ok
|
|
return f, ok, err
|
|
})
|
|
if err != nil && err != fs.ErrorIsFile {
|
|
return nil, err
|
|
}
|
|
f = value.(fs.Fs)
|
|
// Check we stored the Fs at the canonical name
|
|
if created {
|
|
canonicalName := fs.ConfigString(f)
|
|
if canonicalName != fsString {
|
|
// 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)
|
|
}
|
|
}
|
|
}
|
|
return f, err
|
|
}
|
|
|
|
// Pin f into the cache until Unpin is called
|
|
func Pin(f fs.Fs) {
|
|
c.Pin(fs.ConfigString(f))
|
|
}
|
|
|
|
// Unpin f from the cache
|
|
func Unpin(f fs.Fs) {
|
|
c.Pin(fs.ConfigString(f))
|
|
}
|
|
|
|
// Get gets an fs.Fs named fsString either from the cache or creates it afresh
|
|
func Get(fsString string) (f fs.Fs, err error) {
|
|
return GetFn(fsString, fs.NewFs)
|
|
}
|
|
|
|
// Put puts an fs.Fs named fsString into the cache
|
|
func Put(fsString string, f fs.Fs) {
|
|
canonicalName := fs.ConfigString(f)
|
|
c.Put(canonicalName, f)
|
|
addMapping(fsString, canonicalName)
|
|
}
|
|
|
|
// Clear removes everything from the cache
|
|
func Clear() {
|
|
c.Clear()
|
|
}
|