[thin_explore] Explore devices tree, including path support.

This commit is contained in:
Joe Thornber
2020-10-15 11:53:09 +01:00
parent c42b623e39
commit f60ae770c2
4 changed files with 206 additions and 46 deletions

View File

@@ -1,5 +1,5 @@
use anyhow::anyhow;
use byteorder::{ReadBytesExt, WriteBytesExt};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use data_encoding::BASE64;
use nom::{number::complete::*, IResult};
use std::collections::BTreeMap;
@@ -418,6 +418,27 @@ impl Unpack for NodeHeader {
}
}
impl Pack for NodeHeader {
fn pack<W: WriteBytesExt>(&self, w: &mut W) -> anyhow::Result<()> {
// csum needs to be calculated right for the whole metadata block.
w.write_u32::<LittleEndian>(0)?;
let flags;
if self.is_leaf {
flags = LEAF_NODE;
} else {
flags = INTERNAL_NODE;
}
w.write_u32::<LittleEndian>(flags)?;
w.write_u64::<LittleEndian>(self.block)?;
w.write_u32::<LittleEndian>(self.nr_entries)?;
w.write_u32::<LittleEndian>(self.max_entries)?;
w.write_u32::<LittleEndian>(self.value_size)?;
w.write_u32::<LittleEndian>(0)?;
Ok(())
}
}
#[derive(Clone)]
pub enum Node<V: Unpack> {
Internal {
@@ -1020,15 +1041,14 @@ impl<V: Unpack + Copy> NodeVisitor<V> for ValueCollector<V> {
}
pub fn btree_to_map<V: Unpack + Copy>(
_path: &mut Vec<u64>,
path: &mut Vec<u64>,
engine: Arc<dyn IoEngine + Send + Sync>,
ignore_non_fatal: bool,
root: u64,
) -> Result<BTreeMap<u64, V>> {
let walker = BTreeWalker::new(engine, ignore_non_fatal);
let visitor = ValueCollector::<V>::new();
let mut path = Vec::new();
walker.walk(&mut path, &visitor, root)?;
walker.walk(path, &visitor, root)?;
Ok(visitor.values.into_inner().unwrap())
}