fix(remote): report existing name on F7 in SSH panes; clean path in RemoveRecursive

F7 with a taken name in an SSH pane silently succeeded because mkdir went
through MkdirAll (QA-10). The new SSHClient.MkdirNew creates missing
parents but fails with ErrExist on an existing name, like the local
MakeDir.

RemoveRecursive cleans its path first: lstat("link/") resolves the link on
POSIX servers and deleted the target's contents (QA-9; not reachable from
the UI).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-11-01 03:56:54 +03:00
parent 2cbc0c3d70
commit 846ee4d2bc
5 changed files with 75 additions and 4 deletions

View file

@ -399,6 +399,26 @@ func (c *SSHClient) Mkdir(dirPath string) error {
return c.sftpCli.Mkdir(dirPath)
}
// MkdirNew creates dirPath (and missing parents) and fails if dirPath
// already exists, like the local MakeDir: F7 with a taken name must report
// an error (QA-10).
func (c *SSHClient) MkdirNew(dirPath string) error {
if c.unusable() {
return errNotConnected
}
if _, err := c.sftpCli.Lstat(dirPath); err == nil {
return &os.PathError{Op: "mkdir", Path: dirPath, Err: os.ErrExist}
} else if !os.IsNotExist(err) {
return err
}
if parent := path.Dir(dirPath); parent != dirPath && parent != "." {
if err := c.MkdirAll(parent); err != nil {
return err
}
}
return c.sftpCli.Mkdir(dirPath)
}
// Remove deletes a remote file.
func (c *SSHClient) Remove(filePath string) error {
if c.unusable() {
@ -526,10 +546,13 @@ func (c *SSHClient) SameHostAs(other *SSHClient) bool {
// RemoveRecursive recursively deletes a remote file or directory.
// For directories, it walks and removes all children first.
func (c *SSHClient) RemoveRecursive(path string) error {
func (c *SSHClient) RemoveRecursive(target string) error {
if c.unusable() {
return errNotConnected
}
// Clean first: lstat("link/") resolves the link on POSIX servers, and
// the target's contents would be deleted (QA-9).
path := path.Clean(target)
// Lstat, not Stat: a symlink to a directory must be removed as a link.
// Following it would delete the contents of the link's target (N-P0-3).

View file

@ -0,0 +1,50 @@
package remote
import (
"errors"
"os"
"path/filepath"
"testing"
)
// QA-10: MkdirNew creates missing parents but refuses an existing name
// (directory, file or symlink).
func TestMkdirNew(t *testing.T) {
_, c, dir := setup(t)
nested := filepath.Join(dir, "a", "b")
if err := c.MkdirNew(nested); err != nil {
t.Fatalf("nested mkdir: %v", err)
}
if fi, err := os.Stat(nested); err != nil || !fi.IsDir() {
t.Fatalf("not created: %v", err)
}
writeFile(t, filepath.Join(dir, "file"), 1, 0o644)
if err := os.Symlink("/nonexistent", filepath.Join(dir, "broken")); err != nil {
t.Fatal(err)
}
for _, n := range []string{"a", "a/b", "file", "broken"} {
if err := c.MkdirNew(filepath.Join(dir, n)); !errors.Is(err, os.ErrExist) {
t.Errorf("MkdirNew(%s) = %v, want ErrExist", n, err)
}
}
}
// QA-9: a path with "/." or "//" variants of the link name is cleaned too.
func TestRemoveRecursiveCleansPath(t *testing.T) {
_, c, dir := setup(t)
target := filepath.Join(dir, "target")
writeFile(t, filepath.Join(target, "keep"), 10, 0o644)
link := filepath.Join(dir, "link")
if err := os.Symlink(target, link); err != nil {
t.Fatal(err)
}
if err := c.RemoveRecursive(link + "/."); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(link); !os.IsNotExist(err) {
t.Fatal("link not removed")
}
if _, err := os.Stat(filepath.Join(target, "keep")); err != nil {
t.Fatal("target contents deleted")
}
}

View file

@ -82,7 +82,6 @@ func TestQARemoveRecursiveDirWithOutsideLinks(t *testing.T) {
// The UI never builds such paths today, but RemoveRecursive is a public
// helper: with a trailing slash it still empties the target.
func TestQARemoveRecursiveSymlinkTrailingSlash(t *testing.T) {
t.Skip("BUG QA-9 (P2): RemoveRecursive(\"link/\") follows the link (lstat with trailing slash resolves it) and deletes the target's contents (client.go:528)")
_, c, dir := setup(t)
target := filepath.Join(dir, "target")
writeFile(t, filepath.Join(target, "keep.txt"), 10, 0o644)

View file

@ -5341,7 +5341,7 @@ func (m *Model) mkdirCmd(parent, name string) tea.Cmd {
if mount, ok := m.activePane().CurrentRemote(); ok {
return func() tea.Msg {
targetPath := path.Join(parent, name)
err := mount.Client.MkdirAll(targetPath)
err := mount.Client.MkdirNew(targetPath)
return opMsg{kind: opMkdir, targetPath: targetPath, err: err}
}
}

View file

@ -307,7 +307,6 @@ func TestQARemoteDeleteFileDirAndSymlink(t *testing.T) {
// (CHANGELOG: "Creating a directory that already exists reports an
// error"), the SSH pane still reports success.
func TestQARemoteMkdirExisting(t *testing.T) {
t.Skip("BUG QA-10 (P2): F7 with an existing name in an SSH pane silently 'succeeds' (mkdirCmd uses Client.MkdirAll, model.go:5312); the local fix from 1.1 was not applied to remote")
rdir := t.TempDir()
if err := os.Mkdir(filepath.Join(rdir, "taken"), 0o755); err != nil {
t.Fatal(err)