sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::mem;
use std::path::PathBuf;

use crate::platform::unix::utils::cstr_to_rust_with_size;

use super::super::ProcessInner;
use crate::ProcessRefreshKind;

unsafe fn convert_node_path_info(node: &libc::vnode_info_path) -> Option<PathBuf> {
    if node.vip_vi.vi_stat.vst_dev == 0 {
        return None;
    }
    cstr_to_rust_with_size(
        node.vip_path.as_ptr() as _,
        Some(mem::size_of_val(&node.vip_path)),
    )
    .map(PathBuf::from)
}

pub(crate) unsafe fn get_cwd_root(process: &mut ProcessInner, refresh_kind: ProcessRefreshKind) {
    let cwd_needs_update = refresh_kind.cwd().needs_update(|| process.cwd.is_none());
    let root_needs_update = refresh_kind.root().needs_update(|| process.root.is_none());
    if !cwd_needs_update && !root_needs_update {
        return;
    }
    unsafe {
        let mut vnodepathinfo = mem::zeroed::<libc::proc_vnodepathinfo>();
        let result = libc::proc_pidinfo(
            process.pid.0,
            libc::PROC_PIDVNODEPATHINFO,
            0,
            &mut vnodepathinfo as *mut _ as *mut _,
            mem::size_of::<libc::proc_vnodepathinfo>() as _,
        );
        if result < 1 {
            sinf_debug!("Failed to retrieve cwd and root for {}", process.pid.0);
            return;
        }
        if cwd_needs_update {
            process.cwd = convert_node_path_info(&vnodepathinfo.pvi_cdir);
        }
        if root_needs_update {
            process.root = convert_node_path_info(&vnodepathinfo.pvi_rdir);
        }
    }
}