From 694dae2ed712dccac3b76b3aba6d078720ebce68 Mon Sep 17 00:00:00 2001 From: nielash Date: Fri, 16 Oct 2026 11:37:13 -0400 Subject: [PATCH] vfs: fix renamed items listed twice after a case or Unicode-only match Before this change, renaming a file or directory through a name that matched it only by ignoring case (`--vfs-case-insensitive`, the default on macOS and Windows) or Unicode normalization left its old entry in the parent directory. The item was listed twice, once under its new name and once under the old key, and the old name still resolved to it until the directory cache expired. `Dir.Rename` removed the parent's entry under the name it was given rather than the item's real name. For a directory renamed within its own parent, this was masked until `36ce1a28f`, which removed a second deletion in `Dir.rename`. After this change, `Dir.Rename` removes the entry under the item's real name. The issue was discovered on the bisync integration tests. --- vfs/dir.go | 4 +++- vfs/vfs_case_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/vfs/dir.go b/vfs/dir.go index 1d9d47609..111c0995b 100644 --- a/vfs/dir.go +++ b/vfs/dir.go @@ -1223,6 +1223,8 @@ func (d *Dir) Rename(oldName, newName string, destDir *Dir) error { fs.Errorf(oldPath, "Dir.Rename error: %v", err) return err } + // oldName may only match oldNode with case or unicode normalization + oldLeaf := oldNode.Name() switch x := oldNode.DirEntry().(type) { case nil: if oldFile, ok := oldNode.(*File); ok { @@ -1274,7 +1276,7 @@ func (d *Dir) Rename(oldName, newName string, destDir *Dir) error { } // Show moved - delete from old dir and add to new - d.delObject(oldName) + d.delObject(oldLeaf) destDir.addObject(oldNode) if err = d.SetModTime(time.Now()); err != nil { fs.Errorf(d, "Dir.Rename failed to set modtime on parent dir: %v", err) diff --git a/vfs/vfs_case_test.go b/vfs/vfs_case_test.go index 7e634ad4a..428afb37c 100644 --- a/vfs/vfs_case_test.go +++ b/vfs/vfs_case_test.go @@ -195,3 +195,42 @@ func TestUnicodeNormalization(t *testing.T) { assertFileDataVFS(t, vfs, norm.NFD.String(both), "data1") assertFileAbsentVFS(t, vfs, nfd) } + +func TestRenameByNormalizedName(t *testing.T) { + r := fstest.NewRun(t) + features := r.Fremote.Features() + if features.DirMove == nil && features.Move == nil && features.Copy == nil { + t.Skip("can't rename directories") + } + + var ( + nfc = norm.NFC.String(norm.NFD.String("ěáñ")) + nfd = norm.NFD.String(nfc) + ) + + ctx := context.Background() + r.WriteObject(ctx, "FiLeA", "data1", t1) + r.WriteObject(ctx, nfc+".txt", "data2", t2) + + opt := vfscommon.Opt + opt.CaseInsensitive = true + vfs := New(ctx, r.Fremote, &opt) + defer cleanupVFS(t, vfs) + + root, err := vfs.Root() + require.NoError(t, err) + _, err = root.Mkdir("DiRa") + require.NoError(t, err) + _, err = root.Mkdir(nfc) + require.NoError(t, err) + + for _, names := range [][2]string{ + {"FILEA", "file1"}, + {nfd + ".txt", "file2"}, + {"DIRA", "dir1"}, + {nfd, "dir2"}, + } { + require.NoError(t, root.Rename(names[0], names[1], root)) + } + checkListing(t, root, []string{"dir1,0,true", "dir2,0,true", "file1,5,false", "file2,5,false"}) +}