From cb88028abdacb2919b4dbd0ce3e801b2ef85da9e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 29 Oct 2026 21:46:25 +0530 Subject: [PATCH] dnd kitten: Fix filesystem case sensitivity never being detected is_case_sensitive_filesystem lost its true default when it moved into the dnd struct, so remote drops always treated names differing only in case as conflicting. Detect it properly and also probe for Unicode normalization aliasing, since a case sensitive filesystem can still treat differently normalized names as the same. --- docs/changelog.rst | 4 +++- kittens/dnd/drop_test.go | 30 ++++++++++++++++++++++++++++++ kittens/dnd/main.go | 27 +++++++++++++++++++++++---- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 6ef4078ac..297432e29 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -410,7 +410,9 @@ Detailed list of changes equivalent file and directory names on filesystems that treat them as aliases. Also list the existing files that will be overwritten under their actual names, including files inside symlinked directories, and fix dropping - a directory over an existing file (:pull:`10772`) + a directory over an existing file. Also no longer rename remotely dropped + files whose names differ only in case on case sensitive filesystems + (:pull:`10772`) - Clipboard kitten: Fix a race when replacing an existing output file and losing the original file if writing it fails. Also preserve the permissions diff --git a/kittens/dnd/drop_test.go b/kittens/dnd/drop_test.go index 91156218c..bacb68d40 100644 --- a/kittens/dnd/drop_test.go +++ b/kittens/dnd/drop_test.go @@ -1122,3 +1122,33 @@ func equalStringSlices(a, b []string) bool { } return true } + +// TestSetupBaseDirDetectsNameAliases verifies that setup_base_dir detects +// whether the filesystem treats names differing in case or Unicode +// normalization as aliases and leaves no probe files behind. +func TestSetupBaseDirDetectsNameAliases(t *testing.T) { + dest := t.TempDir() + buildTree(t, dest, map[string]string{"Probe": "", "é": ""}, nil) + _, cerr := os.Lstat(filepath.Join(dest, "probe")) + _, nerr := os.Lstat(filepath.Join(dest, "é")) + want := os.IsNotExist(cerr) && os.IsNotExist(nerr) + for _, name := range []string{"Probe", "é"} { + if err := os.Remove(filepath.Join(dest, name)); err != nil { + t.Fatal(err) + } + } + d, err := runLocalDrop(t, dest, nil, copy_on_drop, "") + if err != nil { + t.Fatal(err) + } + if d.is_case_sensitive_filesystem != want { + t.Errorf("is_case_sensitive_filesystem: got %v, want %v", d.is_case_sensitive_filesystem, want) + } + entries, err := os.ReadDir(d.base_tempdir.Name()) + if err != nil { + t.Fatal(err) + } + if len(entries) != 0 { + t.Errorf("probe files left in the base temporary directory: %v", entries) + } +} diff --git a/kittens/dnd/main.go b/kittens/dnd/main.go index 463d3a4c4..5035c246d 100644 --- a/kittens/dnd/main.go +++ b/kittens/dnd/main.go @@ -4,9 +4,11 @@ package dnd import ( "bytes" + "errors" "fmt" "image" "io" + "io/fs" "maps" "net/url" "os" @@ -90,7 +92,7 @@ type dnd struct { drop_output_dir *os.File base_tempdir *os.File tdir_counter int - is_case_sensitive_filesystem bool + is_case_sensitive_filesystem bool // false if names differing in case or Unicode normalization are aliases num_dropped_files int drag_status drag_status in_test_mode bool @@ -131,12 +133,29 @@ func (dnd *dnd) setup_base_dir(base_dir string) (err error) { return err } dnd.base_tempdir = bf - if _, serr := os.Stat(filepath.Join(base_dir, strings.ToUpper(filepath.Base(base_tdir)))); serr == nil { - dnd.is_case_sensitive_filesystem = false - } + dnd.is_case_sensitive_filesystem = names_are_distinct(base_tdir) return err } +// Return true if names that differ only in case or Unicode normalization +// refer to different entries in the empty directory dir. Any failure to +// determine this is treated as them being aliases, which is the safe choice. +func names_are_distinct(dir string) bool { + is_absent := func(path string) bool { + _, err := os.Lstat(path) + return errors.Is(err, fs.ErrNotExist) + } + if !is_absent(filepath.Join(filepath.Dir(dir), strings.ToUpper(filepath.Base(dir)))) { + return false + } + probe := filepath.Join(dir, "\u00e9") + if err := os.WriteFile(probe, nil, 0o600); err != nil { + return false + } + defer os.Remove(probe) + return is_absent(filepath.Join(dir, "e\u0301")) +} + func (dnd *dnd) remove_tdir() error { path := dnd.base_tempdir.Name() dnd.base_tempdir.Close()