From 1e90e6174741f695d52ab1466af3e75d848eea52 Mon Sep 17 00:00:00 2001 From: itzzdev09 Date: Sun, 22 Nov 2026 11:51:52 +0530 Subject: [PATCH 1/3] Try SFTP rename before copying over SSH in mv posix_rename is an SFTP extension. When a server lacks it, mv ran cp over an SSH exec channel, which SFTP-only servers reject. Plain rename is in the base protocol, so try it first and keep the copy for the case it can't cover: a target that already exists. --- sshfs/spec.py | 13 ++++++++----- tests/test_sshfs.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/sshfs/spec.py b/sshfs/spec.py index 2338040..3eca5c5 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -7,7 +7,7 @@ from datetime import UTC, datetime import asyncssh -from asyncssh.sftp import SFTPOpUnsupported +from asyncssh.sftp import SFTPFailure, SFTPFileAlreadyExists, SFTPOpUnsupported from fsspec.asyn import ( AsyncFileSystem, FSTimeoutError, @@ -236,11 +236,14 @@ async def _mv(self, lpath, rpath, **kwargs): with suppress(SFTPOpUnsupported): return await channel.posix_rename(lpath, rpath) - # Some systems doesn't natively support posix_rename - # which is an extension to the original SFTP protocol. - # In that case we are going to copy the file and delete - # it. + # posix_rename is an extension to the SFTP protocol, but plain + # rename is in the base protocol. Servers refuse it when rpath + # already exists, so only that case reaches the copy below. + with suppress(SFTPFailure, SFTPFileAlreadyExists): + return await channel.rename(lpath, rpath) + # Copy the file and delete the original. This runs `cp` over SSH, + # which servers that only allow SFTP reject. await self._cp_file(lpath, rpath) await self._rm_file(lpath) diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 494f523..d74df1f 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -221,7 +221,31 @@ async def cp_file(*args, **kwargs): assert initial_info == secondary_info -def test_move_copy_fallback(fs, remote_dir, no_posix_rename): +def test_move_sftp_rename_fallback( + fs, remote_dir, no_posix_rename, monkeypatch +): + async def cp_file(*args, **kwargs): + pytest.fail("mv must use SFTP rename before copying over SSH") + + monkeypatch.setattr(fs, "_cp_file", cp_file) + fs.pipe_file(remote_dir + "/a.txt", b"data") + + fs.mv(remote_dir + "/a.txt", remote_dir + "/b.txt") + + assert not fs.exists(remote_dir + "/a.txt") + assert fs.cat_file(remote_dir + "/b.txt") == b"data" + + +@pytest.fixture +def no_sftp_rename(monkeypatch, no_posix_rename): + # Plain SFTP rename refuses an existing target on most servers. + async def rename(*args, **kwargs): + raise SFTPFailure("target exists") + + monkeypatch.setattr(SFTPClient, "rename", rename) + + +def test_move_copy_fallback(fs, remote_dir, no_sftp_rename): fs.pipe_file(remote_dir + "/a.txt", b"data") fs.mv(remote_dir + "/a.txt", remote_dir + "/b.txt") @@ -231,7 +255,7 @@ def test_move_copy_fallback(fs, remote_dir, no_posix_rename): def test_move_copy_fallback_failure_keeps_source( - fs, remote_dir, no_posix_rename + fs, remote_dir, no_sftp_rename ): fs.pipe_file(remote_dir + "/a.txt", b"data") From 9595701cd6004b272b607dbd843a5dd2c5ac13c8 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Sun, 29 Nov 2026 09:40:45 -0800 Subject: [PATCH 2/3] test: model an SFTP-only server in the rename fallback test The test now makes shell commands fail through asyncssh's run, the way servers that only allow SFTP refuse them (#40), instead of patching the internal _cp_file. The comments now say that generic SFTP v3 failures fall back to the copy, and that no_posix_rename leads to the plain rename. Co-Authored-By: Claude Opus 5.5 --- sshfs/spec.py | 3 ++- tests/test_sshfs.py | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/sshfs/spec.py b/sshfs/spec.py index 16f5e92..c969035 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -266,7 +266,8 @@ async def _mv(self, lpath, rpath, **kwargs): # posix_rename is an extension to the SFTP protocol, but plain # rename is in the base protocol. Servers refuse it when rpath - # already exists, so only that case reaches the copy below. + # already exists, which SFTP v3 reports as a generic failure, so + # those failures fall back to the copy below. with suppress(SFTPFailure, SFTPFileAlreadyExists): return await channel.rename(lpath, rpath) diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index aab3600..a421085 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -12,6 +12,7 @@ import fsspec import pytest +from asyncssh import OPEN_REQUEST_SESSION_FAILED, ChannelOpenError from asyncssh.sftp import ( SFTPAttrs, SFTPClient, @@ -224,7 +225,7 @@ def test_decode_attributes_missing_timestamps(fs): @pytest.fixture def no_posix_rename(monkeypatch): - # Force the copy + delete fallback in SSHFileSystem._mv. + # A server without the posix-rename extension async def posix_rename(*args, **kwargs): raise SFTPOpUnsupported("posix_rename is unsupported") @@ -258,10 +259,11 @@ async def cp_file(*args, **kwargs): def test_move_sftp_rename_fallback( fs, remote_dir, no_posix_rename, monkeypatch ): - async def cp_file(*args, **kwargs): - pytest.fail("mv must use SFTP rename before copying over SSH") + # A server that only allows SFTP refuses shell commands such as cp + async def run(*args, **kwargs): + raise ChannelOpenError(OPEN_REQUEST_SESSION_FAILED, "Session refused") - monkeypatch.setattr(fs, "_cp_file", cp_file) + monkeypatch.setattr(type(fs.client), "run", run) fs.pipe_file(remote_dir + "/a.txt", b"data") fs.mv(remote_dir + "/a.txt", remote_dir + "/b.txt") From 62fe9d2184767548168deed880dc6b0927ae7c27 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Sun, 29 Nov 2026 09:48:26 -0800 Subject: [PATCH 3/3] test: cover each rename refusal and other rename errors The copy fallback tests now run with rename refusing the target both ways servers report it: SFTPFailure in SFTP v3 and SFTPFileAlreadyExists in later versions. A new test checks that other rename errors reach the caller without copying. Co-Authored-By: Claude Opus 5.5 --- tests/test_sshfs.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index a421085..f63ce13 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -17,6 +17,8 @@ SFTPAttrs, SFTPClient, SFTPFailure, + SFTPFileAlreadyExists, + SFTPNoSuchFile, SFTPNoSuchPath, SFTPOpUnsupported, ) @@ -272,11 +274,12 @@ async def run(*args, **kwargs): assert fs.cat_file(remote_dir + "/b.txt") == b"data" -@pytest.fixture -def no_sftp_rename(monkeypatch, no_posix_rename): - # Plain SFTP rename refuses an existing target on most servers. +@pytest.fixture(params=[SFTPFailure, SFTPFileAlreadyExists]) +def no_sftp_rename(request, monkeypatch, no_posix_rename): + # Plain SFTP rename refuses an existing target, with a generic failure in + # SFTP v3 and SFTPFileAlreadyExists in later versions async def rename(*args, **kwargs): - raise SFTPFailure("target exists") + raise request.param("target exists") monkeypatch.setattr(SFTPClient, "rename", rename) @@ -301,6 +304,21 @@ def test_move_copy_fallback_failure_keeps_source( assert fs.cat_file(remote_dir + "/a.txt") == b"data" +def test_move_sftp_rename_error(fs, remote_dir, no_posix_rename, monkeypatch): + # Other rename errors reach the caller instead of falling back to the copy + async def rename(*args, **kwargs): + raise SFTPNoSuchFile("No such file") + + monkeypatch.setattr(SFTPClient, "rename", rename) + fs.pipe_file(remote_dir + "/a.txt", b"data") + + with pytest.raises(FileNotFoundError): + fs.mv(remote_dir + "/a.txt", remote_dir + "/b.txt") + + assert fs.cat_file(remote_dir + "/a.txt") == b"data" + assert not fs.exists(remote_dir + "/b.txt") + + def test_copy(fs, remote_dir): fs.touch(remote_dir + "/a.txt") initial_info = fs.info(remote_dir + "/a.txt")