use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
#[derive(Debug)]
pub struct UnixEntry {
pub ino: u64,
pub ref_count: u32,
pub protocol: u32,
pub type_: u32,
pub state: u32,
pub inode: u64,
pub path: Option<Box<str>>,
}
pub fn unix() -> impl Iterator<Item = Result<UnixEntry>> {
let path = Path::new("/proc/net/unix");
let bytes = match parse::read_file(path) {
Ok(b) => b,
Err(e) => return vec![Err(e)].into_iter(),
};
let mut entries = Vec::with_capacity(parse::count_byte(b'\n', &bytes));
for line in bytes
.split(|&b| b == b'\n')
.filter(|l| !l.is_empty())
.skip(1)
{
let fields = parse::SplitFields::<8>::new(line);
if fields.len() < 7 {
entries.push(Err(Error::Parse {
path: path.to_path_buf(),
line: 0,
msg: "not enough fields",
}));
continue;
}
let inode = parse::parse_dec_fast(fields[fields.len() - 2]);
let state = parse::parse_hex_fast(fields[fields.len() - 3]) as u32;
let type_ = parse::parse_hex_fast(fields[fields.len() - 4]) as u32;
let protocol = parse::parse_hex_fast(fields[fields.len() - 5]) as u32;
let ref_count = parse::parse_hex_fast(fields[fields.len() - 6]) as u32;
let ino = parse::parse_hex_fast(fields[0]);
let path_str = if fields.len() > 7 {
std::str::from_utf8(fields[fields.len() - 1])
.ok()
.map(|s| s.to_string().into_boxed_str())
} else {
None
};
entries.push(Ok(UnixEntry {
ino,
ref_count,
protocol,
type_,
state,
inode,
path: path_str,
}));
}
entries.into_iter()
}