implement std::fs::set_permissions_nofollow on unix

This commit is contained in:
binarycat 2025-06-23 19:06:36 -05:00
parent 3b9d04c62f
commit aa3008d52e
2 changed files with 34 additions and 0 deletions

View file

@ -3128,6 +3128,25 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result
fs_imp::set_permissions(path.as_ref(), perm.0)
}
/// Set the permissions of a file, unless it is a symlink.
///
/// Note that the non-final path elements are allowed to be symlinks.
///
/// # Platform-specific behavior
///
/// Currently unimplemented on Windows.
///
/// On Unix platforms, this results in a [`FilesystemLoop`] error if the last element is a symlink.
///
/// This behavior may change in the future.
///
/// [`FilesystemLoop`]: crate::io::ErrorKind::FilesystemLoop
#[doc(alias = "chmod", alias = "SetFileAttributes")]
#[unstable(feature = "set_permissions_nofollow", issue = "141607")]
pub fn set_permissions_nofollow<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> {
fs_imp::set_permissions_nofollow(path.as_ref(), perm)
}
impl DirBuilder {
/// Creates a new set of options with default mode/security settings for all
/// platforms and also non-recursive.

View file

@ -108,6 +108,21 @@ pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
}
#[cfg(unix)]
pub fn set_permissions_nofollow(path: &Path, perm: crate::fs::Permissions) -> io::Result<()> {
use crate::fs::OpenOptions;
use crate::os::unix::fs::OpenOptionsExt;
OpenOptions::new().custom_flags(libc::O_NOFOLLOW).open(path)?.set_permissions(perm)
}
#[cfg(not(unix))]
pub fn set_permissions_nofollow(_path: &Path, _perm: crate::fs::Permissions) -> io::Result<()> {
crate::unimplemented!(
"`set_permissions_nofollow` is currently only implemented on Unix platforms"
)
}
pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
with_native_path(path, &imp::canonicalize)
}