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
19 changes: 14 additions & 5 deletions sshfs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Comment thread
shcheklein marked this conversation as resolved.
Resolved

# 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)

Expand Down
50 changes: 47 additions & 3 deletions tests/test_sshfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")
Expand All @@ -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")

Expand All @@ -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")
Expand Down
Loading