Skip to content

Mkdir setgid #10428#10456

Draft
cerdelen wants to merge 5 commits intouutils:mainfrom
cerdelen:mkdir_setgid
Draft

Mkdir setgid #10428#10456
cerdelen wants to merge 5 commits intouutils:mainfrom
cerdelen:mkdir_setgid

Conversation

@cerdelen
Copy link
Contributor

Fix: #10428

Note: when testing on my macos system with GNU utils i saw that mkdir does not inherit the setgid flag. So this change would not make macos uutils act like macos GNU utils but rather macos uutils like linux GNU uutils.

I leave it for a maintainer to decide which way is more desired.

@cerdelen
Copy link
Contributor Author

Also another note about why i use chmod after the creation of the directory to change the permission.

I tried first creating the right mode with setgid bit set for line 283
stf::fs::Dirbuilder
but the resulting dir had the setgid bit unset leaving only the option to manually set it afterwards.

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/seq/seq-epipe is now passing!

@github-actions
Copy link

GNU testsuite comparison:

Skip an intermittent issue tests/shuf/shuf-reservoir (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/sort/sort-stale-thread-mem (fails in this run but passes in the 'main' branch)

@cerdelen cerdelen marked this pull request as ready for review January 23, 2026 21:32
@sylvestre
Copy link
Contributor

sorry but what is the issue being fixed here ?
#10428 isn't super explicit here

@cerdelen
Copy link
Contributor Author

On Linux using mkdir creates a directory which inherits the setgid bitflag from its parent folder. In the linked comment of this issue it was discussed that this behaviour doesn't work in macOS/bsd.

When trying to reproduce it with gnu utils mkdir on macOS it doesn't seem to inherit the setgid bit though.

@cerdelen
Copy link
Contributor Author

#10412 (comment)

As this comment notices it's done on a kernel level and the question is if we want the same behaviour if uutils mkdir between Linux and macOS that setgid is always beeing inherited or if we want uutils mkdir to behave like gnu uutils on macOS which does in fact not inherit setgid

if let Some(parent) = path.parent() {
if let Ok(parent_metadata) = parent.metadata() {
if let Ok(paths_metadata) = path.metadata() {
if (parent_metadata.mode() & 0o2000) != (paths_metadata.mode() & 0o2000) {
Copy link
Contributor

Choose a reason for hiding this comment

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

condition checks if bits are different, should check if parent has setgid AND child doesn't: `(parent_metadata.mode() & 0o2000) != 0 && (paths_metadata.mode() & 0o2000) == 0

no ?

#[cfg(all(unix, not(target_os = "linux")))]
fn inherit_setgid_bit(path: &Path) -> UResult<()> {
if let Some(parent) = path.parent() {
if let Ok(parent_metadata) = parent.metadata() {
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens in case of error on metadata() ?

if (parent_metadata.mode() & 0o2000) != (paths_metadata.mode() & 0o2000) {
chmod(
path,
paths_metadata.mode() | parent_metadata.mode() & 0o2000,
Copy link
Contributor

Choose a reason for hiding this comment

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

should be paths_metadata.mode() | 0o2000 since we already verified parent has the bit, no?

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/pr/bounded-memory is no longer failing!

@cerdelen cerdelen marked this pull request as draft February 12, 2026 21:04
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.

mkdir won't inherit setgid bit automatically in macos and bsd

2 participants