dnd: Only remove original sources after a local move

This commit is contained in:
Zhizhen He 2026-10-30 20:29:16 +08:00
parent 6adff8efff
commit 7629533607
3 changed files with 100 additions and 7 deletions

View file

@ -432,7 +432,8 @@ Detailed list of changes
- 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
inside it, which would delete the moved files. Preserve files placed through
destination symlinks over other move sources. 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

View file

@ -522,10 +522,11 @@ const (
)
type parsed_uri struct {
kind parsed_uri_kind
path string // for file URIs: the local filesystem path (empty if not a valid file URI)
mime string // for data URIs: the MIME type
data []byte // for data URIs: the decoded payload
kind parsed_uri_kind
path string // for file URIs: the local filesystem path (empty if not a valid file URI)
mime string // for data URIs: the MIME type
data []byte // for data URIs: the decoded payload
source_info os.FileInfo // identity of a local move source before staging
}
// ext_for_mime returns a file extension (with leading dot) for a MIME type.
@ -686,7 +687,8 @@ func (dnd *dnd) end_drop(success bool) {
continue
}
st, err := os.Lstat(u.path)
if err != nil {
// Placement can replace a source path through a destination symlink.
if err != nil || u.source_info == nil || !os.SameFile(u.source_info, st) {
continue
}
if st.IsDir() {
@ -736,7 +738,7 @@ func (dnd *dnd) move_source_dirs() (ans []os.FileInfo, err error) {
}
return nil, err
}
if info.IsDir() {
if info.IsDir() && u.source_info != nil && os.SameFile(u.source_info, info) {
ans = append(ans, info)
}
}
@ -916,6 +918,7 @@ func (dnd *dnd) all_mime_data_dropped() (err error) {
if drop_status.action != move_on_drop {
continue
}
drop_status.uri_list[i].source_info = source_info
parent_info, err := os.Stat(filepath.Dir(u.path))
if err != nil {
return err

View file

@ -329,6 +329,95 @@ func TestLocalMoveSourceRemovedWhileAwaitingConfirmation(t *testing.T) {
}
}
func TestLocalMoveDirectoryAndFileThroughAlias(t *testing.T) {
for _, mode := range []string{"auto", "independent"} {
for _, confirm := range []bool{false, true} {
for _, selection := range []string{"folder-only", "folder-first", "file-first"} {
t.Run(fmt.Sprintf("%s/confirm=%t/%s", mode, confirm, selection), func(t *testing.T) {
tmp := t.TempDir()
src, dst := filepath.Join(tmp, "src"), filepath.Join(tmp, "dst")
buildTree(t, src, map[string]string{"folder/file.txt": "directory payload", "file.txt": "top level payload"}, nil)
buildTree(t, dst, nil, map[string]string{"folder": src})
uris := []parsed_uri{{kind: parsed_uri_file, path: filepath.Join(src, "folder")}}
if selection != "folder-only" {
file := parsed_uri{kind: parsed_uri_file, path: filepath.Join(src, "file.txt")}
if selection == "file-first" {
uris = append([]parsed_uri{file}, uris...)
} else {
uris = append(uris, file)
}
}
run := runLocalDrop
if confirm {
run = runLocalDropConfirming
}
d, err := run(t, dst, uris, move_on_drop, mode)
if err != nil {
t.Fatal(err)
}
if confirm {
if d.confirm_drop.staging_dir == nil {
t.Fatal("expected overwrite confirmation")
}
if err := d.drop_confirm(true); err != nil {
t.Fatal(err)
}
}
if got, err := os.ReadFile(filepath.Join(dst, "folder/file.txt")); err != nil || string(got) != "directory payload" {
t.Errorf("merged directory payload: got %q, %v", got, err)
}
if selection != "folder-only" {
if got, err := os.ReadFile(filepath.Join(dst, "file.txt")); err != nil || string(got) != "top level payload" {
t.Errorf("top level payload: got %q, %v", got, err)
}
}
if _, err := os.Lstat(filepath.Join(src, "folder")); !os.IsNotExist(err) {
t.Errorf("move source directory still present: %v", err)
}
})
}
}
}
}
func TestLocalMoveSourceReplacedWhileAwaitingConfirmation(t *testing.T) {
for _, mode := range []string{"auto", "independent"} {
for _, directory := range []bool{false, true} {
t.Run(fmt.Sprintf("%s/directory=%t", mode, directory), func(t *testing.T) {
tmp := t.TempDir()
src, dst := filepath.Join(tmp, "src"), filepath.Join(tmp, "dst")
name := "item"
if directory {
name = "item/file.txt"
}
buildTree(t, src, map[string]string{name: "original"}, nil)
buildTree(t, dst, map[string]string{name: "old destination"}, nil)
source := filepath.Join(src, "item")
d, err := runLocalDropConfirming(t, dst, []parsed_uri{{kind: parsed_uri_file, path: source}}, move_on_drop, mode)
if err != nil {
t.Fatal(err)
}
if d.confirm_drop.staging_dir == nil {
t.Fatal("expected overwrite confirmation")
}
if err := os.Rename(source, source+".original"); err != nil {
t.Fatal(err)
}
buildTree(t, src, map[string]string{name: "replacement"}, nil)
if err := d.drop_confirm(true); err != nil {
t.Fatal(err)
}
if got, err := os.ReadFile(filepath.Join(src, name)); err != nil || string(got) != "replacement" {
t.Errorf("replacement source: got %q, %v", got, err)
}
if got, err := os.ReadFile(filepath.Join(dst, name)); err != nil || string(got) != "original" {
t.Errorf("destination: got %q, %v", got, err)
}
})
}
}
}
func TestLocalMoveToSourceDirectory(t *testing.T) {
for _, copy_mode := range []string{"auto", "independent"} {
for _, alias := range []string{"none", "source", "destination"} {