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
12 changes: 10 additions & 2 deletions sshfs/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
_DEFAULT_MAX_SESSIONS = 10


def _quote(path):
"""Quote a path for a POSIX shell as a command's file argument."""
# Without "./", commands read "-r" as an option and "-" as stdin
if path.startswith("-"):
path = "./" + path
return shlex.quote(path)


class SSHFileSystem(AsyncFileSystem):
def __init__(
self,
Expand Down Expand Up @@ -280,7 +288,7 @@ async def _get_file(
async def _cp_file(self, lpath, rpath, **kwargs):
lpath = self._strip_protocol(lpath)
rpath = self._strip_protocol(rpath)
cmd = f"cp {shlex.quote(lpath)} {shlex.quote(rpath)}"
cmd = f"cp {_quote(lpath)} {_quote(rpath)}"
await self._execute(cmd)

@wrap_exceptions
Expand Down Expand Up @@ -379,7 +387,7 @@ async def _checksum(self, path):
else:
raise ValueError(f"{system!r} doesn't support checksum operation")

cmd = f"{command} {shlex.quote(path)}"
cmd = f"{command} {_quote(path)}"
result = await self._execute(cmd)

parts = result.stdout.strip().split()
Expand Down
21 changes: 21 additions & 0 deletions tests/test_sshfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ def test_copy(fs, remote_dir):
assert strip_keys(initial_info) == strip_keys(secondary_info)


def test_cp_file_leading_dash(fs, tmp_path, monkeypatch):
# A relative path is the only way to start a name with "-". The mock
# server resolves it against the current directory.
monkeypatch.chdir(tmp_path)
(tmp_path / "-r").write_bytes(b"data")
fs.cp_file("-r", "-r-copy")
assert (tmp_path / "-r-copy").read_bytes() == b"data"


def test_rm(fs, remote_dir):
fs.touch(remote_dir + "/a.txt")
fs.rm(remote_dir + "/a.txt")
Expand Down Expand Up @@ -282,6 +291,18 @@ def test_checksum(fs, remote_dir):
assert fs.checksum(remote_dir + "/a.txt") == checksum


@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
# server resolves it against the current directory.
monkeypatch.chdir(tmp_path)
(tmp_path / name).write_bytes(b"data")
# md5sum and md5 read stdin when they get "-" or no file, and the
# command's stdin is never closed, so a regression hangs without timeout
checksum = fs.checksum(name, timeout=10)
assert checksum == hashlib.md5(b"data").hexdigest()


def test_ls(fs, remote_dir, plain_remote_dir):
fs.mkdir(remote_dir + "dir/")
files = set()
Expand Down
Loading