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: fix reads returning zeros after a file changes during handle caching

Neil Cawsee0fb01084d5c
diff --git a/vfs/vfscache/item.go b/vfs/vfscache/item.go
index 475f97513..f25f90734 100644
--- a/vfs/vfscache/item.go
+++ b/vfs/vfscache/item.go
@@ -579,6 +579,13 @@ func (item *Item) open(o fs.Object) (err error) {
_ = dls.Close(nil)
item.mu.Lock()
}
+ // _checkObject sized the old fd, so size the new file here
+ // or GetSize reads 0 and ReadAt returns unfetched zeros.
+ err = item._truncateToCurrentSize()
+ if err != nil {
+ item.opens--
+ return fmt.Errorf("vfs cache item: recreate cache file failed: %w", err)
+ }
}
err = item._createFile(osPath)
diff --git a/vfs/vfscache/item_test.go b/vfs/vfscache/item_test.go
index bf75a819b..852dba46e 100644
--- a/vfs/vfscache/item_test.go
+++ b/vfs/vfscache/item_test.go
@@ -823,3 +823,35 @@ func TestItemHandleCachingReopenDuringGraceClose(t *testing.T) {
}
item.mu.Unlock()
}
+
+// Reopening in the grace period after a remote modtime change must
+// read the remote data, not zeros from an unfetched recreated file.
+func TestItemHandleCachingStaleReopenReadsRemote(t *testing.T) {
+ r, c := newItemTestCacheHandleCaching(t, 10*time.Second)
+ ctx := context.Background()
+
+ contents, obj, item := newFile(t, r, c, "existing")
+
+ require.NoError(t, item.Open(obj))
+ buf := make([]byte, len(contents))
+ n, err := item.ReadAt(buf, 0)
+ require.NoError(t, err)
+ require.Equal(t, contents, string(buf[:n]))
+ require.NoError(t, item.Close(nil))
+
+ require.NoError(t, obj.SetModTime(ctx, time.Now().Add(time.Hour)))
+ obj, err = r.Fremote.NewObject(ctx, "existing")
+ require.NoError(t, err)
+
+ require.NoError(t, item.Open(obj))
+ defer func() { require.NoError(t, item.Close(nil)) }()
+
+ size, err := item.GetSize()
+ require.NoError(t, err)
+ assert.Equal(t, int64(len(contents)), size)
+
+ buf = make([]byte, len(contents))
+ n, err = item.ReadAt(buf, 0)
+ require.NoError(t, err)
+ assert.Equal(t, contents, string(buf[:n]))
+}