vfs: create cache.opens and use it in place of cache.get to avoid potential race

This commit is contained in:
Nick Craig-Wood 2018-02-26 16:58:02 +00:00
parent 724120d2f3
commit 88d830c7b7
2 changed files with 16 additions and 2 deletions

View File

@ -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

View File

@ -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 {