1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

mknod: cleanup mode handling

This commit is contained in:
Daniel Hofstetter 2025-04-23 11:37:47 +02:00
parent 482f882b4c
commit a3c181dce3

View file

@ -42,6 +42,16 @@ enum FileType {
Fifo,
}
impl FileType {
fn as_mode(&self) -> mode_t {
match self {
Self::Block => S_IFBLK,
Self::Character => S_IFCHR,
Self::Fifo => S_IFIFO,
}
}
}
/// Configuration for directory creation.
pub struct Config<'a> {
pub mode: mode_t,
@ -101,13 +111,14 @@ fn mknod(file_name: &str, config: Config) -> i32 {
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let mode = get_mode(matches.get_one::<String>("mode")).map_err(|e| USimpleError::new(1, e))?;
let file_type = matches.get_one::<FileType>("type").unwrap();
let mode = get_mode(matches.get_one::<String>("mode")).map_err(|e| USimpleError::new(1, e))?
| file_type.as_mode();
let file_name = matches
.get_one::<String>("name")
.expect("Missing argument 'NAME'");
let file_type = matches.get_one::<FileType>("type").unwrap();
// Extract the SELinux related flags and options
let set_selinux_context = matches.get_flag(options::SELINUX);
let context = matches.get_one::<String>(options::CONTEXT);
@ -126,7 +137,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
"Fifos do not have major and minor device numbers.",
))
} else {
config.mode = S_IFIFO | mode;
let exit_code = mknod(file_name, config);
set_exit_code(exit_code);
Ok(())
@ -143,19 +153,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
(Some(&major), Some(&minor)) => {
let dev = makedev(major, minor);
config.dev = dev;
let exit_code = match file_type {
FileType::Block => {
config.mode |= S_IFBLK;
mknod(file_name, config)
}
FileType::Character => {
config.mode |= S_IFCHR;
mknod(file_name, config)
}
FileType::Fifo => {
unreachable!("file_type was validated to be only block or character")
}
};
let exit_code = mknod(file_name, config);
set_exit_code(exit_code);
Ok(())
}