[rust] squash clippy (linter) warnings
This commit is contained in:
parent
f90010e22a
commit
7a85e47a20
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ impl BlockManager {
|
||||
|
||||
Ok(BlockManager {
|
||||
nr_blocks: get_nr_blocks(path)?,
|
||||
input: input,
|
||||
input,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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<u64> {
|
||||
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<u64> {
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,49 +11,55 @@ pub enum Delta {
|
||||
use Delta::*;
|
||||
|
||||
pub fn to_delta(ns: &[u64]) -> Vec<Delta> {
|
||||
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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
pub mod pack;
|
||||
pub mod toplevel;
|
||||
|
||||
mod node_encode;
|
||||
mod delta_list;
|
||||
mod node_encode;
|
||||
mod vm;
|
||||
|
||||
|
@ -17,17 +17,17 @@ impl std::error::Error for PackError {}
|
||||
pub type PResult<T> = Result<T, PackError>;
|
||||
|
||||
fn nom_to_pr<T>(r: IResult<&[u8], T>) -> PResult<(&[u8], T)> {
|
||||
return match r {
|
||||
match r {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => Err(PackError::ParseError),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn io_to_pr<T>(r: io::Result<T>) -> PResult<T> {
|
||||
return match r {
|
||||
match r {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => Err(PackError::IOError),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------
|
||||
@ -79,11 +79,11 @@ pub fn pack_btree_node<W: Write>(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: Write>(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: Write>(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(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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::<LittleEndian>()?;
|
||||
if magic != MAGIC {
|
||||
eprintln!("Not a pack file.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
let version = r.read_u64::<LittleEndian>()?;
|
||||
if version != PACK_VERSION {
|
||||
eprintln!("unsupported pack file version ({}).", PACK_VERSION);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
let block_size = r.read_u64::<LittleEndian>()?;
|
||||
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<T>(r: &PResult<T>) {
|
||||
match r {
|
||||
Ok(_) => {
|
||||
return;
|
||||
}
|
||||
Ok(_) => {}
|
||||
Err(PackError::ParseError) => panic!("parse error"),
|
||||
Err(PackError::IOError) => panic!("io error"),
|
||||
}
|
||||
@ -257,12 +253,10 @@ fn check<T>(r: &PResult<T>) {
|
||||
fn pack_block<W: Write>(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")}
|
||||
}
|
||||
}
|
||||
|
@ -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: Write>(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::<LittleEndian>(*n as u16);
|
||||
w.write_u16::<LittleEndian>(*n as u16)
|
||||
} else if *n <= u32::MAX as u64 {
|
||||
pack_tag(w, Set, 4)?;
|
||||
return w.write_u32::<LittleEndian>(*n as u32);
|
||||
w.write_u32::<LittleEndian>(*n as u32)
|
||||
} else {
|
||||
pack_tag(w, Set, 8)?;
|
||||
return w.write_u64::<LittleEndian>(*n);
|
||||
w.write_u64::<LittleEndian>(*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::<LittleEndian>(*delta as u16);
|
||||
w.write_u16::<LittleEndian>(*delta as u16)
|
||||
} else if *delta <= u32::MAX as u64 {
|
||||
pack_tag(w, PosW, 4)?;
|
||||
return w.write_u32::<LittleEndian>(*delta as u32);
|
||||
w.write_u32::<LittleEndian>(*delta as u32)
|
||||
} else {
|
||||
pack_tag(w, PosW, 8)?;
|
||||
return w.write_u64::<LittleEndian>(*delta as u64);
|
||||
w.write_u64::<LittleEndian>(*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::<LittleEndian>(*delta as u16);
|
||||
w.write_u16::<LittleEndian>(*delta as u16)
|
||||
} else if *delta <= u32::MAX as u64 {
|
||||
pack_tag(w, NegW, 4)?;
|
||||
return w.write_u32::<LittleEndian>(*delta as u32);
|
||||
w.write_u32::<LittleEndian>(*delta as u32)
|
||||
} else {
|
||||
pack_tag(w, NegW, 8)?;
|
||||
return w.write_u64::<LittleEndian>(*delta as u64);
|
||||
w.write_u64::<LittleEndian>(*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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user