forked from TrueCloudLab/rclone
vfs: Only make the VFS cache if --vfs-cache-mode > Off
This stops the cache cleaner running unnecessarily and saves resources. This also helps with issue #2227 which was caused by a second mount deleting objects in the first mounts cache.
This commit is contained in:
parent
3d5106e52b
commit
2b7957cc74
4 changed files with 33 additions and 18 deletions
|
@ -215,7 +215,7 @@ func (r *Run) cacheMode(cacheMode vfs.CacheMode) {
|
||||||
log.Printf("Failed to cleanup the VFS cache: %v", err)
|
log.Printf("Failed to cleanup the VFS cache: %v", err)
|
||||||
}
|
}
|
||||||
// Reset the cache mode
|
// Reset the cache mode
|
||||||
r.vfs.Opt.CacheMode = cacheMode
|
r.vfs.SetCacheMode(cacheMode)
|
||||||
// Flush the directory cache
|
// Flush the directory cache
|
||||||
r.vfs.FlushDirCache()
|
r.vfs.FlushDirCache()
|
||||||
|
|
||||||
|
|
|
@ -481,8 +481,7 @@ func (f *File) Open(flags int) (fd Handle, err error) {
|
||||||
|
|
||||||
// Open the correct sort of handle
|
// Open the correct sort of handle
|
||||||
CacheMode := f.d.vfs.Opt.CacheMode
|
CacheMode := f.d.vfs.Opt.CacheMode
|
||||||
opens := f.d.vfs.cache.opens(f.Path())
|
if CacheMode >= CacheModeMinimal && f.d.vfs.cache.opens(f.Path()) > 0 {
|
||||||
if CacheMode >= CacheModeMinimal && opens > 0 {
|
|
||||||
fd, err = f.openRW(flags)
|
fd, err = f.openRW(flags)
|
||||||
} else if read && write {
|
} else if read && write {
|
||||||
if CacheMode >= CacheModeMinimal {
|
if CacheMode >= CacheModeMinimal {
|
||||||
|
|
|
@ -21,8 +21,9 @@ func cleanup(t *testing.T, r *fstest.Run, vfs *VFS) {
|
||||||
|
|
||||||
// Open a file for write
|
// Open a file for write
|
||||||
func rwHandleCreateReadOnly(t *testing.T, r *fstest.Run) (*VFS, *RWFileHandle) {
|
func rwHandleCreateReadOnly(t *testing.T, r *fstest.Run) (*VFS, *RWFileHandle) {
|
||||||
vfs := New(r.Fremote, nil)
|
opt := DefaultOpt
|
||||||
vfs.Opt.CacheMode = CacheModeFull
|
opt.CacheMode = CacheModeFull
|
||||||
|
vfs := New(r.Fremote, &opt)
|
||||||
|
|
||||||
file1 := r.WriteObject("dir/file1", "0123456789abcdef", t1)
|
file1 := r.WriteObject("dir/file1", "0123456789abcdef", t1)
|
||||||
fstest.CheckItems(t, r.Fremote, file1)
|
fstest.CheckItems(t, r.Fremote, file1)
|
||||||
|
@ -37,8 +38,9 @@ func rwHandleCreateReadOnly(t *testing.T, r *fstest.Run) (*VFS, *RWFileHandle) {
|
||||||
|
|
||||||
// Open a file for write
|
// Open a file for write
|
||||||
func rwHandleCreateWriteOnly(t *testing.T, r *fstest.Run) (*VFS, *RWFileHandle) {
|
func rwHandleCreateWriteOnly(t *testing.T, r *fstest.Run) (*VFS, *RWFileHandle) {
|
||||||
vfs := New(r.Fremote, nil)
|
opt := DefaultOpt
|
||||||
vfs.Opt.CacheMode = CacheModeFull
|
opt.CacheMode = CacheModeFull
|
||||||
|
vfs := New(r.Fremote, &opt)
|
||||||
|
|
||||||
h, err := vfs.OpenFile("file1", os.O_WRONLY|os.O_CREATE, 0777)
|
h, err := vfs.OpenFile("file1", os.O_WRONLY|os.O_CREATE, 0777)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -555,10 +557,11 @@ func testRWFileHandleOpenTest(t *testing.T, vfs *VFS, test *openTest) {
|
||||||
|
|
||||||
func TestRWFileHandleOpenTests(t *testing.T) {
|
func TestRWFileHandleOpenTests(t *testing.T) {
|
||||||
r := fstest.NewRun(t)
|
r := fstest.NewRun(t)
|
||||||
vfs := New(r.Fremote, nil)
|
opt := DefaultOpt
|
||||||
|
opt.CacheMode = CacheModeFull
|
||||||
|
vfs := New(r.Fremote, &opt)
|
||||||
defer cleanup(t, r, vfs)
|
defer cleanup(t, r, vfs)
|
||||||
|
|
||||||
vfs.Opt.CacheMode = CacheModeFull
|
|
||||||
for _, test := range openTests {
|
for _, test := range openTests {
|
||||||
testRWFileHandleOpenTest(t, vfs, &test)
|
testRWFileHandleOpenTest(t, vfs, &test)
|
||||||
}
|
}
|
||||||
|
|
31
vfs/vfs.go
31
vfs/vfs.go
|
@ -224,21 +224,31 @@ func New(f fs.Fs, opt *Options) *VFS {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the cache
|
vfs.SetCacheMode(vfs.Opt.CacheMode)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
vfs.cancel = cancel
|
|
||||||
cache, err := newCache(ctx, f, &vfs.Opt) // FIXME pass on context or get from Opt?
|
|
||||||
if err != nil {
|
|
||||||
// FIXME
|
|
||||||
panic(fmt.Sprintf("failed to create local cache: %v", err))
|
|
||||||
}
|
|
||||||
vfs.cache = cache
|
|
||||||
|
|
||||||
// add the remote control
|
// add the remote control
|
||||||
vfs.addRC()
|
vfs.addRC()
|
||||||
return vfs
|
return vfs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetCacheMode change the cache mode
|
||||||
|
func (vfs *VFS) SetCacheMode(cacheMode CacheMode) {
|
||||||
|
vfs.Shutdown()
|
||||||
|
vfs.cache = nil
|
||||||
|
if vfs.Opt.CacheMode > CacheModeOff {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cache, err := newCache(ctx, vfs.f, &vfs.Opt) // FIXME pass on context or get from Opt?
|
||||||
|
if err != nil {
|
||||||
|
fs.Errorf(nil, "Failed to create vfs cache - disabling: %v", err)
|
||||||
|
vfs.Opt.CacheMode = CacheModeOff
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vfs.cancel = cancel
|
||||||
|
vfs.cache = cache
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Shutdown stops any background go-routines
|
// Shutdown stops any background go-routines
|
||||||
func (vfs *VFS) Shutdown() {
|
func (vfs *VFS) Shutdown() {
|
||||||
if vfs.cancel != nil {
|
if vfs.cancel != nil {
|
||||||
|
@ -249,6 +259,9 @@ func (vfs *VFS) Shutdown() {
|
||||||
|
|
||||||
// CleanUp deletes the contents of the on disk cache
|
// CleanUp deletes the contents of the on disk cache
|
||||||
func (vfs *VFS) CleanUp() error {
|
func (vfs *VFS) CleanUp() error {
|
||||||
|
if vfs.Opt.CacheMode == CacheModeOff {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return vfs.cache.cleanUp()
|
return vfs.cache.cleanUp()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue