Skip to content

Commit

Permalink
Display device IDs when listing devices
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ogham committed May 19, 2017
1 parent de60b95 commit ef5fa90
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/fs/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}


Expand Down
7 changes: 7 additions & 0 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
6 changes: 6 additions & 0 deletions src/output/colours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
19 changes: 17 additions & 2 deletions src/output/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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!
Expand Down
6 changes: 3 additions & 3 deletions xtests/specials
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions xtests/specials_F
Original file line number Diff line number Diff line change
@@ -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|

0 comments on commit ef5fa90

Please sign in to comment.