haggis_rs/src/nix/mod.rs

82 lines
2.2 KiB
Rust

#![allow(clippy::must_use_candidate)]
//! Low level Unix api functions
use {
crate::Error,
std::{ffi::CString, io},
};
/// Get the effective user id of the current process
pub fn geteuid() -> u32 {
unsafe { libc::geteuid() }
}
pub fn chown(path: &str, uid: u32, gid: u32) -> Result<(), Error> {
let ret = unsafe { libc::chown(CString::new(path)?.as_ptr(), uid, gid) };
if ret == 0 {
Ok(())
} else {
Err(Error::Io(io::Error::last_os_error()))
}
}
#[cfg(target_os = "linux")]
pub fn chmod(path: &str, mode: u32) -> Result<(), Error> {
let ret = unsafe { libc::chmod(CString::new(path)?.as_ptr(), mode) };
if ret == 0 {
Ok(())
} else {
Err(Error::Io(io::Error::last_os_error()))
}
}
#[cfg(target_os = "freebsd")]
pub fn chmod(path: &str, mode: u32) -> Result<(), Error> {
let ret = unsafe { libc::chmod(CString::new(path)?.as_ptr(), mode.try_into()?) };
if ret == 0 {
Ok(())
} else {
Err(Error::Io(io::Error::last_os_error()))
}
}
#[cfg(target_os = "linux")]
pub fn mkfifo(path: &str, mode: u32) -> Result<(), Error> {
let ret = unsafe { libc::mkfifo(CString::new(path)?.as_ptr(), mode) };
if ret == 0 {
Ok(())
} else {
Err(Error::Io(io::Error::last_os_error()))
}
}
#[cfg(target_os = "freebsd")]
pub fn mkfifo(path: &str, mode: u32) -> Result<(), Error> {
let ret = unsafe { libc::mkfifo(CString::new(path)?.as_ptr(), mode.try_into()?) };
if ret == 0 {
Ok(())
} else {
Err(Error::Io(io::Error::last_os_error()))
}
}
#[cfg(target_os = "linux")]
pub fn mknod(path: &str, mode: u32, major: u32, minor: u32) -> Result<(), Error> {
let dev = libc::makedev(major, minor);
let res = unsafe { libc::mknod(CString::new(path)?.as_ptr(), mode, dev) };
if res == 0 {
Ok(())
} else {
Err(Error::Io(io::Error::last_os_error()))
}
}
#[cfg(target_os = "freebsd")]
pub fn mknod(path: &str, mode: u32, major: u32, minor: u32) -> Result<(), Error> {
let dev = libc::makedev(major, minor);
let res = unsafe { libc::mknod(CString::new(path)?.as_ptr(), mode.try_into()?, dev) };
if res == 0 {
Ok(())
} else {
Err(Error::Io(io::Error::last_os_error()))
}
}