#![allow(clippy::cast_precision_loss)]
use std::io::Read;
use anyhow::Result;
pub(crate) fn random_bytes<const N: usize>() -> Result<[u8; N]> {
let mut buf = [0u8; N];
let mut f = std::fs::File::open("/dev/urandom")?;
f.read_exact(&mut buf)?;
Ok(buf)
}
pub fn human_size(size_b: u64) -> String {
const UNITS: [&str; 6] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
let mut size = size_b as f64;
let mut unit = 0;
while size >= 1024.0 && unit < UNITS.len() - 1 {
size /= 1024.0;
unit += 1;
}
format!("{size:.1} {}", UNITS[unit])
}
pub(crate) fn running_as_root() -> bool {
unsafe { libc::geteuid() == 0 }
}
pub(crate) fn sync_all() {
unsafe { libc::sync() };
}