Skip to content

Commit e10d467

Browse files
committed
mknod: match GNU error format and drop duplicate umask test
1 parent d9aca1e commit e10d467

2 files changed

Lines changed: 13 additions & 48 deletions

File tree

‎src/uu/mknod/src/mknod.rs‎

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use clap::{Arg, ArgAction, Command, value_parser};
99
use nix::libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, mode_t};
1010
use nix::sys::stat::{Mode, SFlag, dev_t, mknod as nix_mknod};
1111
use std::ffi::{OsStr, OsString};
12-
use std::io::{self, Write as _};
12+
use std::io;
1313

1414
use uucore::display::Quotable;
15-
use uucore::error::{ExitCode, UResult, USimpleError, UUsageError, set_exit_code};
15+
use uucore::error::{ExitCode, FromIo, UResult, USimpleError, UUsageError};
1616
use uucore::format_usage;
1717
use uucore::fs::makedev;
1818
use uucore::{show_warning, translate};
@@ -69,7 +69,7 @@ struct Config {
6969
context: Option<String>,
7070
}
7171

72-
fn mknod(file_name: &OsStr, config: Config) -> i32 {
72+
fn mknod(file_name: &OsStr, config: Config) -> UResult<()> {
7373
#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
7474
let selinux_enabled = uucore::selinux::is_selinux_enabled();
7575
#[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))]
@@ -93,10 +93,7 @@ fn mknod(file_name: &OsStr, config: Config) -> i32 {
9393
config.context.as_ref(),
9494
) {
9595
Ok(guard) => Some(guard),
96-
Err(e) => {
97-
let _ = writeln!(io::stderr(), "mknod: {e}");
98-
return 1;
99-
}
96+
Err(e) => return Err(USimpleError::new(1, e.to_string())),
10097
}
10198
} else {
10299
None
@@ -109,23 +106,14 @@ fn mknod(file_name: &OsStr, config: Config) -> i32 {
109106
config.mode,
110107
config.dev,
111108
)
112-
.err()
113109
};
114-
let mknod_err = if config.use_umask {
110+
let res = if config.use_umask {
115111
create_node()
116112
} else {
117113
uucore::mode::with_umask(0, create_node)
118114
};
119-
let errno = if mknod_err.is_some() { -1 } else { 0 };
120-
121-
if let Some(err) = mknod_err {
122-
let _ = writeln!(
123-
io::stderr(),
124-
"{}: {}",
125-
uucore::execution_phrase(),
126-
io::Error::from(err)
127-
);
128-
}
115+
res.map_err(io::Error::from)
116+
.map_err_context(|| file_name.maybe_quote().to_string())?;
129117

130118
// Apply SMACK context if requested
131119
#[cfg(all(feature = "smack", target_os = "linux"))]
@@ -136,11 +124,10 @@ fn mknod(file_name: &OsStr, config: Config) -> i32 {
136124
std::fs::remove_file(p)
137125
})
138126
{
139-
let _ = writeln!(io::stderr(), "mknod: {e}");
140-
return 1;
127+
return Err(USimpleError::new(1, e.to_string()));
141128
}
142129

143-
errno
130+
Ok(())
144131
}
145132

146133
#[uucore::main]
@@ -227,9 +214,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
227214
context,
228215
};
229216

230-
let exit_code = mknod(file_name, config);
231-
set_exit_code(exit_code);
232-
Ok(())
217+
mknod(file_name, config)
233218
}
234219

235220
pub fn uu_app() -> Command {

‎tests/by-util/test_mknod.rs‎

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,15 @@ fn test_mknod_fifo_mnemonic_usage() {
8686
assert!(ts.fixtures.is_fifo("test_file"));
8787
}
8888

89-
#[test]
90-
fn test_mknod_fifo_umask() {
91-
for (mode, expected) in [(None, 0o600), (Some("0644"), 0o644)] {
92-
let ts = TestScenario::new(util_name!());
93-
let mut cmd = ts.ucmd();
94-
if let Some(mode) = mode {
95-
cmd.args(&["-m", mode]);
96-
}
97-
cmd.args(&["test_file", "p"]).umask(0o077).succeeds();
98-
assert!(ts.fixtures.is_fifo("test_file"));
99-
assert_eq!(
100-
ts.fixtures.metadata("test_file").permissions().mode() & 0o777,
101-
expected
102-
);
103-
}
104-
}
105-
10689
#[test]
10790
fn test_mknod_fifo_existing_file() {
10891
let ts = TestScenario::new(util_name!());
10992
ts.fixtures.write("test_file", "keep this content");
11093
ts.ucmd()
11194
.args(&["test_file", "p"])
112-
.fails()
113-
.stderr_is(format!(
114-
"{} mknod: {}\n",
115-
ts.bin_path.display(),
116-
std::io::Error::from_raw_os_error(libc::EEXIST),
117-
));
95+
.fails_with_code(1)
96+
.no_stdout()
97+
.stderr_is("mknod: test_file: File exists\n");
11898
assert_eq!(ts.fixtures.read("test_file"), "keep this content");
11999
}
120100

0 commit comments

Comments
 (0)