ls: device number for BSDs and solarishOS (#4841)

This commit is contained in:
SteveLauC 2023-05-11 18:58:36 +08:00 committed by GitHub
parent d8f1f1c16c
commit 452be5a220
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 2 deletions

View file

@ -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();

View file

@ -3393,3 +3393,44 @@ fn test_tabsize_formatting() {
.succeeds()
.stdout_is("aaaaaaaa bbbb\ncccc dddddddd");
}
#[cfg(any(
target_os = "linux",
target_os = "macos",
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()
|| 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;
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);
}