8000 df: show error if provided block size is zero by cakebaker · Pull Request #3514 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

df: show error if provided block size is zero #3514

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

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/uu/df/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use crate::OPT_BLOCKSIZE;
use clap::ArgMatches;
use std::{env, fmt};

use uucore::parse_size::{parse_size, ParseSizeError};
use uucore::{
display::Quotable,
parse_size::{parse_size, ParseSizeError},
};

/// The first ten powers of 1024.
const IEC_BASES: [u128; 10] = [
Expand Down Expand Up @@ -180,7 +183,13 @@ impl Default for BlockSize {
pub(crate) fn block_size_from_matches(matches: &ArgMatches) -> Result<BlockSize, ParseSizeError> {
if matches.is_present(OPT_BLOCKSIZE) {
let s = matches.value_of(OPT_BLOCKSIZE).unwrap();
Ok(BlockSize::Bytes(parse_size(s)?))
let bytes = parse_size(s)?;

if bytes > 0 {
Ok(BlockSize::Bytes(bytes))
} else {
Err(ParseSizeError::ParseFailure(format!("{}", s.quote())))
}
} else {
Ok(Default::default())
}
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ fn test_invalid_block_size() {
.arg("--block-size=x")
.fails()
.stderr_contains("invalid --block-size argument 'x'");

new_ucmd!()
.arg("--block-size=0")
.fails()
.stderr_contains("invalid --block-size argument '0'");

new_ucmd!()
.arg("--block-size=0K")
.fails()
.stderr_contains("invalid --block-size argument '0K'");
}

#[test]
Expand Down
0