From ef5fa90660412c1d95a3237be1f4e278a38105e1 Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Fri, 19 May 2017 09:20:47 +0100 Subject: [PATCH] Display device IDs when listing devices Override the size column for block and charater devices, so it shows the major and minor device IDs instead (which are in the Metadata struct somewhere). This is what ls does when faced with a device. --- src/fs/fields.rs | 10 ++++++++++ src/fs/file.rs | 7 +++++++ src/output/colours.rs | 6 ++++++ src/output/details.rs | 19 +++++++++++++++++-- xtests/specials | 6 +++--- xtests/specials_F | 6 +++--- 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/fs/fields.rs b/src/fs/fields.rs index b0635e7..a16e59b 100644 --- a/src/fs/fields.rs +++ b/src/fs/fields.rs @@ -129,6 +129,16 @@ pub enum Size { /// /// See this answer for more: http://unix.stackexchange.com/a/68266 None, + + /// This file is a block or character device, so instead of a size, print + /// out the file’s major and minor device IDs. + /// + /// This is what ls does as well. Without it, the devices will just have + /// file sizes of zero. + DeviceIDs { + major: u8, + minor: u8, + } } diff --git a/src/fs/file.rs b/src/fs/file.rs index 5e3f2f8..eba2475 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -268,6 +268,13 @@ impl<'dir> File<'dir> { if self.is_directory() { f::Size::None } + else if self.is_char_device() || self.is_block_device() { + let dev = self.metadata.rdev(); + f::Size::DeviceIDs { + major: (dev / 256) as u8, + minor: (dev % 256) as u8, + } + } else { f::Size::Some(self.metadata.len()) } diff --git a/src/output/colours.rs b/src/output/colours.rs index 8002308..2797386 100644 --- a/src/output/colours.rs +++ b/src/output/colours.rs @@ -70,6 +70,9 @@ pub struct Size { pub numbers: Style, pub unit: Style, + pub major: Style, + pub minor: Style, + pub scale_byte: Style, pub scale_kilo: Style, pub scale_mega: Style, @@ -148,6 +151,9 @@ impl Colours { numbers: Green.bold(), unit: Green.normal(), + major: Green.bold(), + minor: Green.normal(), + scale_byte: Fixed(118).normal(), scale_kilo: Fixed(190).normal(), scale_mega: Fixed(226).normal(), diff --git a/src/output/details.rs b/src/output/details.rs index fbd7059..aa59fbc 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -579,8 +579,9 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> { use number_prefix::{Prefixed, Standalone, PrefixNames}; let size = match size { - f::Size::Some(s) => s, - f::Size::None => return TextCell::blank(self.opts.colours.punctuation), + f::Size::Some(s) => s, + f::Size::None => return TextCell::blank(self.opts.colours.punctuation), + f::Size::DeviceIDs { major, minor } => return self.render_device_ids(major, minor), }; let result = match size_format { @@ -614,6 +615,20 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> { } } + fn render_device_ids(&self, major: u8, minor: u8) -> TextCell { + let major = major.to_string(); + let minor = minor.to_string(); + + TextCell { + width: DisplayWidth::from(major.len() + 1 + minor.len()), + contents: vec![ + self.opts.colours.size.major.paint(major), + self.opts.colours.punctuation.paint(","), + self.opts.colours.size.minor.paint(minor), + ].into(), + } + } + #[allow(trivial_numeric_casts)] fn render_time(&self, timestamp: f::Time) -> TextCell { // TODO(ogham): This method needs some serious de-duping! diff --git a/xtests/specials b/xtests/specials index 635f0e0..523dc68 100644 --- a/xtests/specials +++ b/xtests/specials @@ -1,3 +1,3 @@ -brw-r--r-- 0 root  1 Jan 12:34 block-device -crw-r--r-- 0 root  1 Jan 12:34 char-device -|rw-r--r-- 0 root  1 Jan 12:34 named-pipe +brw-r--r-- 3,60 root  1 Jan 12:34 block-device +crw-r--r-- 14,40 root  1 Jan 12:34 char-device +|rw-r--r-- 0 root  1 Jan 12:34 named-pipe diff --git a/xtests/specials_F b/xtests/specials_F index 8c5d3dd..4b6fd1c 100644 --- a/xtests/specials_F +++ b/xtests/specials_F @@ -1,3 +1,3 @@ -brw-r--r-- 0 root  1 Jan 12:34 block-device -crw-r--r-- 0 root  1 Jan 12:34 char-device -|rw-r--r-- 0 root  1 Jan 12:34 named-pipe| +brw-r--r-- 3,60 root  1 Jan 12:34 block-device +crw-r--r-- 14,40 root  1 Jan 12:34 char-device +|rw-r--r-- 0 root  1 Jan 12:34 named-pipe|