Skip to content
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

dd: allow B as a suffix for count, seek, and skip #4137

Merged
merged 2 commits into from
Nov 19, 2022

Conversation

jfinkels
Copy link
Collaborator

Allow uppercase "B" on its own as a unit specifier for the count, seek, and skip arguments to dd.

For example,

$ printf "abcdef" | dd count=3B status=none
abc

This should fix GNU test suite file tests/dd/bytes.sh.

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/dd/bytes is no longer failing!
Congrats! The gnu test tests/misc/timeout is no longer failing!

@jfinkels
Copy link
Collaborator Author

Looks like this doesn't quite work as-is:


run: /home/runner/work/coreutils/coreutils/target/debug/coreutils truncate -s 0B file
thread 'test_truncate::test_invalid_numbers' panicked at 'Command was expected to fail.

@tertsdiepraam
Copy link
Member

tertsdiepraam commented Nov 14, 2022

Maybe we could report it back to GNU? Seems like an oversight on their part to not support B on truncate.

On our end, parse_size is due for a refactor that allows for specifying some options on how it should work. So that we don't end up with stuff like this:

fn parse_byte_count(input: &str) -> Result<usize, ParseSizeError> {
    // GNU sort (8.32)   valid: 1b,        k, K, m, M, g, G, t, T, P, E, Z, Y
    // GNU sort (8.32) invalid:  b, B, 1B,                         p, e, z, y
    const ALLOW_LIST: &[char] = &[
        'b', 'k', 'K', 'm', 'M', 'g', 'G', 't', 'T', 'P', 'E', 'Z', 'Y',
    ];
    let mut size_string = input.trim().to_string();

    if size_string.ends_with(|c: char| ALLOW_LIST.contains(&c) || c.is_ascii_digit()) {
        // b 1, K 1024 (default)
        if size_string.ends_with(|c: char| c.is_ascii_digit()) {
            size_string.push('K');
        } else if size_string.ends_with('b') {
            size_string.pop();
        }
        let size = parse_size(&size_string)?;
        usize::try_from(size).map_err(|_| {
            ParseSizeError::SizeTooBig(format!(
                "Buffer size {} does not fit in address space",
                size
            ))
        })
    } else if size_string.starts_with(|c: char| c.is_ascii_digit()) {
        Err(ParseSizeError::InvalidSuffix("invalid suffix".to_string()))
    } else {
        Err(ParseSizeError::ParseFailure("parse failure".to_string()))
    }
}

We'd need some configuration on what the allowed suffixes are, what the default factor is etc.

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/dd/bytes is no longer failing!
Congrats! The gnu test tests/misc/timeout is no longer failing!

Add a `uucore::parse_size::Parser` struct which will allow future
commits to add fields that change the behavior of `parse_size()`.
Allow uppercase "B" on its own as a unit specifier for the `count`,
`seek`, and `skip` arguments to `dd`.

For example,

    $ printf "abcdef" | dd count=3B status=none
    abc
@jfinkels
Copy link
Collaborator Author

Okay, I've updated this pull request to add a uucore::parse_size::Parser struct, which has a parse() method, and the parse_size() function now delegates to Parser::default().parse(s). If we merge this pull request, then we can add more options to the Parse struct to cover the needs of parse_byte_count in a future pull request.

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/dd/bytes is no longer failing!

Copy link
Member

@tertsdiepraam tertsdiepraam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better than what I had in mind!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants