Skip to content

fix: upload to relative and top-level paths - #91

Merged
shcheklein merged 2 commits into
mainfrom
fix/upload-top-level-paths
Nov 29, 2026
Merged

shcheklein merged 2 commits into
mainfrom
fix/upload-top-level-paths

Conversation

@shcheklein

Copy link
Copy Markdown
Collaborator

put_file and pipe_file create the parent directory with _makedirs(self._parent(path), exist_ok=True). For a relative path such as "file" or a top-level path such as "/file", _parent returns "", and asyncssh's makedirs("") sends mkdir(""), which the server rejects. So these uploads fail with FileNotFoundError:

fs.put_file("local.txt", "file")  # FileNotFoundError
fs.pipe_file("/file", b"data")    # FileNotFoundError

The parent of such a path is the home directory or the root, which always exists, so this skips _makedirs when _parent returns "".

Split out of #86.

🤖 Generated with Claude Code

put_file and pipe_file create the parent directory first. For a
relative path such as "file" or a top-level path such as "/file",
_parent returns "", and asyncssh's makedirs("") sends mkdir(""), which
the server rejects, so the upload failed with FileNotFoundError. The
parent of such a path is the home directory or the root, which always
exists, so skip makedirs when the parent is "".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shcheklein added a commit to kushaldave002/sshfs that referenced this pull request Nov 28, 2026
Leave put_file and pipe_file as they are on main, and give bytes paths
in _parent the parents fsspec gives str paths, so str behavior doesn't
change. Uploads to relative and top-level paths are fixed in fsspec#91, and
"--" before paths in shell commands is added in fsspec#92.

The upload test uses absolute paths, since relative uploads depend on
fsspec#91.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(AI-generated review by Copilot)

Clean targeted fix for relative and top-level uploads (and works with b"" from #86 as well). Inline notes on _makedirs / _parent at the bigger-picture level, removing the internal _makedirs mock in test_put_file_top_level_path, and covering pipe_file("/file", ...).

Comment thread sshfs/spec.py
await self._makedirs(self._parent(rpath), exist_ok=True)
# "" means the parent is the root or the home directory, which exist
if parent := self._parent(rpath):
await self._makedirs(parent, exist_ok=True)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI-generated — Copilot] Gap & bigger picture:

  • Skipping _makedirs here (and in _pipe_file) fixes uploads and avoids acquiring an SFTP channel when no parent needs creating, and also works for b"" once fix: support bytes paths in native filesystem operations #86 lands.
  • Gap: Direct calls like fs.makedirs("", exist_ok=True) or fs.makedirs("/", exist_ok=True) (or fsspec generic code calling makedirs(self._parent(path), exist_ok=True)) still fail: asyncssh sends mkdir("") (FileNotFoundError) and mkdir("/") (which raises SFTPPermissionDenied on non-root servers because i == last in asyncssh's makedirs).
  • Bigger picture: _parent("/file") returning "" conflates / (root) with "" (home dir), even though _strip_protocol explicitly distinguishes them. Long-term, having _parent return "/" for "/file" and "" for "file" (and skipping _makedirs on ("", "/", b"", b"/"), or handling empty/root when exist_ok=True in _makedirs) avoids that ambiguity across all callers.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct makedirs("") calls are a separate, older bug. fsspec's FSMap hits it too: fs.get_mapper("")["a"] = b"data" fails on main and with this PR. Filed as #93, along with the _parent("/a") == "" ambiguity. This PR stays scoped to put_file and pipe_file.

makedirs("/", exist_ok=True) works against the test server: mkdir("/") gets a generic failure, and asyncssh then finds a directory. A server that answers permission denied would fail it, so #93 suggests handling both "" and "/" in _makedirs.

Comment thread tests/test_sshfs.py Resolved
Comment thread tests/test_sshfs.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the change to skip directory creation when an upload path has no parent.

No open issues 🎉

What changed in this PR

This PR fixes uploads to relative and top-level paths by avoiding directory creation when the parent path is empty.

Changes:

  • Skip _makedirs for empty parent paths in both put_file and pipe_file.
  • Add regression tests for relative uploads through both methods and a stubbed top-level put_file upload.
File Description
tests/​test_sshfs.py Adds regression coverage for relative and top-level upload paths.
sshfs/​spec.py Guards parent-directory creation in both upload methods.

Review effort: Balanced

- Split the relative-path test into put_file and pipe_file tests, each
  next to its existing test.
- The top-level put_file test no longer mocks _makedirs. It stubs only
  SFTPClient.put, so without the fix it fails on the real makedirs("").
- Add the same top-level test for pipe_file, which stubs SFTPClient.open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the relative and top-level upload-path change.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The upload-path handling change has no active review issues.

No open issues 🎉

Review effort: Balanced

@shcheklein shcheklein self-assigned this Nov 29, 2026
@shcheklein
shcheklein merged commit d940e31 into main Nov 29, 2026
12 checks passed
shcheklein added a commit to kushaldave002/sshfs that referenced this pull request Nov 29, 2026
Resolves the conflicts with fsspec#91 and fsspec#92. The tests from both sides are
kept, and _quote, which now builds bytes, gets the "./" prefix from fsspec#92
for paths that start with "-".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein
shcheklein deleted the fix/upload-top-level-paths branch November 29, 2026 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants