Fix a lot of clippy warnings

This commit is contained in:
Joe Thornber 2021-02-08 10:38:21 +00:00
parent e62fa3e835
commit c3c6d37aea
12 changed files with 154 additions and 152 deletions

View File

@ -115,7 +115,7 @@ fn main() {
let opts = ThinCheckOptions { let opts = ThinCheckOptions {
dev: &input_file, dev: &input_file,
async_io: async_io, async_io,
sb_only: matches.is_present("SB_ONLY"), sb_only: matches.is_present("SB_ONLY"),
skip_mappings: matches.is_present("SKIP_MAPPINGS"), skip_mappings: matches.is_present("SKIP_MAPPINGS"),
ignore_non_fatal: matches.is_present("IGNORE_NON_FATAL"), ignore_non_fatal: matches.is_present("IGNORE_NON_FATAL"),

View File

@ -72,7 +72,6 @@ impl Events {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let ignore_exit_key = Arc::new(AtomicBool::new(false)); let ignore_exit_key = Arc::new(AtomicBool::new(false));
let input_handle = { let input_handle = {
let tx = tx.clone();
let ignore_exit_key = ignore_exit_key.clone(); let ignore_exit_key = ignore_exit_key.clone();
thread::spawn(move || { thread::spawn(move || {
let stdin = io::stdin(); let stdin = io::stdin();
@ -110,6 +109,12 @@ impl Events {
} }
} }
impl Default for Events {
fn default() -> Self {
Self::new()
}
}
//------------------------------------ //------------------------------------
fn ls_next(ls: &mut ListState, max: usize) { fn ls_next(ls: &mut ListState, max: usize) {
@ -158,7 +163,7 @@ impl<'a> StatefulWidget for SBWidget<'a> {
let sb = self.sb; let sb = self.sb;
let flags = ["flags".to_string(), format!("{}", sb.flags)]; let flags = ["flags".to_string(), format!("{}", sb.flags)];
let block = ["block".to_string(), format!("{}", sb.block)]; let block = ["block".to_string(), format!("{}", sb.block)];
let uuid = ["uuid".to_string(), format!("-")]; let uuid = ["uuid".to_string(), "-".to_string()];
let version = ["version".to_string(), format!("{}", sb.version)]; let version = ["version".to_string(), format!("{}", sb.version)];
let time = ["time".to_string(), format!("{}", sb.time)]; let time = ["time".to_string(), format!("{}", sb.time)];
let transaction_id = [ let transaction_id = [
@ -208,9 +213,9 @@ impl<'a> StatefulWidget for SBWidget<'a> {
Widget::render(table, chunks[0], buf); Widget::render(table, chunks[0], buf);
let items = vec![ let items = vec![
ListItem::new(Span::raw(format!("Device tree"))), ListItem::new(Span::raw("Device tree".to_string())),
ListItem::new(Span::raw(format!("Mapping tree"))) ListItem::new(Span::raw("Mapping tree".to_string())),
]; ];
let items = List::new(items) let items = List::new(items)
@ -315,7 +320,7 @@ impl<X: Adjacent, Y: Adjacent> Adjacent for (X, Y) {
fn adjacent_runs<V: Adjacent + Copy>(mut ns: Vec<V>) -> Vec<(V, usize)> { fn adjacent_runs<V: Adjacent + Copy>(mut ns: Vec<V>) -> Vec<(V, usize)> {
let mut result = Vec::new(); let mut result = Vec::new();
if ns.len() == 0 { if ns.is_empty() {
return result; return result;
} }
@ -330,13 +335,13 @@ fn adjacent_runs<V: Adjacent + Copy>(mut ns: Vec<V>) -> Vec<(V, usize)> {
current = v; current = v;
len += 1; len += 1;
} else { } else {
result.push((base.clone(), len)); result.push((base, len));
base = v.clone(); base = v;
current = v.clone(); current = v;
len = 1; len = 1;
} }
} }
result.push((base.clone(), len)); result.push((base, len));
result result
} }
@ -344,7 +349,7 @@ fn adjacent_runs<V: Adjacent + Copy>(mut ns: Vec<V>) -> Vec<(V, usize)> {
fn mk_runs<V: Adjacent + Sized + Copy>(keys: &[u64], values: &[V]) -> Vec<((u64, V), usize)> { fn mk_runs<V: Adjacent + Sized + Copy>(keys: &[u64], values: &[V]) -> Vec<((u64, V), usize)> {
let mut pairs = Vec::new(); let mut pairs = Vec::new();
for (k, v) in keys.iter().zip(values.iter()) { for (k, v) in keys.iter().zip(values.iter()) {
pairs.push((k.clone(), v.clone())); pairs.push((*k, *v));
} }
adjacent_runs(pairs) adjacent_runs(pairs)
@ -498,7 +503,7 @@ impl Panel for SBPanel {
} else { } else {
Some(PushTopLevel(self.sb.mapping_root)) Some(PushTopLevel(self.sb.mapping_root))
} }
}, }
Key::Char('h') | Key::Left => Some(PopPanel), Key::Char('h') | Key::Left => Some(PopPanel),
_ => None, _ => None,
} }
@ -571,14 +576,14 @@ impl Panel for DeviceDetailPanel {
fn path_action(&mut self, child: u64) -> Option<Action> { fn path_action(&mut self, child: u64) -> Option<Action> {
match &self.node { match &self.node {
btree::Node::Internal { values, .. } => { btree::Node::Internal { values, .. } => {
for i in 0..values.len() { for (i, v) in values.iter().enumerate() {
if values[i] == child { if *v == child {
self.state.select(Some(i)); self.state.select(Some(i));
return Some(PushDeviceDetail(child)); return Some(PushDeviceDetail(child));
} }
} }
return None; None
} }
btree::Node::Leaf { .. } => None, btree::Node::Leaf { .. } => None,
} }
@ -645,14 +650,14 @@ impl Panel for TopLevelPanel {
fn path_action(&mut self, child: u64) -> Option<Action> { fn path_action(&mut self, child: u64) -> Option<Action> {
match &self.node { match &self.node {
btree::Node::Internal { values, .. } => { btree::Node::Internal { values, .. } => {
for i in 0..values.len() { for (i, v) in values.iter().enumerate() {
if values[i] == child { if *v == child {
self.state.select(Some(i)); self.state.select(Some(i));
return Some(PushTopLevel(child)); return Some(PushTopLevel(child));
} }
} }
return None; None
} }
btree::Node::Leaf { keys, values, .. } => { btree::Node::Leaf { keys, values, .. } => {
for i in 0..values.len() { for i in 0..values.len() {
@ -662,7 +667,7 @@ impl Panel for TopLevelPanel {
} }
} }
return None; None
} }
} }
} }
@ -726,14 +731,14 @@ impl Panel for BottomLevelPanel {
fn path_action(&mut self, child: u64) -> Option<Action> { fn path_action(&mut self, child: u64) -> Option<Action> {
match &self.node { match &self.node {
btree::Node::Internal { values, .. } => { btree::Node::Internal { values, .. } => {
for i in 0..values.len() { for (i, v) in values.iter().enumerate() {
if values[i] == child { if *v == child {
self.state.select(Some(i)); self.state.select(Some(i));
return Some(PushBottomLevel(self.thin_id, child)); return Some(PushBottomLevel(self.thin_id, child));
} }
} }
return None; None
} }
btree::Node::Leaf { .. } => None, btree::Node::Leaf { .. } => None,
} }
@ -789,7 +794,7 @@ fn explore(path: &Path, node_path: Option<Vec<u64>>) -> Result<()> {
} }
} else { } else {
let sb = read_superblock(&engine, 0)?; let sb = read_superblock(&engine, 0)?;
panels.push(Box::new(SBPanel::new(sb.clone()))); panels.push(Box::new(SBPanel::new(sb)));
} }
let events = Events::new(); let events = Events::new();
@ -826,12 +831,11 @@ fn explore(path: &Path, node_path: Option<Vec<u64>>) -> Result<()> {
if let Event::Input(key) = events.next()? { if let Event::Input(key) = events.next()? {
match key { match key {
Key::Char('q') => break 'main, Key::Char('q') => break 'main,
_ => match active_panel.input(key) { _ => {
Some(action) => { if let Some(action) = active_panel.input(key) {
perform_action(&mut panels, &engine, action)?; perform_action(&mut panels, &engine, action)?;
} }
_ => {} }
},
} }
} }
} }

View File

@ -269,10 +269,8 @@ impl AsyncIoEngine {
cqes.sort_by(|a, b| a.user_data().partial_cmp(&b.user_data()).unwrap()); cqes.sort_by(|a, b| a.user_data().partial_cmp(&b.user_data()).unwrap());
let mut rs = Vec::new(); let mut rs = Vec::new();
let mut i = 0; for (i, b) in blocks.into_iter().enumerate() {
for b in blocks {
let c = &cqes[i]; let c = &cqes[i];
i += 1;
let r = c.result(); let r = c.result();
if r < 0 { if r < 0 {

View File

@ -173,7 +173,7 @@ pub fn pack_literal<W: Write>(w: &mut W, bs: &[u8]) -> io::Result<()> {
use Tag::LitW; use Tag::LitW;
let len = bs.len() as u64; let len = bs.len() as u64;
if len < 16 as u64 { if len < 16 {
pack_tag(w, Tag::Lit, len as u8)?; pack_tag(w, Tag::Lit, len as u8)?;
} else if len <= u8::MAX as u64 { } else if len <= u8::MAX as u64 {
pack_tag(w, LitW, 1)?; pack_tag(w, LitW, 1)?;
@ -370,6 +370,12 @@ impl VM {
} }
} }
impl Default for VM {
fn default() -> Self {
Self::new()
}
}
pub fn unpack<R: Read>(r: &mut R, count: usize) -> io::Result<Vec<u8>> { pub fn unpack<R: Read>(r: &mut R, count: usize) -> io::Result<Vec<u8>> {
let mut w = Vec::with_capacity(4096); let mut w = Vec::with_capacity(4096);
let mut cursor = Cursor::new(&mut w); let mut cursor = Cursor::new(&mut w);

View File

@ -26,6 +26,12 @@ impl KeyRange {
} }
} }
impl Default for KeyRange {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for KeyRange { impl fmt::Display for KeyRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match (self.start, self.end) { match (self.start, self.end) {
@ -182,22 +188,20 @@ fn test_split_range() {
} }
} }
fn split_one(path: &Vec<u64>, kr: &KeyRange, k: u64) -> Result<(KeyRange, KeyRange)> { fn split_one(path: &[u64], kr: &KeyRange, k: u64) -> Result<(KeyRange, KeyRange)> {
match kr.split(k) { match kr.split(k) {
None => { None => Err(node_err(
return Err(node_err( path,
path, &format!("couldn't split key range {} at {}", kr, k),
&format!("couldn't split key range {} at {}", kr, k), )),
));
}
Some(pair) => Ok(pair), Some(pair) => Ok(pair),
} }
} }
pub fn split_key_ranges(path: &Vec<u64>, kr: &KeyRange, keys: &[u64]) -> Result<Vec<KeyRange>> { pub fn split_key_ranges(path: &[u64], kr: &KeyRange, keys: &[u64]) -> Result<Vec<KeyRange>> {
let mut krs = Vec::with_capacity(keys.len()); let mut krs = Vec::with_capacity(keys.len());
if keys.len() == 0 { if keys.is_empty() {
return Err(node_err(path, "split_key_ranges: no keys present")); return Err(node_err(path, "split_key_ranges: no keys present"));
} }
@ -207,8 +211,8 @@ pub fn split_key_ranges(path: &Vec<u64>, kr: &KeyRange, keys: &[u64]) -> Result<
end: kr.end, end: kr.end,
}; };
for i in 1..keys.len() { for k in keys.iter().skip(1) {
let (first, rest) = split_one(path, &kr, keys[i])?; let (first, rest) = split_one(path, &kr, *k)?;
krs.push(first); krs.push(first);
kr = rest; kr = rest;
} }
@ -229,7 +233,7 @@ pub fn encode_node_path(path: &[u64]) -> String {
// The first entry is normally the superblock (0), so we // The first entry is normally the superblock (0), so we
// special case this. // special case this.
if path.len() > 0 && path[0] == 0 { if !path.is_empty() && path[0] == 0 {
let count = ((path.len() as u8) - 1) << 1; let count = ((path.len() as u8) - 1) << 1;
cursor.write_u8(count as u8).unwrap(); cursor.write_u8(count as u8).unwrap();
vm::pack_u64s(&mut cursor, &path[1..]).unwrap(); vm::pack_u64s(&mut cursor, &path[1..]).unwrap();
@ -349,19 +353,19 @@ impl fmt::Display for BTreeError {
} }
} }
} }
pub fn node_err(path: &Vec<u64>, msg: &str) -> BTreeError { pub fn node_err(path: &[u64], msg: &str) -> BTreeError {
BTreeError::Path( BTreeError::Path(
path.clone(), path.to_vec(),
Box::new(BTreeError::NodeError(msg.to_string())), Box::new(BTreeError::NodeError(msg.to_string())),
) )
} }
pub fn node_err_s(path: &Vec<u64>, msg: String) -> BTreeError { pub fn node_err_s(path: &[u64], msg: String) -> BTreeError {
BTreeError::Path(path.clone(), Box::new(BTreeError::NodeError(msg))) BTreeError::Path(path.to_vec(), Box::new(BTreeError::NodeError(msg)))
} }
pub fn io_err(path: &Vec<u64>) -> BTreeError { pub fn io_err(path: &[u64]) -> BTreeError {
BTreeError::Path(path.clone(), Box::new(BTreeError::IoError)) BTreeError::Path(path.to_vec(), Box::new(BTreeError::IoError))
} }
pub fn value_err(msg: String) -> BTreeError { pub fn value_err(msg: String) -> BTreeError {
@ -424,22 +428,22 @@ impl Unpack for NodeHeader {
impl Pack for NodeHeader { impl Pack for NodeHeader {
fn pack<W: WriteBytesExt>(&self, w: &mut W) -> anyhow::Result<()> { fn pack<W: WriteBytesExt>(&self, w: &mut W) -> anyhow::Result<()> {
// csum needs to be calculated right for the whole metadata block. // csum needs to be calculated right for the whole metadata block.
w.write_u32::<LittleEndian>(0)?; w.write_u32::<LittleEndian>(0)?;
let flags; let flags;
if self.is_leaf { if self.is_leaf {
flags = LEAF_NODE; flags = LEAF_NODE;
} else { } else {
flags = INTERNAL_NODE; flags = INTERNAL_NODE;
} }
w.write_u32::<LittleEndian>(flags)?; w.write_u32::<LittleEndian>(flags)?;
w.write_u64::<LittleEndian>(self.block)?; w.write_u64::<LittleEndian>(self.block)?;
w.write_u32::<LittleEndian>(self.nr_entries)?; w.write_u32::<LittleEndian>(self.nr_entries)?;
w.write_u32::<LittleEndian>(self.max_entries)?; w.write_u32::<LittleEndian>(self.max_entries)?;
w.write_u32::<LittleEndian>(self.value_size)?; w.write_u32::<LittleEndian>(self.value_size)?;
w.write_u32::<LittleEndian>(0)?; w.write_u32::<LittleEndian>(0)?;
Ok(()) Ok(())
} }
} }
@ -487,16 +491,16 @@ impl<V: Unpack> Node<V> {
} }
} }
pub fn convert_result<'a, V>(path: &Vec<u64>, r: IResult<&'a [u8], V>) -> Result<(&'a [u8], V)> { pub fn convert_result<'a, V>(path: &[u64], r: IResult<&'a [u8], V>) -> Result<(&'a [u8], V)> {
r.map_err(|_e| node_err(path, "parse error")) r.map_err(|_e| node_err(path, "parse error"))
} }
pub fn convert_io_err<V>(path: &Vec<u64>, r: std::io::Result<V>) -> Result<V> { pub fn convert_io_err<V>(path: &[u64], r: std::io::Result<V>) -> Result<V> {
r.map_err(|_| io_err(path)) r.map_err(|_| io_err(path))
} }
pub fn unpack_node<V: Unpack>( pub fn unpack_node<V: Unpack>(
path: &Vec<u64>, path: &[u64],
data: &[u8], data: &[u8],
ignore_non_fatal: bool, ignore_non_fatal: bool,
is_root: bool, is_root: bool,
@ -554,7 +558,7 @@ pub fn unpack_node<V: Unpack>(
for k in &keys { for k in &keys {
if let Some(l) = last { if let Some(l) = last {
if k <= l { if k <= l {
return Err(node_err(path, "keys out of order")); return Err(node_err(&path, "keys out of order"));
} }
} }
@ -573,7 +577,7 @@ pub fn unpack_node<V: Unpack>(
values, values,
}) })
} else { } else {
let (_i, values) = convert_result(path, count(le_u64, header.nr_entries as usize)(i))?; let (_i, values) = convert_result(&path, count(le_u64, header.nr_entries as usize)(i))?;
Ok(Node::Internal { Ok(Node::Internal {
header, header,
keys, keys,

View File

@ -153,7 +153,7 @@ pub struct NodeSummary {
fn write_node_<V: Unpack + Pack>(w: &mut WriteBatcher, mut node: Node<V>) -> Result<(u64, u64)> { fn write_node_<V: Unpack + Pack>(w: &mut WriteBatcher, mut node: Node<V>) -> Result<(u64, u64)> {
let keys = node.get_keys(); let keys = node.get_keys();
let first_key = keys.first().unwrap_or(&0u64).clone(); let first_key = *keys.first().unwrap_or(&0u64);
let loc = w.alloc()?; let loc = w.alloc()?;
node.set_block(loc); node.set_block(loc);

View File

@ -102,8 +102,7 @@ impl<'a> LeafWalker<'a> {
.read_many(&blocks[0..]) .read_many(&blocks[0..])
.map_err(|_e| io_err(path))?; .map_err(|_e| io_err(path))?;
let mut i = 0; for (i, rb) in rblocks.into_iter().enumerate() {
for rb in rblocks {
match rb { match rb {
Err(_) => { Err(_) => {
return Err(io_err(path).keys_context(&filtered_krs[i])); return Err(io_err(path).keys_context(&filtered_krs[i]));
@ -112,8 +111,6 @@ impl<'a> LeafWalker<'a> {
self.walk_node(depth - 1, path, visitor, &filtered_krs[i], &b, false)?; self.walk_node(depth - 1, path, visitor, &filtered_krs[i], &b, false)?;
} }
} }
i += 1;
} }
Ok(()) Ok(())
@ -225,14 +222,12 @@ impl<'a> LeafWalker<'a> {
self.leaves.insert(root as usize); self.leaves.insert(root as usize);
visitor.visit(&kr, root)?; visitor.visit(&kr, root)?;
Ok(()) Ok(())
} else if self.sm_inc(root) > 0 {
visitor.visit_again(root)
} else { } else {
if self.sm_inc(root) > 0 { let root = self.engine.read(root).map_err(|_| io_err(path))?;
visitor.visit_again(root)
} else {
let root = self.engine.read(root).map_err(|_| io_err(path))?;
self.walk_node(depth - 1, path, visitor, &kr, &root, true) self.walk_node(depth - 1, path, visitor, &kr, &root, true)
}
} }
} }

View File

@ -49,21 +49,21 @@ impl LeafVisitor {
impl<V: Unpack> NodeVisitor<V> for LeafVisitor { impl<V: Unpack> NodeVisitor<V> for LeafVisitor {
fn visit( fn visit(
&self, &self,
path: &Vec<u64>, path: &[u64],
_kr: &KeyRange, _kr: &KeyRange,
_header: &NodeHeader, _header: &NodeHeader,
keys: &[u64], keys: &[u64],
_values: &[V], _values: &[V],
) -> btree::Result<()> { ) -> btree::Result<()> {
// ignore empty nodes // ignore empty nodes
if keys.len() == 0 { if keys.is_empty() {
return Ok(()); return Ok(());
} }
let mut inner = self.inner.lock().unwrap(); let mut inner = self.inner.lock().unwrap();
// Check keys are ordered. // Check keys are ordered.
if inner.leaves.len() > 0 { if !inner.leaves.is_empty() {
let last_key = inner.leaves.last().unwrap().key_high; let last_key = inner.leaves.last().unwrap().key_high;
if keys[0] <= last_key { if keys[0] <= last_key {
return Err(BTreeError::NodeError( return Err(BTreeError::NodeError(
@ -83,7 +83,7 @@ impl<V: Unpack> NodeVisitor<V> for LeafVisitor {
Ok(()) Ok(())
} }
fn visit_again(&self, _path: &Vec<u64>, _b: u64) -> btree::Result<()> { fn visit_again(&self, _path: &[u64], _b: u64) -> btree::Result<()> {
Ok(()) Ok(())
} }

View File

@ -14,7 +14,7 @@ pub trait NodeVisitor<V: Unpack> {
// &self is deliberately non mut to allow the walker to use multiple threads. // &self is deliberately non mut to allow the walker to use multiple threads.
fn visit( fn visit(
&self, &self,
path: &Vec<u64>, path: &[u64],
kr: &KeyRange, kr: &KeyRange,
header: &NodeHeader, header: &NodeHeader,
keys: &[u64], keys: &[u64],
@ -24,7 +24,7 @@ pub trait NodeVisitor<V: Unpack> {
// Nodes may be shared and thus visited multiple times. The walker avoids // Nodes may be shared and thus visited multiple times. The walker avoids
// doing repeated IO, but it does call this method to keep the visitor up to // doing repeated IO, but it does call this method to keep the visitor up to
// date. // date.
fn visit_again(&self, path: &Vec<u64>, b: u64) -> Result<()>; fn visit_again(&self, path: &[u64], b: u64) -> Result<()>;
fn end_walk(&self) -> Result<()>; fn end_walk(&self) -> Result<()>;
} }
@ -154,8 +154,7 @@ impl BTreeWalker {
} }
} }
Ok(rblocks) => { Ok(rblocks) => {
let mut i = 0; for (i, rb) in rblocks.into_iter().enumerate() {
for rb in rblocks {
match rb { match rb {
Err(_) => { Err(_) => {
let e = io_err(path).keys_context(&filtered_krs[i]); let e = io_err(path).keys_context(&filtered_krs[i]);
@ -169,8 +168,6 @@ impl BTreeWalker {
Ok(()) => {} Ok(()) => {}
}, },
} }
i += 1;
} }
} }
} }
@ -215,7 +212,7 @@ impl BTreeWalker {
values, values,
} => { } => {
if let Err(e) = visitor.visit(path, &kr, &header, &keys, &values) { if let Err(e) = visitor.visit(path, &kr, &header, &keys, &values) {
let e = BTreeError::Path(path.clone(), Box::new(e.clone())); let e = BTreeError::Path(path.clone(), Box::new(e));
self.set_fail(b.loc, e.clone()); self.set_fail(b.loc, e.clone());
return Err(e); return Err(e);
} }
@ -251,7 +248,7 @@ impl BTreeWalker {
{ {
if self.sm_inc(root) > 0 { if self.sm_inc(root) > 0 {
if let Some(e) = self.failed(root) { if let Some(e) = self.failed(root) {
Err(e.clone()) Err(e)
} else { } else {
visitor.visit_again(path, root) visitor.visit_again(path, root)
} }
@ -382,10 +379,9 @@ where
} }
} }
Ok(rblocks) => { Ok(rblocks) => {
let mut i = 0;
let errs = Arc::new(Mutex::new(Vec::new())); let errs = Arc::new(Mutex::new(Vec::new()));
for rb in rblocks { for (i, rb) in rblocks.into_iter().enumerate() {
match rb { match rb {
Err(_) => { Err(_) => {
let e = io_err(path).keys_context(&filtered_krs[i]); let e = io_err(path).keys_context(&filtered_krs[i]);
@ -411,8 +407,6 @@ where
}); });
} }
} }
i += 1;
} }
pool.join(); pool.join();
@ -435,7 +429,7 @@ where
{ {
if w.sm_inc(root) > 0 { if w.sm_inc(root) > 0 {
if let Some(e) = w.failed(root) { if let Some(e) = w.failed(root) {
Err(e.clone()) Err(e)
} else { } else {
visitor.visit_again(path, root) visitor.visit_again(path, root)
} }
@ -467,7 +461,7 @@ impl<V> ValueCollector<V> {
impl<V: Unpack + Copy> NodeVisitor<V> for ValueCollector<V> { impl<V: Unpack + Copy> NodeVisitor<V> for ValueCollector<V> {
fn visit( fn visit(
&self, &self,
_path: &Vec<u64>, _path: &[u64],
_kr: &KeyRange, _kr: &KeyRange,
_h: &NodeHeader, _h: &NodeHeader,
keys: &[u64], keys: &[u64],
@ -475,13 +469,13 @@ impl<V: Unpack + Copy> NodeVisitor<V> for ValueCollector<V> {
) -> Result<()> { ) -> Result<()> {
let mut vals = self.values.lock().unwrap(); let mut vals = self.values.lock().unwrap();
for n in 0..keys.len() { for n in 0..keys.len() {
vals.insert(keys[n], values[n].clone()); vals.insert(keys[n], values[n]);
} }
Ok(()) Ok(())
} }
fn visit_again(&self, _path: &Vec<u64>, _b: u64) -> Result<()> { fn visit_again(&self, _path: &[u64], _b: u64) -> Result<()> {
Ok(()) Ok(())
} }
@ -533,7 +527,7 @@ impl<V> ValuePathCollector<V> {
impl<V: Unpack + Clone> NodeVisitor<V> for ValuePathCollector<V> { impl<V: Unpack + Clone> NodeVisitor<V> for ValuePathCollector<V> {
fn visit( fn visit(
&self, &self,
path: &Vec<u64>, path: &[u64],
_kr: &KeyRange, _kr: &KeyRange,
_h: &NodeHeader, _h: &NodeHeader,
keys: &[u64], keys: &[u64],
@ -541,13 +535,13 @@ impl<V: Unpack + Clone> NodeVisitor<V> for ValuePathCollector<V> {
) -> Result<()> { ) -> Result<()> {
let mut vals = self.values.lock().unwrap(); let mut vals = self.values.lock().unwrap();
for n in 0..keys.len() { for n in 0..keys.len() {
vals.insert(keys[n], (path.clone(), values[n].clone())); vals.insert(keys[n], (path.to_vec(), values[n].clone()));
} }
Ok(()) Ok(())
} }
fn visit_again(&self, _path: &Vec<u64>, _b: u64) -> Result<()> { fn visit_again(&self, _path: &[u64], _b: u64) -> Result<()> {
Ok(()) Ok(())
} }

View File

@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::io::Cursor; use std::io::Cursor;
use std::path::Path; use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle}; use std::thread::{self, JoinHandle};
use threadpool::ThreadPool; use threadpool::ThreadPool;
@ -28,7 +29,7 @@ struct BottomLevelVisitor {
impl NodeVisitor<BlockTime> for BottomLevelVisitor { impl NodeVisitor<BlockTime> for BottomLevelVisitor {
fn visit( fn visit(
&self, &self,
_path: &Vec<u64>, _path: &[u64],
_kr: &KeyRange, _kr: &KeyRange,
_h: &NodeHeader, _h: &NodeHeader,
_k: &[u64], _k: &[u64],
@ -36,7 +37,7 @@ impl NodeVisitor<BlockTime> for BottomLevelVisitor {
) -> btree::Result<()> { ) -> btree::Result<()> {
// FIXME: do other checks // FIXME: do other checks
if values.len() == 0 { if values.is_empty() {
return Ok(()); return Ok(());
} }
@ -45,8 +46,8 @@ impl NodeVisitor<BlockTime> for BottomLevelVisitor {
let mut start = values[0].block; let mut start = values[0].block;
let mut len = 1; let mut len = 1;
for n in 1..values.len() { for b in values.iter().skip(1) {
let block = values[n].block; let block = b.block;
if block == start + len { if block == start + len {
len += 1; len += 1;
} else { } else {
@ -60,7 +61,7 @@ impl NodeVisitor<BlockTime> for BottomLevelVisitor {
Ok(()) Ok(())
} }
fn visit_again(&self, _path: &Vec<u64>, _b: u64) -> btree::Result<()> { fn visit_again(&self, _path: &[u64], _b: u64) -> btree::Result<()> {
Ok(()) Ok(())
} }
@ -84,7 +85,7 @@ impl<'a> OverflowChecker<'a> {
impl<'a> NodeVisitor<u32> for OverflowChecker<'a> { impl<'a> NodeVisitor<u32> for OverflowChecker<'a> {
fn visit( fn visit(
&self, &self,
_path: &Vec<u64>, _path: &[u64],
_kr: &KeyRange, _kr: &KeyRange,
_h: &NodeHeader, _h: &NodeHeader,
keys: &[u64], keys: &[u64],
@ -103,7 +104,7 @@ impl<'a> NodeVisitor<u32> for OverflowChecker<'a> {
Ok(()) Ok(())
} }
fn visit_again(&self, _path: &Vec<u64>, _b: u64) -> btree::Result<()> { fn visit_again(&self, _path: &[u64], _b: u64) -> btree::Result<()> {
Ok(()) Ok(())
} }
@ -152,13 +153,12 @@ fn check_space_map(
} }
// FIXME: we should do this in batches // FIXME: we should do this in batches
let blocks = engine.read_many(&mut blocks)?; let blocks = engine.read_many(&blocks)?;
let mut leaks = 0; let mut leaks = 0;
let mut blocknr = 0; let mut blocknr = 0;
let mut bitmap_leaks = Vec::new(); let mut bitmap_leaks = Vec::new();
for n in 0..entries.len() { for b in blocks.iter().take(entries.len()) {
let b = &blocks[n];
match b { match b {
Err(_e) => { Err(_e) => {
return Err(anyhow!("Unable to read bitmap block")); return Err(anyhow!("Unable to read bitmap block"));
@ -233,12 +233,8 @@ fn repair_space_map(ctx: &Context, entries: Vec<BitmapLeak>, sm: ASpaceMap) -> R
let rblocks = engine.read_many(&blocks[0..])?; let rblocks = engine.read_many(&blocks[0..])?;
let mut write_blocks = Vec::new(); let mut write_blocks = Vec::new();
let mut i = 0; for (i, rb) in rblocks.into_iter().enumerate() {
for rb in rblocks { if let Ok(b) = rb {
if rb.is_err() {
return Err(anyhow!("Unable to reread bitmap blocks for repair"));
} else {
let b = rb.unwrap();
let be = &entries[i]; let be = &entries[i];
let mut blocknr = be.blocknr; let mut blocknr = be.blocknr;
let mut bitmap = unpack::<Bitmap>(b.get_data())?; let mut bitmap = unpack::<Bitmap>(b.get_data())?;
@ -262,9 +258,9 @@ fn repair_space_map(ctx: &Context, entries: Vec<BitmapLeak>, sm: ASpaceMap) -> R
checksum::write_checksum(b.get_data(), checksum::BT::BITMAP)?; checksum::write_checksum(b.get_data(), checksum::BT::BITMAP)?;
write_blocks.push(b); write_blocks.push(b);
} else {
return Err(anyhow!("Unable to reread bitmap blocks for repair"));
} }
i += 1;
} }
engine.write_many(&write_blocks[0..])?; engine.write_many(&write_blocks[0..])?;
@ -305,20 +301,17 @@ fn spawn_progress_thread(
sm: Arc<Mutex<dyn SpaceMap + Send + Sync>>, sm: Arc<Mutex<dyn SpaceMap + Send + Sync>>,
nr_allocated_metadata: u64, nr_allocated_metadata: u64,
report: Arc<Report>, report: Arc<Report>,
) -> Result<(JoinHandle<()>, Arc<Mutex<bool>>)> { ) -> Result<(JoinHandle<()>, Arc<AtomicBool>)> {
let tid; let tid;
let stop_progress = Arc::new(Mutex::new(false)); let stop_progress = Arc::new(AtomicBool::new(false));
{ {
let stop_progress = stop_progress.clone(); let stop_progress = stop_progress.clone();
tid = thread::spawn(move || { tid = thread::spawn(move || {
let interval = std::time::Duration::from_millis(250); let interval = std::time::Duration::from_millis(250);
loop { loop {
{ if stop_progress.load(Ordering::Relaxed) {
let stop_progress = stop_progress.lock().unwrap(); break;
if *stop_progress {
break;
}
} }
let sm = sm.lock().unwrap(); let sm = sm.lock().unwrap();
@ -364,7 +357,7 @@ fn check_mapping_bottom_level(
if roots.len() > 64 { if roots.len() > 64 {
let errs = Arc::new(Mutex::new(Vec::new())); let errs = Arc::new(Mutex::new(Vec::new()));
for (_thin_id, (path, root)) in roots { for (path, root) in roots.values() {
let data_sm = data_sm.clone(); let data_sm = data_sm.clone();
let root = *root; let root = *root;
let v = BottomLevelVisitor { data_sm }; let v = BottomLevelVisitor { data_sm };
@ -381,12 +374,12 @@ fn check_mapping_bottom_level(
} }
ctx.pool.join(); ctx.pool.join();
let errs = Arc::try_unwrap(errs).unwrap().into_inner().unwrap(); let errs = Arc::try_unwrap(errs).unwrap().into_inner().unwrap();
if errs.len() > 0 { if !errs.is_empty() {
ctx.report.fatal(&format!("{}", aggregate_error(errs))); ctx.report.fatal(&format!("{}", aggregate_error(errs)));
failed = true; failed = true;
} }
} else { } else {
for (_thin_id, (path, root)) in roots { for (path, root) in roots.values() {
let w = w.clone(); let w = w.clone();
let data_sm = data_sm.clone(); let data_sm = data_sm.clone();
let root = *root; let root = *root;
@ -468,7 +461,12 @@ pub fn check(opts: ThinCheckOptions) -> Result<()> {
// Device details. We read this once to get the number of thin devices, and hence the // Device details. We read this once to get the number of thin devices, and hence the
// maximum metadata ref count. Then create metadata space map, and reread to increment // maximum metadata ref count. Then create metadata space map, and reread to increment
// the ref counts for that metadata. // the ref counts for that metadata.
let devs = btree_to_map::<DeviceDetail>(&mut path, engine.clone(), opts.ignore_non_fatal, sb.details_root)?; let devs = btree_to_map::<DeviceDetail>(
&mut path,
engine.clone(),
opts.ignore_non_fatal,
sb.details_root,
)?;
let nr_devs = devs.len(); let nr_devs = devs.len();
let metadata_sm = core_sm(engine.get_nr_blocks(), nr_devs as u32); let metadata_sm = core_sm(engine.get_nr_blocks(), nr_devs as u32);
inc_superblock(&metadata_sm)?; inc_superblock(&metadata_sm)?;
@ -574,21 +572,18 @@ pub fn check(opts: ThinCheckOptions) -> Result<()> {
bail_out(&ctx, "metadata space map")?; bail_out(&ctx, "metadata space map")?;
if opts.auto_repair { if opts.auto_repair {
if data_leaks.len() > 0 { if !data_leaks.is_empty() {
ctx.report.info("Repairing data leaks."); ctx.report.info("Repairing data leaks.");
repair_space_map(&ctx, data_leaks, data_sm.clone())?; repair_space_map(&ctx, data_leaks, data_sm.clone())?;
} }
if metadata_leaks.len() > 0 { if !metadata_leaks.is_empty() {
ctx.report.info("Repairing metadata leaks."); ctx.report.info("Repairing metadata leaks.");
repair_space_map(&ctx, metadata_leaks, metadata_sm.clone())?; repair_space_map(&ctx, metadata_leaks, metadata_sm.clone())?;
} }
} }
{ stop_progress.store(true, Ordering::Relaxed);
let mut stop_progress = stop_progress.lock().unwrap();
*stop_progress = true;
}
tid.join().unwrap(); tid.join().unwrap();
Ok(()) Ok(())

View File

@ -38,7 +38,7 @@ impl RunBuilder {
self.run = Some(xml::Map { self.run = Some(xml::Map {
thin_begin: thin_block, thin_begin: thin_block,
data_begin: data_block, data_begin: data_block,
time: time, time,
len: 1, len: 1,
}); });
None None
@ -59,7 +59,7 @@ impl RunBuilder {
self.run.replace(Map { self.run.replace(Map {
thin_begin: thin_block, thin_begin: thin_block,
data_begin: data_block, data_begin: data_block,
time: time, time,
len: 1, len: 1,
}) })
} }
@ -99,7 +99,7 @@ impl<'a> MappingVisitor<'a> {
impl<'a> NodeVisitor<BlockTime> for MappingVisitor<'a> { impl<'a> NodeVisitor<BlockTime> for MappingVisitor<'a> {
fn visit( fn visit(
&self, &self,
_path: &Vec<u64>, _path: &[u64],
_kr: &KeyRange, _kr: &KeyRange,
_h: &NodeHeader, _h: &NodeHeader,
keys: &[u64], keys: &[u64],
@ -118,7 +118,7 @@ impl<'a> NodeVisitor<BlockTime> for MappingVisitor<'a> {
Ok(()) Ok(())
} }
fn visit_again(&self, _path: &Vec<u64>, b: u64) -> btree::Result<()> { fn visit_again(&self, _path: &[u64], b: u64) -> btree::Result<()> {
let mut inner = self.inner.lock().unwrap(); let mut inner = self.inner.lock().unwrap();
inner inner
.md_out .md_out
@ -337,7 +337,7 @@ fn build_metadata(ctx: &Context, sb: &Superblock) -> Result<Metadata> {
let (mut shared, sm) = find_shared_nodes(ctx, &roots)?; let (mut shared, sm) = find_shared_nodes(ctx, &roots)?;
// Add in the roots, because they may not be shared. // Add in the roots, because they may not be shared.
for (_thin_id, (_path, root)) in &roots { for (_path, root) in roots.values() {
shared.insert(*root); shared.insert(*root);
} }
@ -354,7 +354,7 @@ fn build_metadata(ctx: &Context, sb: &Superblock) -> Result<Metadata> {
let kr = KeyRange::new(); // FIXME: finish let kr = KeyRange::new(); // FIXME: finish
devs.push(Device { devs.push(Device {
thin_id: thin_id as u32, thin_id: thin_id as u32,
detail: detail.clone(), detail: *detail,
map: Mapping { map: Mapping {
kr, kr,
entries: es.to_vec(), entries: es.to_vec(),
@ -381,7 +381,7 @@ fn build_metadata(ctx: &Context, sb: &Superblock) -> Result<Metadata> {
//------------------------------------------ //------------------------------------------
fn gather_entries(g: &mut Gatherer, es: &Vec<Entry>) { fn gather_entries(g: &mut Gatherer, es: &[Entry]) {
g.new_seq(); g.new_seq();
for e in es { for e in es {
match e { match e {
@ -395,7 +395,7 @@ fn gather_entries(g: &mut Gatherer, es: &Vec<Entry>) {
} }
} }
fn entries_to_runs(runs: &BTreeMap<u64, Vec<u64>>, es: &Vec<Entry>) -> Vec<Entry> { fn entries_to_runs(runs: &BTreeMap<u64, Vec<u64>>, es: &[Entry]) -> Vec<Entry> {
use Entry::*; use Entry::*;
let mut result = Vec::new(); let mut result = Vec::new();
@ -542,7 +542,7 @@ fn emit_leaves(ctx: &Context, out: &mut dyn xml::MetadataVisitor, ls: &[u64]) ->
fn emit_entries<W: Write>( fn emit_entries<W: Write>(
ctx: &Context, ctx: &Context,
out: &mut xml::XmlWriter<W>, out: &mut xml::XmlWriter<W>,
entries: &Vec<Entry>, entries: &[Entry],
) -> Result<()> { ) -> Result<()> {
let mut leaves = Vec::new(); let mut leaves = Vec::new();
@ -552,7 +552,7 @@ fn emit_entries<W: Write>(
leaves.push(*b); leaves.push(*b);
} }
Entry::Ref(id) => { Entry::Ref(id) => {
if leaves.len() > 0 { if !leaves.is_empty() {
emit_leaves(&ctx, out, &leaves[0..])?; emit_leaves(&ctx, out, &leaves[0..])?;
leaves.clear(); leaves.clear();
} }
@ -562,7 +562,7 @@ fn emit_entries<W: Write>(
} }
} }
if leaves.len() > 0 { if !leaves.is_empty() {
emit_leaves(&ctx, out, &leaves[0..])?; emit_leaves(&ctx, out, &leaves[0..])?;
} }

View File

@ -139,6 +139,12 @@ impl Gatherer {
} }
} }
impl Default for Gatherer {
fn default() -> Self {
Self::new()
}
}
//------------------------------------------ //------------------------------------------
#[cfg(test)] #[cfg(test)]