Repository navigation
feat(readlink): add -f, -e, -m canonicalize modes and -n - #220
Conversation
a33336f to
7a54f67
Compare
7a54f67 to
46d8e21
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested, one blocker: -f/-e/-m don't follow any symlink under a [[bind]] mode = "direct" mount, and they exit 0 with the wrong path.
@ferdingler On the in-memory VFS the walker matches GNU readlink 9.1 everywhere I looked, and the full suite is green. The bind issue is a single-function fix, with a suggestion inline. Under a direct bind, readlink -f ld prints /tmp/bx/ld instead of /tmp/bx/d, and readlink -f loop1 prints a path with exit 0 where GNU exits 1. The root cause is older: VfsKernel::lstat follows links on host paths, filed as #254. The walker is the first caller that depends on is_symlink there, so the PR needs a fix or a workaround before merge.
With the suggested lookup() change, my bind differential goes from 90 differences to 4 out of 248 cases. Those 4 are plain readlink x/ with a trailing slash and behave the same on main. The readlink and bind tests still pass (60).
✅ What I verified
- Head
46d8e21(basemaind4c57a7).cargo test --workspace --all-targets: 2246 passed, 0 failed. - Differential against GNU readlink 9.1, 62 paths × (none,
-f,-e,-m) = 248 cases. In-memory VFS: 4 differences, all the pre-existing trailing-slash case. Direct bind: 90 differences, 86 of them from-f/-e/-m. With the suggested fix: 4 differences on both. - Adversarial run on the VFS, 339 cases (
/..,//x,x/..over files, missing paths and dangling links, links to/and..chains,'',-,--, loops, long chains,-n, combined flags): everything matched except the two ⚪ below. Nothing hung: the worst crafted..-expansion link set takes about 0.1 s and exits 1. - The new hand-written walker is justified.
vfs.canonicalize_pathcollapses..before it follows links (lsub/..gives/tmp/fxwhere GNU gives/tmp/fx/d), andKernel::canonicalizeonly covers the-ecase, so neither can back-f/-m. - Repeated flags: the last one wins, as in GNU. A failure exits 1 with no message, which matches GNU without
-v. For a growing link (g -> g/x), GNU 9.1 actually hangs rather than printing a garbled path, so exiting 1 is the better outcome.
Questions (non-blocking)
- Would you add one direct-bind case next to
bind_direct_readlink_prints_stored_link_text(tests/shell_integration.rs:6877)? All the new tests build their tree under/tmp/rcin the in-memory VFS, which is how this got past the 80-case comparison.
Appendix: non-blocking (3)
- ⚪
readlink.rs:12: "no component must exist" can be read as "components must not exist". GNU says "without requirements on components existence". Something like "components need not exist" is clearer, and agents do read--help. - ⚪
MAX_SYMLINKS = 256(readlink.rs:30): a chain of 256 or more links exits 1, while GNU resolves it. The kernel can't open such a path anyway (its limit is 40), so this is fine to leave. - ⚪ Under
-m, a loop reached through another link (lviala -> d/sub/../../la, withlaandlbpointing at each other) stops atlawhere GNU stops atlb. The-mresult for a loop doesn't mean anything either way. - Pre-existing, filed as #254:
lstatfollows links on direct binds, sotest -Land[ -h ]are also wrong there.
Reading order for human reviewers
canonicalize (readlink.rs:81-200), then lookup (:207), then the readlink_links_case! tests in tests/shell_integration.rs.
Evidence (portal): strands-agents/shell/pr/220/{cargo-test.log, differential-harness.txt, differential-results.txt, adversarial-divergences.txt}
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The 🔴 from my last review is fixed at c8dc96a, and the fix holds up.
@ferdingler Status of earlier findings:
- 🔴 symlinks not followed under a direct bind: fixed. My 248-case comparison against GNU readlink 9.1 now shows the same 4 differences on the in-memory VFS and on a direct bind (down from 90 on the bind). All 4 are plain
readlink x/with a trailing slash, which behaves the same onmain. - 🟡 missing direct-bind test: fixed, by
bind_direct_readlink_canonicalizes_symlinks. - ⚪
-mhelp wording: fixed. ⚪ 256-link cap and ⚪-mloop endpoint: kept on purpose, and that's fine.
✅ What I verified
- Head
c8dc96a.cargo test --workspace --all-targets: 2247 passed, 0 failed. - Attacked the fix itself:
- Bind confinement: host links that point outside the bind (absolute
/tmp/cf/outside, and relative../outsideand../outside/secret) resolve in the shell's own namespace and exit 1 under-e/-f. Nothing about the host leaks. -m /b/hrel/../b/lin→/b/in, which is correct.
- Bind confinement: host links that point outside the bind (absolute
- Keeping
st.is_symlink || link_target.is_some()is right. If a link's target can't be read,st.link_target?(readlink.rs:180) fails the lookup instead of returning a half-resolved path. - In-memory VFS spot checks (
ld/../dthrough a link,-e ld/,-m ld/zz/..) match GNU. - #254 (
lstatfollowing links on binds, sotest -Lis wrong there) is still open and is still the root fix. This PR's workaround is local to readlink and doesn't depend on it.
Evidence (portal): strands-agents/shell/pr/220/differential-results-c8dc96a.txt, cargo-test-c8dc96a.log
Description
readlinkonly printed a link's target and rejected every option, so the usual way to get an absolute path or a script's directory,dirname "$(readlink -f "$0")", failed:Now
readlinksupports GNU's-f,-eand-m(follow every symlink, with all but the last part, all parts or no part required to exist) and-n. Relative paths resolve against the shell's cwd, and plainreadlink linkworks as before. With the change the example prints/tmp/proj/bin/run. I didn't add multiple operands or-v/-q, and I didn't touch the direct-bind code, so this shouldn't overlap with #194.I compared output and exit codes with GNU readlink on Linux for about 80 cases: symlink chains (including ones longer than 40 links), links to directories,
.., missing parts, links whose target ends in a slash, and loops. All of them matched. The one difference I know of is a link that keeps growing the path (g -> g/x) under-m. GNU prints a garbled path there, and we exit 1.Related Issues
Part of #207
Documentation PR
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.