Cleanup previous PR

This commit is contained in:
Kovid Goyal 2026-10-30 14:24:59 +05:30
parent d324482f5f
commit f5c6ad38be
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 162 additions and 107 deletions

View file

@ -430,11 +430,10 @@ Detailed list of changes
write to a temporary file that is renamed into place, so an interrupted
transfer no longer leaves a truncated file behind (:pull:`10713`)
- Drag and drop kitten: Reject moves that merge into a destination symlink
pointing inside a source directory, preventing source cleanup from deleting
the dropped files. Reject copying or moving a directory into itself or
one of its subdirectories. Respect overwrite confirmation for case and
Unicode equivalent names on filesystems that treat them as aliases, list the
- Drag and drop kitten: Reject copying or moving a directory into itself or
one of its subdirectories, and moving a directory into a symlink that points
inside it, which would delete the moved files. Respect overwrite
confirmation for case and Unicode equivalent names on filesystems that treat them as aliases, list the
existing files that will be overwritten under their actual names, including
inside symlinked directories, and fix dropping a directory over an existing
file. No longer rename remotely dropped files whose names differ only in case

View file

@ -302,16 +302,22 @@ func stored_name(dir *os.File, dest_entries []os.DirEntry, name string, info os.
return name, nil
}
type overwrite_dir struct{ src, dest string }
type overwrite_dir struct {
src, dest string
// Whether dest is the root or was reached through a symlink, so that its
// actual ancestors may differ from the directories it was reached through
via_symlink bool
}
// Return a list of relative paths, as stored in dest_dir, for all entries in
// the tree rooted at src_dir that also exist in the tree rooted at dest_dir,
// except for directories that exist in both places. Symlinks to directories
// in dest_dir are treated as directories, matching rename_contents. When move
// sources are supplied, reject merge targets inside them before moving anything.
func find_overwrites(src_dir *os.File, dest_dir *os.File, move_sources ...os.FileInfo) (ans []string, err error) {
// in dest_dir are treated as directories, matching rename_contents. Fail if
// any directory that would be merged into is inside one of move_sources,
// since those are removed once the drop completes.
func find_overwrites(src_dir *os.File, dest_dir *os.File, move_sources []os.FileInfo) (ans []string, err error) {
stack := path_stack[overwrite_dir]{}
stack.push(overwrite_dir{".", "."})
stack.push(overwrite_dir{".", ".", true})
join := func(parent, name string) string {
return utils.IfElse(parent == ".", name, filepath.Join(parent, name))
}
@ -328,13 +334,21 @@ func find_overwrites(src_dir *os.File, dest_dir *os.File, move_sources ...os.Fil
return err
}
defer dd.Close()
for _, source := range move_sources {
inside, err := destination_is_within_source(source, dd)
if len(move_sources) > 0 {
// A real subdirectory of a directory that is not inside a
// source is itself inside one only if it is that source.
// Directories reached through symlinks need a full check.
var inside bool
if relpath.via_symlink {
inside, err = destination_is_within_source(dd, move_sources)
} else {
inside, err = is_any_of(dd, move_sources)
}
if err != nil {
return err
}
if inside {
return fmt.Errorf("cannot merge dropped files into move source directory %s", dd.Name())
return fmt.Errorf("cannot merge dropped files into the directory %s as it is inside a directory being moved", dd.Name())
}
}
src_children, err := sd.ReadDir(0)
@ -362,15 +376,15 @@ func find_overwrites(src_dir *os.File, dest_dir *os.File, move_sources ...os.Fil
if err != nil {
return err
}
dest_is_dir := d.IsDir()
if !dest_is_dir && d.Mode()&fs.ModeSymlink != 0 {
dest_is_dir, is_symlink := d.IsDir(), d.Mode()&fs.ModeSymlink != 0
if is_symlink {
if dest_is_dir, err = is_dir_at(dd, x.Name()); err != nil {
return err
}
}
switch {
case x.IsDir() && dest_is_dir:
stack.push(overwrite_dir{join(relpath.src, x.Name()), join(relpath.dest, name)})
stack.push(overwrite_dir{join(relpath.src, x.Name()), join(relpath.dest, name), is_symlink})
case dest_is_dir:
return fmt.Errorf("cannot replace the existing directory %s with a dropped file", join(relpath.dest, name))
default:
@ -706,30 +720,48 @@ func (dnd *dnd) count_files_in_drop() int {
return n
}
func (dnd *dnd) rename_drop_contents(staging_dir *os.File) error {
if dnd.drop_status.action == move_on_drop && !dnd.drop_status.is_remote_client {
var sources []os.FileInfo
for _, u := range dnd.drop_status.uri_list {
if u.kind != parsed_uri_file || u.path == "" {
continue
}
info, err := os.Lstat(u.path)
if err != nil {
return err
}
if info.IsDir() {
sources = append(sources, info)
}
// Return the directories that end_drop will remove once the drop succeeds
func (dnd *dnd) move_source_dirs() (ans []os.FileInfo, err error) {
if dnd.drop_status.action != move_on_drop || dnd.drop_status.is_remote_client {
return nil, nil
}
for _, u := range dnd.drop_status.uri_list {
if u.kind != parsed_uri_file || u.path == "" {
continue
}
// end_drop removes every move source. Check the entire merge tree first,
// including symlinks that may have changed while awaiting confirmation.
if len(sources) > 0 {
if _, err := find_overwrites(staging_dir, dnd.drop_output_dir, sources...); err != nil {
return err
info, err := os.Lstat(u.path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue // end_drop has nothing to remove
}
return nil, err
}
if info.IsDir() {
ans = append(ans, info)
}
}
return rename_contents(staging_dir, dnd.drop_output_dir)
return
}
// Rename the staged files into the output directory. If confirm_overwrites
// is true and existing files would be overwritten, return them instead
// without changing anything. end_drop removes the sources of a local move, so
// merging into them, for example via a symlink in the output directory, is
// rejected first.
func (dnd *dnd) place_staged_drop(staging_dir *os.File, confirm_overwrites bool) (overwrites []string, err error) {
move_sources, err := dnd.move_source_dirs()
if err != nil {
return nil, err
}
if confirm_overwrites || len(move_sources) > 0 {
if overwrites, err = find_overwrites(staging_dir, dnd.drop_output_dir, move_sources); err != nil {
return nil, err
}
if confirm_overwrites && len(overwrites) > 0 {
return overwrites, nil
}
}
return nil, rename_contents(staging_dir, dnd.drop_output_dir)
}
func (dnd *dnd) all_drop_data_received() (err error) {
@ -750,23 +782,14 @@ func (dnd *dnd) all_drop_data_received() (err error) {
}
}()
if staging_dir != nil {
if dnd.opts.ConfirmDropOverwrite {
overwrites, err := find_overwrites(staging_dir, dnd.drop_output_dir)
if err != nil {
return err
}
if len(overwrites) > 0 {
dnd.confirm_drop.overwrites = overwrites
dnd.confirm_drop.staging_dir = staging_dir
return dnd.render_screen()
}
overwrites, err := dnd.place_staged_drop(staging_dir, dnd.opts.ConfirmDropOverwrite)
if err == nil && len(overwrites) > 0 {
dnd.confirm_drop.overwrites = overwrites
dnd.confirm_drop.staging_dir = staging_dir
return dnd.render_screen()
}
err := dnd.rename_drop_contents(staging_dir)
staging_dir.Close()
if err != nil {
return err
}
return nil
return err
}
return nil
}
@ -818,17 +841,36 @@ func same_file(a, b os.FileInfo) (bool, error) {
return adev == bdev && aino == bino, nil
}
func matches_any(info os.FileInfo, candidates []os.FileInfo) (bool, error) {
for _, c := range candidates {
if same, err := same_file(info, c); err != nil || same {
return same, err
}
}
return false, nil
}
// Return whether dir is one of candidates
func is_any_of(dir *os.File, candidates []os.FileInfo) (bool, error) {
info, err := dir.Stat()
if err != nil {
return false, err
}
return matches_any(info, candidates)
}
// Return whether dest_dir is one of sources or inside one of them.
// Walk up from dest_dir via .. relative to its file descriptor rather than
// its path, so that the result is correct even if dest_dir has been moved or
// its path contains symlinks. Comparing identities handles case aliases and
// stat only requires search permission on the ancestors.
func destination_is_within_source(source_info os.FileInfo, dest_dir *os.File) (bool, error) {
func destination_is_within_source(dest_dir *os.File, sources []os.FileInfo) (bool, error) {
info, err := dest_dir.Stat()
if err != nil {
return false, err
}
for rel := ".."; ; rel = filepath.Join(rel, "..") {
if same, err := same_file(source_info, info); err != nil || same {
if same, err := matches_any(info, sources); err != nil || same {
return same, err
}
parent, err := utils.StatAt(dest_dir, rel)
@ -863,7 +905,7 @@ func (dnd *dnd) all_mime_data_dropped() (err error) {
return err
}
if source_info.IsDir() {
inside, err := destination_is_within_source(source_info, dnd.drop_output_dir)
inside, err := destination_is_within_source(dnd.drop_output_dir, []os.FileInfo{source_info})
if err != nil {
return err
}
@ -1178,7 +1220,9 @@ func (dnd *dnd) drop_confirm(accepted bool) error {
dnd.num_dropped_files = 0
}
if accepted {
if err := dnd.rename_drop_contents(staging_dir); err != nil {
// Symlinks in the output directory may have changed while awaiting
// confirmation, so place_staged_drop checks them again.
if _, err := dnd.place_staged_drop(staging_dir, false); err != nil {
dnd.end_drop(false)
return err
}

View file

@ -115,16 +115,31 @@ func runLocalDrop(t *testing.T, dest string, uris []parsed_uri, action int, copy
// Like runLocalDrop but calls before_drop, if not nil, after the kitten has
// opened dest and before the drop is processed.
func runLocalDropAfter(t *testing.T, dest string, uris []parsed_uri, action int, copy_mode string, before_drop func()) (*dnd, error) {
t.Helper()
return runLocalDropWithOptions(t, dest, uris, action, &Options{CopyMode: copy_mode}, before_drop)
}
// Like runLocalDrop but with overwrite confirmation enabled. If the drop
// would overwrite existing files, it is left awaiting confirmation.
func runLocalDropConfirming(t *testing.T, dest string, uris []parsed_uri, action int, copy_mode string) (*dnd, error) {
t.Helper()
return runLocalDropWithOptions(t, dest, uris, action, &Options{CopyMode: copy_mode, ConfirmDropOverwrite: true}, nil)
}
func runLocalDropWithOptions(t *testing.T, dest string, uris []parsed_uri, action int, opts *Options, before_drop func()) (*dnd, error) {
t.Helper()
lp, err := loop.New()
if err != nil {
t.Fatal(err)
}
d := &dnd{lp: lp, opts: &Options{CopyMode: copy_mode}, in_test_mode: true}
d := &dnd{lp: lp, opts: opts, in_test_mode: true}
if err := d.setup_base_dir(dest); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if d.confirm_drop.staging_dir != nil {
d.confirm_drop.staging_dir.Close()
}
d.reset_drop()
d.remove_tdir()
d.drop_output_dir.Close()
@ -149,42 +164,6 @@ func runLocalDropAfter(t *testing.T, dest string, uris []parsed_uri, action int,
return d, nil
}
// Stage a drop awaiting confirmation without drawing a prompt on a terminal.
func prepareLocalDropConfirmation(t *testing.T, dest string, uris []parsed_uri, action int, copy_mode string) (*dnd, error) {
t.Helper()
lp, err := loop.New()
if err != nil {
t.Fatal(err)
}
d := &dnd{lp: lp, opts: &Options{CopyMode: copy_mode, ConfirmDropOverwrite: true}, in_test_mode: true}
if err := d.setup_base_dir(dest); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if d.confirm_drop.staging_dir != nil {
d.confirm_drop.staging_dir.Close()
}
d.reset_drop()
d.remove_tdir()
d.drop_output_dir.Close()
})
d.drop_status = drop_status{action: action, reading_data: true, uri_list: uris}
staging, err := d.new_tdir()
if err != nil {
t.Fatal(err)
}
d.confirm_drop.staging_dir = staging
paths := make([]string, len(uris))
for i, u := range uris {
paths[i] = u.path
}
if err := do_local_copy(context.Background(), staging, paths, action, copy_mode); err != nil {
return d, err
}
d.confirm_drop.overwrites, err = find_overwrites(staging, d.drop_output_dir)
return d, err
}
func TestLocalMoveDestinationAliasesSource(t *testing.T) {
for _, mode := range []string{"auto", "independent"} {
for _, confirm := range []bool{false, true} {
@ -209,13 +188,13 @@ func TestLocalMoveDestinationAliasesSource(t *testing.T) {
buildTree(t, dst, nil, map[string]string{link: linkTarget})
run := runLocalDrop
if confirm {
run = prepareLocalDropConfirmation
run = runLocalDropConfirming
}
d, err := run(t, dst, uris, move_on_drop, mode)
if err == nil && confirm {
if err == nil && d.confirm_drop.staging_dir != nil {
err = d.drop_confirm(true)
}
if err == nil || !strings.Contains(err.Error(), "move source") {
if err == nil || !strings.Contains(err.Error(), "directory being moved") {
t.Errorf("expected a move source overlap error, got %v", err)
}
for name, want := range files {
@ -254,10 +233,13 @@ func TestLocalDropSafeDestinationSymlinks(t *testing.T) {
buildTree(t, dst, nil, map[string]string{"folder": linkTarget})
run := runLocalDrop
if confirm {
run = prepareLocalDropConfirmation
run = runLocalDropConfirming
}
d, err := run(t, dst, []parsed_uri{{kind: parsed_uri_file, path: filepath.Join(src, "folder")}}, action, mode)
if err == nil && confirm {
if d.confirm_drop.staging_dir == nil {
t.Fatal("expected overwrite confirmation")
}
err = d.drop_confirm(true)
}
if err != nil {
@ -290,7 +272,7 @@ func TestLocalMoveDestinationAliasAfterConfirmation(t *testing.T) {
buildTree(t, src, map[string]string{"folder/file.txt": "original"}, nil)
buildTree(t, other, map[string]string{"file.txt": "old"}, nil)
buildTree(t, dst, nil, map[string]string{"folder": other})
d, err := prepareLocalDropConfirmation(t, dst, []parsed_uri{{kind: parsed_uri_file, path: filepath.Join(src, "folder")}}, move_on_drop, mode)
d, err := runLocalDropConfirming(t, dst, []parsed_uri{{kind: parsed_uri_file, path: filepath.Join(src, "folder")}}, move_on_drop, mode)
if err != nil {
t.Fatal(err)
}
@ -304,7 +286,7 @@ func TestLocalMoveDestinationAliasAfterConfirmation(t *testing.T) {
if err := os.Symlink(filepath.Join(src, "folder"), link); err != nil {
t.Fatal(err)
}
if err := d.drop_confirm(true); err == nil || !strings.Contains(err.Error(), "move source") {
if err := d.drop_confirm(true); err == nil || !strings.Contains(err.Error(), "directory being moved") {
t.Errorf("expected a move source overlap error, got %v", err)
}
if got, err := os.ReadFile(filepath.Join(src, "folder/file.txt")); err != nil || string(got) != "original" {
@ -317,6 +299,36 @@ func TestLocalMoveDestinationAliasAfterConfirmation(t *testing.T) {
}
}
func TestLocalMoveSourceRemovedWhileAwaitingConfirmation(t *testing.T) {
tmp := t.TempDir()
src, dst := filepath.Join(tmp, "src"), filepath.Join(tmp, "dst")
buildTree(t, src, map[string]string{"a/file.txt": "a", "b/file.txt": "b"}, nil)
buildTree(t, dst, map[string]string{"a/file.txt": "old"}, nil)
uris := []parsed_uri{{kind: parsed_uri_file, path: filepath.Join(src, "a")}, {kind: parsed_uri_file, path: filepath.Join(src, "b")}}
d, err := runLocalDropConfirming(t, dst, uris, move_on_drop, "independent")
if err != nil {
t.Fatal(err)
}
if d.confirm_drop.staging_dir == nil {
t.Fatal("expected overwrite confirmation")
}
if err := os.RemoveAll(filepath.Join(src, "b")); err != nil {
t.Fatal(err)
}
// The staged copy is now the only copy of b, so the drop must still succeed
if err := d.drop_confirm(true); err != nil {
t.Fatal(err)
}
for name, want := range map[string]string{"a/file.txt": "a", "b/file.txt": "b"} {
if got, err := os.ReadFile(filepath.Join(dst, name)); err != nil || string(got) != want {
t.Errorf("destination %s: got %q, %v, want %q", name, got, err, want)
}
}
if _, err := os.Lstat(filepath.Join(src, "a")); !os.IsNotExist(err) {
t.Errorf("move source still present: %v", err)
}
}
func TestLocalMoveToSourceDirectory(t *testing.T) {
for _, copy_mode := range []string{"auto", "independent"} {
for _, alias := range []string{"none", "source", "destination"} {
@ -803,7 +815,7 @@ func TestFindOverwrites_NoOverlap(t *testing.T) {
srcDir := openDir(t, src)
dstDir := openDir(t, dst)
got, err := find_overwrites(srcDir, dstDir)
got, err := find_overwrites(srcDir, dstDir, nil)
if err != nil {
t.Fatalf("find_overwrites: %v", err)
}
@ -826,7 +838,7 @@ func TestFindOverwrites_FileOverlap(t *testing.T) {
srcDir := openDir(t, src)
dstDir := openDir(t, dst)
got, err := find_overwrites(srcDir, dstDir)
got, err := find_overwrites(srcDir, dstDir, nil)
if err != nil {
t.Fatalf("find_overwrites: %v", err)
}
@ -858,7 +870,7 @@ func TestFindOverwritesFilesystemAliases(t *testing.T) {
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))
got, err := find_overwrites(openDir(t, src), openDir(t, dest), nil)
if err != nil {
t.Fatal(err)
}
@ -894,7 +906,7 @@ func TestFindOverwrites_DirsNotReported(t *testing.T) {
srcDir := openDir(t, src)
dstDir := openDir(t, dst)
got, err := find_overwrites(srcDir, dstDir)
got, err := find_overwrites(srcDir, dstDir, nil)
if err != nil {
t.Fatalf("find_overwrites: %v", err)
}
@ -948,7 +960,7 @@ func TestFindOverwrites_NestedTree(t *testing.T) {
srcDir := openDir(t, src)
dstDir := openDir(t, dst)
got, err := find_overwrites(srcDir, dstDir)
got, err := find_overwrites(srcDir, dstDir, nil)
if err != nil {
t.Fatalf("find_overwrites: %v", err)
}
@ -975,7 +987,7 @@ func TestFindOverwrites_SymlinksTransferredAsIs(t *testing.T) {
srcDir := openDir(t, src)
dstDir := openDir(t, dst)
got, err := find_overwrites(srcDir, dstDir)
got, err := find_overwrites(srcDir, dstDir, nil)
if err != nil {
t.Fatalf("find_overwrites: %v", err)
}
@ -999,7 +1011,7 @@ func TestFindOverwrites_DirVsFile(t *testing.T) {
srcDir := openDir(t, src)
dstDir := openDir(t, dst)
got, err := find_overwrites(srcDir, dstDir)
got, err := find_overwrites(srcDir, dstDir, nil)
if err != nil {
t.Fatalf("find_overwrites: %v", err)
}
@ -1016,7 +1028,7 @@ func TestFindOverwrites_FileVsDir(t *testing.T) {
src, dst := t.TempDir(), t.TempDir()
buildTree(t, src, map[string]string{"conflict": "file"}, nil)
buildTree(t, dst, map[string]string{"conflict/x.txt": "x"}, nil)
if got, err := find_overwrites(openDir(t, src), openDir(t, dst)); err == nil {
if got, err := find_overwrites(openDir(t, src), openDir(t, dst), nil); err == nil {
t.Fatalf("expected an error, got overwrites %v", got)
}
}
@ -1032,7 +1044,7 @@ func TestSymlinkToDirInDestIsMerged(t *testing.T) {
buildTree(t, dst, nil, map[string]string{"folder": target})
srcDir, dstDir := openDir(t, src), openDir(t, dst)
got, err := find_overwrites(srcDir, dstDir)
got, err := find_overwrites(srcDir, dstDir, nil)
if err != nil {
t.Fatalf("find_overwrites: %v", err)
}