use std::io::Write;
use crate::protocol::rpc::Context;
use crate::xdr;
use crate::xdr::portmap::{mapping, pmaplist};
use crate::xdr::Serialize;
pub fn pmapproc_dump(
xid: u32,
output: &mut impl Write,
context: &Context,
) -> Result<(), anyhow::Error> {
let binding = context.portmap_table.read().unwrap();
let entries: Vec<mapping> = binding
.table
.iter()
.map(|(entry, port)| mapping {
prog: entry.prog,
vers: entry.vers,
prot: entry.prot,
port: *port as u32,
})
.collect();
drop(binding);
let result = {
let mut list_head = None;
for map in entries.iter().rev() {
list_head = Some(pmaplist { map: *map, next: Box::from(list_head) });
}
list_head
};
xdr::rpc::make_success_reply(xid).serialize(output)?;
if let Some(list) = result {
let sent = Some(list);
sent.serialize(output)?;
} else {
0_u32.serialize(output)?;
}
Ok(())
}