fix(cksum): check metadata of the path (#1951)

* fix: check metadata of the path

* chore: use existing path
This commit is contained in:
Yagiz Degirmenci 2021-03-29 19:44:42 +03:00 committed by GitHub
parent d88de3c6a6
commit 25df51a525
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -140,7 +140,20 @@ fn cksum(fname: &str) -> io::Result<(u32, usize)> {
let mut rd: Box<dyn Read> = match fname { let mut rd: Box<dyn Read> = match fname {
"-" => Box::new(stdin()), "-" => Box::new(stdin()),
_ => { _ => {
file = File::open(&Path::new(fname))?; let path = &Path::new(fname);
if path.is_dir() {
return Err(std::io::Error::new(
io::ErrorKind::InvalidInput,
"Is a directory",
));
};
if !path.metadata().is_ok() {
return Err(std::io::Error::new(
io::ErrorKind::NotFound,
"No such file or directory",
));
};
file = File::open(&path)?;
Box::new(BufReader::new(file)) Box::new(BufReader::new(file))
} }
}; };