-
-
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
base: main
Are you sure you want to change the base?
Changes from all commits
69de9ae
a793546
c1de9c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2036,35 +2036,77 @@ 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); | ||
| target_os = "linux" => { | ||
| let res = cvt_r(|| unsafe { | ||
| libc::fchmodat(libc::AT_FDCWD, p.as_ptr(), perm.mode, libc::AT_SYMLINK_NOFOLLOW) | ||
| }) | ||
| .map(|_| ()); | ||
|
|
||
| match res { | ||
| Ok(_) => return Ok(()), | ||
| Err(err) => { | ||
| if err.kind() == crate::io::ErrorKind::Unsupported { | ||
| use crate::fs::OpenOptions; | ||
| use crate::fs::Permissions; | ||
| use crate::os::unix::ffi::OsStrExt; | ||
| use crate::os::unix::fs::OpenOptionsExt; | ||
|
|
||
| let mut options = OpenOptions::new(); | ||
| options.read(true).custom_flags(libc::O_NOFOLLOW); | ||
|
|
||
| let os_str = OsStr::from_bytes(p.to_bytes()); | ||
| let path = Path::new(os_str); | ||
| match options.open(path) { | ||
| Ok(file) => { | ||
| return file.set_permissions(Permissions::from_inner(perm)); | ||
| }, | ||
| Err(e) => { | ||
| if e.kind() == crate::io::ErrorKind::FilesystemLoop { | ||
| // When O_NOFOLLOW flag is enabled, if the trailing component of | ||
| // a path is a symbolic link, open should fail with ELOOP error | ||
| // For consistency with other Linux distributions, we return | ||
| // `ErrorKind::Unsupported`. | ||
| return Err(err); | ||
| } | ||
| return Err(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let bytes = p.to_bytes(); | ||
| let os_str = OsStr::from_bytes(bytes); | ||
| options.open(Path::new(os_str))?.set_permissions(Permissions::from_inner(perm)) | ||
| return Err(err); | ||
| } | ||
| } | ||
| } | ||
| all(target_os = "linux", not(any(target_os = "espidf", target_os = "horizon"))) => { | ||
| any(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.read(true).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)) | ||
|
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. A part of me is wondering whether this should return |
||
| } | ||
| } | ||
| } | ||
|
|
||
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