sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::fs::File;
use std::io::Read;

use crate::Error;
use crate::sys::ffi;
use crate::sys::utils::to_u64;

pub(super) fn boot_time() -> Result<u64, Error> {
    if let Ok(buf) = File::open("/proc/stat").and_then(|mut f| {
        let mut buf = Vec::new();
        f.read_to_end(&mut buf)?;
        Ok(buf)
    }) {
        let line = buf.split(|c| *c == b'\n').find(|l| l.starts_with(b"btime"));

        if let Some(line) = line
            && let Some(btime) = line
                .split(|x| *x == b' ')
                .filter(|s| !s.is_empty())
                .nth(1)
                .map(to_u64)
        {
            return Ok(btime);
        }
    }
    // Fall back because either "btime" was missing or "/proc/stat" was unavailable.
    ffi::boot_time_clock()
        .ok_or_else(|| Error::from("clock_gettime failed: boot time cannot be retrieved"))
}

pub(crate) struct SystemInfo {
    pub(crate) page_size_b: u64,
    pub(crate) clock_cycle: u64,
    pub(crate) boot_time: u64,
}

impl SystemInfo {
    pub(super) fn new() -> Self {
        Self {
            page_size_b: ffi::page_size(),
            clock_cycle: ffi::clock_ticks(),
            boot_time: boot_time().unwrap_or(0),
        }
    }
}