shitbox/src/cmd/md5sum/mod.rs

51 lines
1.5 KiB
Rust
Raw Normal View History

2023-02-03 19:12:40 -05:00
use super::Cmd;
use crate::{
args,
hash::{self, HashType},
2023-02-03 19:12:40 -05:00
};
use clap::Command;
use md5::{Digest, Md5};
use std::{io, process};
2023-02-03 19:12:40 -05:00
#[derive(Debug, Default)]
pub struct Md5sum;
impl Cmd for Md5sum {
fn cli(&self) -> clap::Command {
Command::new("md5sum")
.about("compute and check MD5 message digest")
.author("Nathan Fisher")
.version(env!("CARGO_PKG_VERSION"))
.args([args::check(), args::file()])
2023-02-03 19:12:40 -05:00
}
fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
2023-02-03 19:12:40 -05:00
if let Some(files) = matches.get_many::<String>("file") {
let mut erred = 0;
for f in files {
if matches.get_flag("check") {
if f == "-" {
return Err(
io::Error::new(io::ErrorKind::Other, "no file specified").into()
);
}
hash::check_sums(f, HashType::Md5, &mut erred)?;
2023-02-03 19:12:40 -05:00
} else {
let hasher = Md5::new();
let s = hash::compute_hash(f, hasher)?;
println!("{s} {f}");
2023-02-03 19:12:40 -05:00
}
}
if erred > 0 {
println!("md5sum: WARNING: {erred} computed checksum did NOT match");
process::exit(1);
}
}
Ok(())
}
fn path(&self) -> Option<crate::Path> {
Some(crate::Path::UsrBin)
}
}