Repository navigation
fix: support bytes paths in native filesystem operations - #86
kushaldave002 wants to merge 7 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #86 +/- ##
==========================================
+ Coverage 94.30% 95.33% +1.02%
==========================================
Files 13 13
Lines 738 1093 +355
==========================================
+ Hits 696 1042 +346
- Misses 42 51 +9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Build the cp and md5sum commands as bytes, so bytes paths run on the server the same way str paths do, instead of copying over SFTP (through the client when the server lacks copy-data) or downloading the whole file to hash it. Command output is decoded with errors="replace": a file name in it that isn't valid UTF-8 otherwise makes asyncssh close the SSH connection. _parent now handles str and bytes with the same code and returns "/" for a top-level path, since "" is the home directory in sshfs. Also test relative str paths in put_file and pipe_file, which failed before this branch, and list the methods that accept bytes paths in the README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the fix! I pushed a few commits on top of yours. The PR now only adds bytes-path support and leaves str behavior as it is:
Split out into their own PRs: #91 fixes uploads to relative and top-level paths, which fail for str paths too, and #92 fixes |
The class docstring already says that bytes paths are sent to the server unchanged and must be used with path_encoding=None. fsspec's generic methods taking only str paths is fsspec's behavior, not something sshfs needs to document. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the bytes-path support changes.
No open issues 🎉
What changed in this PR
Addresses #84 by extending bytes-path support across native SSH filesystem operations, while documenting the limits of fsspec’s generic methods.
Changes:
- Handles bytes paths in metadata, directory listings, parent resolution, and server-side copy and checksum commands.
- Avoids creating empty parent paths and tolerates non-UTF-8 command output without closing the SSH connection.
- Adds regression tests and documents which methods accept bytes paths.
| File | Description |
|---|---|
| tests/test_sshfs.py | Adds coverage for bytes paths, relative uploads, shell errors, and dot-entry filtering. |
| sshfs/spec.py | Implements bytes-aware path handling, shell quoting, and command-output decoding. |
| README.md | Documents bytes-path configuration, supported methods, and generic-method limitations. |
Review effort: Balanced
put_file and pipe_file skip creating the parent directory when it's
the root as well as the home directory. asyncssh's makedirs("/") sends
mkdir("/") twice and raises when the server denies it, as chrooted
servers can, so uploads to a top-level path such as "/file" failed
there.
cp, md5sum and md5 get "--" before the paths, so a name starting with
"-" isn't parsed as an option.
Tests now cover checksum of a name with spaces, quotes and "$", cp_file
and checksum of an existing file whose name isn't valid UTF-8 (skipped
where the filesystem rejects such names), names starting with "-", an
upload to "/" on a server that denies mkdir, and the mv copy fallback
with bytes paths.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Leave put_file and pipe_file as they are on main, and give bytes paths in _parent the parents fsspec gives str paths, so str behavior doesn't change. Uploads to relative and top-level paths are fixed in fsspec#91, and "--" before paths in shell commands is added in fsspec#92. The upload test uses absolute paths, since relative uploads depend on fsspec#91. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shcheklein
left a comment
There was a problem hiding this comment.
(AI-generated review by Copilot)
Solid, focused fix for bytes paths across native operations. The errors="replace" handling for non-UTF-8 shell output is a great catch.
Main themes in the inline comments:
- Bigger picture:
_parentmirrorsAbstractFileSystem's"/a" -> ""quirk (conflating root/with home""), and_execute_with_pathsrelies onasyncssh's undocumentedbytescommand pass-through. - Simplification:
_execute_with_pathscan join(command, *map(_quote, paths))instead of%sformat strings (which also makes integrating--from #92 cleaner). - Tests: Several bytes tests duplicate coverage or branch across unrelated operations (
test_bytes_upload_and_downloadvstest_bytes_file_operations),fs_bytescan be reused intest_sftp_client_kwargs_path_encoding, andinfo(parent)without trailing/isn't covered.
| # The same result as fsspec's _parent gives for the str path, | ||
| # whose trailing slashes _strip_protocol removes | ||
| return path.rstrip(b"/").rpartition(b"/")[0] | ||
| return super()._parent(path) |
There was a problem hiding this comment.
[AI-generated — Copilot] Logic / bigger picture (_parent vs _strip_protocol):
- Minor: for
strpaths,super()._parent(path)callscls._strip_protocol(path)a second time. - Bigger picture:
_strip_protocolexplicitly distinguishes"/"(filesystem root) from""(remote home directory), whereasAbstractFileSystem._parent(withroot_marker = "") returns""for"/a". Mirroring that forbytes(_parent(b"/a") == b"") locks in the conflation of root and home (e.g.fs.ls(fs._parent("/a"))lists home instead of/), which fix: upload to relative and top-level paths #91 then has to work around in_put_file/_pipe_file.
Worth considering whether _parent should follow posixpath.dirname ("/a" -> "/", "a" -> "", b"/a" -> b"/", b"a" -> b"") and have _put_file/_pipe_file skip _makedirs on ("", "/", b"", b"/").
There was a problem hiding this comment.
Fixed the double strip in cc78957: _parent now calls _strip_protocol once and handles str and bytes with the same expression. The str results don't change, and test_parent pins them.
The root/home ambiguity is how _parent already behaves for str paths, so this PR keeps bytes consistent with str. Filed it as #93, together with the makedirs("") failure it causes in fsspec's generic code.
| command %= tuple(map(_quote, paths)) | ||
| # Output can include those paths, and failing to decode it strictly | ||
| # closes the SSH connection. | ||
| return await self._execute(command, errors="replace") |
There was a problem hiding this comment.
[AI-generated — Copilot] Logic / simplification / alternatives:
- Undocumented
asyncsshbehavior &ProcessError.commandtype:SSHClientConnection.create_session/SSHClientChannel.createtypecommandasOptional[str]; passingbytesworks today becauseasyncssh.packet.Stringacceptsbytes. Buildingbytesforstrpaths too also changesProcessError.commandfromstrtobytesforstrcallers. That's a reasonable trade-off for a single code path, though only encoding tobyteswhen a path isbyteswould keepstrcallers on the documentedstrpath. - Simplification (and synergy with fix: prefix paths starting with - with ./ in cp, md5sum and md5 #92): Passing the command binary (
b"cp",b"md5sum",b"md5") and buildingb" ".join((command, *map(_quote, paths)))(or(command, b"--", *map(_quote, paths))with fix: prefix paths starting with - with ./ in cp, md5sum and md5 #92) avoids%sformat strings and centralizes--in one place.
There was a problem hiding this comment.
Done in cc78957: _execute_with_paths(program, *paths) joins the quoted paths to the program.
I kept bytes commands for str paths too, so there's one code path. asyncssh's String() accepts bytes, and every cp_file and checksum test sends one, so CI would catch an asyncssh change. ProcessError.command does become bytes, but it isn't part of the fsspec API.
#92 now uses a ./ prefix inside a _quote helper instead of --, because -- doesn't help with a bare -. Whichever PR merges second folds it into this _quote.
- _execute_with_paths takes the program and joins the quoted paths to it instead of formatting them into a command string. - _parent strips the protocol once and handles str and bytes with the same expression. The str results are unchanged. - The bytes tests are split into one test per operation, each next to its str counterpart, without branches on the operation. - test_bytes_info also checks a directory path without a trailing slash and the root. - test_sftp_client_kwargs_path_encoding uses the fs_bytes fixture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the work on this, and sorry for the change of direction after all the back and forth. Looking at the bigger picture, we decided not to support bytes paths. No other fsspec filesystem takes them, fsspec's generic methods ( #94 fixes the underlying problem instead. asyncssh's Closing in favor of #94. |
Fixes #84.
With
path_encoding=None, several native operations passed bytes paths to str-only separators or shell quoting. This branch:info(and soexists,isdir,isfile,size,modified,rm) and in_parent(used byput_fileandpipe_file), which gives bytes paths the same parents as str paths..and..entries inls.cpandmd5sumcommands as bytes, socp_fileandchecksumrun on the server for bytes paths, as they do for str paths. Command output is decoded witherrors="replace": a file name in it that isn't valid UTF-8 otherwise makes asyncssh close the SSH connection.str paths behave as before; the only difference for them is that the shell commands are now built as bytes. Uploads to relative and top-level paths fail with
FileNotFoundErrorfor str and bytes paths alike, which #91 fixes, and #92 fixes shell commands for paths that start with-.The test for an existing file whose name isn't valid UTF-8 runs on Linux only: macOS rejects such names, and mock-ssh-server decodes SFTP paths as UTF-8, so it covers
cp_fileandchecksum.