Repository navigation
feat(ln): add -f/--force and link into an existing directory - #234
Conversation
e4a7dd5 to
552e7a2
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Verdict: comment. Close to mergeable; there's one 🟡 worth fixing first. The ln behaviour matches GNU coreutils 9.1 in every case we tried. The problem is underneath it: each successful ln -sf over an existing file leaks one VFS inode. After about 9,950 replacements in a session, the VFS hits its inode limit and every command that creates a file fails.
- 🟡
ln.rs:64: the create-then-remove order relies on a failedVfs::symlink, and that call leaks an inode (src/vfs.rs:371-372). The fix is a two-line swap invfs.rs(inline). - 🟡
tests/shell_integration.rs:7330: the bind_direct test also passes onmain, because-fis rejected there as an invalid option (inline).
The adversarial pass found no data loss and no case where this succeeds when GNU fails.
✅ What I verified, plus the appendix
Verified at 552e7a2
cargo test --test shell_integration ln_: 15/15 pass. Fullcargo test --test shell_integration: 1789/1789 pass.- About 30 reviewer cases and 17 adversarial cases compared side by side with GNU
ln9.1. Covered: replacing a file, replacing a symlink (not followed), a symlink to a directory, a real directory, linking into a directory,.and..,/, a dangling destination, trailing slashes, the same-file check, and-fwithout-s(returns before anything is removed). In every case the files end up the same as with GNU and the command succeeds or fails the same way. - Self-loop parity:
ln -sf l landln -sf a d/a(withd/aa file) create self-loop symlinks, andln -sf a ais refused as the same file. GNU does exactly the same, so these aren't findings. - Direct bind: on a
mode="direct"mount,ln -sfleaves the host files untouched, read-only or writable, so the claim in the PR description holds. - Leak repro (fresh VFS for each
-c):The control loop$ strands-shell -c 'cd /tmp; ln -sf a l; i=0; while [ $i -lt 10000 ]; do ln -sf a l || { echo "ln failed at $i"; break; }; i=$((i+1)); done; touch newfile; echo touch=$?' strands-shell: ln: filesystem inode limit exceeded ln failed at 9947 strands-shell: touch: filesystem inode limit exceeded touch=1rm -f l; ln -s a lruns 10,000 iterations and then printstouch=0.
Reading order: read ln.rs:35-58 (directory handling and the same-file check), then ln.rs:62-73 (create, then remove on AlreadyExists), then vfs.rs:361-374.
Appendix: non-blocking (3)
- ⚪ Pre-existing:
ln.rs:35-36(let target = &paths[0]; let mut link = paths[1].clone();) silently drops extra operands.ln -sf a b extrareturns 0 and createsb -> a, while GNU saysln: target 'extra': No such file or directoryand returns 1. Now that a directory can be the link name,ln -s a b DIRis an easy thing to type. A one-line guard would fix it:if paths.len() > 2 { return Err("ln: extra operand".into()); }. Fine as a follow-up under #207. - ⚪ Some error messages differ from GNU's wording. Both tools fail and nothing changes in each case.
mkdir d; ln -sf / dprintsd//: cannot overwrite directorybecausefile_name("/")falls back to"/"atln.rs:47-50.ln -sf .. .reports "same file" where GNU reports "cannot overwrite directory", because the check at:55runs before the one at:66. - ⚪
vfs.rscreate_file(around:301-302) uses the same insert-before-dir_insertorder assymlink. It's pre-existing and not on this PR's path. It would be worth fixing in the same pass if you touchsymlink. - Question (non-blocking): agents often use
ln -sfn new curto repoint a symlink at a directory. Without-n,ln -sf new curnow createscur/newinside the old target, which matches GNU but may surprise. Is-nworth tracking under #207?
Repro script and output: strands-agents/shell/pr/234/repro.sh and repro-output.txt in the artifacts bucket.
|
I kept the remaining appendix items separate from these fixes. Multiple-source operands and |
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve: 3776f7d fixes both findings and I found no new issues.
- ✅ Inode leak (
ln.rs:64→vfs.rs:372-373): fixed. The 10,000-replacement CLI probe now ends withtouch=0. It previously failed at iteration 9947. - ✅ The bind_direct test now fails if
-fis rejected as an invalid option.
I'm fine leaving the appendix items (multiple sources, -n/-T, diagnostic wording, create_file) as follow-ups under #207.
What I verified
- The fix itself:
dir_insertonly touches the parent directory and doesn't read the child inode, and nothing that can fail runs between it andinodes.insert, so a failed call can't leave a dangling directory entry. A plainln -s x bover an existingbstill errors withb: already exists, leavesb -> a, and later file creation works. - The new tests catch the bug: I reverted the two-line swap locally, and
failed_symlinks_preserve_inode_budgetandln_force_replacements_preserve_inode_budgetboth fail. With the fix restored they pass. - Test runs at
3776f7d:cargo test --test shell_integration --test vfs_unitpasses 1790 + 97, including 16ln_tests. I couldn't runfmt/clippylocally because those components aren't installed. CI's Rust jobs on ubuntu, macOS and Windows are green. The red rollup is a cancelled duplicatevalidate-pr-titlerun; its rerun passed.
Description
The shell rejected
ln -sf target link, so scripts could not replace an existing symbolic link. Retrying link creation also left unused inodes behind when the destination already existed, eventually exhausting long-lived sessions.This adds force replacement and creates the link inside an existing directory, following GNU behavior while refusing same-file replacement and directory overwrites. It keeps direct-bind files intact when link creation is unsupported, and failed VFS link creation no longer consumes an inode. Hard links, multiple source operands, and
-n/-Tremain outside this change.I compared 12 cases with GNU and ran 10,000 successful replacements followed by creating another file. The inode regressions failed before the fix, and the direct-bind test now checks that force was accepted. The full Rust suite, formatting, lint and documentation checks pass locally.
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.