Skip to content

Commit 9c46cdb

Browse files
authored
Rollup merge of #142938 - lolbinarycat:std-set_permissions_nofollow, r=ibraheemdev
implement std::fs::set_permissions_nofollow on unix implementation of #141607
2 parents bd0e768 + aa3008d commit 9c46cdb

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

library/std/src/fs.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,6 +3156,25 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result
31563156
fs_imp::set_permissions(path.as_ref(), perm.0)
31573157
}
31583158

3159+
/// Set the permissions of a file, unless it is a symlink.
3160+
///
3161+
/// Note that the non-final path elements are allowed to be symlinks.
3162+
///
3163+
/// # Platform-specific behavior
3164+
///
3165+
/// Currently unimplemented on Windows.
3166+
///
3167+
/// On Unix platforms, this results in a [`FilesystemLoop`] error if the last element is a symlink.
3168+
///
3169+
/// This behavior may change in the future.
3170+
///
3171+
/// [`FilesystemLoop`]: crate::io::ErrorKind::FilesystemLoop
3172+
#[doc(alias = "chmod", alias = "SetFileAttributes")]
3173+
#[unstable(feature = "set_permissions_nofollow", issue = "141607")]
3174+
pub fn set_permissions_nofollow<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> {
3175+
fs_imp::set_permissions_nofollow(path.as_ref(), perm)
3176+
}
3177+
31593178
impl DirBuilder {
31603179
/// Creates a new set of options with default mode/security settings for all
31613180
/// platforms and also non-recursive.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ pub fn set_permissions(path: &Path, perm: FilePermissions) -> io::Result<()> {
114114
with_native_path(path, &|path| imp::set_perm(path, perm.clone()))
115115
}
116116

117+
#[cfg(unix)]
118+
pub fn set_permissions_nofollow(path: &Path, perm: crate::fs::Permissions) -> io::Result<()> {
119+
use crate::fs::OpenOptions;
120+
use crate::os::unix::fs::OpenOptionsExt;
121+
122+
OpenOptions::new().custom_flags(libc::O_NOFOLLOW).open(path)?.set_permissions(perm)
123+
}
124+
125+
#[cfg(not(unix))]
126+
pub fn set_permissions_nofollow(_path: &Path, _perm: crate::fs::Permissions) -> io::Result<()> {
127+
crate::unimplemented!(
128+
"`set_permissions_nofollow` is currently only implemented on Unix platforms"
129+
)
130+
}
131+
117132
pub fn canonicalize(path: &Path) -> io::Result<PathBuf> {
118133
with_native_path(path, &imp::canonicalize)
119134
}

0 commit comments

Comments
 (0)