[rust] squash clippy (linter) warnings
This commit is contained in:
parent
f90010e22a
commit
7a85e47a20
@ -31,7 +31,7 @@ fn main() {
|
|||||||
exit(1);
|
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);
|
println!("Application error: {}\n", reason);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ fn main() {
|
|||||||
exit(1);
|
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);
|
println!("Application error: {}", reason);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ impl BlockManager {
|
|||||||
|
|
||||||
Ok(BlockManager {
|
Ok(BlockManager {
|
||||||
nr_blocks: get_nr_blocks(path)?,
|
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 {
|
pub fn file_exists(path: &str) -> bool {
|
||||||
match stat::stat(path) {
|
match stat::stat(path) {
|
||||||
Ok(info) => {
|
Ok(info) => {
|
||||||
return is_file_or_blk(info);
|
is_file_or_blk(info)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// FIXME: assuming all errors indicate the file doesn't
|
// FIXME: assuming all errors indicate the file doesn't
|
||||||
// exist.
|
// exist.
|
||||||
eprintln!("couldn't stat '{}'", path);
|
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;
|
let mut cap = 0u64;
|
||||||
unsafe {
|
unsafe {
|
||||||
match ioctl_blkgetsize64(fd, &mut cap) {
|
match ioctl_blkgetsize64(fd, &mut cap) {
|
||||||
Ok(_) => {return Ok(cap);}
|
Ok(_) => {Ok(cap)}
|
||||||
_ => {return fail("BLKGETSIZE64 ioctl failed");}
|
_ => {fail("BLKGETSIZE64 ioctl failed")}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,15 +56,15 @@ pub fn file_size(path: &str) -> io::Result<u64> {
|
|||||||
match stat::stat(path) {
|
match stat::stat(path) {
|
||||||
Ok(info) => {
|
Ok(info) => {
|
||||||
if check_bits(info.st_mode, &SFlag::S_IFREG) {
|
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) {
|
} else if check_bits(info.st_mode, &SFlag::S_IFBLK) {
|
||||||
return get_device_size(path);
|
get_device_size(path)
|
||||||
} else {
|
} 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::*;
|
use Delta::*;
|
||||||
|
|
||||||
pub fn to_delta(ns: &[u64]) -> Vec<Delta> {
|
pub fn to_delta(ns: &[u64]) -> Vec<Delta> {
|
||||||
|
use std::cmp::Ordering::*;
|
||||||
|
|
||||||
let mut ds = Vec::new();
|
let mut ds = Vec::new();
|
||||||
|
|
||||||
if ns.len() > 0 {
|
if !ns.is_empty() {
|
||||||
let mut base = ns[0];
|
let mut base = ns[0];
|
||||||
ds.push(Base { n: base });
|
ds.push(Base { n: base });
|
||||||
|
|
||||||
let mut i = 1;
|
let mut i = 1;
|
||||||
while i < ns.len() {
|
while i < ns.len() {
|
||||||
let n = ns[i];
|
let n = ns[i];
|
||||||
if n > base {
|
match n.cmp(&base) {
|
||||||
let delta = n - base;
|
Less => {
|
||||||
let mut count = 1;
|
let delta = base - n;
|
||||||
while i < ns.len() && (ns[i] == (base + (count * delta))) {
|
let mut count = 1;
|
||||||
i += 1;
|
while i < ns.len() && (ns[i] + (count * delta) == base) {
|
||||||
count += 1;
|
i += 1;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
count -= 1;
|
||||||
|
ds.push(Neg {
|
||||||
|
delta,
|
||||||
|
count,
|
||||||
|
});
|
||||||
|
base -= delta * count;
|
||||||
}
|
}
|
||||||
count -= 1;
|
Equal => {
|
||||||
ds.push(Pos {
|
let mut count = 1;
|
||||||
delta: delta,
|
while i < ns.len() && ns[i] == base {
|
||||||
count: count,
|
i += 1;
|
||||||
});
|
count += 1;
|
||||||
base += delta * count;
|
}
|
||||||
} else if n < base {
|
count -= 1;
|
||||||
let delta = base - n;
|
ds.push(Const { count });
|
||||||
let mut count = 1;
|
|
||||||
while i < ns.len() && (ns[i] + (count * delta) == base) {
|
|
||||||
i += 1;
|
|
||||||
count += 1;
|
|
||||||
}
|
}
|
||||||
count -= 1;
|
Greater => {
|
||||||
ds.push(Neg {
|
let delta = n - base;
|
||||||
delta: delta,
|
let mut count = 1;
|
||||||
count: count,
|
while i < ns.len() && (ns[i] == (base + (count * delta))) {
|
||||||
});
|
i += 1;
|
||||||
base -= delta * count;
|
count += 1;
|
||||||
} else {
|
}
|
||||||
let mut count = 1;
|
count -= 1;
|
||||||
while i < ns.len() && ns[i] == base {
|
ds.push(Pos {
|
||||||
i += 1;
|
delta,
|
||||||
count += 1;
|
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 delta_list;
|
||||||
|
mod node_encode;
|
||||||
mod vm;
|
mod vm;
|
||||||
|
|
||||||
|
@ -17,17 +17,17 @@ impl std::error::Error for PackError {}
|
|||||||
pub type PResult<T> = Result<T, PackError>;
|
pub type PResult<T> = Result<T, PackError>;
|
||||||
|
|
||||||
fn nom_to_pr<T>(r: IResult<&[u8], T>) -> PResult<(&[u8], T)> {
|
fn nom_to_pr<T>(r: IResult<&[u8], T>) -> PResult<(&[u8], T)> {
|
||||||
return match r {
|
match r {
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok(v),
|
||||||
Err(_) => Err(PackError::ParseError),
|
Err(_) => Err(PackError::ParseError),
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn io_to_pr<T>(r: io::Result<T>) -> PResult<T> {
|
fn io_to_pr<T>(r: io::Result<T>) -> PResult<T> {
|
||||||
return match r {
|
match r {
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok(v),
|
||||||
Err(_) => Err(PackError::IOError),
|
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_literal(w, hdr))?;
|
||||||
io_to_pr(pack_u64s(w, &keys))?;
|
io_to_pr(pack_u64s(w, &keys))?;
|
||||||
io_to_pr(pack_shifted_u64s(w, &values))?;
|
io_to_pr(pack_shifted_u64s(w, &values))?;
|
||||||
if tail.len() > 0 {
|
if !tail.is_empty() {
|
||||||
io_to_pr(pack_literal(w, tail))?;
|
io_to_pr(pack_literal(w, tail))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
// We don't bother packing the values if they aren't u64
|
// We don't bother packing the values if they aren't u64
|
||||||
let (i, hdr) = nom_to_pr(take(32usize)(data))?;
|
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_u64s(w, &keys))?;
|
||||||
io_to_pr(pack_literal(w, tail))?;
|
io_to_pr(pack_literal(w, tail))?;
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Internal node, values are also u64s
|
// 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_literal(w, hdr))?;
|
||||||
io_to_pr(pack_u64s(w, &keys))?;
|
io_to_pr(pack_u64s(w, &keys))?;
|
||||||
io_to_pr(pack_u64s(w, &values))?;
|
io_to_pr(pack_u64s(w, &values))?;
|
||||||
if tail.len() > 0 {
|
if !tail.is_empty() {
|
||||||
io_to_pr(pack_literal(w, tail))?;
|
io_to_pr(pack_literal(w, tail))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,8 +18,8 @@ use std::{
|
|||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use std::sync::mpsc::{sync_channel, Receiver};
|
use std::sync::mpsc::{sync_channel, Receiver};
|
||||||
|
|
||||||
use crate::pack::node_encode::*;
|
|
||||||
use crate::file_utils;
|
use crate::file_utils;
|
||||||
|
use crate::pack::node_encode::*;
|
||||||
|
|
||||||
const BLOCK_SIZE: u64 = 4096;
|
const BLOCK_SIZE: u64 = 4096;
|
||||||
const MAGIC: u64 = 0xa537a0aa6309ef77;
|
const MAGIC: u64 = 0xa537a0aa6309ef77;
|
||||||
@ -170,19 +170,19 @@ where
|
|||||||
R: byteorder::ReadBytesExt,
|
R: byteorder::ReadBytesExt,
|
||||||
{
|
{
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
let magic = r.read_u64::<LittleEndian>()?;
|
let magic = r.read_u64::<LittleEndian>()?;
|
||||||
if magic != MAGIC {
|
if magic != MAGIC {
|
||||||
eprintln!("Not a pack file.");
|
eprintln!("Not a pack file.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let version = r.read_u64::<LittleEndian>()?;
|
let version = r.read_u64::<LittleEndian>()?;
|
||||||
if version != PACK_VERSION {
|
if version != PACK_VERSION {
|
||||||
eprintln!("unsupported pack file version ({}).", PACK_VERSION);
|
eprintln!("unsupported pack file version ({}).", PACK_VERSION);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let block_size = r.read_u64::<LittleEndian>()?;
|
let block_size = r.read_u64::<LittleEndian>()?;
|
||||||
if block_size != BLOCK_SIZE {
|
if block_size != BLOCK_SIZE {
|
||||||
eprintln!("block size is not {}", BLOCK_SIZE);
|
eprintln!("block size is not {}", BLOCK_SIZE);
|
||||||
@ -216,7 +216,7 @@ fn checksum(buf: &[u8]) -> u32 {
|
|||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum BT {
|
enum BT {
|
||||||
SUPERBLOCK,
|
SUPERBLOCK,
|
||||||
BTREE,
|
NODE,
|
||||||
INDEX,
|
INDEX,
|
||||||
BITMAP,
|
BITMAP,
|
||||||
UNKNOWN,
|
UNKNOWN,
|
||||||
@ -234,21 +234,17 @@ fn metadata_block_type(buf: &[u8]) -> BT {
|
|||||||
let btype = csum ^ sum_on_disk;
|
let btype = csum ^ sum_on_disk;
|
||||||
|
|
||||||
match btype {
|
match btype {
|
||||||
SUPERBLOCK_CSUM_XOR => return BT::SUPERBLOCK,
|
SUPERBLOCK_CSUM_XOR => BT::SUPERBLOCK,
|
||||||
BTREE_CSUM_XOR => return BT::BTREE,
|
BTREE_CSUM_XOR => BT::NODE,
|
||||||
BITMAP_CSUM_XOR => return BT::BITMAP,
|
BITMAP_CSUM_XOR => BT::BITMAP,
|
||||||
INDEX_CSUM_XOR => return BT::INDEX,
|
INDEX_CSUM_XOR => BT::INDEX,
|
||||||
_ => {
|
_ => BT::UNKNOWN,
|
||||||
return BT::UNKNOWN;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check<T>(r: &PResult<T>) {
|
fn check<T>(r: &PResult<T>) {
|
||||||
match r {
|
match r {
|
||||||
Ok(_) => {
|
Ok(_) => {}
|
||||||
return;
|
|
||||||
}
|
|
||||||
Err(PackError::ParseError) => panic!("parse error"),
|
Err(PackError::ParseError) => panic!("parse error"),
|
||||||
Err(PackError::IOError) => panic!("io 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]) {
|
fn pack_block<W: Write>(w: &mut W, kind: BT, buf: &[u8]) {
|
||||||
match kind {
|
match kind {
|
||||||
BT::SUPERBLOCK => check(&pack_superblock(w, buf)),
|
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::INDEX => check(&pack_index(w, buf)),
|
||||||
BT::BITMAP => check(&pack_bitmap(w, buf)),
|
BT::BITMAP => check(&pack_bitmap(w, buf)),
|
||||||
BT::UNKNOWN => {
|
BT::UNKNOWN => {panic!("asked to pack an unknown block type")}
|
||||||
assert!(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -48,16 +48,16 @@ where
|
|||||||
W: Write,
|
W: Write,
|
||||||
{
|
{
|
||||||
if count == 1u64 {
|
if count == 1u64 {
|
||||||
return Ok(());
|
Ok(())
|
||||||
} else if count < 16 {
|
} else if count < 16 {
|
||||||
return pack_tag(w, Tag::Count, count as u8);
|
pack_tag(w, Tag::Count, count as u8)
|
||||||
} else {
|
} else {
|
||||||
assert!(count < 4096);
|
assert!(count < 4096);
|
||||||
let nibble = count >> 8;
|
let nibble = count >> 8;
|
||||||
assert!(nibble < 16);
|
assert!(nibble < 16);
|
||||||
let byte = count & 0xff;
|
let byte = count & 0xff;
|
||||||
pack_tag(w, Tag::Count8, nibble as u8)?;
|
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 } => {
|
Delta::Base { n } => {
|
||||||
if *n <= std::u8::MAX as u64 {
|
if *n <= std::u8::MAX as u64 {
|
||||||
pack_tag(w, Set, 1)?;
|
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 {
|
} else if *n <= std::u16::MAX as u64 {
|
||||||
pack_tag(w, Set, 2)?;
|
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 {
|
} else if *n <= u32::MAX as u64 {
|
||||||
pack_tag(w, Set, 4)?;
|
pack_tag(w, Set, 4)?;
|
||||||
return w.write_u32::<LittleEndian>(*n as u32);
|
w.write_u32::<LittleEndian>(*n as u32)
|
||||||
} else {
|
} else {
|
||||||
pack_tag(w, Set, 8)?;
|
pack_tag(w, Set, 8)?;
|
||||||
return w.write_u64::<LittleEndian>(*n);
|
w.write_u64::<LittleEndian>(*n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Delta::Pos { delta, count } => {
|
Delta::Pos { delta, count } => {
|
||||||
pack_count(w, *count)?;
|
pack_count(w, *count)?;
|
||||||
if *delta < 16 {
|
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 {
|
} else if *delta <= u8::MAX as u64 {
|
||||||
pack_tag(w, PosW, 1)?;
|
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 {
|
} else if *delta <= u16::MAX as u64 {
|
||||||
pack_tag(w, PosW, 2)?;
|
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 {
|
} else if *delta <= u32::MAX as u64 {
|
||||||
pack_tag(w, PosW, 4)?;
|
pack_tag(w, PosW, 4)?;
|
||||||
return w.write_u32::<LittleEndian>(*delta as u32);
|
w.write_u32::<LittleEndian>(*delta as u32)
|
||||||
} else {
|
} else {
|
||||||
pack_tag(w, PosW, 8)?;
|
pack_tag(w, PosW, 8)?;
|
||||||
return w.write_u64::<LittleEndian>(*delta as u64);
|
w.write_u64::<LittleEndian>(*delta as u64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Delta::Neg { delta, count } => {
|
Delta::Neg { delta, count } => {
|
||||||
pack_count(w, *count)?;
|
pack_count(w, *count)?;
|
||||||
|
|
||||||
if *delta < 16 {
|
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 {
|
} else if *delta <= u8::MAX as u64 {
|
||||||
pack_tag(w, NegW, 1)?;
|
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 {
|
} else if *delta <= u16::MAX as u64 {
|
||||||
pack_tag(w, NegW, 2)?;
|
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 {
|
} else if *delta <= u32::MAX as u64 {
|
||||||
pack_tag(w, NegW, 4)?;
|
pack_tag(w, NegW, 4)?;
|
||||||
return w.write_u32::<LittleEndian>(*delta as u32);
|
w.write_u32::<LittleEndian>(*delta as u32)
|
||||||
} else {
|
} else {
|
||||||
pack_tag(w, NegW, 8)?;
|
pack_tag(w, NegW, 8)?;
|
||||||
return w.write_u64::<LittleEndian>(*delta as u64);
|
w.write_u64::<LittleEndian>(*delta as u64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Delta::Const { count } => {
|
Delta::Const { count } => {
|
||||||
if *count < 16 {
|
if *count < 16 {
|
||||||
return pack_tag(w, Tag::Const, *count as u8);
|
pack_tag(w, Tag::Const, *count as u8)
|
||||||
} else {
|
} else {
|
||||||
assert!(*count < 4096);
|
assert!(*count < 4096);
|
||||||
let nibble = *count >> 8;
|
let nibble = *count >> 8;
|
||||||
assert!(nibble < 16);
|
assert!(nibble < 16);
|
||||||
pack_tag(w, Tag::Const8, nibble as u8)?;
|
pack_tag(w, Tag::Const8, nibble as u8)?;
|
||||||
return w.write_u8((*count & 0xff) as u8);
|
w.write_u8((*count & 0xff) as u8)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user