Skip to content

Commit 7d69442

Browse files
committed
implement std::fs::set_permissions_nofollow on unix
1 parent 42245d3 commit 7d69442

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

library/std/src/fs.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,6 +3128,23 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result
31283128
fs_imp::set_permissions(path.as_ref(), perm.0)
31293129
}
31303130

3131+
/// Set the permissions of a file, unless it is a symlink.
3132+
///
3133+
/// Note that the non-final path elements are allowed to be symlinks.
3134+
///
3135+
/// # Platform-specific behavior
3136+
///
3137+
/// Currently unimplmented on windows.
3138+
///
3139+
/// On unix platforms, this results in a [`io::FilesystemLoop`] error if the last element is a symlink.
3140+
///
3141+
/// This behavior may change in the future.
3142+
#[doc(alias = "chmod", alias = "SetFileAttributes")]
3143+
#[unstable(feature = "set_permissions_nofollow", issue = "141607")]
3144+
pub fn set_permissions_nofollow<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> {
3145+
fs_imp::set_permissions(path.as_ref(), perm)
3146+
}
3147+
31313148
impl DirBuilder {
31323149
/// Creates a new set of options with default mode/security settings for all
31333150
/// platforms and also non-recursive.

library/std/src/sys/fs/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
108108
with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
109109
}
110110

111+
#[cfg(unix)]
112+
pub fn set_permissions_nofollow(path: &Path, perm: fs::Permissions) -> io::Result<()> {
113+
use crate::fs::OpenOptions;
114+
use crate::os::unix::fs::OpenOptionsExt;
115+
116+
OpenOptions::new().custom_flags(libc::O_NOFOLLOW).open(path)?.set_permissions(perm)
117+
}
118+
119+
#[cfg(not(unix))]
120+
pub fn set_permissions_nofollow(path: &Path, perm: FilePermissions) -> io::Result<()> {
121+
unimplimented!("set_permissions_nofollow is not implmented on non-unix platforms")
122+
}
123+
111124
pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
112125
with_native_path(path, &imp::canonicalize)
113126
}

0 commit comments

Comments
 (0)