From 078cbb7473388817338a62a521557abb34722153 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sun, 7 May 2023 10:41:50 +0800 Subject: [PATCH 1/5] ls: device number for BSDs --- src/uu/ls/src/ls.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index db8024c2e79..9219876879f 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -41,7 +41,13 @@ use unicode_width::UnicodeWidthStr; target_os = "linux", target_os = "macos", target_os = "android", - target_os = "ios" + target_os = "ios", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + target_os = "openbsd", + target_os = "illumos", + target_os = "solaris" ))] use uucore::libc::{dev_t, major, minor}; #[cfg(unix)] @@ -2716,7 +2722,13 @@ fn display_len_or_rdev(metadata: &Metadata, config: &Config) -> SizeOrDeviceId { target_os = "linux", target_os = "macos", target_os = "android", - target_os = "ios" + target_os = "ios", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + target_os = "openbsd", + target_os = "illumos", + target_os = "solaris" ))] { let ft = metadata.file_type(); From 16b9e8807d486fdc73b48a851ec0bd4db480a23a Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sun, 7 May 2023 19:59:48 +0800 Subject: [PATCH 2/5] test(ls): device number ls --- tests/by-util/test_ls.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 84efb9daa44..2b68abb632c 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -3393,3 +3393,42 @@ fn test_tabsize_formatting() { .succeeds() .stdout_is("aaaaaaaa bbbb\ncccc dddddddd"); } + +#[cfg(any( + target_os = "linux", + target_os = "macos", + target_os = "android", + target_os = "ios", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + target_os = "openbsd", + target_os = "illumos", + target_os = "solaris" +))] +#[test] +fn test_device_number() { + use std::fs::{metadata, read_dir}; + use std::os::unix::fs::{FileTypeExt, MetadataExt}; + use uucore::libc::{dev_t, major, minor}; + + let dev_dir = read_dir("/dev").unwrap(); + // let's use the first device for test + let blk_dev = dev_dir + .map(|res_entry| res_entry.unwrap()) + .find(|entry| entry.file_type().unwrap().is_block_device()) + .expect("Expect a block device"); + let blk_dev_path = blk_dev.path(); + let blk_dev_meta = metadata(blk_dev_path.as_path()).unwrap(); + let blk_dev_number = blk_dev_meta.rdev() as dev_t; + let (major, minor) = unsafe { (major(blk_dev_number), minor(blk_dev_number)) }; + let major_minor_str = format!("{}, {}", major, minor); + + let scene = TestScenario::new(util_name!()); + scene + .ucmd() + .arg("-l") + .arg(blk_dev_path.to_str().expect("should be UTF-8 encoded")) + .succeeds() + .stdout_contains(major_minor_str); +} From beca8e58216318ac56e509fe4ddeed30a92b7682 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Thu, 11 May 2023 11:31:47 +0800 Subject: [PATCH 3/5] let's try char devices --- tests/by-util/test_ls.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 2b68abb632c..debc95705fd 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -3416,8 +3416,8 @@ fn test_device_number() { // let's use the first device for test let blk_dev = dev_dir .map(|res_entry| res_entry.unwrap()) - .find(|entry| entry.file_type().unwrap().is_block_device()) - .expect("Expect a block device"); + .find(|entry| entry.file_type().unwrap().is_block_device() || entry.file_type().unwrap().is_char_device()) + .expect("Expect a block/char device"); let blk_dev_path = blk_dev.path(); let blk_dev_meta = metadata(blk_dev_path.as_path()).unwrap(); let blk_dev_number = blk_dev_meta.rdev() as dev_t; From 2c7eab879cd7eface23c9fe63a6c5ef24a629c8f Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Thu, 11 May 2023 11:34:25 +0800 Subject: [PATCH 4/5] fmt --- tests/by-util/test_ls.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index debc95705fd..35d537147b6 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -3416,7 +3416,10 @@ fn test_device_number() { // let's use the first device for test let blk_dev = dev_dir .map(|res_entry| res_entry.unwrap()) - .find(|entry| entry.file_type().unwrap().is_block_device() || entry.file_type().unwrap().is_char_device()) + .find(|entry| { + entry.file_type().unwrap().is_block_device() + || entry.file_type().unwrap().is_char_device() + }) .expect("Expect a block/char device"); let blk_dev_path = blk_dev.path(); let blk_dev_meta = metadata(blk_dev_path.as_path()).unwrap(); From 3e35c5a501f49c6ec45a73afd659a07d78ea9be6 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Thu, 11 May 2023 15:14:19 +0800 Subject: [PATCH 5/5] fix CI: ignore test on android --- tests/by-util/test_ls.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 35d537147b6..61f6d8d8dcc 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -3397,7 +3397,6 @@ fn test_tabsize_formatting() { #[cfg(any( target_os = "linux", target_os = "macos", - target_os = "android", target_os = "ios", target_os = "freebsd", target_os = "dragonfly",