Skip to content

Added implementation on set_permissions_nofollow for all primary platforms#158168

Open
asder8215 wants to merge 1 commit into
rust-lang:mainfrom
asder8215:windows_set_permissions_nofollow
Open

Added implementation on set_permissions_nofollow for all primary platforms#158168
asder8215 wants to merge 1 commit into
rust-lang:mainfrom
asder8215:windows_set_permissions_nofollow

Conversation

@asder8215

@asder8215 asder8215 commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

View all comments

For context, this PR is related to the tracking issue for std::fs::set_permissions_nofollow. This PR does a few different things:

  • Windows support is provided for std::fs::set_permissions_nofollow. On Windows, it uses OpenOptions::open with the custom flag FILE_FLAG_OPEN_REPARSE_POINT enabled and then sets the permissions on the reparse point directly. All other platforms (hermit, motor, solid, uefi, etc.) defer to what fs::set_permissions does since symlinks aren't supported on those platforms, so they effectively do the same thing.
  • The implementation for Unix was modified to use fchmodat instead of doing two separate calls (open and then fchmod via OpenOptions).
  • Because the previous implementations actually used fchmod instead of chmod underneath the hood (as that's what File::set_permissions, which is not to be confused with fs::set_permissions as that does use chmod, does underneath the hood), it actually had some incorrect documentation about the error returned by fchmod on symlinks. Instead of io::ErrorKind::FilesystemLoop, it returns io::ErrorKind::InvalidInput. You can read the documentation for fchmod. fchmodat does effectively the same thing as the open + fchmod combo, but it does return a different error, io::ErrorKind::Unsupported, that makes more sense. Regardless, I corrected the documentation for std::fs::set_permissions_nofollow and added a lot more clarifying information.
  • A test case has been added to verify if set_permissions_nofollow is operating correctly.

cc @ChrisDenton and @lolbinarycat

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 20, 2026
@rustbot

rustbot commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

r? @clarfonthey

rustbot has assigned @clarfonthey.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 12 candidates
  • Random selection from Darksonn, Mark-Simulacrum, clarfonthey, jhpratt

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from 3411717 to 96bc9a1 Compare June 20, 2026 07:00
@rust-log-analyzer

This comment has been minimized.

Comment thread library/std/src/sys/fs/hermit.rs Outdated
Comment thread library/std/src/sys/fs/uefi.rs Outdated
Comment thread library/std/src/fs/tests.rs Outdated
Comment thread library/std/src/fs/tests.rs
Comment thread library/std/src/sys/fs/motor.rs Outdated
}

pub fn set_perm_nofollow(path: &Path, perm: FilePermissions) -> io::Result<()> {
set_perm(path, perm)

@clarfonthey clarfonthey Jun 20, 2026

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.

Similar to UEFI, I would swap the method bodies here, so that set_perm defers to set_perm_nofollow plus a comment that says symlinks aren't supported.

View changes since the review

Comment thread library/std/src/sys/fs/solid.rs Outdated
@clarfonthey

Copy link
Copy Markdown
Contributor

@rustbot author

CI failure appears to be a simple typo but I also left some feedback on other stuff to fix. Otherwise, implementation looks good, although I would add an extra comment on the tracking issue once this is merged to have some Windows folks double-check that opening all reparse points in this method doesn't do anything we don't want, since that technically includes more than just symlinks. It should work in pretty much all normal use cases, just want to double-check the edge cases before stabilisation.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 20, 2026
@rustbot

rustbot commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

This comment has been minimized.

@asder8215 asder8215 requested a review from clarfonthey June 21, 2026 07:27
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 21, 2026
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from f9c87ed to feafe24 Compare June 21, 2026 08:24
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from feafe24 to b286acf Compare June 21, 2026 08:51
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from b286acf to 14df6fa Compare June 21, 2026 19:11
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from 14df6fa to 7029495 Compare June 21, 2026 19:32
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from 7029495 to 6e1b03a Compare June 21, 2026 20:31
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from 6e1b03a to e62cd46 Compare June 21, 2026 20:56
check!(file.set_permissions(p));
}

#[test]

@clarfonthey clarfonthey Jun 22, 2026

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.

I assume you didn't mean to keep this in?

View changes since the review

@asder8215 asder8215 Jun 22, 2026

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.

No, I did mean to keep this in. I templated this test off the two tests before this chmod_works()/fchmod_works() (it's before this test). But to be honest, this test is using both fchmod/fchmodat depending on platform.

Which reminds me that I need to update documentation to mention that it uses open + fchmodon wasi platforms.

@clarfonthey clarfonthey Jun 22, 2026

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.

I guess that the main confusion here is that aren't there platforms where this will just fail, e.g. ones without any permissions, like VexOS?

@asder8215 asder8215 Jun 23, 2026

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.

Now that you ask that, does CI run these tests on platforms like VexOS/UEFI? I know it checks if the code compiles on those platforms, but I haven't noticed if CI actually runs these tests on those platforms.

This should definitely panic on certain lines like 1413.

@clarfonthey

Copy link
Copy Markdown
Contributor

r=me minus one small mistake, plus figuring out why the tests aren't running atm.

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from e62cd46 to 18fa184 Compare June 22, 2026 04:27
@rust-log-analyzer

This comment has been minimized.

@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch 2 times, most recently from c739454 to a7222bd Compare June 22, 2026 23:36
@rust-log-analyzer

This comment has been minimized.

@asder8215

Copy link
Copy Markdown
Contributor Author

This is the same error as #156269. I'm not sure what the CI error is for.

…pported (windows, unix, uefi, etc.); modified the Unix implementation to use fchmodat instead of open + fchmod; clarified documentations for set_permissions_nofollow; added a test case for set_permissions_nofollow
@asder8215 asder8215 force-pushed the windows_set_permissions_nofollow branch from a7222bd to 4804060 Compare June 23, 2026 14:38
@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@asder8215

Copy link
Copy Markdown
Contributor Author

@clarfonthey Finally got this to work with CI (sorry about the many force pushes!).

Let me know if you want me to remove the fchmodat_works test or not.

@asder8215 asder8215 requested a review from clarfonthey June 23, 2026 16:24
@clarfonthey

Copy link
Copy Markdown
Contributor

Force-pushes are totally fine; I can deal. Will try and take a look later today.

@clarfonthey

clarfonthey commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Just kidding, had some extra time to check this out and it looks good to me.

@bors r+ rollup=iffy

(touches multiple targets that might not be tested in PR CI. if rollup fails, we can do a dedicated try job first.)

@rust-bors

rust-bors Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4804060 has been approved by clarfonthey

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 23, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 23, 2026
…nofollow, r=clarfonthey

Added implementation on `set_permissions_nofollow` for all primary platforms

For context, this PR is related to the tracking issue for [`std::fs::set_permissions_nofollow`](rust-lang#141607). This PR does a few different things:

* Windows support is provided for `std::fs::set_permissions_nofollow`. On Windows, it uses `OpenOptions::open` with the custom flag `FILE_FLAG_OPEN_REPARSE_POINT` enabled and then sets the permissions on the reparse point directly. All other platforms (hermit, motor, solid, uefi, etc.) defer to what `fs::set_permissions` does since symlinks aren't supported on those platforms, so they effectively do the same thing.
* The implementation for Unix was modified to use [`fchmodat`](https://linux.die.net/man/2/fchmodat) instead of doing two separate calls (`open` and then `fchmod` via `OpenOptions`).
* Because the previous implementations actually used `fchmod` instead of `chmod` underneath the hood (as that's what `File::set_permissions`, which is not to be confused with `fs::set_permissions` as that does use `chmod`, does underneath the hood), it actually had some incorrect documentation about the error returned by `fchmod` on symlinks. Instead of `io::ErrorKind::FilesystemLoop`, it returns `io::ErrorKind::InvalidInput`. You can read the documentation for [`fchmod`](https://pubs.opengroup.org/onlinepubs/009696699/functions/fchmod.html). `fchmodat` does effectively the same thing as the `open` + `fchmod` combo, but it does return a different error, `io::ErrorKind::Unsupported`, that makes more sense. Regardless, I corrected the documentation for `std::fs::set_permissions_nofollow` and added a lot more clarifying information.
* A test case has been added to verify if `set_permissions_nofollow` is operating correctly.

cc @ChrisDenton and @lolbinarycat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants