From 499f2ce08ed0ffbd29cbb7033f59d04f2cdcdb01 Mon Sep 17 00:00:00 2001 From: AleksZyro <178569539+AleksZyro@users.noreply.github.com> Date: Sun, 9 Aug 2026 17:08:49 +0200 Subject: [PATCH 1/4] Fallback to md5sum when uname fails --- sshfs/spec.py | 15 +++++++++------ tests/test_sshfs.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/sshfs/spec.py b/sshfs/spec.py index f8f5940..191bfa6 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -8,6 +8,8 @@ from typing import Optional import asyncssh +from asyncssh import ProcessError +from asyncssh.misc import ChannelOpenError from asyncssh.sftp import SFTPOpUnsupported from fsspec.asyn import ( AsyncFileSystem, @@ -343,19 +345,20 @@ async def _rm(self, path, recursive=False, **kwargs): @wrap_exceptions async def _checksum(self, path): - system = await self._get_system() + try: + system = await self._get_system() + except (ChannelOpenError, ProcessError): + system = "Linux" + if system == "Linux": - command = "md5sum" + result = await self._execute(f"md5sum {shlex.quote(path)}") part = 0 elif system == "Darwin": - command = "md5" + result = await self._execute(f"md5 {shlex.quote(path)}") part = -1 else: raise ValueError(f"{system!r} doesn't support checksum operation") - cmd = f"{command} {shlex.quote(path)}" - result = await self._execute(cmd) - parts = result.stdout.strip().split() assert len(parts) >= 1 diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 141baf7..aadcd1e 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -1,3 +1,4 @@ +import asyncio import hashlib import posixpath import secrets @@ -10,6 +11,7 @@ import fsspec import pytest +from asyncssh.misc import ChannelOpenError from asyncssh.sftp import SFTPAttrs, SFTPFailure from importlib_metadata import entry_points @@ -239,6 +241,22 @@ def test_checksum(fs, remote_dir): assert fs.checksum(remote_dir + "/a.txt") == checksum +def test_checksum_falls_back_to_md5sum_when_uname_session_fails(): + checksum = "0123456789abcdef0123456789abcdef" + calls = [] + + class DummyFS: + async def _get_system(self): + raise ChannelOpenError(None, None) + + async def _execute(self, command): + calls.append(command) + return SimpleNamespace(stdout=f"{checksum} /tmp/a.txt\n") + + assert asyncio.run(SSHFileSystem._checksum(DummyFS(), "/tmp/a.txt")) == checksum + assert calls == ["md5sum /tmp/a.txt"] + + def test_ls(fs, remote_dir): fs.mkdir(remote_dir + "dir/") files = set() From 2cab2530908886999d3c5e4c06080b611e191da7 Mon Sep 17 00:00:00 2001 From: AleksZyro <178569539+AleksZyro@users.noreply.github.com> Date: Tue, 11 Aug 2026 18:44:54 +0200 Subject: [PATCH 2/4] Format checksum fallback test --- tests/test_sshfs.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index aadcd1e..d7583b4 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -253,7 +253,10 @@ async def _execute(self, command): calls.append(command) return SimpleNamespace(stdout=f"{checksum} /tmp/a.txt\n") - assert asyncio.run(SSHFileSystem._checksum(DummyFS(), "/tmp/a.txt")) == checksum + assert ( + asyncio.run(SSHFileSystem._checksum(DummyFS(), "/tmp/a.txt")) + == checksum + ) assert calls == ["md5sum /tmp/a.txt"] From 3a3565f269d0693e78716040670d202f577f657e Mon Sep 17 00:00:00 2001 From: AleksZyro <178569539+AleksZyro@users.noreply.github.com> Date: Sat, 17 Oct 2026 02:20:08 +0200 Subject: [PATCH 3/4] test: cover checksum fallback errors and quoted paths --- tests/test_sshfs.py | 50 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index d7583b4..4e0cdd1 100644 --- a/tests/test_sshfs.py +++ b/tests/test_sshfs.py @@ -11,6 +11,7 @@ import fsspec import pytest +from asyncssh import ProcessError from asyncssh.misc import ChannelOpenError from asyncssh.sftp import SFTPAttrs, SFTPFailure from importlib_metadata import entry_points @@ -241,23 +242,58 @@ def test_checksum(fs, remote_dir): assert fs.checksum(remote_dir + "/a.txt") == checksum -def test_checksum_falls_back_to_md5sum_when_uname_session_fails(): +@pytest.mark.parametrize( + "exception", + [ + ChannelOpenError(2, "session failed"), + ProcessError(None, "uname", None, 127, None, 127, "", "uname failed"), + ], +) +@pytest.mark.parametrize( + "path, command", + [ + ("/tmp/a.txt", "md5sum /tmp/a.txt"), + ("/tmp/a file.txt", "md5sum '/tmp/a file.txt'"), + ], +) +def test_checksum_falls_back_to_md5sum_when_uname_fails( + exception, path, command +): checksum = "0123456789abcdef0123456789abcdef" calls = [] class DummyFS: - async def _get_system(self): - raise ChannelOpenError(None, None) + _get_system = SSHFileSystem._get_system async def _execute(self, command): calls.append(command) + if command == "uname": + raise exception return SimpleNamespace(stdout=f"{checksum} /tmp/a.txt\n") - assert ( - asyncio.run(SSHFileSystem._checksum(DummyFS(), "/tmp/a.txt")) - == checksum + assert asyncio.run(SSHFileSystem._checksum(DummyFS(), path)) == checksum + assert calls == ["uname", command] + + +def test_checksum_propagates_md5sum_failure(): + calls = [] + failure = ProcessError( + None, "md5sum", None, 1, None, 1, "", "checksum failed" ) - assert calls == ["md5sum /tmp/a.txt"] + + class DummyFS: + _get_system = SSHFileSystem._get_system + + async def _execute(self, command): + calls.append(command) + if command == "uname": + raise ChannelOpenError(2, "session failed") + raise failure + + with pytest.raises(ProcessError) as exc: + asyncio.run(SSHFileSystem._checksum(DummyFS(), "/tmp/a.txt")) + assert exc.value is failure + assert calls == ["uname", "md5sum /tmp/a.txt"] def test_ls(fs, remote_dir): From f742844afa4c0f400407fbc64edda565d85e6b0c Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Sun, 29 Nov 2026 09:49:16 -0800 Subject: [PATCH 4/4] fix: fall back when uname fails with "No such file or directory" wrap_exceptions turns a ProcessError whose message ends in "No such file or directory" into FileNotFoundError, so that uname failure skipped the md5sum fallback. The fallback tests now cover it, and they skip when the machine that runs the mock server has no md5sum, as on older macOS. Co-Authored-By: Claude Opus 5.5 --- sshfs/spec.py | 11 ++++++++--- tests/test_sshfs.py | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/sshfs/spec.py b/sshfs/spec.py index 5a42656..46d9821 100644 --- a/sshfs/spec.py +++ b/sshfs/spec.py @@ -405,10 +405,15 @@ async def _checksum(self, path): path = self._strip_protocol(path) try: system = await self._get_system() - except (asyncssh.ChannelOpenError, asyncssh.ProcessError): + except ( + asyncssh.ChannelOpenError, + asyncssh.ProcessError, + FileNotFoundError, + ): # Servers that allow only some commands can refuse uname and - # still run md5sum. The failure isn't cached, because it can be - # temporary. + # 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" diff --git a/tests/test_sshfs.py b/tests/test_sshfs.py index 5e48f26..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 @@ -392,9 +393,22 @@ async def record(self, command, **kwargs): 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"], + 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" ):