Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sshfs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ async def _put_file(
**kwargs,
):
rpath = self._strip_protocol(rpath)
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.

async with self._pool.get() as channel:
await channel.put(
lpath,
Expand Down Expand Up @@ -422,7 +424,9 @@ async def _cat_file(self, path, **kwargs):
async def _pipe_file(self, path, data, chunksize=50 * 2**20, **kwargs):
"""Asynchronously writes the given data to a remote file in chunks."""
path = self._strip_protocol(path)
await self._makedirs(self._parent(path), exist_ok=True)
# "" means the parent is the root or the home directory, which exist
if parent := self._parent(path):
await self._makedirs(parent, exist_ok=True)

async with self._pool.get() as channel:
async with channel.open(path, "wb") as f:
Expand Down
49 changes: 49 additions & 0 deletions tests/test_sshfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import secrets
import warnings
from concurrent import futures
from contextlib import asynccontextmanager
from datetime import UTC, datetime
from importlib.metadata import entry_points
from pathlib import Path
Expand Down Expand Up @@ -542,6 +543,27 @@ def test_put_file(fs, remote_dir, file_path, tmp_path):
assert stream.read() == b"data"


def test_put_file_relative_path(fs, tmp_path, monkeypatch):
# The mock server resolves relative paths against the current directory
monkeypatch.chdir(tmp_path)
(tmp_path / "upload.txt").write_bytes(b"data")
fs.put_file(str(tmp_path / "upload.txt"), "a.txt")
assert (tmp_path / "a.txt").read_bytes() == b"data"


def test_put_file_top_level_path(fs, tmp_path, monkeypatch):
# The tests can't write to "/", so the upload is stubbed out
uploads = []

async def put(self, lpath, rpath, **kwargs):
uploads.append(rpath)

monkeypatch.setattr(SFTPClient, "put", put)
(tmp_path / "upload.txt").write_bytes(b"data")
fs.put_file(str(tmp_path / "upload.txt"), "/a.txt")
assert uploads == ["/a.txt"]


@pytest.mark.parametrize("file_path", ["a.txt", "dir/a.txt"])
def test_get_file(fs, remote_dir, file_path, tmp_path):
remote_file = remote_dir + f"/{file_path}"
Expand Down Expand Up @@ -606,3 +628,30 @@ def test_pipe_file(fs, remote_dir):
assert (
f.read() == test_data
), "The data read from the file does not match the data written."


def test_pipe_file_relative_path(fs, tmp_path, monkeypatch):
# The mock server resolves relative paths against the current directory
monkeypatch.chdir(tmp_path)
fs.pipe_file("a.txt", b"data")
assert (tmp_path / "a.txt").read_bytes() == b"data"


def test_pipe_file_top_level_path(fs, monkeypatch):
# The tests can't write to "/", so the file is written to memory
written = {}

class File:
def __init__(self, path):
self.path = path

async def write(self, data):
written[self.path] = data

@asynccontextmanager
async def open(self, path, mode):
yield File(path)

monkeypatch.setattr(SFTPClient, "open", open)
fs.pipe_file("/a.txt", b"data")
assert written == {"/a.txt": b"data"}
Loading