Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions library/std/src/sys/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,10 @@ pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {

#[cfg(all(unix, not(target_os = "vxworks")))]
pub fn set_permissions_nofollow(path: &Path, perm: crate::fs::Permissions) -> io::Result<()> {
use crate::fs::OpenOptions;

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")))]
{
use crate::os::unix::fs::OpenOptionsExt;
options.custom_flags(libc::O_NOFOLLOW);
}

options.open(path)?.set_permissions(perm)
// Operate on the path itself (do not follow a trailing symlink) via fchmodat
// where supported. Opening with O_NOFOLLOW fails with ELOOP on Linux/macOS
// when the path is a symlink, which cannot implement "chmod the link".
with_native_path(path, &|path| imp::set_perm_nofollow(path, perm.0.clone()))
}

#[cfg(any(not(unix), target_os = "vxworks"))]
Expand Down
17 changes: 17 additions & 0 deletions library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,23 @@ pub fn set_perm(p: &CStr, perm: FilePermissions) -> io::Result<()> {
cvt_r(|| unsafe { libc::chmod(p.as_ptr(), perm.mode) }).map(|_| ())
}

/// Change permissions without following the final path component if it is a symlink.
///
/// Uses `fchmodat(..., AT_SYMLINK_NOFOLLOW)` where available. Platforms without
/// that API (or without symlinks) fall back to normal `chmod` via `set_perm`.
#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita", target_os = "vxworks")))]
pub fn set_perm_nofollow(p: &CStr, perm: FilePermissions) -> io::Result<()> {
cvt_r(|| unsafe {
libc::fchmodat(libc::AT_FDCWD, p.as_ptr(), perm.mode, libc::AT_SYMLINK_NOFOLLOW)
})
.map(|_| ())
}

#[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita", target_os = "vxworks"))]
pub fn set_perm_nofollow(p: &CStr, perm: FilePermissions) -> io::Result<()> {
set_perm(p, perm)
}

pub fn rmdir(p: &CStr) -> io::Result<()> {
cvt(unsafe { libc::rmdir(p.as_ptr()) }).map(|_| ())
}
Expand Down