use dialoguer::Input;
use std::error::Error;
use std::path::{Path, PathBuf};
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Resource {
pub at: usize,
pub location: PathBuf,
}
impl Resource {}
pub fn try_interaction(
interactive: bool,
prompt: String,
) -> (Result<Option<bool>, Box<dyn Error>>, bool) {
let interaction = if interactive {
match get_interactive_confirmation(prompt) {
Ok(i) => Ok(Some(i)),
Err(e) => Err(e),
}
} else {
Ok(None)
};
let confirmed = match interaction {
Ok(maybe_input) => maybe_input.unwrap_or(false),
Err(_) => false,
};
(interaction, confirmed)
}
#[allow(dead_code)]
fn get_interactive_confirmation(prompt: String) -> Result<bool, Box<dyn Error>> {
let input = Input::new()
.with_prompt(prompt)
.validate_with(|c_input: &String| -> Result<(), String> {
if c_input.len() == 1
&& (c_input == "y" || c_input == "Y" || c_input == "n" || c_input == "N")
{
Ok(())
} else if c_input.len() > 1 {
Err("Enter only one letter (Y/N)".to_string())
} else {
Err("Input not valid".to_string())
}
})
.default("Y/N".into())
.interact_text()?;
if input == "y" || input == "Y" {
Ok(true)
} else {
Ok(false)
}
}
pub fn is_older(src: &Path, dst: &Path) -> Option<bool> {
if !dst.exists() || !src.exists() {
return None;
}
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
let src_ctime = std::fs::metadata(src)
.map(|m| m.ctime())
.unwrap_or(i64::MIN);
let dst_ctime = std::fs::metadata(dst)
.map(|m| m.ctime())
.unwrap_or(i64::MAX);
Some(src_ctime <= dst_ctime)
}
#[cfg(windows)]
{
use std::os::windows::fs::MetadataExt;
let src_ctime = std::fs::metadata(src)
.map(|m| m.last_write_time())
.unwrap_or(u64::MIN);
let dst_ctime = std::fs::metadata(dst)
.map(|m| m.last_write_time())
.unwrap_or(u64::MAX);
Some(src_ctime <= dst_ctime)
}
}
#[cfg(unix)]
pub mod users {
use libc::{gid_t, uid_t};
use nix::unistd::{Gid, Group, Uid, User};
pub fn get_user_by_uid(uid: uid_t) -> Option<User> {
User::from_uid(Uid::from_raw(uid)).ok().flatten()
}
pub fn get_group_by_gid(gid: gid_t) -> Option<Group> {
Group::from_gid(Gid::from_raw(gid)).ok().flatten()
}
pub fn get_current_uid() -> uid_t {
Uid::current().as_raw()
}
pub fn get_current_gid() -> gid_t {
Gid::current().as_raw()
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn get_current_username() -> Option<String> {
User::from_uid(Uid::current())
.ok()
.flatten()
.map(|user| user.name)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn current_user_groups() -> Option<Vec<Gid>> {
let ngroups = unsafe { libc::getgroups(0, core::ptr::null::<gid_t> as *mut _) };
let mut buff: Vec<gid_t> = vec![0; ngroups as usize];
let found = unsafe { libc::getgroups(ngroups, buff.as_mut_ptr()) };
if found < 0 {
None
} else {
buff.truncate(found as usize);
buff.sort_unstable();
buff.dedup();
buff.into_iter()
.filter_map(|i| get_group_by_gid(i as gid_t))
.map(|group| group.gid)
.collect::<Vec<_>>()
.into()
}
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub fn get_user_groups(username: &str, gid: gid_t) -> Option<Vec<Gid>> {
use std::ffi::CString;
#[cfg(target_os = "macos")]
let mut buff: Vec<i32> = vec![0; 1024];
#[cfg(not(target_os = "macos"))]
let mut buff: Vec<gid_t> = vec![0; 1024];
let Ok(name) = CString::new(username.as_bytes()) else {
return None;
};
let mut count = buff.len() as libc::c_int;
#[cfg(target_os = "macos")]
let res =
unsafe { libc::getgrouplist(name.as_ptr(), gid as i32, buff.as_mut_ptr(), &mut count) };
#[cfg(not(target_os = "macos"))]
let res = unsafe { libc::getgrouplist(name.as_ptr(), gid, buff.as_mut_ptr(), &mut count) };
if res < 0 {
None
} else {
buff.truncate(count as usize);
buff.sort_unstable();
buff.dedup();
#[allow(trivial_numeric_casts)]
buff.into_iter()
.filter_map(|i| get_group_by_gid(i as gid_t))
.map(|group| group.gid)
.collect::<Vec<_>>()
.into()
}
}
}