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
13 changes: 12 additions & 1 deletion sshfs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions tests/test_sshfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import posixpath
import secrets
import shutil
import warnings
from concurrent import futures
from contextlib import asynccontextmanager
Expand All @@ -10,6 +11,7 @@
from pathlib import Path
from types import SimpleNamespace

import asyncssh
import fsspec
import pytest
from asyncssh.sftp import (
Expand Down Expand Up @@ -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
Expand Down
Loading