-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Refactored docs for std::fs::set_permissions_nofollow + fix BSD-based systems to use fchmodat with AT_SYMLINK_NOFOLLOW flag
#160170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asder8215
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
asder8215:docs_set_perms_nofollow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+46
−48
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2036,35 +2036,36 @@ pub fn set_perm(p: &CStr, perm: FilePermissions) -> io::Result<()> { | |
| } | ||
|
|
||
| pub fn set_perm_nofollow(p: &CStr, perm: FilePermissions) -> io::Result<()> { | ||
| // ESP-IDF and Horizon do not support O_NOFOLLOW, so we skip setting it. | ||
| // Their filesystems do not have symbolic links, so no special handling is required. | ||
| cfg_select! { | ||
| // wasm32-wasip1 targets do not support fchmodat, so we fall down to | ||
| // open + fchmod | ||
| target_os = "wasi" => { | ||
| use crate::fs::OpenOptions; | ||
| use crate::fs::Permissions; | ||
| use crate::os::wasi::ffi::OsStrExt; | ||
| use crate::os::wasi::fs::OpenOptionsExt; | ||
|
|
||
| let mut options = OpenOptions::new(); | ||
| options.custom_flags(libc::O_NOFOLLOW); | ||
|
|
||
| let bytes = p.to_bytes(); | ||
| let os_str = OsStr::from_bytes(bytes); | ||
| options.open(Path::new(os_str))?.set_permissions(Permissions::from_inner(perm)) | ||
| } | ||
| all(target_os = "linux", not(any(target_os = "espidf", target_os = "horizon"))) => { | ||
| any(target_os = "linux", target_os = "macos", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "dragonfly", target_os = "android") => { | ||
| cvt_r(|| unsafe { | ||
| libc::fchmodat(libc::AT_FDCWD, p.as_ptr(), perm.mode, libc::AT_SYMLINK_NOFOLLOW) | ||
| }) | ||
| .map(|_| ()) | ||
| }, | ||
| // Not all targets support fchmodat, so we fall back to | ||
| // open + fchmod. | ||
| _ => { | ||
| cvt_r(|| unsafe { | ||
| libc::fchmodat(libc::AT_FDCWD, p.as_ptr(), perm.mode, 0) | ||
| }) | ||
| .map(|_| ()) | ||
| use crate::fs::OpenOptions; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be conservative, I just have the fallback use the previous implementation of |
||
| use crate::fs::Permissions; | ||
| let mut options = OpenOptions::new(); | ||
| // ESP-IDF and Horizon do not support O_NOFOLLOW, so we skip setting it. | ||
| // Their filesystems do not have symbolic links, so no special handling is required. | ||
| #[cfg(not(any(target_os = "espidf", target_os = "horizon")))] | ||
| { | ||
| #[cfg(target_os = "wasi")] | ||
| use crate::os::wasi::fs::OpenOptionsExt; | ||
| #[cfg(not(target_os = "wasi"))] | ||
| use crate::os::unix::fs::OpenOptionsExt; | ||
| options.custom_flags(libc::O_NOFOLLOW); | ||
| } | ||
|
|
||
| // SAFETY: Since this function is called with `with_native_path` | ||
| // and that successfully converted the `&Path` to a `CString`, it | ||
| // should be safe to slice away the nul byte from `&CStr` and convert | ||
| // it back to a `&Path`. | ||
| let path = unsafe { Path::from_u8_slice(p.to_bytes()) }; | ||
| options.open(path)?.set_permissions(Permissions::from_inner(perm)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if I'm bringing useless noise, but the AI keeps alerting me that using
fchmodatwithAT_SYMLINK_NOFOLLOWon Linux behaves very differently compared to BSDs, and might return an error when called on a symlink.For brevity, I won't paste its output here, but just mentioning in case you are not aware.
View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm aware. I've mentioned in the documentation for
set_permissions_nofollowthat on Linux it throws anUnsupportederror and on BSD it can change the symlink permission bits (both behaviors should be verified in a test I created for symlinks).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably reading our docs https://doc.rust-lang.org/nightly/std/fs/fn.set_permissions_nofollow.html#errors