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 crash renaming a directory while the dir cache expires - fixes #10071

Shreyansh Jain36ce1a28fe8d
diff --git a/vfs/dir.go b/vfs/dir.go
index 9e1c437a3..465b2bc03 100644
--- a/vfs/dir.go
+++ b/vfs/dir.go
@@ -390,6 +390,9 @@ func (d *Dir) renameTree(dirPath string) {
//
// Reset the directory to new state, discarding all the objects and
// reading everything again
+//
+// The caller must move the entry between the old and the new parent's
+// directory entries itself, holding each parent's lock as it does so.
func (d *Dir) rename(newParent *Dir, fsDir fs.Directory) {
d.ForgetAll()
@@ -402,8 +405,6 @@ func (d *Dir) rename(newParent *Dir, fsDir fs.Directory) {
d.entry = fsDir
d.path = fsDir.Remote()
newPath := d.path
- delete(d.parent.items, name(oldPath))
- d.parent.items[name(d.path)] = d
d.read = time.Time{}
d.mu.Unlock()
diff --git a/vfs/dir_test.go b/vfs/dir_test.go
index 6a869f655..c3ac395bb 100644
--- a/vfs/dir_test.go
+++ b/vfs/dir_test.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "maps"
"os"
"runtime"
"slices"
@@ -857,3 +858,29 @@ func TestDirMetadataExtension(t *testing.T) {
require.NoError(t, err)
}
}
+
+// Check that renaming a directory leaves its parent's directory entries alone,
+// so it can't corrupt the parent's map while the parent is iterating it.
+func TestDirRenameParentEntries(t *testing.T) {
+ _, vfs := newTestVFS(t)
+
+ root, err := vfs.Root()
+ require.NoError(t, err)
+ dir, err := root.Mkdir("dir")
+ require.NoError(t, err)
+
+ // Hold the root's read lock, as ForgetAll and ReadDirAll do while they
+ // iterate the root's entries, and rename the directory underneath it.
+ root.mu.RLock()
+ defer root.mu.RUnlock()
+ before := slices.Sorted(maps.Keys(root.items))
+
+ done := make(chan struct{})
+ go func() {
+ defer close(done)
+ dir.rename(root, fs.NewDir("dir2", t1))
+ }()
+ <-done
+
+ assert.Equal(t, before, slices.Sorted(maps.Keys(root.items)))
+}