1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

df: show error if provided block size is zero

This commit is contained in:
Daniel Hofstetter 2022-05-10 09:34:33 +02:00 committed by Sylvestre Ledru
parent 0ebd9c9391
commit a6b100a5ca
2 changed files with 21 additions and 2 deletions

View file

@ -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] = [
@ -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())
}

View file

@ -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]