From 924dacc9556781ef6d224c46cec0a5ded3138572 Mon Sep 17 00:00:00 2001 From: Zhizhen He Date: Thu, 29 Oct 2026 23:55:24 +0800 Subject: [PATCH] dnd: Detect filesystem aliases before overwrite confirmation --- docs/changelog.rst | 3 +++ kittens/dnd/drop.go | 25 ++++++++++----------- kittens/dnd/drop_test.go | 34 +++++++++++++++++++++++++++++ kitty_tests/dnd_kitten.py | 46 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 13 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 55868dac6..96ff9d8fb 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -406,6 +406,9 @@ Detailed list of changes one of its subdirectories before staging files, preventing recursive copying of the staging directory (:pull:`10769`) +- Drag and drop kitten: Respect overwrite confirmation for case and Unicode + equivalent file and directory names on filesystems that treat them as aliases + - Clipboard kitten: Fix a race when replacing an existing output file and losing the original file if writing it fails. Also preserve the permissions of and follow symlinks at existing output files (:pull:`10738`) diff --git a/kittens/dnd/drop.go b/kittens/dnd/drop.go index e9b88e596..ddce7e1c2 100644 --- a/kittens/dnd/drop.go +++ b/kittens/dnd/drop.go @@ -270,25 +270,24 @@ func find_overwrites(src_dir *os.File, dest_dir *os.File) (ans []string, err err return err } defer dd.Close() - dest_children, err := dd.ReadDir(0) src_children, err := sd.ReadDir(0) if err != nil { return err } - dest_map := make(map[string]os.DirEntry) - for _, x := range dest_children { - dest_map[x.Name()] = x - } for _, x := range src_children { - if d, found := dest_map[x.Name()]; found { - crelpath := utils.IfElse(relpath == ".", x.Name(), filepath.Join(relpath, x.Name())) - if !d.IsDir() || !x.IsDir() { - ans = append(ans, crelpath) - } else { - stack.push(crelpath) - } - } else { + // Let the destination filesystem resolve case and Unicode aliases, + // just as it does when renaming the staged entries into place. + d, err := utils.LstatAt(dd, x.Name()) + if errors.Is(err, fs.ErrNotExist) { continue + } else if err != nil { + return err + } + crelpath := utils.IfElse(relpath == ".", x.Name(), filepath.Join(relpath, x.Name())) + if !d.IsDir() || !x.IsDir() { + ans = append(ans, crelpath) + } else { + stack.push(crelpath) } } return diff --git a/kittens/dnd/drop_test.go b/kittens/dnd/drop_test.go index d7b389678..834a6642d 100644 --- a/kittens/dnd/drop_test.go +++ b/kittens/dnd/drop_test.go @@ -666,6 +666,40 @@ func TestFindOverwrites_FileOverlap(t *testing.T) { } } +func TestFindOverwritesFilesystemAliases(t *testing.T) { + for _, tc := range []struct{ name, src, dest string }{ + {"case", "report.txt", "Report.txt"}, + {"unicode", "cafe\u0301.txt", "caf\u00e9.txt"}, + {"directory", "folder/file.txt", "Folder/file.txt"}, + } { + t.Run(tc.name, func(t *testing.T) { + src, dest := t.TempDir(), t.TempDir() + buildTree(t, src, map[string]string{tc.src: "incoming"}, nil) + buildTree(t, dest, map[string]string{tc.dest: "original"}, nil) + alias, err := os.Stat(filepath.Join(dest, tc.src)) + if os.IsNotExist(err) { + t.Skip("destination filesystem does not alias these names") + } else if err != nil { + t.Fatal(err) + } + original, err := os.Stat(filepath.Join(dest, tc.dest)) + if err != nil { + t.Fatal(err) + } + if !os.SameFile(alias, original) { + t.Fatal("names do not refer to the same destination file") + } + got, err := find_overwrites(openDir(t, src), openDir(t, dest)) + if err != nil { + t.Fatal(err) + } + if len(got) != 1 || got[0] != tc.src { + t.Errorf("got overwrites %q, want [%q]", got, tc.src) + } + }) + } +} + // TestFindOverwrites_DirsNotReported verifies that matching *directories* in // both trees are not reported as overwrites — only non-directory conflicts are. func TestFindOverwrites_DirsNotReported(t *testing.T) { diff --git a/kitty_tests/dnd_kitten.py b/kitty_tests/dnd_kitten.py index 17512b667..9a4402534 100644 --- a/kitty_tests/dnd_kitten.py +++ b/kitty_tests/dnd_kitten.py @@ -210,6 +210,52 @@ class TestDnDKitten(BaseTest): self.dnd_kitten_drop(True, img_drop_path) self.exit_kitten() + def test_dnd_kitten_overwrite_filesystem_aliases(self): + self.finish_setup(cli_args=('--confirm-drop-overwrite',)) + aliases = ( + ('case', 'report.txt', 'Report.txt'), + ('unicode', 'cafe\u0301.txt', 'caf\u00e9.txt'), + ('directory', 'folder/file.txt', 'Folder/file.txt'), + ) + for (name, src_rel, dest_rel), remote, action, accepted in itertools.product( + aliases, (False, True), (GLFW_DRAG_OPERATION_COPY, GLFW_DRAG_OPERATION_MOVE), (False, True) + ): + with self.subTest(alias=name, remote=remote, action=action, accepted=accepted): + self.reset_kitten(remote) + src = os.path.join(self.src_data_dir, src_rel) + dest = os.path.join(self.kitten_wd, dest_rel) + for path, content in ((src, b'incoming'), (dest, b'original')): + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, 'wb') as f: + f.write(content) + alias = os.path.join(self.kitten_wd, src_rel) + if not os.path.exists(alias): + self.skipTest(f'The destination filesystem does not alias {src_rel!r} to {dest_rel!r}') + self.assertTrue(os.path.samefile(alias, dest)) + copy, move = self.get_button_geometry() + button = copy if action == GLFW_DRAG_OPERATION_COPY else move + dnd_test_fake_drop_event(self.capture.window_id, False, ['text/uri-list'], button[0] + 1, button[1] + 1) + self.wait_for_state('drop_action', action) + dnd_test_fake_drop_event(self.capture.window_id, True, ['text/uri-list'], button[0] + 1, button[1] + 1) + self.wait_for_state('drop_data_requests', ((1, 0, 0),)) + uri = as_file_url(self.src_data_dir, src_rel.split('/')[0]) + dnd_test_fake_drop_data(self.capture.window_id, 'text/uri-list', (uri + '\r\n').encode()) + self.pty.wait_till(lambda: 'overwrite existing' in self.pty.screen_contents() or self.probe_state('drop_action') == 0) + with open(dest, 'rb') as f: + self.assertEqual(b'original', f.read(), 'destination changed before overwrite confirmation') + self.assertIn('overwrite existing', self.pty.screen_contents()) + self.pty.write_to_child('\x1b[13u' if accepted else '\x1b[27u') + self.wait_for_state('last_drop_action', action if accepted else 0) + self.wait_for_state('drop_action', 0) + with open(dest, 'rb') as f: + self.assertEqual(b'incoming' if accepted else b'original', f.read()) + if accepted and action == GLFW_DRAG_OPERATION_MOVE and not remote: + self.assertFalse(os.path.exists(src)) + else: + with open(src, 'rb') as f: + self.assertEqual(b'incoming', f.read()) + self.exit_kitten() + def dnd_kitten_drop(self, remote_client, img_drop_path): copy, move = self.get_button_geometry() all_mimes = 'text/uri-list a/b c/d'