From 846ee4d2bc887fa10b4fd3a4a3f029ba08a3d474 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Sun, 1 Nov 2026 03:56:54 +0300 Subject: [PATCH] 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 --- internal/fs/remote/client.go | 25 +++++++++++++- internal/fs/remote/qa_fixes_test.go | 50 ++++++++++++++++++++++++++++ internal/fs/remote/qa_remote_test.go | 1 - internal/ui/model.go | 2 +- internal/ui/qa_regression_test.go | 1 - 5 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 internal/fs/remote/qa_fixes_test.go diff --git a/internal/fs/remote/client.go b/internal/fs/remote/client.go index e46e590..67afb9d 100644 --- a/internal/fs/remote/client.go +++ b/internal/fs/remote/client.go @@ -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). diff --git a/internal/fs/remote/qa_fixes_test.go b/internal/fs/remote/qa_fixes_test.go new file mode 100644 index 0000000..dd8b772 --- /dev/null +++ b/internal/fs/remote/qa_fixes_test.go @@ -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") + } +} diff --git a/internal/fs/remote/qa_remote_test.go b/internal/fs/remote/qa_remote_test.go index 2886c75..1ebcebc 100644 --- a/internal/fs/remote/qa_remote_test.go +++ b/internal/fs/remote/qa_remote_test.go @@ -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) diff --git a/internal/ui/model.go b/internal/ui/model.go index 0e2300a..0deea78 100644 --- a/internal/ui/model.go +++ b/internal/ui/model.go @@ -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} } } diff --git a/internal/ui/qa_regression_test.go b/internal/ui/qa_regression_test.go index eab5785..c632fcf 100644 --- a/internal/ui/qa_regression_test.go +++ b/internal/ui/qa_regression_test.go @@ -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)