From 077b45322d892ce72dee253decaa83db57598a38 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 3 Aug 2019 14:50:48 +0100 Subject: [PATCH] vfs: fix --vfs-cache-mode minimal,writes ignoring cached files Before this change, with --vfs-cache-mode minimal,writes if files were opened they would always be read from the remote, regardless of whether they were in the cache or not. This change checks to see if the file is in the cache when opening a file with --vfs-cache-mode >= minimal and if so then it uses it from the cache. This makes --vfs-cache-mode writes in particular much more efficient. No longer is a file uploaded (with write mode) then immediately downloaded (with read only mode). Fixes #3330 --- vfs/cache.go | 13 +++++++++++++ vfs/file.go | 4 +--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/vfs/cache.go b/vfs/cache.go index 87b0b3104..acb51138a 100644 --- a/vfs/cache.go +++ b/vfs/cache.go @@ -263,6 +263,19 @@ func (c *cache) cacheDir(name string) { } } +// exists checks to see if the file exists in the cache or not +func (c *cache) exists(name string) bool { + osPath := c.toOSPath(name) + fi, err := os.Stat(osPath) + if err != nil { + return false + } + if fi.IsDir() { + return false + } + return true +} + // _close marks name as closed - must be called with the lock held func (c *cache) _close(isFile bool, name string) { for { diff --git a/vfs/file.go b/vfs/file.go index 0622f4cf9..a28480235 100644 --- a/vfs/file.go +++ b/vfs/file.go @@ -546,11 +546,9 @@ func (f *File) Open(flags int) (fd Handle, err error) { write = true } - // FIXME discover if file is in cache or not? - // Open the correct sort of handle CacheMode := f.d.vfs.Opt.CacheMode - if CacheMode >= CacheModeMinimal && f.d.vfs.cache.opens(f.Path()) > 0 { + if CacheMode >= CacheModeMinimal && (f.d.vfs.cache.opens(f.Path()) > 0 || f.d.vfs.cache.exists(f.Path())) { fd, err = f.openRW(flags) } else if read && write { if CacheMode >= CacheModeMinimal {