From aea96c09d0581f7b289d1f1da06cd0191e87ace3 Mon Sep 17 00:00:00 2001 From: Omer Ben-Amram Date: Mon, 6 Jan 2020 21:11:48 +0200 Subject: migrated to anyhow, and fixed more lints --- src/attribute/raw.rs | 2 +- src/attribute/x30.rs | 2 +- src/attribute/x80.rs | 2 +- src/bin/mft_dump.rs | 37 ++++++++----------------------------- src/entry.rs | 2 +- src/err.rs | 10 +++++----- src/mft.rs | 2 ++ tests/fixtures.rs | 4 ++-- tests/test_cli_interactive.rs | 10 +++++----- 9 files changed, 26 insertions(+), 45 deletions(-) diff --git a/src/attribute/raw.rs b/src/attribute/raw.rs index c00fb1d..d416340 100644 --- a/src/attribute/raw.rs +++ b/src/attribute/raw.rs @@ -1,5 +1,5 @@ use crate::attribute::MftAttributeType; -use crate::err::{self, Result}; +use crate::err::{Result}; use crate::{utils, ReadSeek}; use serde::{ser, Serialize}; diff --git a/src/attribute/x30.rs b/src/attribute/x30.rs index 1228039..22aa8ec 100644 --- a/src/attribute/x30.rs +++ b/src/attribute/x30.rs @@ -1,5 +1,5 @@ use crate::attribute::FileAttributeFlags; -use crate::err::{self, Error, Result}; +use crate::err::{Error, Result}; use crate::ReadSeek; use log::trace; diff --git a/src/attribute/x80.rs b/src/attribute/x80.rs index b9d8ec0..d295e16 100644 --- a/src/attribute/x80.rs +++ b/src/attribute/x80.rs @@ -1,4 +1,4 @@ -use crate::err::{self, Result}; +use crate::err::{Result}; use crate::{utils, ReadSeek}; use serde::ser; diff --git a/src/bin/mft_dump.rs b/src/bin/mft_dump.rs index 793581a..6655524 100644 --- a/src/bin/mft_dump.rs +++ b/src/bin/mft_dump.rs @@ -13,7 +13,6 @@ use anyhow::{anyhow, Context, Error, Result}; use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; -use std::process::exit; use mft::entry::ZERO_HEADER; use std::fmt::Write as FmtWrite; @@ -136,7 +135,6 @@ struct MftDump { verbosity_level: Option, output_format: OutputFormat, ranges: Option, - backtraces: bool, } impl MftDump { @@ -145,7 +143,9 @@ impl MftDump { OutputFormat::from_str(matches.value_of("output-format").unwrap_or_default()) .expect("Validated with clap default values"); - let backtraces = matches.is_present("backtraces"); + if matches.is_present("backtraces") { + std::env::set_var("RUST_LIB_BACKTRACE", "1"); + } let output: Option> = if let Some(path) = matches.value_of("output-target") { match Self::create_output_file(path, !matches.is_present("no-confirm-overwrite")) { @@ -193,7 +193,6 @@ impl MftDump { verbosity_level, output_format, ranges, - backtraces, }) } @@ -268,16 +267,7 @@ impl MftDump { pub fn run(&mut self) -> Result<()> { self.try_to_initialize_logging(); - let mut parser = match MftParser::from_path(&self.filepath) { - Ok(parser) => parser, - Err(e) => { - return Err(anyhow!( - "Failed to open file {}.\n\tcaused by: {}", - self.filepath.display(), - &e - )) - } - }; + let mut parser = MftParser::from_path(&self.filepath)?; // Since the JSON parser can do away with a &mut Write, but the csv parser needs ownership // of `Write`, we eagerly create the csv writer here, moving the Box out from @@ -456,7 +446,7 @@ pub fn sanitized(component: &str) -> String { buf } -fn main() { +fn main() -> Result<()> { let matches = App::new("MFT Parser") .version(env!("CARGO_PKG_VERSION")) .author("Omer B. ") @@ -520,19 +510,8 @@ fn main() { .help("If set, a backtrace will be printed with some errors if available")) .get_matches(); - let mut app = match MftDump::from_cli_matches(&matches) { - Ok(app) => app, - Err(e) => { - eprintln!("An error occurred while setting up the app: {}", &e); - exit(1); - } - }; + let mut app = MftDump::from_cli_matches(&matches).context("Failed setting up the app")?; + app.run().context("A runtime error has occurred")?; - match app.run() { - Ok(()) => {} - Err(e) => { - eprintln!("A runtime error has occurred: {}", &e); - exit(1); - } - }; + Ok(()) } diff --git a/src/entry.rs b/src/entry.rs index 2f89d04..1ce8f35 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,4 +1,4 @@ -use crate::err::{self, Error, Result}; +use crate::err::{Error, Result}; use crate::impl_serialize_for_bitflags; use log::trace; diff --git a/src/err.rs b/src/err.rs index 838c55a..6073830 100644 --- a/src/err.rs +++ b/src/err.rs @@ -5,12 +5,12 @@ pub type Result = ::std::result::Result; #[derive(Debug, Error)] pub enum Error { - #[error("An I/O error has occurred: {}", source)] + #[error("An I/O error has occurred")] IoError { #[from] source: std::io::Error, }, - #[error("Failed to open file {}: {}", path.display(), source)] + #[error("Failed to open file {}", path.display())] FailedToOpenFile { path: PathBuf, source: std::io::Error, @@ -39,11 +39,11 @@ pub enum Error { end_of_sector_bytes: Vec, fixup_bytes: Vec, }, - #[error("Failed to read MftReference: `{}`", source)] + #[error("Failed to read MftReference")] FailedToReadMftReference { source: winstructs::err::Error }, - #[error("Failed to read WindowsTime: `{}`", source)] + #[error("Failed to read WindowsTime")] FailedToReadWindowsTime { source: winstructs::err::Error }, - #[error("Failed to read GUID: `{}`", source)] + #[error("Failed to read GUID")] FailedToReadGuid { source: winstructs::err::Error }, #[error("An unexpected error has occurred: {}", detail)] Any { detail: String }, diff --git a/src/mft.rs b/src/mft.rs index 7cf3b0b..4cb2c91 100644 --- a/src/mft.rs +++ b/src/mft.rs @@ -180,6 +180,8 @@ mod tests { count += 1; } } + + assert!(count > 0) } #[test] diff --git a/tests/fixtures.rs b/tests/fixtures.rs index 17dfed6..580c4e8 100644 --- a/tests/fixtures.rs +++ b/tests/fixtures.rs @@ -1,9 +1,9 @@ #![allow(dead_code)] use std::path::PathBuf; -use std::sync::{Once, ONCE_INIT}; +use std::sync::{Once}; -static LOGGER_INIT: Once = ONCE_INIT; +static LOGGER_INIT: Once = Once::new(); // Rust runs the tests concurrently, so unless we synchronize logging access // it will crash when attempting to run `cargo test` with some logging facilities. diff --git a/tests/test_cli_interactive.rs b/tests/test_cli_interactive.rs index 6667ac0..987f54a 100644 --- a/tests/test_cli_interactive.rs +++ b/tests/test_cli_interactive.rs @@ -4,13 +4,13 @@ mod fixtures; -use fixtures::*; -use std::fs::File; -use std::io::{Read, Write}; -use tempfile::tempdir; -use assert_cmd::cargo::cargo_bin; + + + + + #[cfg(not(target_os = "windows"))] use rexpect::spawn; -- cgit v1.2.3