X-Git-Url: https://code.octet-stream.net/hashgood/blobdiff_plain/1dec1ec82f55d639d9fad0d0933545aa509c4272..afd3bc461e6107350e584d20dc37926c62f76844:/src/calculate.rs diff --git a/src/calculate.rs b/src/calculate.rs index 870115c..59395d3 100644 --- a/src/calculate.rs +++ b/src/calculate.rs @@ -8,7 +8,7 @@ use crypto::sha2::Sha256; use std::error::Error; use std::fs::File; use std::io::prelude::*; -use std::path::PathBuf; +use std::path::Path; use std::sync::Arc; use std::thread; use std::thread::JoinHandle; @@ -16,12 +16,27 @@ use std::thread::JoinHandle; pub type CalculateResult = Result)>, Box>; /// For a given path to the input (may be "-" for STDIN), try to obtain a reader for the data within it. -pub fn get_input_reader(input: &PathBuf) -> Result, Box> { +pub fn get_input_reader(input: &Path) -> Result, String> { if input.to_str() == Some("-") { // Special case: standard input return Ok(Box::new(std::io::stdin())); } - Ok(Box::new(File::open(input)?)) + if !input.exists() { + return Err(format!( + "The path '{}' does not exist.", + input.to_string_lossy() + )); + } + if !input.is_file() { + return Err(format!( + "The path '{}' is not a regular file.", + input.to_string_lossy() + )); + } + match File::open(input) { + Ok(f) => Ok(Box::new(f)), + Err(e) => Err(format!("File open: {}", e)), + } } /// For the given input stream, calculate all requested digest types