Skip to content

Commit b2e2527

Browse files
committed
ls: preserve suffix-only block size units
Same rule as the previous commit, applied to ls: --block-size=K (and LS_BLOCK_SIZE/BLOCK_SIZE/BLOCKSIZE) echoes the unit next to the size, --block-size=1K does not. ls tracks the file-size and allocation suffixes separately, since BLOCKSIZE only affects the allocation column (-s and the total line) and must not leak into the -l size column. -k resets the allocation block size and its unit, leaving a file-size unit from the environment alone, matching GNU.
1 parent 7f98d20 commit b2e2527

3 files changed

Lines changed: 314 additions & 31 deletions

File tree

‎src/uu/ls/src/config.rs‎

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -124,42 +124,86 @@ const POSIXLY_CORRECT_BLOCK_SIZE: u64 = 512;
124124
const DEFAULT_BLOCK_SIZE: u64 = 1024;
125125
const DEFAULT_FILE_SIZE_BLOCK_SIZE: u64 = 1;
126126

127-
/// Resolve `(file_size_block_size, block_size)` from environment variables.
128-
///
129-
/// `LS_BLOCK_SIZE` and `BLOCK_SIZE` affect both values.
130-
/// `BLOCKSIZE` only affects `block_size` (allocation display with `-s`).
131-
/// `POSIXLY_CORRECT` sets `block_size` to 512 as a last resort.
132-
/// `-k` (`opt_kb`) forces `block_size` to `DEFAULT_BLOCK_SIZE`.
133-
fn resolve_block_sizes_from_env(opt_kb: bool) -> (u64, u64) {
134-
match parse_block_size::block_size_from_env(&["LS_BLOCK_SIZE", "BLOCK_SIZE"]) {
135-
parse_block_size::BlockSizeEnv::Found(size) => {
127+
/// The block sizes `ls` divides by, plus the units echoed next to them.
128+
struct BlockSizes {
129+
/// Divisor for file sizes (the size column of `-l`).
130+
file_size: u64,
131+
/// Divisor for allocated sizes (`-s`, and the `total` line).
132+
alloc: u64,
133+
/// Suffix echoed next to a file size, if any.
134+
file_size_suffix: Option<String>,
135+
/// Suffix echoed next to an allocated size, if any.
136+
alloc_suffix: Option<String>,
137+
}
138+
139+
impl BlockSizes {
140+
/// Both columns share one spec, as with `--block-size=SIZE`.
141+
fn uniform(size: u64, suffix: Option<String>) -> Self {
142+
Self {
143+
file_size: size,
144+
alloc: size,
145+
file_size_suffix: suffix.clone(),
146+
alloc_suffix: suffix,
147+
}
148+
}
149+
150+
/// Plain sizes with no suffix to echo.
151+
fn plain(file_size: u64, alloc: u64) -> Self {
152+
Self {
153+
file_size,
154+
alloc,
155+
file_size_suffix: None,
156+
alloc_suffix: None,
157+
}
158+
}
159+
}
160+
161+
/// Resolve the block sizes from the environment, in GNU's precedence order:
162+
/// `LS_BLOCK_SIZE`/`BLOCK_SIZE` set both, `BLOCKSIZE` only the allocation
163+
/// one, `POSIXLY_CORRECT` 512 as a last resort. `-k` (`opt_kb`) resets the
164+
/// allocation block size and its unit, leaving the file-size ones alone.
165+
fn resolve_block_sizes_from_env(opt_kb: bool) -> BlockSizes {
166+
match parse_block_size::block_size_from_env_with_suffix(&["LS_BLOCK_SIZE", "BLOCK_SIZE"]) {
167+
(parse_block_size::BlockSizeEnv::Found(size), suffix) => {
136168
if opt_kb {
137-
(size, DEFAULT_BLOCK_SIZE)
169+
BlockSizes {
170+
file_size: size,
171+
alloc: DEFAULT_BLOCK_SIZE,
172+
file_size_suffix: suffix,
173+
alloc_suffix: None,
174+
}
138175
} else {
139-
(size, size)
176+
BlockSizes::uniform(size, suffix)
140177
}
141178
}
142-
parse_block_size::BlockSizeEnv::SetButInvalid => (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE),
143-
parse_block_size::BlockSizeEnv::NotSet => {
179+
(parse_block_size::BlockSizeEnv::SetButInvalid, _) => {
180+
BlockSizes::plain(DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
181+
}
182+
(parse_block_size::BlockSizeEnv::NotSet, _) => {
144183
// Neither LS_BLOCK_SIZE nor BLOCK_SIZE was set; check BLOCKSIZE
145184
// which only affects allocation display, not file size.
146-
match parse_block_size::block_size_from_env(&["BLOCKSIZE"]) {
147-
parse_block_size::BlockSizeEnv::Found(size) => {
185+
match parse_block_size::block_size_from_env_with_suffix(&["BLOCKSIZE"]) {
186+
(parse_block_size::BlockSizeEnv::Found(size), suffix) => {
148187
if opt_kb {
149-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
188+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
150189
} else {
151-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, size)
190+
BlockSizes {
191+
file_size: DEFAULT_FILE_SIZE_BLOCK_SIZE,
192+
alloc: size,
193+
file_size_suffix: None,
194+
alloc_suffix: suffix,
195+
}
152196
}
153197
}
154-
parse_block_size::BlockSizeEnv::SetButInvalid => {
198+
(parse_block_size::BlockSizeEnv::SetButInvalid, _) => {
155199
// BLOCKSIZE was set but invalid: stop lookup, use defaults.
156-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
200+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
157201
}
158-
parse_block_size::BlockSizeEnv::NotSet => {
202+
(parse_block_size::BlockSizeEnv::NotSet, _) => {
159203
if std::env::var_os("POSIXLY_CORRECT").is_some() && !opt_kb {
160-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, POSIXLY_CORRECT_BLOCK_SIZE)
204+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, POSIXLY_CORRECT_BLOCK_SIZE)
161205
} else {
162-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
206+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
163207
}
164208
}
165209
}
@@ -212,6 +256,13 @@ pub struct Config {
212256
pub(crate) file_size_block_size: u64,
213257
#[allow(dead_code)]
214258
pub(crate) block_size: u64, // is never read on Windows
259+
/// Display suffix (e.g. `"K"`) echoed back next to a file size, set when
260+
/// `--block-size`/`LS_BLOCK_SIZE`/`BLOCK_SIZE` held a suffix-only spec
261+
/// rather than a numeric one.
262+
pub(crate) file_size_block_size_suffix: Option<String>,
263+
/// Display suffix echoed back next to an allocated size (`-s`, `total`).
264+
/// `BLOCKSIZE` feeds this one only; `-k` clears it.
265+
pub(crate) block_size_suffix: Option<String>,
215266
pub(crate) width: u16,
216267
// Dir and vdir needs access to this field
217268
pub quoting_style: QuotingStyle,
@@ -754,13 +805,13 @@ impl Config {
754805
SizeFormat::Bytes
755806
};
756807

757-
let (file_size_block_size, block_size) = if let Some(opt_block_size) = opt_block_size {
808+
let block_sizes = if let Some(opt_block_size) = opt_block_size {
758809
// --block-size command-line argument: parse it, error on invalid
759810
// If --block-size=si or --block-size=human-readable, skip numeric parsing
760811
if opt_si {
761-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
812+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
762813
} else if opt_hr {
763-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
814+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
764815
} else {
765816
let size = parse_size_non_zero_u64(opt_block_size).map_err(|error| {
766817
let ls_error = LsError::BlockSizeParseError(opt_block_size.clone());
@@ -778,15 +829,22 @@ impl Config {
778829
)
779830
})?;
780831
// --block-size overrides -k
781-
(size, size)
832+
let suffix = parse_block_size::suffix_from_parsed_block_size(opt_block_size);
833+
BlockSizes::uniform(size, suffix)
782834
}
783835
} else if !opt_si && !opt_hr {
784836
resolve_block_sizes_from_env(opt_kb)
785837
} else if opt_si {
786-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
838+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
787839
} else {
788-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
840+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
789841
};
842+
let BlockSizes {
843+
file_size: file_size_block_size,
844+
alloc: block_size,
845+
file_size_suffix: file_size_block_size_suffix,
846+
alloc_suffix: block_size_suffix,
847+
} = block_sizes;
790848

791849
let long = {
792850
let author = options.get_flag(options::AUTHOR);
@@ -1004,6 +1062,8 @@ impl Config {
10041062
alloc_size: options.get_flag(options::size::ALLOCATION_SIZE),
10051063
file_size_block_size,
10061064
block_size,
1065+
file_size_block_size_suffix,
1066+
block_size_suffix,
10071067
width,
10081068
quoting_style,
10091069
locale_quoting,

‎src/uu/ls/src/display.rs‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use uucore::fsxattr::has_acl;
3939
use uucore::libc::{dev_t, major, minor};
4040
use uucore::{
4141
error::UResult,
42-
format::human::human_readable,
42+
format::human::{SizeFormat, human_readable},
4343
fs::display_permissions,
4444
fsext::metadata_get_time,
4545
i18n::{UEncoding, get_ctype_encoding},
@@ -722,11 +722,26 @@ fn display_len_or_rdev(metadata: &Metadata, config: &Config) -> SizeOrDeviceId {
722722
let r = metadata.len() % config.file_size_block_size;
723723
if r == 0 { d } else { d + 1 }
724724
};
725-
SizeOrDeviceId::Size(display_size(len_adjusted, config))
725+
SizeOrDeviceId::Size(display_file_size(len_adjusted, config))
726726
}
727727

728+
/// Render an allocated size (`-s`, and the `total` line).
728729
pub fn display_size(size: u64, config: &Config) -> String {
729-
human_readable(size, config.size_format)
730+
scaled_size(size, config, config.block_size_suffix.as_deref())
731+
}
732+
733+
/// Render a file size (the size column of `-l`).
734+
fn display_file_size(size: u64, config: &Config) -> String {
735+
scaled_size(size, config, config.file_size_block_size_suffix.as_deref())
736+
}
737+
738+
fn scaled_size(size: u64, config: &Config, suffix: Option<&str>) -> String {
739+
let s = human_readable(size, config.size_format);
740+
// -h/--si scale dynamically and print their own units.
741+
match (&config.size_format, suffix) {
742+
(SizeFormat::Bytes, Some(suffix)) => format!("{s}{suffix}"),
743+
_ => s,
744+
}
730745
}
731746

732747
/// Takes a [`PathData`] struct and returns a cell with a name ready for displaying.

0 commit comments

Comments
 (0)