From 09d30e953a927e60d441d074f9d43428aec9c345 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Wed, 9 Nov 2022 08:23:58 +0800 Subject: [PATCH] ls: use libc::{major, minor} to calculate device number --- src/uu/ls/src/ls.rs | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 054ff9127c..2f50918b79 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -39,6 +39,13 @@ use std::{ }; use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; use unicode_width::UnicodeWidthStr; +#[cfg(any( + target_os = "linux", + target_os = "macos", + target_os = "android", + target_os = "ios" +))] +use uucore::libc::{dev_t, major, minor}; #[cfg(unix)] use uucore::libc::{S_IXGRP, S_IXOTH, S_IXUSR}; use uucore::parse_glob; @@ -63,13 +70,13 @@ Ignore files and directories starting with a '.' by default"#; const USAGE: &str = "{} [OPTION]... [FILE]..."; pub mod options { - pub mod format { pub static ONE_LINE: &str = "1"; pub static LONG: &str = "long"; pub static COLUMNS: &str = "C"; pub static ACROSS: &str = "x"; - pub static TAB_SIZE: &str = "tabsize"; // silently ignored (see #3624) + pub static TAB_SIZE: &str = "tabsize"; + // silently ignored (see #3624) pub static COMMAS: &str = "m"; pub static LONG_NO_OWNER: &str = "g"; pub static LONG_NO_GROUP: &str = "o"; @@ -290,7 +297,8 @@ enum Sort { #[derive(PartialEq)] enum SizeFormat { Bytes, - Binary, // Powers of 1024, --human-readable, -h + Binary, + // Powers of 1024, --human-readable, -h Decimal, // Powers of 1000, --si } @@ -354,6 +362,7 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result { Ok(TimeStyle::Locale) } } + enum Dereference { None, DirArgs, @@ -2383,7 +2392,7 @@ fn display_item_long( padding .size .saturating_sub(padding.minor.saturating_add(2usize)) - ) + ), ), pad_left( &minor, @@ -2643,23 +2652,19 @@ enum SizeOrDeviceId { } fn display_len_or_rdev(metadata: &Metadata, config: &Config) -> SizeOrDeviceId { - #[cfg(any(target_os = "macos", target_os = "ios"))] - { - let ft = metadata.file_type(); - if ft.is_char_device() || ft.is_block_device() { - let dev: u64 = metadata.rdev(); - let major = (dev >> 24) as u8; - let minor = (dev & 0xff) as u8; - return SizeOrDeviceId::Device(major.to_string(), minor.to_string()); - } - } - #[cfg(any(target_os = "linux", target_os = "android"))] + #[cfg(any( + target_os = "linux", + target_os = "macos", + target_os = "android", + target_os = "ios" + ))] { 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 & 0xff) as u8; + // A type cast is needed here as the `dev_t` type varies across OSes. + let dev = metadata.rdev() as dev_t; + let major = unsafe { major(dev) }; + let minor = unsafe { minor(dev) }; return SizeOrDeviceId::Device(major.to_string(), minor.to_string()); } }