Skip to content

feat: a leading ~ means the home directory - #107

Merged
shcheklein merged 8 commits into
mainfrom
fix/home-tilde
Nov 30, 2026
Merged

shcheklein merged 8 commits into
mainfrom
fix/home-tilde

Conversation

@shcheklein

@shcheklein shcheklein commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

There was no way to name a path in the home directory in a URL. ssh://host/~/file became /~/file, a directory named ~ at the root (#55).

A leading ~ element now means the home directory, in URLs and in paths alike. _strip_protocol turns it into ., which SFTP servers resolve to the home directory:

Input Path used
ssh://host/~/data/file ./data/file
~/data/file ./data/file
ssh://host/~, ~ . (the home directory)
~/~/file (a directory named ~) ./~/file
ssh://host/data/file /data/file (unchanged)
/~/file (a plain path, such as one from ls("/")) /~/file (unchanged: a directory named ~ at the root)
~data, data/~/file unchanged

makedirs and uploads treat . like "": it always exists, so it's never created. fs.get_mapper("") roots the mapper at ., so a key named ~ is a file there too. Results keep the ./ prefix: ls("~") returns ./a.txt, so glob("~/*") matches every entry, and a ~ element after the prefix stays a file named ~ when the path is stripped again, as rm does. Listings of "" (#106) name a file called ~ ./~, so find("") doesn't recurse into the home directory through it.

This follows curl, which treats sftp://host/~/file as home-relative and other URL paths as absolute, and the expired IETF draft for sftp:// URIs, which uses the same ~ element. fsspec's local filesystem also expands ~ in paths. OpenSSH's sftp and scp resolve every URL path against the home directory (absolute paths need //). Switching to that would change where existing URLs like ssh://host/data/file point, so URL paths stay absolute, and the class docstring now says so.

The branch includes #106's commit, so merge #106 first. Checked against OpenSSH 10.0: cat_file, ls, info, glob, find, makedirs, pipe_file and rm work with ~ paths, and rm("~/~/a.txt") removes only the file inside the ~ directory. The new tests fail on main.

Fixes #55.

🤖 Generated with Claude Code

shcheklein and others added 2 commits November 29, 2026 10:25
sshfs uses "" for the home directory, but ls and info sent it to the
server as is, and an empty path isn't valid there: OpenSSH's
sftp-server fails with ENOENT. So ls(""), info("") and
ls("ssh://host") raised FileNotFoundError, and find("") returned
nothing. They now send ".", which servers resolve to the home
directory. Listing names stay relative, and info("") keeps the name ""
so it isn't confused with "/".

Fixes #105.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ssh://host/~/file, ~/file and ~ now name paths in the home directory:
_strip_protocol turns them into relative paths, which SFTP servers
resolve against it. curl's sftp URLs and the IETF draft for sftp URIs
use the same ~ element, and fsspec's local filesystem expands ~ in
paths. Other URL paths stay absolute.

Fixes #55.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Repeated path normalization can strip a literal ~ directory, causing fs.rm("~/~/victim") to delete the wrong file in the home directory.

1 open issue
What changed in this PR

Adds leading ~ support to SSHFileSystem paths and URLs, addressing #55 while preserving absolute paths for ordinary URLs. Listing the home directory itself remains covered by #106.

Changes:

  • Converts a leading whole ~ component into a home-relative path.
  • Documents URL path behavior and differences from OpenSSH.
  • Adds normalization tests and a file-read test for home-relative paths.
File Description
tests/​test_sshfs.py Tests tilde normalization, unchanged paths, and home-relative file reads.
sshfs/​spec.py Implements leading-tilde handling and documents path semantics.

Review effort: Balanced

Comment thread sshfs/spec.py Resolved
shcheklein and others added 2 commits November 29, 2026 10:31
_strip_protocol turned ~/~/victim into ~/victim, and methods that strip
a path again, such as rm and then _rm_file, turned that into victim, so
rm("~/~/victim") deleted victim in the home directory. Listing the
home directory also named a file called ~ as "~", which meant the home
directory when passed back, so find("~") recursed into it forever. A
relative path whose first element is a file named ~ now starts with
"./", both when stripped and in listings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Tilde escaping remains inconsistent, causing absolute /~ listing entries to resolve to the home directory and home-relative globs to omit literal ~ files.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Escape absolute leading tildes in listing paths

sshfs/​spec.py:55

This only escapes relative names, although _strip_protocol also interprets /~ as the home directory. If the server root contains a literal directory named ~, ls("/") returns /~, and passing that name to info, cat_file, or recursive traversal redirects it to the home directory. Escape absolute leading ~ elements too, for example as /./~, so listing results continue to identify the original entry.

1 resolved since last review

Review effort: Balanced

Comment thread sshfs/spec.py Resolved
With ~ mapped to "", glob("~/*") became the pattern "*", which
doesn't match the escaped name "./~" of a file named ~ in the home
directory, so the glob left it out. ~ now becomes "." and ~/x becomes
./x: servers resolve "." to the home directory, listings of ~ name every
entry with "./", and a "~" element after it stays a file named ~
without a special case. Listings of "" still escape a file named ~.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread sshfs/spec.py Resolved
Comment thread sshfs/spec.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Unresolved tilde-path handling can redirect operations on root listing entries to the user's home directory and misaddress literal ~ keys in empty-root mappers.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

High severity Escape root listing paths that begin with a literal ~

sshfs/​spec.py:55

A literal ~ entry at the filesystem root is still returned as /~ by ls('/'). The new _strip_protocol turns that name into ., so passing the listing result to info or rm(..., recursive=True) targets the user's home directory instead of the listed entry. Escape absolute /~ and /~/... listing names too, for example as /./~, so returned paths continue to identify the correct object.

Medium severity Normalize empty mapper roots to preserve literal ~ keys

sshfs/​spec.py:170

This also changes literal keys in an empty-root mapper: if the home directory contains a file named ~, fs.get_mapper("")["~"] now targets . instead of that file, while iteration exposes the key as ./~. FSMap's key construction on fsspec's default branch concatenates the empty root prefix directly with the key. Normalize an empty mapper root to . before constructing FSMap, so literal keys receive a protective ./ path prefix without changing their names.

1 resolved since last review

Review effort: Balanced

Only a URL's path starts with "/" before a home-directory "~", so a
plain /~ or /~/file, such as a root entry from ls("/"), now stays a file
named ~ at the root. Before, rm("/~/file") targeted the home directory.

Since ~ becomes ".", makedirs skips "." with exist_ok like "", and
_upload treats a "." parent like "": a missing file under ~ raises
without creating "." and retrying.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein

Copy link
Copy Markdown
Collaborator Author

About the two issues in Copilot's latest review:

  • Root listing entries named ~: fixed in f84e26e. A plain /~ or /~/... now stays a path at the root, and only a URL's /~/ means the home directory. So the /~ that ls("/") returns names the root entry.
  • A literal ~ key in a mapper rooted at "": leaving this as is. A relative path that starts with ~ means the home directory by design, the same way fsspec's local filesystem expands ~. A key named exactly ~ in a mapper rooted at the home directory is the one case that collides. fs.get_mapper(".") addresses it as ./~. Overriding get_mapper wouldn't cover fsspec.get_mapper("ssh://host") or an FSMap built directly.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the home-relative SSH path changes.

No open issues 🎉

Review effort: Balanced

Comment thread sshfs/spec.py Resolved
FSMap builds paths by joining its root to each key, so in a mapper
rooted at "" the key "~" became the path "~", which means the home
directory, while iteration listed that file as "./~".
SSHFileSystem.get_mapper now roots a home-directory mapper at ".", so
keys become ./key paths and iteration still returns plain names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The get_mapper override breaks compatibility with supported positional arguments, causing previously valid calls to raise TypeError instead of creating a mapper.

1 open issue

Review effort: Balanced

Comment thread sshfs/spec.py Resolved
The override only took keyword arguments, so fsspec's positional check,
create and missing_exceptions raised TypeError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein
shcheklein merged commit 41f158c into main Nov 30, 2026
11 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues requiring resolution.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@shcheklein
shcheklein deleted the fix/home-tilde branch November 30, 2026 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

To be able to use relative (to HOME) path

2 participants