Skip to content

Commit 2d679ce

Browse files
Socialprankersylvestre
authored andcommitted
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 fe657b9 commit 2d679ce

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
@@ -132,42 +132,86 @@ const POSIXLY_CORRECT_BLOCK_SIZE: u64 = 512;
132132
const DEFAULT_BLOCK_SIZE: u64 = 1024;
133133
const DEFAULT_FILE_SIZE_BLOCK_SIZE: u64 = 1;
134134

135-
/// Resolve `(file_size_block_size, block_size)` from environment variables.
136-
///
137-
/// `LS_BLOCK_SIZE` and `BLOCK_SIZE` affect both values.
138-
/// `BLOCKSIZE` only affects `block_size` (allocation display with `-s`).
139-
/// `POSIXLY_CORRECT` sets `block_size` to 512 as a last resort.
140-
/// `-k` (`opt_kb`) forces `block_size` to `DEFAULT_BLOCK_SIZE`.
141-
fn resolve_block_sizes_from_env(opt_kb: bool) -> (u64, u64) {
142-
match parse_block_size::block_size_from_env(&["LS_BLOCK_SIZE", "BLOCK_SIZE"]) {
143-
parse_block_size::BlockSizeEnv::Found(size) => {
135+
/// The block sizes `ls` divides by, plus the units echoed next to them.
136+
struct BlockSizes {
137+
/// Divisor for file sizes (the size column of `-l`).
138+
file_size: u64,
139+
/// Divisor for allocated sizes (`-s`, and the `total` line).
140+
alloc: u64,
141+
/// Suffix echoed next to a file size, if any.
142+
file_size_suffix: Option<String>,
143+
/// Suffix echoed next to an allocated size, if any.
144+
alloc_suffix: Option<String>,
145+
}
146+
147+
impl BlockSizes {
148+
/// Both columns share one spec, as with `--block-size=SIZE`.
149+
fn uniform(size: u64, suffix: Option<String>) -> Self {
150+
Self {
151+
file_size: size,
152+
alloc: size,
153+
file_size_suffix: suffix.clone(),
154+
alloc_suffix: suffix,
155+
}
156+
}
157+
158+
/// Plain sizes with no suffix to echo.
159+
fn plain(file_size: u64, alloc: u64) -> Self {
160+
Self {
161+
file_size,
162+
alloc,
163+
file_size_suffix: None,
164+
alloc_suffix: None,
165+
}
166+
}
167+
}
168+
169+
/// Resolve the block sizes from the environment, in GNU's precedence order:
170+
/// `LS_BLOCK_SIZE`/`BLOCK_SIZE` set both, `BLOCKSIZE` only the allocation
171+
/// one, `POSIXLY_CORRECT` 512 as a last resort. `-k` (`opt_kb`) resets the
172+
/// allocation block size and its unit, leaving the file-size ones alone.
173+
fn resolve_block_sizes_from_env(opt_kb: bool) -> BlockSizes {
174+
match parse_block_size::block_size_from_env_with_suffix(&["LS_BLOCK_SIZE", "BLOCK_SIZE"]) {
175+
(parse_block_size::BlockSizeEnv::Found(size), suffix) => {
144176
if opt_kb {
145-
(size, DEFAULT_BLOCK_SIZE)
177+
BlockSizes {
178+
file_size: size,
179+
alloc: DEFAULT_BLOCK_SIZE,
180+
file_size_suffix: suffix,
181+
alloc_suffix: None,
182+
}
146183
} else {
147-
(size, size)
184+
BlockSizes::uniform(size, suffix)
148185
}
149186
}
150-
parse_block_size::BlockSizeEnv::SetButInvalid => (DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE),
151-
parse_block_size::BlockSizeEnv::NotSet => {
187+
(parse_block_size::BlockSizeEnv::SetButInvalid, _) => {
188+
BlockSizes::plain(DEFAULT_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
189+
}
190+
(parse_block_size::BlockSizeEnv::NotSet, _) => {
152191
// Neither LS_BLOCK_SIZE nor BLOCK_SIZE was set; check BLOCKSIZE
153192
// which only affects allocation display, not file size.
154-
match parse_block_size::block_size_from_env(&["BLOCKSIZE"]) {
155-
parse_block_size::BlockSizeEnv::Found(size) => {
193+
match parse_block_size::block_size_from_env_with_suffix(&["BLOCKSIZE"]) {
194+
(parse_block_size::BlockSizeEnv::Found(size), suffix) => {
156195
if opt_kb {
157-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
196+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
158197
} else {
159-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, size)
198+
BlockSizes {
199+
file_size: DEFAULT_FILE_SIZE_BLOCK_SIZE,
200+
alloc: size,
201+
file_size_suffix: None,
202+
alloc_suffix: suffix,
203+
}
160204
}
161205
}
162-
parse_block_size::BlockSizeEnv::SetButInvalid => {
206+
(parse_block_size::BlockSizeEnv::SetButInvalid, _) => {
163207
// BLOCKSIZE was set but invalid: stop lookup, use defaults.
164-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
208+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
165209
}
166-
parse_block_size::BlockSizeEnv::NotSet => {
210+
(parse_block_size::BlockSizeEnv::NotSet, _) => {
167211
if std::env::var_os("POSIXLY_CORRECT").is_some() && !opt_kb {
168-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, POSIXLY_CORRECT_BLOCK_SIZE)
212+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, POSIXLY_CORRECT_BLOCK_SIZE)
169213
} else {
170-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
214+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
171215
}
172216
}
173217
}
@@ -231,6 +275,13 @@ pub struct Config {
231275
pub(crate) file_size_block_size: u64,
232276
#[allow(dead_code)]
233277
pub(crate) block_size: u64, // is never read on Windows
278+
/// Display suffix (e.g. `"K"`) echoed back next to a file size, set when
279+
/// `--block-size`/`LS_BLOCK_SIZE`/`BLOCK_SIZE` held a suffix-only spec
280+
/// rather than a numeric one.
281+
pub(crate) file_size_block_size_suffix: Option<String>,
282+
/// Display suffix echoed back next to an allocated size (`-s`, `total`).
283+
/// `BLOCKSIZE` feeds this one only; `-k` clears it.
284+
pub(crate) block_size_suffix: Option<String>,
234285
pub(crate) width: u16,
235286
// Dir and vdir needs access to this field
236287
pub quoting_style: QuotingStyle,
@@ -737,13 +788,13 @@ impl Config {
737788
SizeFormat::Bytes
738789
};
739790

740-
let (file_size_block_size, block_size) = if let Some(opt_block_size) = opt_block_size {
791+
let block_sizes = if let Some(opt_block_size) = opt_block_size {
741792
// --block-size command-line argument: parse it, error on invalid
742793
// If --block-size=si or --block-size=human-readable, skip numeric parsing
743794
if opt_si {
744-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
795+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
745796
} else if opt_hr {
746-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
797+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
747798
} else {
748799
let size = parse_size_non_zero_u64(opt_block_size).map_err(|error| {
749800
let ls_error = LsError::BlockSizeParseError(opt_block_size.clone());
@@ -761,15 +812,22 @@ impl Config {
761812
)
762813
})?;
763814
// --block-size overrides -k
764-
(size, size)
815+
let suffix = parse_block_size::suffix_from_parsed_block_size(opt_block_size);
816+
BlockSizes::uniform(size, suffix)
765817
}
766818
} else if !opt_si && !opt_hr {
767819
resolve_block_sizes_from_env(opt_kb)
768820
} else if opt_si {
769-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
821+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, 1000)
770822
} else {
771-
(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
823+
BlockSizes::plain(DEFAULT_FILE_SIZE_BLOCK_SIZE, DEFAULT_BLOCK_SIZE)
772824
};
825+
let BlockSizes {
826+
file_size: file_size_block_size,
827+
alloc: block_size,
828+
file_size_suffix: file_size_block_size_suffix,
829+
alloc_suffix: block_size_suffix,
830+
} = block_sizes;
773831

774832
let long = {
775833
let author = options.get_flag(options::AUTHOR);
@@ -969,6 +1027,8 @@ impl Config {
9691027
alloc_size: options.get_flag(options::size::ALLOCATION_SIZE),
9701028
file_size_block_size,
9711029
block_size,
1030+
file_size_block_size_suffix,
1031+
block_size_suffix,
9721032
width,
9731033
quoting_style,
9741034
show_control_chars: options.get_flag(options::SHOW_CONTROL_CHARS),

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use uucore::fsxattr::has_acl;
4545
use uucore::libc::{dev_t, major, minor};
4646
use uucore::{
4747
error::UResult,
48-
format::human::human_readable,
48+
format::human::{SizeFormat, human_readable},
4949
fs::display_permissions,
5050
fsext::metadata_get_time,
5151
line_ending::LineEnding,
@@ -693,11 +693,26 @@ fn display_len_or_rdev(metadata: &Metadata, config: &Config) -> SizeOrDeviceId {
693693
let r = metadata.len() % config.file_size_block_size;
694694
if r == 0 { d } else { d + 1 }
695695
};
696-
SizeOrDeviceId::Size(display_size(len_adjusted, config))
696+
SizeOrDeviceId::Size(display_file_size(len_adjusted, config))
697697
}
698698

699+
/// Render an allocated size (`-s`, and the `total` line).
699700
pub fn display_size(size: u64, config: &Config) -> String {
700-
human_readable(size, config.size_format)
701+
scaled_size(size, config, config.block_size_suffix.as_deref())
702+
}
703+
704+
/// Render a file size (the size column of `-l`).
705+
fn display_file_size(size: u64, config: &Config) -> String {
706+
scaled_size(size, config, config.file_size_block_size_suffix.as_deref())
707+
}
708+
709+
fn scaled_size(size: u64, config: &Config, suffix: Option<&str>) -> String {
710+
let s = human_readable(size, config.size_format);
711+
// -h/--si scale dynamically and print their own units.
712+
match (&config.size_format, suffix) {
713+
(SizeFormat::Bytes, Some(suffix)) => format!("{s}{suffix}"),
714+
_ => s,
715+
}
701716
}
702717

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

0 commit comments

Comments
 (0)