sinf 0.1.0

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

use crate::hardware::StorageDevice;

use super::helpers::{read_bool, read_trim, read_u64};

pub(super) fn load() -> Vec<StorageDevice> {
    let Ok(entries) = fs::read_dir("/sys/class/block") else {
        return Vec::new();
    };
    let mut devices: Vec<StorageDevice> = entries
        .flatten()
        .filter_map(|entry| {
            let path = entry.path();
            if path.join("partition").exists() {
                return None;
            }
            let name = path.file_name()?.to_string_lossy().into_owned();
            if name.starts_with("loop") || name.starts_with("ram") || name.starts_with("zram") {
                return None;
            }
            let sectors = read_u64(&path.join("size"))?;
            let device = path.join("device");
            Some(StorageDevice {
                name: name.clone(),
                path: PathBuf::from("/dev").join(&name),
                model: read_trim(&device.join("model")),
                serial: read_trim(&device.join("serial")),
                firmware: read_trim(&device.join("firmware_rev")),
                vendor: read_trim(&device.join("vendor")),
                size_bytes: sectors.saturating_mul(512),
                removable: read_bool(&path.join("removable")).unwrap_or(false),
                rotational: read_bool(&path.join("queue/rotational")).unwrap_or(false),
            })
        })
        .collect();
    devices.sort_by(|a, b| a.name.cmp(&b.name));
    devices
}