1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #2145 from tertsdiepraam/ls/device_information

`ls`: implement device symbol and id
This commit is contained in:
Sylvestre Ledru 2021-05-09 00:50:35 +02:00 committed by GitHub
commit d43af35147
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1332,7 +1332,7 @@ fn display_dir_entry_size(entry: &PathData, config: &Config) -> (usize, usize) {
if let Some(md) = entry.md() { if let Some(md) = entry.md() {
( (
display_symlink_count(&md).len(), display_symlink_count(&md).len(),
display_size(md.len(), config).len(), display_size_or_rdev(&md, config).len(),
) )
} else { } else {
(0, 0) (0, 0)
@ -1502,7 +1502,7 @@ fn display_item_long(
let _ = writeln!( let _ = writeln!(
out, out,
" {} {} {}", " {} {} {}",
pad_left(display_size(md.len(), config), max_size), pad_left(display_size_or_rdev(md, config), max_size),
display_date(&md, config), display_date(&md, config),
// unwrap is fine because it fails when metadata is not available // unwrap is fine because it fails when metadata is not available
// but we already know that it is because it's checked at the // but we already know that it is because it's checked at the
@ -1657,13 +1657,28 @@ fn format_prefixed(prefixed: NumberPrefix<f64>) -> String {
} }
} }
fn display_size(len: u64, config: &Config) -> String { fn display_size_or_rdev(metadata: &Metadata, config: &Config) -> String {
#[cfg(unix)]
{
let ft = metadata.file_type();
if ft.is_char_device() || ft.is_block_device() {
let dev: u64 = metadata.rdev();
let major = (dev >> 8) as u8;
let minor = dev as u8;
return format!("{}, {}", major, minor);
}
}
display_size(metadata.len(), config)
}
fn display_size(size: u64, config: &Config) -> String {
// NOTE: The human-readable behaviour deviates from the GNU ls. // NOTE: The human-readable behaviour deviates from the GNU ls.
// The GNU ls uses binary prefixes by default. // The GNU ls uses binary prefixes by default.
match config.size_format { match config.size_format {
SizeFormat::Binary => format_prefixed(NumberPrefix::binary(len as f64)), SizeFormat::Binary => format_prefixed(NumberPrefix::binary(size as f64)),
SizeFormat::Decimal => format_prefixed(NumberPrefix::decimal(len as f64)), SizeFormat::Decimal => format_prefixed(NumberPrefix::decimal(size as f64)),
SizeFormat::Bytes => len.to_string(), SizeFormat::Bytes => size.to_string(),
} }
} }