sinf 0.1.0

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

use libc::statvfs;

use crate::sys::utils::to_cpath;

#[allow(clippy::unnecessary_cast)]
pub(crate) fn statvfs_values(mount_point: &Path) -> Option<(u64, u64, bool)> {
    let mount_point = to_cpath(mount_point);
    let mut stat = MaybeUninit::<statvfs>::uninit();
    // SAFETY: `statvfs` writes a valid structure on success.
    if unsafe { libc::statvfs(mount_point.as_ptr() as *const _, stat.as_mut_ptr()) } != 0 {
        return None;
    }
    // SAFETY: initialized by the successful call above.
    let stat = unsafe { stat.assume_init() };
    let frsize = stat.f_frsize as u64;
    let total = frsize.saturating_mul(stat.f_blocks as u64);
    if total == 0 {
        return None;
    }
    let available = frsize.saturating_mul(stat.f_bavail as u64);
    let read_only = (stat.f_flag & libc::ST_RDONLY) != 0;
    Some((total, available, read_only))
}