-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
cksum: implement -a #4356
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
cksum: implement -a #4356
Conversation
|
I know it is a draft and we will squash them but could you please make the commit message a bit more explicit :) |
I'm sorry. Let me squash them and present a little better commit first. |
cfd060c to
25a3d86
Compare
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
e0fcfe2 to
a87ad2e
Compare
|
GNU testsuite comparison: |
3 similar comments
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
f872e52 to
7fc81ec
Compare
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
c512388 to
8032c96
Compare
Implement option -a --algorithm. Move digest to src/uucore/src/lib/features and rename it to hash. fix lint fix Cargo.toml
|
GNU testsuite comparison: |
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.
Since it's almost completely implemented in uucore now, maybe we could make the abstraction less leaky? What I mean is that hashsum and cksum do not have to depend on all the separate crates anymore if uucore exposes wrappers around the types from all the separate crates. For instance:
// Note that the field is private, so can't be constructed directly from cksum or hashsum
pub struct Sha1(sha1::Sha1);
impl Digest for Sha1 { ... }Then detect_algo also just becomes:
match program {
"sha1" => Sha1::new(),
...
}Where new has the return type Box<dyn Digest>. Do you think that's possible? Ultimately that might even be an interesting crate to publish separately (though not in this PR).
|
GNU testsuite comparison: |
| @@ -0,0 +1,486 @@ | |||
| // spell-checker:ignore memmem algo | |||
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.
please add the license header
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.
done
tertsdiepraam
left a comment
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.
I like how the abstraction turned out! I have a few more suggestions, but at some point we should probably just merge this and continue improving it in other PRs.
|
The object safety thing is annoying. I tried some things to get rid of it. What worked is to never box it, because we don't really have to. Instead we can do this: fn cksum_algo<'a, I>(algorithm: &str, files: I) -> UResult<()>
where
I: Iterator<Item = &'a OsStr>,
{
match algorithm {
"sysv" => cksum::<_, SYSV>("SYSV", files),
"bsd" => cksum::<_, BSD>("BSD", files),
...
}
fn cksum<'a, I, D>(algo_name: &'static str, files: I) -> UResult<()>
where
I: Iterator<Item = &'a OsStr>,
D: Digest
{ ... }
pub trait Digest {
const OUTPUT_BITS: usize;
const OUTPUT_BYTES: usize = (Self::OUTPUT_BITS + 7) / 8;
fn new() -> Self;
fn hash_update(&mut self, input: &[u8]);
fn hash_finalize(&mut self, out: &mut [u8]);
fn result_str(&mut self) -> String {
let mut buf: Vec<u8> = vec![0; Self::OUTPUT_BYTES];
self.hash_finalize(&mut buf);
encode(buf)
}
}But I can also do this after this PR is merged. |
tertsdiepraam
left a comment
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.
Alright, it's time to wrap this up. Like I said, the trait might need a bit of a redesign, but that's not really related to this PR. I have one final comment and then it's good in my opinion.
Cool I can give it a try in another PR. |
src/uu/cksum/src/cksum.rs
Outdated
| "sha512" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512), | ||
| "blake2b" => ("BLAKE2", Box::new(Blake2b::new()) as Box<dyn Digest>, 512), | ||
| "sm3" => ("SM3", Box::new(Sm3::new()) as Box<dyn Digest>, 512), | ||
| _ => panic!("unknown algorithm"), |
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.
One more thing. This is probably not a great error to show to the user. We can restrict the values clap accepts with Arg::possible_values, so that this panic will never be reached.
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.
but I need a default option in match.
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.
Yeah the _ case will need to be there still, but we can change it to unreachable!("unknown algorithm: clap should have prevented this case")
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.
done
|
I'll do that change to |
|
Thank you! |
src/uu/cksum/src/cksum.rs
Outdated
| .long(options::ALGORITHM) | ||
| .short('a') | ||
| .help("select the digest type to use. See DIGEST below") | ||
| .value_name("ALGORITHM"), |
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.
Here you can add .value_parser(["bsd", "sysv", ...]) to actually restrict the arguments. Tha't's what I was referring to in the other comment. Here's an example of du doing the same thing: https://github.com/uutils/coreutils/blob/main/src/uu/du/src/du.rs#L877.
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.
Thanks for advice. I have put a fix
|
GNU testsuite comparison: |
|
Alright, time to merge this! Thanks! |
|
thank you too! |
Closes #3812