[all] Apply cargo fmt, and fix clippy warnings

This commit is contained in:
Ming-Hung Tsai
2021-10-19 22:35:06 +08:00
parent 13aeefcdeb
commit c8a1da1df9
39 changed files with 125 additions and 151 deletions

View File

@@ -23,7 +23,7 @@ fn main_() -> Result<()> {
let mut new_args = vec![OsString::from(&name)];
for a in args.into_iter() {
new_args.push(OsString::from(a));
new_args.push(a);
}
if name_eq(name, "cache_check") {

View File

@@ -266,7 +266,7 @@ impl<'a> Widget for HeaderWidget<'a> {
fn read_node<V: Unpack>(engine: &dyn IoEngine, loc: u64) -> Result<btree::Node<V>> {
let b = engine.read(loc)?;
let path = Vec::new();
btree::unpack_node(&path, &b.get_data(), true, false)
btree::unpack_node(&path, b.get_data(), true, false)
.map_err(|_| anyhow!("couldn't unpack btree node"))
}
@@ -765,7 +765,7 @@ fn perform_action(
}
fn explore(path: &Path, node_path: Option<Vec<u64>>) -> Result<()> {
let engine = SyncIoEngine::new(&path, 1, false)?;
let engine = SyncIoEngine::new(path, 1, false)?;
let mut panels: Vec<Box<dyn Panel>> = Vec::new();
@@ -861,7 +861,7 @@ fn main() -> Result<()> {
.map(|text| btree::decode_node_path(text).unwrap());
let input_file = Path::new(matches.value_of("INPUT").unwrap());
explore(&input_file, node_path)
explore(input_file, node_path)
}
//------------------------------------

10
src/cache/check.rs vendored
View File

@@ -92,10 +92,10 @@ mod format1 {
let mut errs: Vec<ArrayError> = Vec::new();
for m in b.values.iter() {
if let Err(e) = self.check_flags(&m) {
if let Err(e) = self.check_flags(m) {
errs.push(e);
}
if let Err(e) = self.check_oblock(&m) {
if let Err(e) = self.check_oblock(m) {
errs.push(e);
}
}
@@ -182,10 +182,10 @@ mod format2 {
let cbegin = index as u32 * b.header.max_entries;
let cend = cbegin + b.header.nr_entries;
for (m, cblock) in b.values.iter().zip(cbegin..cend) {
if let Err(e) = self.check_flags(&m, inner.dirty_bits.contains(cblock as usize)) {
if let Err(e) = self.check_flags(m, inner.dirty_bits.contains(cblock as usize)) {
errs.push(e);
}
if let Err(e) = self.check_oblock(&m, &mut inner.seen_oblocks) {
if let Err(e) = self.check_oblock(m, &mut inner.seen_oblocks) {
errs.push(e);
}
}
@@ -271,7 +271,7 @@ pub fn check(opts: CacheCheckOptions) -> anyhow::Result<()> {
let sb = match read_superblock(engine.as_ref(), SUPERBLOCK_LOCATION) {
Ok(sb) => sb,
Err(e) => {
check_not_xml(&opts.dev, &opts.report);
check_not_xml(opts.dev, &opts.report);
return Err(e);
}
};

View File

@@ -96,7 +96,7 @@ fn unpack(data: &[u8]) -> IResult<&[u8], Superblock> {
if version >= 2 {
let (m, root) = le_u64(i)?;
dirty_root = Some(root);
i = &m;
i = m;
}
Ok((

View File

@@ -7,8 +7,8 @@ use std::process;
use std::sync::Arc;
use crate::cache::check::{check, CacheCheckOptions};
use crate::report::*;
use crate::commands::utils::*;
use crate::report::*;
//------------------------------------------
@@ -76,7 +76,7 @@ pub fn run(args: &[std::ffi::OsString]) {
check_file_not_tiny(input_file, &report);
let opts = CacheCheckOptions {
dev: &input_file,
dev: input_file,
async_io: matches.is_present("ASYNC_IO"),
sb_only: matches.is_present("SB_ONLY"),
skip_mappings: matches.is_present("SKIP_MAPPINGS"),

View File

@@ -60,8 +60,8 @@ pub fn run(args: &[std::ffi::OsString]) {
check_input_file(input_file, &report);
let opts = CacheRepairOptions {
input: &input_file,
output: &output_file,
input: input_file,
output: output_file,
async_io: matches.is_present("ASYNC_IO"),
report: report.clone(),
};

View File

@@ -51,8 +51,8 @@ pub fn run(args: &[std::ffi::OsString]) {
check_output_file(output_file, &report);
let opts = CacheRestoreOptions {
input: &input_file,
output: &output_file,
input: input_file,
output: output_file,
async_io: matches.is_present("ASYNC_IO"),
report: report.clone(),
};

View File

@@ -5,9 +5,9 @@ use std::path::Path;
use std::process;
use std::sync::Arc;
use crate::commands::utils::*;
use crate::io_engine::*;
use crate::thin::check::{check, ThinCheckOptions, MAX_CONCURRENT_IO};
use crate::commands::utils::*;
pub fn run(args: &[std::ffi::OsString]) {
let parser = App::new("thin_check")
@@ -88,7 +88,7 @@ pub fn run(args: &[std::ffi::OsString]) {
.index(1),
);
let matches = parser.get_matches_from(args.into_iter());
let matches = parser.get_matches_from(args.iter());
let input_file = Path::new(matches.value_of("INPUT").unwrap());
let report = mk_report(matches.is_present("QUIET"));
@@ -101,14 +101,13 @@ pub fn run(args: &[std::ffi::OsString]) {
if matches.is_present("ASYNC_IO") {
engine = Arc::new(
AsyncIoEngine::new(&input_file, MAX_CONCURRENT_IO, writable)
AsyncIoEngine::new(input_file, MAX_CONCURRENT_IO, writable)
.expect("unable to open input file"),
);
} else {
let nr_threads = std::cmp::max(8, num_cpus::get() * 2);
engine = Arc::new(
SyncIoEngine::new(&input_file, nr_threads, writable)
.expect("unable to open input file"),
SyncIoEngine::new(input_file, nr_threads, writable).expect("unable to open input file"),
);
}

View File

@@ -31,7 +31,7 @@ pub fn run(args: &[std::ffi::OsString]) {
let report = std::sync::Arc::new(mk_simple_report());
check_input_file(input_file, &report);
if let Err(reason) = crate::pack::toplevel::pack(&input_file, &output_file) {
if let Err(reason) = crate::pack::toplevel::pack(input_file, output_file) {
report.fatal(&format!("Application error: {}\n", reason));
exit(1);
}

View File

@@ -1,9 +1,9 @@
extern crate clap;
use crate::file_utils;
use clap::{App, Arg};
use std::path::Path;
use std::process;
use crate::file_utils;
use std::process::exit;
@@ -37,7 +37,7 @@ pub fn run(args: &[std::ffi::OsString]) {
exit(1);
}
if let Err(reason) = crate::pack::toplevel::unpack(&input_file, &output_file) {
if let Err(reason) = crate::pack::toplevel::unpack(input_file, output_file) {
eprintln!("Application error: {}", reason);
process::exit(1);
}

View File

@@ -91,8 +91,8 @@ pub fn run(args: &[std::ffi::OsString]) {
});
let opts = ThinRepairOptions {
input: &input_file,
output: &output_file,
input: input_file,
output: output_file,
async_io: matches.is_present("ASYNC_IO"),
report: report.clone(),
overrides: SuperblockOverrides {

View File

@@ -51,8 +51,8 @@ pub fn run(args: &[std::ffi::OsString]) {
check_output_file(output_file, &report);
let opts = ThinRestoreOptions {
input: &input_file,
output: &output_file,
input: input_file,
output: output_file,
async_io: matches.is_present("ASYNC_IO"),
report: report.clone(),
};

View File

@@ -70,7 +70,7 @@ pub fn run(args: &[std::ffi::OsString]) {
check_input_file(input_file, &report);
if let Err(reason) =
crate::shrink::toplevel::shrink(&input_file, &output_file, &data_file, size, do_copy)
crate::shrink::toplevel::shrink(input_file, output_file, data_file, size, do_copy)
{
eprintln!("Application error: {}\n", reason);
exit(1);

View File

@@ -10,10 +10,7 @@ use crate::report::*;
pub fn check_input_file(input_file: &Path, report: &Report) {
if !file_utils::file_exists(input_file) {
report.fatal(&format!(
"Couldn't find input file '{:?}'.",
&input_file
));
report.fatal(&format!("Couldn't find input file '{:?}'.", &input_file));
exit(1);
}
@@ -62,15 +59,11 @@ pub fn mk_report(quiet: bool) -> std::sync::Arc<Report> {
}
fn is_xml(line: &[u8]) -> bool {
line.starts_with(b"<superblock") ||
line.starts_with(b"?xml") ||
line.starts_with(b"<!DOCTYPE")
line.starts_with(b"<superblock") || line.starts_with(b"?xml") || line.starts_with(b"<!DOCTYPE")
}
pub fn check_not_xml_(input_file: &Path, report: &Report) -> Result<()> {
let mut file = OpenOptions::new()
.read(true)
.open(input_file)?;
let mut file = OpenOptions::new().read(true).open(input_file)?;
let mut data = vec![0; 16];
file.read_exact(&mut data)?;

View File

@@ -18,28 +18,19 @@ pub fn is_file_or_blk_(info: FileStat) -> bool {
}
pub fn file_exists(path: &Path) -> bool {
match stat::stat(path) {
Ok(_) => true,
_ => false,
}
matches!(stat::stat(path), Ok(_))
}
pub fn is_file_or_blk(path: &Path) -> bool {
match stat::stat(path) {
Ok(info) =>is_file_or_blk_(info),
Ok(info) => is_file_or_blk_(info),
_ => false,
}
}
pub fn is_file(path: &Path) -> bool {
match stat::stat(path) {
Ok(info) => {
if test_bit(info.st_mode, SFlag::S_IFREG) {
true
} else {
false
}
}
Ok(info) => test_bit(info.st_mode, SFlag::S_IFREG),
_ => false,
}
}
@@ -113,5 +104,3 @@ pub fn create_sized_file(path: &Path, nr_bytes: u64) -> io::Result<std::fs::File
}
//---------------------------------------

View File

@@ -109,7 +109,7 @@ impl<'a> Deref for FileGuard<'a> {
type Target = File;
fn deref(&self) -> &File {
&self.file.as_ref().expect("empty file guard")
self.file.as_ref().expect("empty file guard")
}
}
@@ -180,7 +180,7 @@ impl SyncIoEngine {
fn write_(output: &mut File, b: &Block) -> Result<()> {
output.seek(io::SeekFrom::Start(b.loc * BLOCK_SIZE as u64))?;
output.write_all(&b.get_data())?;
output.write_all(b.get_data())?;
Ok(())
}
}

View File

@@ -66,7 +66,7 @@ fn mk_chunk_vecs(nr_blocks: u64, nr_jobs: u64) -> Vec<Vec<(u64, u64)>> {
}
pub fn pack(input_file: &Path, output_file: &Path) -> Result<(), Box<dyn Error>> {
let nr_blocks = get_nr_blocks(&input_file)?;
let nr_blocks = get_nr_blocks(input_file)?;
let nr_jobs = std::cmp::max(1, std::cmp::min(num_cpus::get() as u64, nr_blocks / 128));
let chunk_vecs = mk_chunk_vecs(nr_blocks, nr_jobs);
@@ -122,7 +122,7 @@ where
let kind = metadata_block_type(data);
if kind != BT::UNKNOWN {
z.write_u64::<LittleEndian>(b)?;
pack_block(&mut z, kind, &data)?;
pack_block(&mut z, kind, data)?;
written += 1;
if written == 1024 {

View File

@@ -442,7 +442,7 @@ mod tests {
let mut r = Cursor::new(&mut bs);
let unpacked = unpack(&mut r, ns.len() * 8).unwrap();
check_u64s_match(&ns, &unpacked[0..])
check_u64s_match(ns, &unpacked[0..])
}
#[test]
@@ -457,7 +457,7 @@ mod tests {
];
for t in &cases {
assert!(check_pack_u64s(&t));
assert!(check_pack_u64s(t));
}
}

View File

@@ -77,7 +77,7 @@ impl<'a, V: Unpack + Copy> NodeVisitor<u64> for BlockValueVisitor<'a, V> {
for (i, b) in values.iter().enumerate() {
// TODO: report indices of array entries based on the type size
let mut array_errs = self.array_errs.lock().unwrap();
array_errs.push(array::io_err(&path, *b).index_context(keys[i]));
array_errs.push(array::io_err(path, *b).index_context(keys[i]));
}
}
Ok(rblocks) => {
@@ -85,7 +85,7 @@ impl<'a, V: Unpack + Copy> NodeVisitor<u64> for BlockValueVisitor<'a, V> {
match rb {
Err(_) => {
let mut array_errs = self.array_errs.lock().unwrap();
array_errs.push(array::io_err(&path, values[i]).index_context(keys[i]));
array_errs.push(array::io_err(path, values[i]).index_context(keys[i]));
}
Ok(b) => {
let mut path = path.to_vec();

View File

@@ -561,7 +561,7 @@ pub fn unpack_node<V: Unpack>(
if let Some(l) = last {
if k <= l {
return Err(node_err(
&path,
path,
&format!("keys out of order: {} <= {}", k, l),
));
}
@@ -582,7 +582,7 @@ pub fn unpack_node<V: Unpack>(
values,
})
} 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 {
header,
keys,

View File

@@ -132,10 +132,10 @@ impl<'a> LeafWalker<'a> {
.keys_context(kr));
}
let node = unpack_node::<V>(path, &b.get_data(), self.ignore_non_fatal, is_root)?;
let node = unpack_node::<V>(path, b.get_data(), self.ignore_non_fatal, is_root)?;
if let Internal { keys, values, .. } = node {
let krs = split_key_ranges(path, &kr, &keys)?;
let krs = split_key_ranges(path, kr, &keys)?;
if depth == 0 {
// it is the lowest internal
for i in 0..krs.len() {
@@ -187,7 +187,7 @@ impl<'a> LeafWalker<'a> {
));
}
let node = unpack_node::<V>(path, &b.get_data(), self.ignore_non_fatal, is_root)?;
let node = unpack_node::<V>(path, b.get_data(), self.ignore_non_fatal, is_root)?;
match node {
Internal { values, .. } => {

View File

@@ -195,11 +195,11 @@ impl BTreeWalker {
.keys_context(kr));
}
let node = unpack_node::<V>(path, &b.get_data(), self.ignore_non_fatal, is_root)?;
let node = unpack_node::<V>(path, b.get_data(), self.ignore_non_fatal, is_root)?;
match node {
Internal { keys, values, .. } => {
let krs = split_key_ranges(path, &kr, &keys)?;
let krs = split_key_ranges(path, kr, &keys)?;
let errs = self.walk_nodes(path, visitor, &krs, &values);
return self.build_aggregate(b.loc, errs);
}
@@ -208,7 +208,7 @@ impl BTreeWalker {
keys,
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));
self.set_fail(b.loc, e.clone());
return Err(e);
@@ -286,11 +286,11 @@ where
.keys_context(kr));
}
let node = unpack_node::<V>(path, &b.get_data(), w.ignore_non_fatal, is_root)?;
let node = unpack_node::<V>(path, b.get_data(), w.ignore_non_fatal, is_root)?;
match node {
Internal { keys, values, .. } => {
let krs = split_key_ranges(path, &kr, &keys)?;
let krs = split_key_ranges(path, kr, &keys)?;
let errs = walk_nodes_threaded(w.clone(), path, pool, visitor, &krs, &values);
return w.build_aggregate(b.loc, errs);
}

View File

@@ -109,7 +109,7 @@ fn check_low_ref_counts(
return Err(anyhow!("Unable to read bitmap block"));
}
Ok(b) => {
if checksum::metadata_block_type(&b.get_data()) != checksum::BT::BITMAP {
if checksum::metadata_block_type(b.get_data()) != checksum::BT::BITMAP {
report.fatal(&format!(
"Index entry points to block ({}) that isn't a bitmap",
b.loc

View File

@@ -117,7 +117,7 @@ impl ReportInner for PBInner {
fn set_sub_title(&mut self, txt: &str) {
//let mut fmt = "".to_string(); //Checking thin metadata".to_string(); //self.title.clone();
let mut fmt = "Checking thin metadata [{bar:40}] Remaining {eta}, ".to_string();
fmt.push_str(&txt);
fmt.push_str(txt);
self.bar.set_style(
ProgressStyle::default_bar()
.template(&fmt)

View File

@@ -343,16 +343,16 @@ fn find_first(r: &BlockRange, remaps: &[(BlockRange, BlockRange)]) -> Option<usi
Err(n) => {
if n == 0 {
let (from, _) = &remaps[n];
overlaps(&r, &from, n)
overlaps(r, from, n)
} else if n == remaps.len() {
let (from, _) = &remaps[n - 1];
overlaps(&r, from, n - 1)
overlaps(r, from, n - 1)
} else {
// Need to check the previous entry
let (from, _) = &remaps[n - 1];
overlaps(&r, &from, n - 1).or_else(|| {
overlaps(r, from, n - 1).or_else(|| {
let (from, _) = &remaps[n];
overlaps(&r, &from, n)
overlaps(r, from, n)
})
}
}
@@ -368,7 +368,7 @@ fn remap(r: &BlockRange, remaps: &[(BlockRange, BlockRange)]) -> Vec<BlockRange>
let mut remap = Vec::new();
let mut r = r.start..r.end;
if let Some(index) = find_first(&r, &remaps) {
if let Some(index) = find_first(&r, remaps) {
let mut index = index;
loop {
let (from, to) = &remaps[index];
@@ -487,7 +487,7 @@ fn build_copy_regions(remaps: &[(BlockRange, BlockRange)], block_size: u64) -> V
rs.push(Region {
src: from.start * block_size,
dest: to.start * block_size,
len: range_len(&from) * block_size,
len: range_len(from) * block_size,
});
}

View File

@@ -188,7 +188,7 @@ fn emit_leaf(v: &mut MappingVisitor, b: &Block) -> Result<()> {
)));
}
let node = unpack_node::<BlockTime>(&path, &b.get_data(), true, true)?;
let node = unpack_node::<BlockTime>(&path, b.get_data(), true, true)?;
match node {
Internal { .. } => {

View File

@@ -153,7 +153,7 @@ impl<'a> Restorer<'a> {
};
for (_, leaves) in self.sub_trees.iter() {
release_leaves(self.w, &leaves, &mut value_rc)?;
release_leaves(self.w, leaves, &mut value_rc)?;
}
Ok(())

View File

@@ -125,7 +125,7 @@ impl Gatherer {
// Now we need to mark entries that follow a tail as heads.
let mut heads = mem::take(&mut self.heads);
for t in &self.tails {
if let Some(e) = self.entries.get(&t) {
if let Some(e) = self.entries.get(t) {
for n in &e.neighbours {
heads.insert(*n);
}