|
5 | 5 | use super::platform::fs::MetadataExt as _;
|
6 | 6 | use crate::fs::{self, OpenOptions, Permissions};
|
7 | 7 | use crate::io;
|
| 8 | +use crate::os::unix::io::{AsFd, AsRawFd}; |
8 | 9 | use crate::path::Path;
|
9 | 10 | use crate::sys;
|
10 | 11 | use crate::sys_common::{AsInner, AsInnerMut, FromInner};
|
@@ -924,6 +925,75 @@ impl DirBuilderExt for fs::DirBuilder {
|
924 | 925 | }
|
925 | 926 | }
|
926 | 927 |
|
| 928 | +/// Change the owner and group of the specified path. |
| 929 | +/// |
| 930 | +/// Specifying either the uid or gid as `None` will leave it unchanged. |
| 931 | +/// |
| 932 | +/// Changing the owner typically requires privileges, such as root or a specific capability. |
| 933 | +/// Changing the group typically requires either being the owner and a member of the group, or |
| 934 | +/// having privileges. |
| 935 | +/// |
| 936 | +/// If called on a symbolic link, this will change the owner and group of the link target. To |
| 937 | +/// change the owner and group of the link itself, see [`lchown`]. |
| 938 | +/// |
| 939 | +/// # Examples |
| 940 | +/// |
| 941 | +/// ```no_run |
| 942 | +/// #![feature(unix_chown)] |
| 943 | +/// use std::os::unix::fs; |
| 944 | +/// |
| 945 | +/// fn main() -> std::io::Result<()> { |
| 946 | +/// fs::chown("/sandbox", Some(0), Some(0))?; |
| 947 | +/// Ok(()) |
| 948 | +/// } |
| 949 | +/// ``` |
| 950 | +#[unstable(feature = "unix_chown", issue = "88989")] |
| 951 | +pub fn chown<P: AsRef<Path>>(dir: P, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> { |
| 952 | + sys::fs::chown(dir.as_ref(), uid.unwrap_or(u32::MAX), gid.unwrap_or(u32::MAX)) |
| 953 | +} |
| 954 | + |
| 955 | +/// Change the owner and group of the file referenced by the specified open file descriptor. |
| 956 | +/// |
| 957 | +/// For semantics and required privileges, see [`chown`]. |
| 958 | +/// |
| 959 | +/// # Examples |
| 960 | +/// |
| 961 | +/// ```no_run |
| 962 | +/// #![feature(unix_chown)] |
| 963 | +/// use std::os::unix::fs; |
| 964 | +/// |
| 965 | +/// fn main() -> std::io::Result<()> { |
| 966 | +/// let f = std::fs::File::open("/file")?; |
| 967 | +/// fs::fchown(f, Some(0), Some(0))?; |
| 968 | +/// Ok(()) |
| 969 | +/// } |
| 970 | +/// ``` |
| 971 | +#[unstable(feature = "unix_chown", issue = "88989")] |
| 972 | +pub fn fchown<F: AsFd>(fd: F, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> { |
| 973 | + sys::fs::fchown(fd.as_fd().as_raw_fd(), uid.unwrap_or(u32::MAX), gid.unwrap_or(u32::MAX)) |
| 974 | +} |
| 975 | + |
| 976 | +/// Change the owner and group of the specified path, without dereferencing symbolic links. |
| 977 | +/// |
| 978 | +/// Identical to [`chown`], except that if called on a symbolic link, this will change the owner |
| 979 | +/// and group of the link itself rather than the owner and group of the link target. |
| 980 | +/// |
| 981 | +/// # Examples |
| 982 | +/// |
| 983 | +/// ```no_run |
| 984 | +/// #![feature(unix_chown)] |
| 985 | +/// use std::os::unix::fs; |
| 986 | +/// |
| 987 | +/// fn main() -> std::io::Result<()> { |
| 988 | +/// fs::lchown("/symlink", Some(0), Some(0))?; |
| 989 | +/// Ok(()) |
| 990 | +/// } |
| 991 | +/// ``` |
| 992 | +#[unstable(feature = "unix_chown", issue = "88989")] |
| 993 | +pub fn lchown<P: AsRef<Path>>(dir: P, uid: Option<u32>, gid: Option<u32>) -> io::Result<()> { |
| 994 | + sys::fs::lchown(dir.as_ref(), uid.unwrap_or(u32::MAX), gid.unwrap_or(u32::MAX)) |
| 995 | +} |
| 996 | + |
927 | 997 | /// Change the root directory of the current process to the specified path.
|
928 | 998 | ///
|
929 | 999 | /// This typically requires privileges, such as root or a specific capability.
|
|
0 commit comments