//! Tiny system probes (kept dependency-free to stay lightweight). /// Total physical memory in MiB (best effort; 8 GiB if unknown). pub fn total_memory_mb() -> u64 { imp().unwrap_or(8192) } #[cfg(windows)] fn imp() -> Option { use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; let mut status: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; status.dwLength = std::mem::size_of::() as u32; let ok = unsafe { GlobalMemoryStatusEx(&mut status) }; (ok != 0).then_some(status.ullTotalPhys / 1024 / 1024) } #[cfg(target_os = "linux")] fn imp() -> Option { let info = std::fs::read_to_string("/proc/meminfo").ok()?; let line = info.lines().find(|l| l.starts_with("MemTotal:"))?; let kb: u64 = line.split_whitespace().nth(1)?.parse().ok()?; Some(kb / 1024) } #[cfg(not(any(windows, target_os = "linux")))] fn imp() -> Option { None } /// Recursively sum file sizes under `path`. pub fn dir_size(path: &std::path::Path) -> u64 { let Ok(entries) = std::fs::read_dir(path) else { return 0 }; entries .filter_map(|e| e.ok()) .map(|e| match e.file_type() { Ok(t) if t.is_dir() => dir_size(&e.path()), Ok(t) if t.is_file() => e.metadata().map(|m| m.len()).unwrap_or(0), _ => 0, }) .sum() }