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 5, 2022
1 parent f6b9d36 commit d46509c
Show file tree
Hide file tree
Showing 2 changed files with 37 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, OPT_HUMAN_READABLE_BINARY, OPT_HUMAN_READABLE_DECIMAL};
use clap::ArgMatches;
use std::fmt;
use std::{env, fmt};

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

Expand Down Expand Up @@ -127,6 +127,7 @@ fn to_magnitude_and_suffix(n: u128) -> Result<String, ()> {
/// so on in the case of [`BlockSize::HumanReadableDecimal`]).
///
/// The default variant is `Bytes(1024)`.
#[derive(Debug, PartialEq)]
pub(crate) enum BlockSize {
/// A fixed number of bytes.
///
Expand All @@ -150,7 +151,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 Down Expand Up @@ -183,6 +188,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 @@ -238,4 +245,12 @@ mod tests {
"3M-blocks"
);
}

#[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");
}
}
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 d46509c

Please sign in to comment.