Skip to content

chmod: keep operand order with --reference - #15676

Open
fixfon wants to merge 2 commits into
uutils:mainfrom
fixfon:fix/15666-chmod-reference-order
Open

fixfon wants to merge 2 commits into
uutils:mainfrom
fixfon:fix/15666-chmod-reference-order

Conversation

@fixfon

@fixfon fixfon commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #15666

With --reference, clap takes the first FILE operand as MODE, and uumain() pushed it onto the end of the file list, so it was processed last. It's now inserted at the front, so operands are handled in command-line order like GNU.

This changes the -v/-c output order, and it also fixes chmod --reference=ref dir/file dir, which used to fail with "Permission denied" because dir lost its search bit before dir/file was reached.

I added two tests to tests/by-util/test_chmod.rs: one checks the -v order for a b c, the other covers the dir/file dir case (skipped when running as root). Both fail without the fix, and test_chmod passes with it. cargo fmt and clippy -D warnings are clean for uu_chmod. Tested on macOS only.

@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.04 MB (-304 KB, -0.19%)

Significant per-binary changes:
  date     2.61 MB ->    2.30 MB  (-316 KB, -11.81%)

Comment thread src/uu/chmod/src/chmod.rs Outdated
// "--reference" and MODE are mutually exclusive
// if "--reference" was used MODE needs to be interpreted as another FILE
// if "--reference" was used MODE needs to be interpreted as another FILE,
// the first one, so it must go in front to keep the command-line order

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could be simplified to "it is the first FILE, keep the command-line order"

at.set_mode("dir", 0o755);

ucmd.args(&["--reference=ref", "dir/file", "dir"])
.succeeds();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please run cargo fmt, this should fit on one line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I simplified the comment in chmod.rs (5126783). For test_chmod.rs I tried joining the two lines, but rustfmt (chain_width = 60) splits that chain back onto two lines, so the single-line form would fail cargo fmt --check in CI. I left it as is; happy to restructure the chain if you prefer.

With --reference, clap parses the first FILE operand as MODE and uumain() appended it after the other operands, so it was processed last. Insert it at the front instead.

Closes uutils#15666
@sylvestre
sylvestre force-pushed the fix/15666-chmod-reference-order branch from 5126783 to f8aba5d Compare October 26, 2026 19:56

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.

chmod --reference: the first FILE operand is processed last

2 participants