Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

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

Nick Craig-Wood88d830c7b721
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 {