dnd kitten: Fix failure to start in a non-writable directory

Create the temporary directory used for staging drops lazily, on the first
drop, instead of at startup, so the kitten works in directories that are not
writable when nothing is dropped into them.

Fixes #10855
This commit is contained in:
Kovid Goyal 2026-11-12 18:54:46 +05:30
parent 1eaecbcead
commit b9e7070cc8
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 60 additions and 6 deletions

View file

@ -154,6 +154,9 @@ Detailed list of changes
- ssh kitten: Fix remote directories such as :file:`~/.terminfo` being created
world writable (:iss:`10834`)
- :doc:`dnd kitten <kittens/dnd>`: Fix the kitten failing to start in a directory
that is not writable (:iss:`10855`)
- :doc:`choose-files <kittens/choose-files>`: Fix hangs previewing special files
and various preview caching bugs (:pull:`10843`, :pull:`10850`)

View file

@ -810,6 +810,9 @@ func (dnd *dnd) drop_on_wakeup() error {
}
func (dnd *dnd) new_tdir() (dir_file *os.File, err error) {
if err = dnd.ensure_base_tempdir(); err != nil {
return nil, err
}
dnd.tdir_counter++
name := strconv.Itoa(dnd.tdir_counter)
return utils.CreateDirAt(dnd.base_tempdir, name, 0o700)

View file

@ -1414,7 +1414,7 @@ func equalStringSlices(a, b []string) bool {
return true
}
// TestSetupBaseDirDetectsNameAliases verifies that setup_base_dir detects
// TestSetupBaseDirDetectsNameAliases verifies that ensure_base_tempdir 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) {
@ -1432,6 +1432,12 @@ func TestSetupBaseDirDetectsNameAliases(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if d.base_tempdir != nil {
t.Fatal("base temporary directory created before it was needed")
}
if err = d.ensure_base_tempdir(); 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)
}
@ -1443,3 +1449,31 @@ func TestSetupBaseDirDetectsNameAliases(t *testing.T) {
t.Errorf("probe files left in the base temporary directory: %v", entries)
}
}
// TestSetupBaseDirInUnwritableDir verifies that the kitten can start in a
// directory that is not writable, failing only if a drop is attempted.
func TestSetupBaseDirInUnwritableDir(t *testing.T) {
dest := t.TempDir()
if err := os.Chmod(dest, 0o500); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chmod(dest, 0o700) })
if f, err := os.Create(filepath.Join(dest, "probe")); err == nil {
f.Close()
t.Skip("directory permissions are not enforced, probably running as root")
}
d := &dnd{}
if err := d.setup_base_dir(dest); err != nil {
t.Fatalf("setup_base_dir failed for unwritable directory: %v", err)
}
defer d.drop_output_dir.Close()
if d.base_tempdir != nil {
t.Fatal("base temporary directory created eagerly")
}
if _, err := d.new_tdir(); err == nil {
t.Fatal("creating a staging directory in an unwritable directory unexpectedly succeeded")
}
if err := d.remove_tdir(); err != nil {
t.Fatal(err)
}
}

View file

@ -120,21 +120,32 @@ func (dnd *dnd) setup_base_dir(base_dir string) (err error) {
if dnd.drop_output_dir != nil {
dnd.drop_output_dir.Close()
}
if dnd.drop_output_dir, err = os.Open(base_dir); err != nil {
return err
dnd.drop_output_dir, err = os.Open(base_dir)
return err
}
// Create the base temporary directory used for staging drops. This is done
// lazily, on the first drop, so that the kitten works in directories that are
// not writable when no drop into them is performed.
func (dnd *dnd) ensure_base_tempdir() (err error) {
if dnd.base_tempdir != nil {
return nil
}
base_dir := dnd.drop_output_dir.Name()
base_tdir, err := os.MkdirTemp(base_dir, ".dnd-kitten-drop-*")
if err != nil {
return err
return fmt.Errorf("failed to create temporary directory for drop in %s with error: %w", base_dir, err)
}
bf, err := os.Open(base_tdir)
if err != nil {
os.RemoveAll(base_tdir)
if rerr := os.RemoveAll(base_tdir); rerr != nil {
return errors.Join(err, rerr)
}
return err
}
dnd.base_tempdir = bf
dnd.is_case_sensitive_filesystem = names_are_distinct(base_tdir)
return err
return nil
}
// Return true if names that differ only in case or Unicode normalization
@ -157,6 +168,9 @@ func names_are_distinct(dir string) bool {
}
func (dnd *dnd) remove_tdir() error {
if dnd.base_tempdir == nil {
return nil
}
path := dnd.base_tempdir.Name()
dnd.base_tempdir.Close()
dnd.base_tempdir = nil