summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikolay Amiantov <ab@fmap.me>2016-02-19 17:38:33 +0300
committerNikolay Amiantov <ab@fmap.me>2016-02-20 00:14:41 +0300
commit6a2522cca37a921e39bdf451f46095ba3dfa4566 (patch)
tree94d0fa456e7122196aaea9ac5b26b1a5e8f1e533
parentb07f3752b6f72b57c05b000987b14302eefb195a (diff)
downloadnix-6a2522cca37a921e39bdf451f46095ba3dfa4566.zip
unistd: add chown syscall
-rw-r--r--src/unistd.rs14
1 files changed, 12 insertions, 2 deletions
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<P: ?Sized + NixPath>(path: &P) -> Result<()> {
Errno::result(res).map(drop)
}
+#[inline]
+pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<uid_t>, group: Option<gid_t>) -> 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;