1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 04:27:45 +00:00

Merge pull request #4120 from SteveLauC/ls/device-number-use-major-minor-from-libc

ls: use libc::{major, minor} to calculate device number
This commit is contained in:
Sylvestre Ledru 2022-11-09 11:24:05 +01:00 committed by GitHub
commit 7d8ec3b7f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -39,6 +39,13 @@ use std::{
}; };
use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use unicode_width::UnicodeWidthStr; 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)] #[cfg(unix)]
use uucore::libc::{S_IXGRP, S_IXOTH, S_IXUSR}; use uucore::libc::{S_IXGRP, S_IXOTH, S_IXUSR};
use uucore::parse_glob; use uucore::parse_glob;
@ -63,7 +70,6 @@ Ignore files and directories starting with a '.' by default"#;
const USAGE: &str = "{} [OPTION]... [FILE]..."; const USAGE: &str = "{} [OPTION]... [FILE]...";
pub mod options { pub mod options {
pub mod format { pub mod format {
pub static ONE_LINE: &str = "1"; pub static ONE_LINE: &str = "1";
pub static LONG: &str = "long"; pub static LONG: &str = "long";
@ -354,6 +360,7 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<TimeStyle, LsError> {
Ok(TimeStyle::Locale) Ok(TimeStyle::Locale)
} }
} }
enum Dereference { enum Dereference {
None, None,
DirArgs, DirArgs,
@ -2383,7 +2390,7 @@ fn display_item_long(
padding padding
.size .size
.saturating_sub(padding.minor.saturating_add(2usize)) .saturating_sub(padding.minor.saturating_add(2usize))
) ),
), ),
pad_left( pad_left(
&minor, &minor,
@ -2643,23 +2650,19 @@ enum SizeOrDeviceId {
} }
fn display_len_or_rdev(metadata: &Metadata, config: &Config) -> SizeOrDeviceId { fn display_len_or_rdev(metadata: &Metadata, config: &Config) -> SizeOrDeviceId {
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "android",
target_os = "ios"
))]
{ {
let ft = metadata.file_type(); let ft = metadata.file_type();
if ft.is_char_device() || ft.is_block_device() { if ft.is_char_device() || ft.is_block_device() {
let dev: u64 = metadata.rdev(); // A type cast is needed here as the `dev_t` type varies across OSes.
let major = (dev >> 24) as u8; let dev = metadata.rdev() as dev_t;
let minor = (dev & 0xff) as u8; let major = unsafe { major(dev) };
return SizeOrDeviceId::Device(major.to_string(), minor.to_string()); let minor = unsafe { minor(dev) };
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
{
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;
return SizeOrDeviceId::Device(major.to_string(), minor.to_string()); return SizeOrDeviceId::Device(major.to_string(), minor.to_string());
} }
} }