Skip to content

Commit 0125bbc

Browse files
authored
Merge pull request #7825 from cakebaker/mknod_refactor_uumain
mknod: refactor `uumain`
2 parents 482f882 + be4e691 commit 0125bbc

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

src/uu/mknod/src/mknod.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ enum FileType {
4242
Fifo,
4343
}
4444

45+
impl FileType {
46+
fn as_mode(&self) -> mode_t {
47+
match self {
48+
Self::Block => S_IFBLK,
49+
Self::Character => S_IFCHR,
50+
Self::Fifo => S_IFIFO,
51+
}
52+
}
53+
}
54+
4555
/// Configuration for directory creation.
4656
pub struct Config<'a> {
4757
pub mode: mode_t,
@@ -101,66 +111,49 @@ fn mknod(file_name: &str, config: Config) -> i32 {
101111
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
102112
let matches = uu_app().try_get_matches_from(args)?;
103113

104-
let mode = get_mode(matches.get_one::<String>("mode")).map_err(|e| USimpleError::new(1, e))?;
114+
let file_type = matches.get_one::<FileType>("type").unwrap();
115+
let mode = get_mode(matches.get_one::<String>("mode")).map_err(|e| USimpleError::new(1, e))?
116+
| file_type.as_mode();
105117

106118
let file_name = matches
107119
.get_one::<String>("name")
108120
.expect("Missing argument 'NAME'");
109121

110-
let file_type = matches.get_one::<FileType>("type").unwrap();
111122
// Extract the SELinux related flags and options
112123
let set_selinux_context = matches.get_flag(options::SELINUX);
113124
let context = matches.get_one::<String>(options::CONTEXT);
114125

115-
let mut config = Config {
116-
mode,
117-
dev: 0,
118-
set_selinux_context: set_selinux_context || context.is_some(),
119-
context,
120-
};
121-
122-
if *file_type == FileType::Fifo {
123-
if matches.contains_id(options::MAJOR) || matches.contains_id(options::MINOR) {
124-
Err(UUsageError::new(
126+
let dev = match (
127+
file_type,
128+
matches.get_one::<u64>(options::MAJOR),
129+
matches.get_one::<u64>(options::MINOR),
130+
) {
131+
(FileType::Fifo, None, None) => 0,
132+
(FileType::Fifo, _, _) => {
133+
return Err(UUsageError::new(
125134
1,
126135
"Fifos do not have major and minor device numbers.",
127-
))
128-
} else {
129-
config.mode = S_IFIFO | mode;
130-
let exit_code = mknod(file_name, config);
131-
set_exit_code(exit_code);
132-
Ok(())
136+
));
133137
}
134-
} else {
135-
match (
136-
matches.get_one::<u64>(options::MAJOR),
137-
matches.get_one::<u64>(options::MINOR),
138-
) {
139-
(_, None) | (None, _) => Err(UUsageError::new(
138+
(_, Some(&major), Some(&minor)) => makedev(major, minor),
139+
_ => {
140+
return Err(UUsageError::new(
140141
1,
141142
"Special files require major and minor device numbers.",
142-
)),
143-
(Some(&major), Some(&minor)) => {
144-
let dev = makedev(major, minor);
145-
config.dev = dev;
146-
let exit_code = match file_type {
147-
FileType::Block => {
148-
config.mode |= S_IFBLK;
149-
mknod(file_name, config)
150-
}
151-
FileType::Character => {
152-
config.mode |= S_IFCHR;
153-
mknod(file_name, config)
154-
}
155-
FileType::Fifo => {
156-
unreachable!("file_type was validated to be only block or character")
157-
}
158-
};
159-
set_exit_code(exit_code);
160-
Ok(())
161-
}
143+
));
162144
}
163-
}
145+
};
146+
147+
let config = Config {
148+
mode,
149+
dev,
150+
set_selinux_context: set_selinux_context || context.is_some(),
151+
context,
152+
};
153+
154+
let exit_code = mknod(file_name, config);
155+
set_exit_code(exit_code);
156+
Ok(())
164157
}
165158

166159
pub fn uu_app() -> Command {

0 commit comments

Comments
 (0)