about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbinarycat <binarycat@envs.net>2025-06-23 19:06:36 -0500
committerbinarycat <binarycat@envs.net>2025-08-18 11:37:19 -0500
commitaa3008d52e7372af64bf1beda9dc5da5e653bdb6 (patch)
tree2dc7e5a42c4c31e79f6dbd8df62792e68263e691 /library/std/src
parent3b9d04c62f74b9be46c3ba56cf8393529aa81d26 (diff)
downloadrust-aa3008d52e7372af64bf1beda9dc5da5e653bdb6.tar.gz
rust-aa3008d52e7372af64bf1beda9dc5da5e653bdb6.zip
implement std::fs::set_permissions_nofollow on unix
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/fs.rs19
-rw-r--r--library/std/src/sys/fs/mod.rs15
2 files changed, 34 insertions, 0 deletions
diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs
index 865ea620a28..3da4b0ff855 100644
--- a/library/std/src/fs.rs
+++ b/library/std/src/fs.rs
@@ -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.
diff --git a/library/std/src/sys/fs/mod.rs b/library/std/src/sys/fs/mod.rs
index d55e28074fe..d7f98c86e6e 100644
--- a/library/std/src/sys/fs/mod.rs
+++ b/library/std/src/sys/fs/mod.rs
@@ -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)
 }