Skip to content

ls: implement device symbol and id #2145

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 5 commits into from
May 8, 2021
Merged
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
27 changes: 21 additions & 6 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ fn display_dir_entry_size(entry: &PathData, config: &Config) -> (usize, usize) {
if let Some(md) = entry.md() {
(
display_symlink_count(&md).len(),
display_size(md.len(), config).len(),
display_size_or_rdev(&md, config).len(),
)
} else {
(0, 0)
Expand Down Expand Up @@ -1502,7 +1502,7 @@ fn display_item_long(
let _ = writeln!(
out,
" {} {} {}",
pad_left(display_size(md.len(), config), max_size),
pad_left(display_size_or_rdev(md, config), max_size),
display_date(&md, config),
// unwrap is fine because it fails when metadata is not available
// but we already know that it is because it's checked at the
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it expected that we don't have tests for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't sure how to test that. If you know a way to create (or mock?) devices in the test suite I'd be happy to add some tests!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dunno, sorry

}
}

display_size(metadata.len(), config)
}

fn display_size(size: u64, config: &Config) -> String {
// NOTE: The human-readable behaviour deviates from the GNU ls.
// The GNU ls uses binary prefixes by default.
match config.size_format {
SizeFormat::Binary => format_prefixed(NumberPrefix::binary(len as f64)),
SizeFormat::Decimal => format_prefixed(NumberPrefix::decimal(len as f64)),
SizeFormat::Bytes => len.to_string(),
SizeFormat::Binary => format_prefixed(NumberPrefix::binary(size as f64)),
SizeFormat::Decimal => format_prefixed(NumberPrefix::decimal(size as f64)),
SizeFormat::Bytes => size.to_string(),
}
}

Expand Down