diff --git a/src/bin/thin_metadata_pack.rs b/src/bin/thin_metadata_pack.rs index 6019147..ff3afa7 100644 --- a/src/bin/thin_metadata_pack.rs +++ b/src/bin/thin_metadata_pack.rs @@ -31,7 +31,7 @@ fn main() { exit(1); } - if let Err(reason) = thinp::pack::pack::pack(&input_file, &output_file) { + if let Err(reason) = thinp::pack::toplevel::pack(&input_file, &output_file) { println!("Application error: {}\n", reason); exit(1); } diff --git a/src/bin/thin_metadata_unpack.rs b/src/bin/thin_metadata_unpack.rs index 79cdd87..a87dc8a 100644 --- a/src/bin/thin_metadata_unpack.rs +++ b/src/bin/thin_metadata_unpack.rs @@ -33,7 +33,7 @@ fn main() { exit(1); } - if let Err(reason) = thinp::pack::pack::unpack(&input_file, &output_file) { + if let Err(reason) = thinp::pack::toplevel::unpack(&input_file, &output_file) { println!("Application error: {}", reason); process::exit(1); } diff --git a/src/block_manager.rs b/src/block_manager.rs index a321780..78dd069 100644 --- a/src/block_manager.rs +++ b/src/block_manager.rs @@ -31,7 +31,7 @@ impl BlockManager { Ok(BlockManager { nr_blocks: get_nr_blocks(path)?, - input: input, + input, }) } diff --git a/src/file_utils.rs b/src/file_utils.rs index a63469d..37d5c41 100644 --- a/src/file_utils.rs +++ b/src/file_utils.rs @@ -18,13 +18,13 @@ pub fn is_file_or_blk(info: FileStat) -> bool { pub fn file_exists(path: &str) -> bool { match stat::stat(path) { Ok(info) => { - return is_file_or_blk(info); + is_file_or_blk(info) } _ => { // FIXME: assuming all errors indicate the file doesn't // exist. eprintln!("couldn't stat '{}'", path); - return false; + false } } } @@ -46,8 +46,8 @@ fn get_device_size(path: &str) -> io::Result { let mut cap = 0u64; unsafe { match ioctl_blkgetsize64(fd, &mut cap) { - Ok(_) => {return Ok(cap);} - _ => {return fail("BLKGETSIZE64 ioctl failed");} + Ok(_) => {Ok(cap)} + _ => {fail("BLKGETSIZE64 ioctl failed")} } } } @@ -56,15 +56,15 @@ pub fn file_size(path: &str) -> io::Result { match stat::stat(path) { Ok(info) => { if check_bits(info.st_mode, &SFlag::S_IFREG) { - return Ok(info.st_size as u64); + Ok(info.st_size as u64) } else if check_bits(info.st_mode, &SFlag::S_IFBLK) { - return get_device_size(path); + get_device_size(path) } else { - return fail("not a regular file or block device"); + fail("not a regular file or block device") } } _ => { - return fail("stat failed"); + fail("stat failed") } } } diff --git a/src/pack/delta_list.rs b/src/pack/delta_list.rs index b62aebb..6a8a7e1 100644 --- a/src/pack/delta_list.rs +++ b/src/pack/delta_list.rs @@ -11,49 +11,55 @@ pub enum Delta { use Delta::*; pub fn to_delta(ns: &[u64]) -> Vec { + use std::cmp::Ordering::*; + let mut ds = Vec::new(); - if ns.len() > 0 { + if !ns.is_empty() { let mut base = ns[0]; ds.push(Base { n: base }); let mut i = 1; while i < ns.len() { let n = ns[i]; - if n > base { - let delta = n - base; - let mut count = 1; - while i < ns.len() && (ns[i] == (base + (count * delta))) { - i += 1; - count += 1; + match n.cmp(&base) { + Less => { + let delta = base - n; + let mut count = 1; + while i < ns.len() && (ns[i] + (count * delta) == base) { + i += 1; + count += 1; + } + count -= 1; + ds.push(Neg { + delta, + count, + }); + base -= delta * count; } - count -= 1; - ds.push(Pos { - delta: delta, - count: count, - }); - base += delta * count; - } else if n < base { - let delta = base - n; - let mut count = 1; - while i < ns.len() && (ns[i] + (count * delta) == base) { - i += 1; - count += 1; + Equal => { + let mut count = 1; + while i < ns.len() && ns[i] == base { + i += 1; + count += 1; + } + count -= 1; + ds.push(Const { count }); } - count -= 1; - ds.push(Neg { - delta: delta, - count: count, - }); - base -= delta * count; - } else { - let mut count = 1; - while i < ns.len() && ns[i] == base { - i += 1; - count += 1; + Greater => { + let delta = n - base; + let mut count = 1; + while i < ns.len() && (ns[i] == (base + (count * delta))) { + i += 1; + count += 1; + } + count -= 1; + ds.push(Pos { + delta, + count, + }); + base += delta * count; } - count -= 1; - ds.push(Const { count: count }); } } } diff --git a/src/pack/mod.rs b/src/pack/mod.rs index db8b935..e8ed451 100644 --- a/src/pack/mod.rs +++ b/src/pack/mod.rs @@ -1,6 +1,5 @@ -pub mod pack; +pub mod toplevel; -mod node_encode; mod delta_list; +mod node_encode; mod vm; - diff --git a/src/pack/node_encode.rs b/src/pack/node_encode.rs index e5d2fb4..c5e8370 100644 --- a/src/pack/node_encode.rs +++ b/src/pack/node_encode.rs @@ -17,17 +17,17 @@ impl std::error::Error for PackError {} pub type PResult = Result; fn nom_to_pr(r: IResult<&[u8], T>) -> PResult<(&[u8], T)> { - return match r { + match r { Ok(v) => Ok(v), Err(_) => Err(PackError::ParseError), - }; + } } fn io_to_pr(r: io::Result) -> PResult { - return match r { + match r { Ok(v) => Ok(v), Err(_) => Err(PackError::IOError), - }; + } } //------------------------------------------- @@ -79,11 +79,11 @@ pub fn pack_btree_node(w: &mut W, data: &[u8]) -> PResult<()> { io_to_pr(pack_literal(w, hdr))?; io_to_pr(pack_u64s(w, &keys))?; io_to_pr(pack_shifted_u64s(w, &values))?; - if tail.len() > 0 { + if !tail.is_empty() { io_to_pr(pack_literal(w, tail))?; } - return Ok(()); + Ok(()) } else { // We don't bother packing the values if they aren't u64 let (i, hdr) = nom_to_pr(take(32usize)(data))?; @@ -93,7 +93,7 @@ pub fn pack_btree_node(w: &mut W, data: &[u8]) -> PResult<()> { io_to_pr(pack_u64s(w, &keys))?; io_to_pr(pack_literal(w, tail))?; - return Ok(()); + Ok(()) } } else { // Internal node, values are also u64s @@ -104,11 +104,11 @@ pub fn pack_btree_node(w: &mut W, data: &[u8]) -> PResult<()> { io_to_pr(pack_literal(w, hdr))?; io_to_pr(pack_u64s(w, &keys))?; io_to_pr(pack_u64s(w, &values))?; - if tail.len() > 0 { + if !tail.is_empty() { io_to_pr(pack_literal(w, tail))?; } - return Ok(()); + Ok(()) } } diff --git a/src/pack/pack.rs b/src/pack/toplevel.rs similarity index 95% rename from src/pack/pack.rs rename to src/pack/toplevel.rs index cff11e9..bd1757d 100644 --- a/src/pack/pack.rs +++ b/src/pack/toplevel.rs @@ -18,8 +18,8 @@ use std::{ use rand::prelude::*; use std::sync::mpsc::{sync_channel, Receiver}; -use crate::pack::node_encode::*; use crate::file_utils; +use crate::pack::node_encode::*; const BLOCK_SIZE: u64 = 4096; const MAGIC: u64 = 0xa537a0aa6309ef77; @@ -170,19 +170,19 @@ where R: byteorder::ReadBytesExt, { use std::process::exit; - + let magic = r.read_u64::()?; if magic != MAGIC { eprintln!("Not a pack file."); exit(1); } - + let version = r.read_u64::()?; if version != PACK_VERSION { eprintln!("unsupported pack file version ({}).", PACK_VERSION); exit(1); } - + let block_size = r.read_u64::()?; if block_size != BLOCK_SIZE { eprintln!("block size is not {}", BLOCK_SIZE); @@ -216,7 +216,7 @@ fn checksum(buf: &[u8]) -> u32 { #[derive(PartialEq)] enum BT { SUPERBLOCK, - BTREE, + NODE, INDEX, BITMAP, UNKNOWN, @@ -234,21 +234,17 @@ fn metadata_block_type(buf: &[u8]) -> BT { let btype = csum ^ sum_on_disk; match btype { - SUPERBLOCK_CSUM_XOR => return BT::SUPERBLOCK, - BTREE_CSUM_XOR => return BT::BTREE, - BITMAP_CSUM_XOR => return BT::BITMAP, - INDEX_CSUM_XOR => return BT::INDEX, - _ => { - return BT::UNKNOWN; - } + SUPERBLOCK_CSUM_XOR => BT::SUPERBLOCK, + BTREE_CSUM_XOR => BT::NODE, + BITMAP_CSUM_XOR => BT::BITMAP, + INDEX_CSUM_XOR => BT::INDEX, + _ => BT::UNKNOWN, } } fn check(r: &PResult) { match r { - Ok(_) => { - return; - } + Ok(_) => {} Err(PackError::ParseError) => panic!("parse error"), Err(PackError::IOError) => panic!("io error"), } @@ -257,12 +253,10 @@ fn check(r: &PResult) { fn pack_block(w: &mut W, kind: BT, buf: &[u8]) { match kind { BT::SUPERBLOCK => check(&pack_superblock(w, buf)), - BT::BTREE => check(&pack_btree_node(w, buf)), + BT::NODE => check(&pack_btree_node(w, buf)), BT::INDEX => check(&pack_index(w, buf)), BT::BITMAP => check(&pack_bitmap(w, buf)), - BT::UNKNOWN => { - assert!(false); - } + BT::UNKNOWN => {panic!("asked to pack an unknown block type")} } } diff --git a/src/pack/vm.rs b/src/pack/vm.rs index 15bbc0f..592c005 100644 --- a/src/pack/vm.rs +++ b/src/pack/vm.rs @@ -48,16 +48,16 @@ where W: Write, { if count == 1u64 { - return Ok(()); + Ok(()) } else if count < 16 { - return pack_tag(w, Tag::Count, count as u8); + pack_tag(w, Tag::Count, count as u8) } else { assert!(count < 4096); let nibble = count >> 8; assert!(nibble < 16); let byte = count & 0xff; pack_tag(w, Tag::Count8, nibble as u8)?; - return w.write_u8(byte as u8); + w.write_u8(byte as u8) } } @@ -68,64 +68,64 @@ fn pack_delta(w: &mut W, d: &Delta) -> io::Result<()> { Delta::Base { n } => { if *n <= std::u8::MAX as u64 { pack_tag(w, Set, 1)?; - return w.write_u8(*n as u8); + w.write_u8(*n as u8) } else if *n <= std::u16::MAX as u64 { pack_tag(w, Set, 2)?; - return w.write_u16::(*n as u16); + w.write_u16::(*n as u16) } else if *n <= u32::MAX as u64 { pack_tag(w, Set, 4)?; - return w.write_u32::(*n as u32); + w.write_u32::(*n as u32) } else { pack_tag(w, Set, 8)?; - return w.write_u64::(*n); + w.write_u64::(*n) } } Delta::Pos { delta, count } => { pack_count(w, *count)?; if *delta < 16 { - return pack_tag(w, Tag::Pos, *delta as u8); + pack_tag(w, Tag::Pos, *delta as u8) } else if *delta <= u8::MAX as u64 { pack_tag(w, PosW, 1)?; - return w.write_u8(*delta as u8); + w.write_u8(*delta as u8) } else if *delta <= u16::MAX as u64 { pack_tag(w, PosW, 2)?; - return w.write_u16::(*delta as u16); + w.write_u16::(*delta as u16) } else if *delta <= u32::MAX as u64 { pack_tag(w, PosW, 4)?; - return w.write_u32::(*delta as u32); + w.write_u32::(*delta as u32) } else { pack_tag(w, PosW, 8)?; - return w.write_u64::(*delta as u64); + w.write_u64::(*delta as u64) } } Delta::Neg { delta, count } => { pack_count(w, *count)?; if *delta < 16 { - return pack_tag(w, Neg, *delta as u8); + pack_tag(w, Neg, *delta as u8) } else if *delta <= u8::MAX as u64 { pack_tag(w, NegW, 1)?; - return w.write_u8(*delta as u8); + w.write_u8(*delta as u8) } else if *delta <= u16::MAX as u64 { pack_tag(w, NegW, 2)?; - return w.write_u16::(*delta as u16); + w.write_u16::(*delta as u16) } else if *delta <= u32::MAX as u64 { pack_tag(w, NegW, 4)?; - return w.write_u32::(*delta as u32); + w.write_u32::(*delta as u32) } else { pack_tag(w, NegW, 8)?; - return w.write_u64::(*delta as u64); + w.write_u64::(*delta as u64) } } Delta::Const { count } => { if *count < 16 { - return pack_tag(w, Tag::Const, *count as u8); + pack_tag(w, Tag::Const, *count as u8) } else { assert!(*count < 4096); let nibble = *count >> 8; assert!(nibble < 16); pack_tag(w, Tag::Const8, nibble as u8)?; - return w.write_u8((*count & 0xff) as u8); + w.write_u8((*count & 0xff) as u8) } } }