Skip to content

ls: set correct block size when -k is provided #5700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,31 +828,33 @@ impl Config {

let raw_block_size = if let Some(opt_block_size) = opt_block_size {
OsString::from(opt_block_size)
} else if !opt_kb {
if let Some(env_var_ls_block_size) = env_var_ls_block_size {
env_var_ls_block_size
} else if let Some(env_var_block_size) = env_var_block_size {
env_var_block_size
} else if let Some(env_var_blocksize) = env_var_blocksize {
is_env_var_blocksize = true;
env_var_blocksize
} else {
OsString::from("")
}
} else if let Some(env_var_ls_block_size) = env_var_ls_block_size {
env_var_ls_block_size
} else if let Some(env_var_block_size) = env_var_block_size {
env_var_block_size
} else if let Some(env_var_blocksize) = env_var_blocksize {
is_env_var_blocksize = true;
env_var_blocksize
} else {
OsString::from("")
};

let (file_size_block_size, block_size) = if !opt_si && !opt_hr && !raw_block_size.is_empty()
{
match parse_size_u64(&raw_block_size.to_string_lossy()) {
Ok(size) => {
if is_env_var_blocksize {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, size)
} else {
(size, size)
Ok(size) => match (is_env_var_blocksize, opt_kb) {
(true, true) => (DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE),
(true, false) => (DEFAULT_FILE_SIZE_BLOCK_SIZE, size),
(false, true) => {
// --block-size overrides -k
if opt_block_size.is_some() {
(size, size)
} else {
(size, DEFAULT_BLOCK_SIZE)
}
}
}
(false, false) => (size, size),
},
Err(_) => {
// only fail if invalid block size was specified with --block-size,
// ignore invalid block size from env vars
Expand All @@ -869,7 +871,11 @@ impl Config {
}
}
} else if env_var_posixly_correct.is_some() {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, POSIXLY_CORRECT_BLOCK_SIZE)
if opt_kb {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
} else {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, POSIXLY_CORRECT_BLOCK_SIZE)
}
} else if opt_si {
(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
} else {
Expand Down
50 changes: 50 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3890,6 +3890,56 @@ fn test_posixly_correct_and_block_size_env_vars() {
.stdout_contains(" 1024 ");
}

#[cfg(all(unix, feature = "dd"))]
#[test]
fn test_posixly_correct_and_block_size_env_vars_with_k() {
let scene = TestScenario::new(util_name!());

scene
.ccmd("dd")
.arg("if=/dev/zero")
.arg("of=file")
.arg("bs=1024")
.arg("count=1")
.succeeds();

scene
.ucmd()
.arg("-l")
.arg("-k")
.env("POSIXLY_CORRECT", "some_value")
.succeeds()
.stdout_contains_line("total 4")
.stdout_contains(" 1024 ");

scene
.ucmd()
.arg("-l")
.arg("-k")
.env("LS_BLOCK_SIZE", "512")
.succeeds()
.stdout_contains_line("total 4")
.stdout_contains(" 2 ");

scene
.ucmd()
.arg("-l")
.arg("-k")
.env("BLOCK_SIZE", "512")
.succeeds()
.stdout_contains_line("total 4")
.stdout_contains(" 2 ");

scene
.ucmd()
.arg("-l")
.arg("-k")
.env("BLOCKSIZE", "512")
.succeeds()
.stdout_contains_line("total 4")
.stdout_contains(" 1024 ");
}

#[test]
fn test_ls_invalid_block_size() {
new_ucmd!()
Expand Down