diff --git a/sshfs/spec.py b/sshfs/spec.py index 16f692d..46d9821 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -403,7 +403,18 @@ async def _rm(self, path, recursive=False, **kwargs): @wrap_exceptions async def _checksum(self, path): path = self._strip_protocol(path) - system = await self._get_system() + try: + system = await self._get_system() + except ( + asyncssh.ChannelOpenError, + asyncssh.ProcessError, + FileNotFoundError, + ): + # Servers that allow only some commands can refuse uname and + # still run md5sum. wrap_exceptions turns a ProcessError that ends + # in "No such file or directory" into FileNotFoundError. The + # failure isn't cached, because it can be temporary. + system = "Linux" if system == "Linux": program = b"md5sum" part = 0 diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 3c7f0b5..668c7d3 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -2,6 +2,7 @@ import os import posixpath import secrets +import shutil import warnings from concurrent import futures from contextlib import asynccontextmanager @@ -10,6 +11,7 @@ from pathlib import Path from types import SimpleNamespace +import asyncssh import fsspec import pytest from asyncssh.sftp import ( @@ -380,6 +382,61 @@ async def record(self, command, **kwargs): assert commands.count("uname") == 1 +@pytest.mark.parametrize( + "error", + [ + # The server refuses to run uname + asyncssh.ChannelOpenError( + asyncssh.OPEN_REQUEST_SESSION_FAILED, "Session request failed" + ), + # uname runs and fails + asyncssh.ProcessError( + None, "uname", None, 127, None, 127, "", "uname: not found" + ), + # wrap_exceptions turns this message into FileNotFoundError + asyncssh.ProcessError( + None, + "uname", + None, + 127, + None, + 127, + "", + "uname: No such file or directory", + ), + ], + ids=["refused", "fails", "missing"], +) +# The fallback runs md5sum on the machine that runs the mock server +@pytest.mark.skipif(not shutil.which("md5sum"), reason="needs md5sum") +def test_checksum_without_uname( + ssh_server, plain_remote_dir, monkeypatch, error, user="user" +): + # A new instance, which hasn't run uname yet + fs = SSHFileSystem( + host=ssh_server.host, + port=ssh_server.port, + username=user, + client_keys=[USERS[user]], + skip_instance_cache=True, + ) + connection = type(fs.client) + run = connection.run + + async def refuse_uname(self, command, **kwargs): + if command == "uname": + raise error + return await run(self, command, **kwargs) + + monkeypatch.setattr(connection, "run", refuse_uname) + (Path(plain_remote_dir) / "a file.txt").write_bytes(b"data") + + checksum = fs.checksum(plain_remote_dir + "/a file.txt") + assert checksum == hashlib.md5(b"data").hexdigest() + with pytest.raises(FileNotFoundError): + fs.checksum(plain_remote_dir + "/missing") + + @pytest.mark.parametrize("name", ["-", "-r"]) def test_checksum_leading_dash(fs, tmp_path, monkeypatch, name): # A relative path is the only way to start a name with "-". The mock