-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Correctly detect format when using md5sum with check flag #4645
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
42ee06d
fix: Correctly detect format when using md5sum with check flag
kamilogorek 45e7b77
Include tests to cover all 3 different formats
kamilogorek 971c4c5
Update windows assertions
kamilogorek a896f63
Move assertions to cfg blocks as well
kamilogorek 6655331
Display escaped filename
kamilogorek ac8f8f1
Change display format only for tagged output
kamilogorek 9bddba2
Merge branch 'main' into md5sum-check-format
kamilogorek d1a9836
Refactor captures to external function and add rustdoc
kamilogorek e70e0f8
Make clippy happy
kamilogorek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ use clap::crate_version; | |
use clap::ArgAction; | ||
use clap::{Arg, ArgMatches, Command}; | ||
use hex::encode; | ||
use regex::Captures; | ||
use regex::Regex; | ||
use std::cmp::Ordering; | ||
use std::error::Error; | ||
|
@@ -604,37 +605,78 @@ where | |
// regular expression, otherwise we use the `{n}` modifier, | ||
// where `n` is the number of bytes. | ||
let bytes = options.digest.output_bits() / 4; | ||
let modifier = if bytes > 0 { | ||
let bytes_marker = if bytes > 0 { | ||
format!("{{{bytes}}}") | ||
} else { | ||
"+".to_string() | ||
}; | ||
let gnu_re = Regex::new(&format!( | ||
r"^(?P<digest>[a-fA-F0-9]{modifier}) (?P<binary>[ \*])(?P<fileName>.*)", | ||
)) | ||
.map_err(|_| HashsumError::InvalidRegex)?; | ||
// BSD reversed mode format is similar to the default mode, but doesn’t use a character to distinguish binary and text modes. | ||
let mut bsd_reversed = None; | ||
|
||
/// Creates a Regex for parsing lines based on the given format. | ||
/// The default value of `gnu_re` created with this function has to be recreated | ||
/// after the initial line has been parsed, as this line dictates the format | ||
/// for the rest of them, and mixing of formats is disallowed. | ||
fn gnu_re_template( | ||
bytes_marker: &str, | ||
format_marker: &str, | ||
) -> Result<Regex, HashsumError> { | ||
Regex::new(&format!( | ||
r"^(?P<digest>[a-fA-F0-9]{bytes_marker}) {format_marker}(?P<fileName>.*)" | ||
)) | ||
.map_err(|_| HashsumError::InvalidRegex) | ||
} | ||
let mut gnu_re = gnu_re_template(&bytes_marker, r"(?P<binary>[ \*])?")?; | ||
let bsd_re = Regex::new(&format!( | ||
r"^{algorithm} \((?P<fileName>.*)\) = (?P<digest>[a-fA-F0-9]{digest_size})", | ||
algorithm = options.algoname, | ||
digest_size = modifier, | ||
digest_size = bytes_marker, | ||
)) | ||
.map_err(|_| HashsumError::InvalidRegex)?; | ||
|
||
fn handle_captures( | ||
caps: Captures, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please fix the clippy warnings ? (sorry) |
||
bytes_marker: &str, | ||
bsd_reversed: &mut Option<bool>, | ||
gnu_re: &mut Regex, | ||
) -> Result<(String, String, bool), HashsumError> { | ||
if bsd_reversed.is_none() { | ||
let is_bsd_reversed = caps.name("binary").is_none(); | ||
let format_marker = if is_bsd_reversed { | ||
"" | ||
} else { | ||
r"(?P<binary>[ \*])" | ||
} | ||
.to_string(); | ||
|
||
*bsd_reversed = Some(is_bsd_reversed); | ||
*gnu_re = gnu_re_template(&bytes_marker, &format_marker)?; | ||
} | ||
|
||
Ok(( | ||
caps.name("fileName").unwrap().as_str().to_string(), | ||
caps.name("digest").unwrap().as_str().to_ascii_lowercase(), | ||
if *bsd_reversed == Some(false) { | ||
caps.name("binary").unwrap().as_str() == "*" | ||
} else { | ||
false | ||
}, | ||
)) | ||
} | ||
|
||
let buffer = file; | ||
for (i, maybe_line) in buffer.lines().enumerate() { | ||
let line = match maybe_line { | ||
Ok(l) => l, | ||
Err(e) => return Err(e.map_err_context(|| "failed to read file".to_string())), | ||
}; | ||
let (ck_filename, sum, binary_check) = match gnu_re.captures(&line) { | ||
Some(caps) => ( | ||
caps.name("fileName").unwrap().as_str(), | ||
caps.name("digest").unwrap().as_str().to_ascii_lowercase(), | ||
caps.name("binary").unwrap().as_str() == "*", | ||
), | ||
Some(caps) => { | ||
handle_captures(caps, &bytes_marker, &mut bsd_reversed, &mut gnu_re)? | ||
} | ||
None => match bsd_re.captures(&line) { | ||
Some(caps) => ( | ||
caps.name("fileName").unwrap().as_str(), | ||
caps.name("fileName").unwrap().as_str().to_string(), | ||
caps.name("digest").unwrap().as_str().to_ascii_lowercase(), | ||
true, | ||
), | ||
|
@@ -655,7 +697,7 @@ where | |
} | ||
}, | ||
}; | ||
let f = match File::open(ck_filename) { | ||
let f = match File::open(ck_filename.clone()) { | ||
Err(_) => { | ||
failed_open_file += 1; | ||
println!( | ||
|
@@ -706,7 +748,7 @@ where | |
) | ||
.map_err_context(|| "failed to read input".to_string())?; | ||
if options.tag { | ||
println!("{} ({}) = {}", options.algoname, filename.display(), sum); | ||
println!("{} ({:?}) = {}", options.algoname, filename.display(), sum); | ||
} else if options.nonames { | ||
println!("{sum}"); | ||
} else if options.zero { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please add a rustdoc comment here? thanks