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.
This commit is contained in:
nielash authored and Nick Craig-Wood committed 2026-10-18 17:26:27 +01:00
1 parent a22df73dec
commit 694dae2ed7
2 files changed
+42 -1

No files matched your search

+3 -1
View File
@@ -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)
+39
View File
@@ -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"})
}