From 6a2522cca37a921e39bdf451f46095ba3dfa4566 Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Fri, 19 Feb 2016 17:38:33 +0300 Subject: unistd: add chown syscall --- src/unistd.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/unistd.rs') diff --git a/src/unistd.rs b/src/unistd.rs index 1e3bdfb8..6d793aec 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -13,7 +13,7 @@ pub use self::linux::*; mod ffi { use libc::{c_char, c_int, size_t}; - pub use libc::{fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid, setuid, setgid}; + pub use libc::{fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid, setuid, setgid, chown}; #[allow(improper_ctypes)] extern { @@ -28,7 +28,7 @@ mod ffi { // Execute PATH with arguments ARGV and environment from `environ'. // doc: http://man7.org/linux/man-pages/man3/execv.3.html - pub fn execv (path: *const c_char, argv: *const *const c_char) -> c_int; + pub fn execv(path: *const c_char, argv: *const *const c_char) -> c_int; // execute program // doc: http://man7.org/linux/man-pages/man2/execve.2.html @@ -157,6 +157,16 @@ pub fn chdir(path: &P) -> Result<()> { Errno::result(res).map(drop) } +#[inline] +pub fn chown(path: &P, owner: Option, group: Option) -> Result<()> { + let res = try!(path.with_nix_path(|cstr| { + // We use `0 - 1` to get `-1 : {u,g}id_t` which is specified as the no-op value for chown(3). + unsafe { ffi::chown(cstr.as_ptr(), owner.unwrap_or(0 - 1), group.unwrap_or(0 - 1)) } + })); + + Errno::result(res).map(drop) +} + fn to_exec_array(args: &[CString]) -> Vec<*const c_char> { use std::ptr; use libc::c_char; -- cgit v1.2.3