vfs: pin the Fs in use in the Fs cache

This means we can reliably look up the Fs from the cache when using
`backend/command`.
This commit is contained in:
Nick Craig-Wood 2020-05-01 12:53:31 +01:00
parent 4c98360356
commit 70db13e6e8

View File

@ -34,6 +34,7 @@ import (
"time" "time"
"github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/cache"
"github.com/rclone/rclone/fs/log" "github.com/rclone/rclone/fs/log"
"github.com/rclone/rclone/vfs/vfscache" "github.com/rclone/rclone/vfs/vfscache"
"github.com/rclone/rclone/vfs/vfscommon" "github.com/rclone/rclone/vfs/vfscommon"
@ -204,6 +205,11 @@ func New(f fs.Fs, opt *vfscommon.Options) *VFS {
// add the remote control // add the remote control
vfs.addRC() vfs.addRC()
// Pin the Fs into the cache so that when we use cache.NewFs
// with the same remote string we get this one. The Pin is
// removed by Shutdown
cache.Pin(f)
return vfs return vfs
} }
@ -233,6 +239,8 @@ func (vfs *VFS) SetCacheMode(cacheMode vfscommon.CacheMode) {
// Shutdown stops any background go-routines // Shutdown stops any background go-routines
func (vfs *VFS) Shutdown() { func (vfs *VFS) Shutdown() {
// Unpin the Fs from the cache
cache.Unpin(vfs.f)
if vfs.cancel != nil { if vfs.cancel != nil {
vfs.cancel() vfs.cancel()
vfs.cancel = nil vfs.cancel = nil