From 071012ddb871a09967cab54d9567e490a5ba4681 Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 25 Nov 2020 13:30:41 +0000 Subject: [PATCH] correct return code on threaded --- fcmprs/src/error.rs | 24 +++++++++++++++++++++++- fcmprs/src/main.rs | 35 ++++++++++++++++------------------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/fcmprs/src/error.rs b/fcmprs/src/error.rs index c4041dc..d8aac4d 100644 --- a/fcmprs/src/error.rs +++ b/fcmprs/src/error.rs @@ -1,4 +1,26 @@ +use std::{fmt,error}; +#[derive(Debug)] +/// There was a non-matching file +pub enum UnmatchError +{ + Size, + Data, + Unknown, +} + +impl error::Error for UnmatchError{} +impl fmt::Display for UnmatchError +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + match self { + Self::Size => write!(f, "size differs"), + Self::Data => write!(f, "data differs"), + _ => write!(f, "unknown error"), + } + } +} pub trait ResultPrintExt { @@ -6,7 +28,7 @@ pub trait ResultPrintExt } impl ResultPrintExt for Result - where E: std::fmt::Display +where E: std::fmt::Display { fn discard_msg(self, msg: impl AsRef) -> Option { match self { diff --git a/fcmprs/src/main.rs b/fcmprs/src/main.rs index e95b6da..36fffd6 100644 --- a/fcmprs/src/main.rs +++ b/fcmprs/src/main.rs @@ -5,6 +5,7 @@ use std::{ path::Path, io, fs::{self, OpenOptions,}, + convert::TryInto, }; use smallvec::SmallVec; use cfg_if::cfg_if; @@ -23,22 +24,7 @@ use error::ResultPrintExt as _; mod map; use map::MappedFile as _; -#[derive(Debug)] -/// There was a non-matching file -struct UnmatchError; - -const _:() = { - use std::{fmt,error}; - impl error::Error for UnmatchError{} - impl fmt::Display for UnmatchError - { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result - { - write!(f, "files did not match") - } - } - () -}; +use error::UnmatchError; fn main() { let (map1, rest) = { @@ -53,6 +39,7 @@ fn main() { std::process::exit({ if let Some(map1) = map::map(&map1).discard_msg(format!("Failed to map file {}", map1)) { let slice = map1.as_slice(); + let map1_sz: u64 = slice.len().try_into().expect("File size could not fit into u64. This should never happen."); let mut ok = true; let chk: SmallVec<[_; 32]> = rest.filter_map(|filename| { let path = Path::new(&filename); @@ -73,24 +60,34 @@ fn main() { if #[cfg(feature="threads")] { match chk.into_par_iter() .map(|map| { + if let Ok(stat) = map.as_file().metadata() { + if stat.len() != map1_sz { + return Err(UnmatchError::Size); + } + if !stat.is_file() { + return Err(UnmatchError::Unknown); + } + } if slice == map.as_slice() { Ok(()) } else { - Err(UnmatchError) + Err(UnmatchError::Data) } }) .try_reduce_with(|_, _| Ok(())) { Some(Ok(_)) => 0, - Some(Err(_)) => 1, + Some(Err(UnmatchError::Data)) => 1, + Some(Err(UnmatchError::Size)) => 2, None => usage(), + _ => -1, } } else { match chk.into_iter() .map(|map| { slice == map.as_slice() }) - .try_fold((false, true), |(_, a), b| if a && b {Ok((true, true))} else {Err(UnmatchError)}) + .try_fold((false, true), |(_, a), b| if a && b {Ok((true, true))} else {Err(UnmatchError::Data)}) { Ok((true, _)) => 0, Ok((false, _)) => usage(),