mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 23:02:38 +00:00
cksum/hashsum: move the file mgmt into a function
This commit is contained in:
parent
cc8bda5def
commit
e6aad95055
1 changed files with 19 additions and 17 deletions
|
@ -358,6 +358,24 @@ fn get_expected_checksum(
|
|||
}
|
||||
}
|
||||
|
||||
fn get_input_file(filename_input: &OsStr, input_is_stdin: bool) -> UResult<Box<dyn Read>> {
|
||||
if input_is_stdin {
|
||||
Ok(Box::new(stdin())) // Use stdin if "-" is specified
|
||||
} else {
|
||||
match File::open(filename_input) {
|
||||
Ok(f) => Ok(Box::new(f)),
|
||||
Err(_) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!(
|
||||
"{}: No such file or directory",
|
||||
filename_input.to_string_lossy()
|
||||
),
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* Do the checksum validation (can be strict or not)
|
||||
*/
|
||||
|
@ -386,23 +404,7 @@ where
|
|||
let mut properly_formatted = false;
|
||||
let input_is_stdin = filename_input == OsStr::new("-");
|
||||
|
||||
let file: Box<dyn Read> = if input_is_stdin {
|
||||
Box::new(stdin()) // Use stdin if "-" is specified
|
||||
} else {
|
||||
match File::open(filename_input) {
|
||||
Ok(f) => Box::new(f),
|
||||
Err(_) => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!(
|
||||
"{}: No such file or directory",
|
||||
filename_input.to_string_lossy()
|
||||
),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
};
|
||||
let file: Box<dyn Read> = get_input_file(filename_input, input_is_stdin)?;
|
||||
|
||||
let reader = BufReader::new(file);
|
||||
let lines: Vec<String> = reader.lines().collect::<Result<_, _>>()?;
|
||||
|
|
Loading…
Reference in a new issue