diff --git a/sshfs/spec.py b/sshfs/spec.py index 16f692d..c969035 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -7,7 +7,12 @@ from datetime import UTC, datetime import asyncssh -from asyncssh.sftp import SFTPFailure, SFTPFileIsADirectory, SFTPOpUnsupported +from asyncssh.sftp import ( + SFTPFailure, + SFTPFileAlreadyExists, + SFTPFileIsADirectory, + SFTPOpUnsupported, +) from fsspec.asyn import ( AsyncFileSystem, FSTimeoutError, @@ -259,11 +264,15 @@ 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, 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) + # 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 3c7f0b5..f63ce13 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -12,10 +12,13 @@ import fsspec import pytest +from asyncssh import OPEN_REQUEST_SESSION_FAILED, ChannelOpenError from asyncssh.sftp import ( SFTPAttrs, SFTPClient, SFTPFailure, + SFTPFileAlreadyExists, + SFTPNoSuchFile, SFTPNoSuchPath, SFTPOpUnsupported, ) @@ -224,7 +227,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") @@ -255,7 +258,33 @@ 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 +): + # 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(type(fs.client), "run", run) + 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(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 request.param("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") @@ -265,7 +294,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") @@ -275,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")