use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
#[derive(Debug)]
pub struct Module {
pub name: Box<str>,
pub size: u64,
pub ref_count: u64,
pub deps: Box<str>,
pub state: Box<str>,
pub address: u64,
}
pub fn modules() -> Result<Vec<Module>> {
let path = Path::new("/proc/modules");
let bytes = parse::read_file(path)?;
let mut out: Vec<Module> = Vec::new();
for (line_num, line) in bytes
.split(|&b| b == b'\n')
.filter(|l| !l.is_empty())
.enumerate()
{
let fields = parse::SplitFields::<6>::new(line);
let name = std::str::from_utf8(fields[0]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid name",
})?;
let size = parse::parse_dec_u64(fields[1]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid size",
})?;
let ref_count = parse::parse_dec_u64(fields[2]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid reference count",
})?;
let deps = std::str::from_utf8(fields[3]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid deps",
})?;
let state = std::str::from_utf8(fields[4]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid state",
})?;
let address = parse::parse_hex_u64(fields[5]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid address",
})?;
out.push(Module {
name: name.into(),
size,
ref_count,
deps: deps.into(),
state: state.into(),
address,
});
}
Ok(out)
}