about summary refs log tree commit diff
path: root/library/std/src/sys
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-09-17 17:41:18 +0200
committerGitHub <noreply@github.com>2021-09-17 17:41:18 +0200
commit723d27934bcf7b8ded74c8a90298fdaece99ebd2 (patch)
treebf6fa85463a0f428c8f68b268e97b544bf52fe0a /library/std/src/sys
parent6f5c09849ed46b767922675d18e9d7fcbfbb69e2 (diff)
parent862d89e3b5ba092d95586fc2a33b23d1c8672087 (diff)
downloadrust-723d27934bcf7b8ded74c8a90298fdaece99ebd2.tar.gz
rust-723d27934bcf7b8ded74c8a90298fdaece99ebd2.zip
Rollup merge of #88953 - joshtriplett:chown, r=dtolnay
Add chown functions to std::os::unix::fs to change the owner and group of files

This is a straightforward wrapper that uses the existing helpers for C
string handling and errno handling.

Having this available is convenient for UNIX utility programs written in
Rust, and avoids having to call unsafe functions like `libc::chown`
directly and handle errors manually, in a program that may otherwise be
entirely safe code.

In addition, these functions provide a more Rustic interface by
accepting appropriate traits and using `None` rather than `-1`.
Diffstat (limited to 'library/std/src/sys')
-rw-r--r--library/std/src/sys/unix/fs.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs
index 6d7524a733a..a4fff9b2e64 100644
--- a/library/std/src/sys/unix/fs.rs
+++ b/library/std/src/sys/unix/fs.rs
@@ -1416,6 +1416,23 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
     Ok(bytes_copied as u64)
 }
 
+pub fn chown(path: &Path, uid: u32, gid: u32) -> io::Result<()> {
+    let path = cstr(path)?;
+    cvt(unsafe { libc::chown(path.as_ptr(), uid as libc::uid_t, gid as libc::gid_t) })?;
+    Ok(())
+}
+
+pub fn fchown(fd: c_int, uid: u32, gid: u32) -> io::Result<()> {
+    cvt(unsafe { libc::fchown(fd, uid as libc::uid_t, gid as libc::gid_t) })?;
+    Ok(())
+}
+
+pub fn lchown(path: &Path, uid: u32, gid: u32) -> io::Result<()> {
+    let path = cstr(path)?;
+    cvt(unsafe { libc::lchown(path.as_ptr(), uid as libc::uid_t, gid as libc::gid_t) })?;
+    Ok(())
+}
+
 #[cfg(not(any(target_os = "fuchsia", target_os = "vxworks")))]
 pub fn chroot(dir: &Path) -> io::Result<()> {
     let dir = cstr(dir)?;