1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +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.
use crate::OPT_BLOCKSIZE;
use clap::ArgMatches;
use std::fmt;
use std::{env, fmt};
use uucore::parse_size::{parse_size, ParseSizeError};
@ -159,6 +159,7 @@ pub(crate) enum HumanReadable {
/// size.
///
/// The default variant is `Bytes(1024)`.
#[derive(Debug, PartialEq)]
pub(crate) enum BlockSize {
/// A fixed number of bytes.
///
@ -168,7 +169,11 @@ pub(crate) enum BlockSize {
impl Default for BlockSize {
fn default() -> Self {
Self::Bytes(1024)
if env::var("POSIXLY_CORRECT").is_ok() {
Self::Bytes(512)
} else {
Self::Bytes(1024)
}
}
}
@ -195,6 +200,8 @@ impl fmt::Display for BlockSize {
#[cfg(test)]
mod tests {
use std::env;
use crate::blocks::{to_magnitude_and_suffix, BlockSize};
#[test]
@ -252,4 +259,12 @@ mod tests {
assert_eq!(format!("{}", BlockSize::Bytes(2 * 1024)), "2K");
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\
or all file systems by default.";
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_ALL: &str = "all";
@ -427,6 +434,7 @@ pub fn uu_app<'a>() -> Command<'a> {
.version(crate_version!())
.about(ABOUT)
.override_usage(format_usage(USAGE))
.after_help(LONG_HELP)
.infer_long_args(true)
.arg(
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]
fn test_block_size_1024() {
fn get_header(block_size: u64) -> String {