diff options
| author | 许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com> | 2025-08-19 19:42:02 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-19 19:42:02 +0800 |
| commit | 9c46cdb43a371f35502a23cece900838ee53a5d2 (patch) | |
| tree | a4cf30c81a279c5c10980f93b5f3bc732299d33e /library/std/src | |
| parent | bd0e768fff3b72f3c28075e9dee00b4318a0d7ea (diff) | |
| parent | aa3008d52e7372af64bf1beda9dc5da5e653bdb6 (diff) | |
| download | rust-9c46cdb43a371f35502a23cece900838ee53a5d2.tar.gz rust-9c46cdb43a371f35502a23cece900838ee53a5d2.zip | |
Rollup merge of #142938 - lolbinarycat:std-set_permissions_nofollow, r=ibraheemdev
implement std::fs::set_permissions_nofollow on unix implementation of https://github.com/rust-lang/rust/issues/141607
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/fs.rs | 19 | ||||
| -rw-r--r-- | library/std/src/sys/fs/mod.rs | 15 |
2 files changed, 34 insertions, 0 deletions
diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index d4a584f4d14..1ed4f2f9f0c 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -3156,6 +3156,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 d9740e15789..dbd782f5018 100644 --- a/library/std/src/sys/fs/mod.rs +++ b/library/std/src/sys/fs/mod.rs @@ -114,6 +114,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) } |
