From 88d830c7b7215525791a5ca7938add25278b68a9 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 26 Feb 2018 16:58:02 +0000 Subject: [PATCH] vfs: create cache.opens and use it in place of cache.get to avoid potential race --- vfs/cache.go | 14 ++++++++++++++ vfs/file.go | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/vfs/cache.go b/vfs/cache.go index 302d98dd9..b8a3a103c 100644 --- a/vfs/cache.go +++ b/vfs/cache.go @@ -161,6 +161,20 @@ func (c *cache) _get(isFile bool, name string) (item *cacheItem, found bool) { return item, found } +// opens returns the number of opens that are on the file +// +// name should be a remote path not an osPath +func (c *cache) opens(name string) int { + name = clean(name) + c.itemMu.Lock() + defer c.itemMu.Unlock() + item := c.item[name] + if item == nil { + return 0 + } + return item.opens +} + // get gets name from the cache or creates a new one // // name should be a remote path not an osPath diff --git a/vfs/file.go b/vfs/file.go index 8ac9aaac7..a50abad1f 100644 --- a/vfs/file.go +++ b/vfs/file.go @@ -431,8 +431,8 @@ func (f *File) Open(flags int) (fd Handle, err error) { // Open the correct sort of handle CacheMode := f.d.vfs.Opt.CacheMode - CacheItem := f.d.vfs.cache.get(f.Path()) - if CacheMode >= CacheModeMinimal && CacheItem.opens > 0 { + opens := f.d.vfs.cache.opens(f.Path()) + if CacheMode >= CacheModeMinimal && opens > 0 { fd, err = f.openRW(flags) } else if read && write { if CacheMode >= CacheModeMinimal {