1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

df: use blocksize of 512 if POSIXLY_CORRECT is set

This commit is contained in:
Daniel Hofstetter 2022-05-05 10:53:40 +02:00
parent e5d973718c
commit f668b69a2c
3 changed files with 45 additions and 2 deletions

View file

@ -5,7 +5,7 @@
//! Types for representing and displaying block sizes. //! Types for representing and displaying block sizes.
use crate::OPT_BLOCKSIZE; use crate::OPT_BLOCKSIZE;
use clap::ArgMatches; use clap::ArgMatches;
use std::fmt; use std::{env, fmt};
use uucore::parse_size::{parse_size, ParseSizeError}; use uucore::parse_size::{parse_size, ParseSizeError};
@ -159,6 +159,7 @@ pub(crate) enum HumanReadable {
/// size. /// size.
/// ///
/// The default variant is `Bytes(1024)`. /// The default variant is `Bytes(1024)`.
#[derive(Debug, PartialEq)]
pub(crate) enum BlockSize { pub(crate) enum BlockSize {
/// A fixed number of bytes. /// A fixed number of bytes.
/// ///
@ -168,9 +169,13 @@ pub(crate) enum BlockSize {
impl Default for BlockSize { impl Default for BlockSize {
fn default() -> Self { fn default() -> Self {
if env::var("POSIXLY_CORRECT").is_ok() {
Self::Bytes(512)
} else {
Self::Bytes(1024) Self::Bytes(1024)
} }
} }
}
pub(crate) fn block_size_from_matches(matches: &ArgMatches) -> Result<BlockSize, ParseSizeError> { pub(crate) fn block_size_from_matches(matches: &ArgMatches) -> Result<BlockSize, ParseSizeError> {
if matches.is_present(OPT_BLOCKSIZE) { if matches.is_present(OPT_BLOCKSIZE) {
@ -195,6 +200,8 @@ impl fmt::Display for BlockSize {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::env;
use crate::blocks::{to_magnitude_and_suffix, BlockSize}; use crate::blocks::{to_magnitude_and_suffix, BlockSize};
#[test] #[test]
@ -252,4 +259,12 @@ mod tests {
assert_eq!(format!("{}", BlockSize::Bytes(2 * 1024)), "2K"); assert_eq!(format!("{}", BlockSize::Bytes(2 * 1024)), "2K");
assert_eq!(format!("{}", BlockSize::Bytes(3 * 1024 * 1024)), "3M"); assert_eq!(format!("{}", BlockSize::Bytes(3 * 1024 * 1024)), "3M");
} }
#[test]
fn test_default_block_size() {
assert_eq!(BlockSize::Bytes(1024), BlockSize::default());
env::set_var("POSIXLY_CORRECT", "1");
assert_eq!(BlockSize::Bytes(512), BlockSize::default());
env::remove_var("POSIXLY_CORRECT");
}
} }

View file

@ -32,6 +32,13 @@ use crate::table::Table;
static ABOUT: &str = "Show information about the file system on which each FILE resides,\n\ static ABOUT: &str = "Show information about the file system on which each FILE resides,\n\
or all file systems by default."; or all file systems by default.";
const USAGE: &str = "{} [OPTION]... [FILE]..."; const USAGE: &str = "{} [OPTION]... [FILE]...";
const LONG_HELP: &str = "Display values are in units of the first available SIZE from --block-size,
and the DF_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
SIZE is an integer and optional unit (example: 10M is 10*1024*1024).
Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB,... (powers
of 1000).";
static OPT_HELP: &str = "help"; static OPT_HELP: &str = "help";
static OPT_ALL: &str = "all"; static OPT_ALL: &str = "all";
@ -427,6 +434,7 @@ pub fn uu_app<'a>() -> Command<'a> {
.version(crate_version!()) .version(crate_version!())
.about(ABOUT) .about(ABOUT)
.override_usage(format_usage(USAGE)) .override_usage(format_usage(USAGE))
.after_help(LONG_HELP)
.infer_long_args(true) .infer_long_args(true)
.arg( .arg(
Arg::new(OPT_HELP) Arg::new(OPT_HELP)

View file

@ -375,6 +375,26 @@ fn test_iuse_percentage() {
} }
} }
#[test]
fn test_default_block_size() {
let output = new_ucmd!()
.arg("--output=size")
.succeeds()
.stdout_move_str();
let header = output.lines().next().unwrap().to_string();
assert_eq!(header, "1K-blocks");
let output = new_ucmd!()
.arg("--output=size")
.env("POSIXLY_CORRECT", "1")
.succeeds()
.stdout_move_str();
let header = output.lines().next().unwrap().to_string();
assert_eq!(header, "512B-blocks");
}
#[test] #[test]
fn test_block_size_1024() { fn test_block_size_1024() {
fn get_header(block_size: u64) -> String { fn get_header(block_size: u64) -> String {