Repository navigation
realpath, readlink: keep two-link symlink loop name with -m - #16564
DeepakRoy58 wants to merge 2 commits into
Conversation
| .and_then(|p| p.parent()) | ||
| .map(PathBuf::from) | ||
| == Some(result.clone()) | ||
| && link_path.file_name() == base_symlink.as_ref().and_then(|p| p.file_name()) |
There was a problem hiding this comment.
this also triggers without any loop: with a -> b and b -> sub/a, realpath -m a now prints $PWD/a instead of $PWD/sub/a
please add a test for it
| miss_mode: MissingHandling, | ||
| res_mode: ResolveMode, | ||
| ) -> IOResult<PathBuf> { | ||
| let loop_component = original.as_ref().components().find_map(|c| match c { |
There was a problem hiding this comment.
why only the first component of the argument? realpath -m "$PWD/a" or realpath -m dir/a still fail with ELOOP
| result.pop(); | ||
| // GNU keeps the name of a two-link symlink loop when missing | ||
| // components are allowed (-m), instead of reporting ELOOP. | ||
| if miss_mode == MissingHandling::Missing |
There was a problem hiding this comment.
this is quite complex for what it does. Could we keep a set of the links already visited and, in Missing mode, stop on a revisit instead?
That should also cover the p -> a case from the issue.
| == Some(result.clone()) | ||
| && link_path.file_name() == base_symlink.as_ref().and_then(|p| p.file_name()) | ||
| { | ||
| let mut loop_result = base_symlink.as_ref().unwrap().clone(); |
There was a problem hiding this comment.
please avoid the unwrap(), if let Some(base) = &base_symlink would do
| Ok(result) | ||
| } | ||
|
|
||
| #[derive(Clone, PartialEq)] |
There was a problem hiding this comment.
is this derive needed? i don't see where it is used
| let expect = path_concat!(at.root_dir_resolved(), "a") + "\n"; | ||
| assert_eq!(actual, expect); | ||
|
|
||
| let (at, mut ucmd) = at_and_ucmd!(); |
There was a problem hiding this comment.
same setup twice, one scene with two ucmd() calls would do. Please also add a case with an absolute path
|
thanks for the PR! |
|
GNU testsuite comparison: |
Merging this PR will improve performance by 20.31%2 warnings, 1 note
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
f068705 to
4e94c40
Compare
What changed
With
-m, GNU does not fail on a two-link symlink loop. It keeps the loop'sname instead:
realpath -m aprints the absolute path ofarealpath -m a/xprints the absolute path ofa/xuutils previously reported
Too many levels of symbolic linksfor both. Theshared
canonicalizefunction now detects a two-link loop in missing mode andreturns the loop name plus any trailing components.
Why
GNU treats the loop as an unresolved component when missing components are
allowed. This matches the reported issue and avoids a spurious failure.
Scope
This change only affects two-link symlink loops in missing mode. Growing loops
such as
foo -> foo/barstill fail, as before. Other modes and path cases areunchanged.
Overlapping PRs
PRs #16153, #16034, and #15388 also touch
canonicalize. This change onlyaffects symlink loops and does not alter their dangling-symlink,
non-directory-component, or permission-error handling.
Compared with GNU
GNU coreutils 9.12 on Arch Linux:
a -> b,b -> a:realpath -m a->/tmp/.../a, exit 0realpath -m a/x->/tmp/.../a/x, exit 0readlink -m a->/tmp/.../a, exit 0readlink -m a/x->/tmp/.../a/x, exit 0uutils after the fix produces the same results.
Tests
cargo fmt --all: passedcargo clippy -p uucore -p uu_realpath -p uu_readlink: passedcargo test --features unix --test tests -- symlink_loop_missing: 2 passedcargo test --features unix --test tests -- test_realpath:: test_readlink::: 65 passedcloses #16325