Skip to content
Open
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
15 changes: 9 additions & 6 deletions sshfs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions tests/test_sshfs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import hashlib
import posixpath
import secrets
Expand All @@ -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

Expand Down Expand Up @@ -239,6 +241,25 @@ 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()
Expand Down
Loading