Skip to content

Commit c30593a

Browse files
committed
fix(stat, mknod): replace custom (flawed in stat) logic with libc's.
Now uucore::fs reexports libc's major(), minor() and makedev() directives.
1 parent 7c48912 commit c30593a

5 files changed

Lines changed: 31 additions & 25 deletions

File tree

.vscode/cspell.dictionaries/jargon.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ listxattr
8686
llistxattr
8787
lossily
8888
lstat
89+
makedev
8990
mebi
9091
mebibytes
9192
mergeable

src/uu/mknod/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ path = "src/mknod.rs"
2121
[dependencies]
2222
clap = { workspace = true }
2323
libc = { workspace = true }
24-
uucore = { workspace = true, features = ["mode"] }
24+
uucore = { workspace = true, features = ["mode", "fs"] }
2525
fluent = { workspace = true }
2626

2727
[features]

src/uu/mknod/src/mknod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::ffi::CString;
1313
use uucore::display::Quotable;
1414
use uucore::error::{UResult, USimpleError, UUsageError, set_exit_code};
1515
use uucore::format_usage;
16+
use uucore::fs::makedev;
1617
use uucore::translate;
1718

1819
const MODE_RW_UGO: mode_t = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
@@ -26,12 +27,6 @@ mod options {
2627
pub const CONTEXT: &str = "context";
2728
}
2829

29-
#[inline(always)]
30-
fn makedev(maj: u64, min: u64) -> dev_t {
31-
// pick up from <sys/sysmacros.h>
32-
((min & 0xff) | ((maj & 0xfff) << 8) | ((min & !0xff) << 12) | ((maj & !0xfff) << 32)) as dev_t
33-
}
34-
3530
#[derive(Clone, PartialEq)]
3631
enum FileType {
3732
Block,
@@ -145,7 +140,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
145140
translate!("mknod-error-fifo-no-major-minor"),
146141
));
147142
}
148-
(_, Some(&major), Some(&minor)) => makedev(major, minor),
143+
(_, Some(&major), Some(&minor)) => makedev(major as _, minor as _),
149144
_ => {
150145
return Err(UUsageError::new(
151146
1,

src/uu/stat/src/stat.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use uucore::translate;
99

1010
use clap::builder::ValueParser;
1111
use uucore::display::Quotable;
12-
use uucore::fs::display_permissions;
12+
use uucore::fs::{display_permissions, major, minor};
1313
use uucore::fsext::{
1414
FsMeta, MetadataTimeField, StatFs, metadata_get_time, pretty_filetype, pretty_fstype,
1515
read_fs_list, statfs,
@@ -301,16 +301,6 @@ fn group_num(s: &str) -> Cow<'_, str> {
301301
res.into()
302302
}
303303

304-
/// Keeps major part of an integer
305-
fn major(n: u64) -> u64 {
306-
(n >> 8) & 0xFF
307-
}
308-
309-
// Keeps minor part of an integer
310-
fn minor(n: u64) -> u64 {
311-
n & 0xFF
312-
}
313-
314304
struct Stater {
315305
follow: bool,
316306
show_fs: bool,
@@ -1075,8 +1065,8 @@ impl Stater {
10751065
}
10761066
}
10771067
// device number in decimal
1078-
'd' if flag.major => OutputType::Unsigned(major(meta.dev())),
1079-
'd' if flag.minor => OutputType::Unsigned(minor(meta.dev())),
1068+
'd' if flag.major => OutputType::Unsigned(major(meta.dev() as _) as u64),
1069+
'd' if flag.minor => OutputType::Unsigned(minor(meta.dev() as _) as u64),
10801070
'd' => OutputType::Unsigned(meta.dev()),
10811071
// device number in hex
10821072
'D' => OutputType::UnsignedHex(meta.dev()),
@@ -1115,10 +1105,10 @@ impl Stater {
11151105
's' => OutputType::Integer(meta.len() as i64),
11161106
// major device type in hex, for character/block device special
11171107
// files
1118-
't' => OutputType::UnsignedHex(major(meta.rdev())),
1108+
't' => OutputType::UnsignedHex(major(meta.rdev() as _) as u64),
11191109
// minor device type in hex, for character/block device special
11201110
// files
1121-
'T' => OutputType::UnsignedHex(minor(meta.rdev())),
1111+
'T' => OutputType::UnsignedHex(minor(meta.rdev() as _) as u64),
11221112
// user ID of owner
11231113
'u' => OutputType::Unsigned(meta.uid() as u64),
11241114
// user name of owner
@@ -1162,8 +1152,8 @@ impl Stater {
11621152
OutputType::Float(sec as f64 + nsec as f64 / 1_000_000_000.0)
11631153
}
11641154
'R' => OutputType::UnsignedHex(meta.rdev()),
1165-
'r' if flag.major => OutputType::Unsigned(major(meta.rdev())),
1166-
'r' if flag.minor => OutputType::Unsigned(minor(meta.rdev())),
1155+
'r' if flag.major => OutputType::Unsigned(major(meta.rdev() as _) as u64),
1156+
'r' if flag.minor => OutputType::Unsigned(minor(meta.rdev() as _) as u64),
11671157
'r' => OutputType::Unsigned(meta.rdev()),
11681158
_ => OutputType::Unknown,
11691159
};

src/uucore/src/lib/features/fs.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use libc::{
1313
S_IRUSR, S_ISGID, S_ISUID, S_ISVTX, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR,
1414
mkfifo, mode_t,
1515
};
16+
#[cfg(all(unix, not(target_os = "redox")))]
17+
pub use libc::{major, makedev, minor};
1618
use std::collections::HashSet;
1719
use std::collections::VecDeque;
1820
use std::env;
@@ -839,6 +841,24 @@ pub fn make_fifo(path: &Path) -> std::io::Result<()> {
839841
}
840842
}
841843

844+
// Redox's libc appears not to include the following utilities
845+
846+
#[cfg(target_os = "redox")]
847+
pub fn major(dev: libc::dev_t) -> libc::c_uint {
848+
(((dev >> 8) & 0xFFF) | ((dev >> 32) & 0xFFFFF000)) as _
849+
}
850+
851+
#[cfg(target_os = "redox")]
852+
pub fn minor(dev: libc::dev_t) -> libc::c_uint {
853+
((dev & 0xFF) | ((dev >> 12) & 0xFFFFF00)) as _
854+
}
855+
856+
#[cfg(target_os = "redox")]
857+
pub fn makedev(maj: libc::c_uint, min: libc::c_uint) -> libc::dev_t {
858+
let [maj, min] = [maj as libc::dev_t, min as libc::dev_t];
859+
(min & 0xff) | ((maj & 0xfff) << 8) | ((min & !0xff) << 12) | ((maj & !0xfff) << 32)
860+
}
861+
842862
#[cfg(test)]
843863
mod tests {
844864
// Note this useful idiom: importing names from outer (for mod tests) scope.

0 commit comments

Comments
 (0)