Skip to content

Commit

Permalink
df: use blocksize of 512 if POSIXLY_CORRECT is set
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed May 8, 2022
1 parent e5d9737 commit f668b69
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/uu/df/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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.
///
Expand All @@ -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)
}
}
}

Expand All @@ -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]
Expand Down Expand Up @@ -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");
}
}
8 changes: 8 additions & 0 deletions src/uu/df/src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f668b69

Please sign in to comment.