Skip to content

chown, chgrp, chmod: let the last of --dereference and -h win - #15881

Open
fixfon wants to merge 1 commit into
uutils:mainfrom
fixfon:fix/15791-chown-dereference-override
Open

fixfon wants to merge 1 commit into
uutils:mainfrom
fixfon:fix/15791-chown-dereference-override

Conversation

@fixfon

@fixfon fixfon commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #15791

chown, chgrp and chmod take --dereference and -h/--no-dereference from common_args(). The two were independent flags, and configure_symlink_and_recursion tests --dereference first. So with --dereference -h both flags were set, -h was ignored, and the command followed the symlink and changed its target. -H, -L and -P already override each other, and chcon does the same for this pair.

--dereference now carries .overrides_with(NO_DEREFERENCE). clap applies this in both directions, so the last of the two options wins whichever order they come in.

One side effect: -R --dereference -h used to fail with "-R --dereference requires -H or -L" and is now accepted, because -h wins. -R -h --dereference still fails with that message.

I added one test per utility (test_chown_dereference_then_no_dereference, and the same for chgrp and chmod). chown and chgrp use a dangling symlink and the caller's own uid/gid, so no root is needed: --dereference -h must succeed and -h --dereference must fail with "cannot dereference". chmod checks the target's mode after each order. The three tests fail on current main and pass with this change. I also ran the issue's option sequences through the CLI before and after as an unprivileged user.

cargo fmt --all -- --check, cargo clippy --all-targets -- -D warnings at the repo root, and cargo test --no-default-features --features "chown chgrp chmod" --test tests -- test_chown:: test_chgrp:: test_chmod:: pass. Tested on macOS only.

--dereference and -h were independent flags and --dereference was
tested first, so a later -h was ignored and the link's target was
changed. They now override each other like -H/-L/-P.

Fixes uutils#15791

This branch has not been deployed

No deployments
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.

chown, chgrp, chmod: -h ignored after --dereference, symlinks followed

1 participant