Skip to content

Commit c531fb3

Browse files
authored
Merge pull request #7090 from sylvestre/env
env/stbuf: better handling of the errors
2 parents 62e6dad + 20e043c commit c531fb3

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

src/uu/env/src/env.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -539,16 +539,19 @@ impl EnvAppData {
539539
}
540540
return Err(exit.code().unwrap().into());
541541
}
542-
Err(ref err)
543-
if (err.kind() == io::ErrorKind::NotFound)
544-
|| (err.kind() == io::ErrorKind::InvalidInput) =>
545-
{
546-
return Err(self.make_error_no_such_file_or_dir(prog.deref()));
547-
}
548-
Err(e) => {
549-
uucore::show_error!("unknown error: {:?}", e);
550-
return Err(126.into());
551-
}
542+
Err(ref err) => match err.kind() {
543+
io::ErrorKind::NotFound | io::ErrorKind::InvalidInput => {
544+
return Err(self.make_error_no_such_file_or_dir(prog.deref()));
545+
}
546+
io::ErrorKind::PermissionDenied => {
547+
uucore::show_error!("{}: Permission denied", prog.quote());
548+
return Err(126.into());
549+
}
550+
_ => {
551+
uucore::show_error!("unknown error: {:?}", err);
552+
return Err(126.into());
553+
}
554+
},
552555
Ok(_) => (),
553556
}
554557
Ok(())

src/uu/stdbuf/src/stdbuf.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
157157
set_command_env(&mut command, "_STDBUF_E", &options.stderr);
158158
command.args(command_params);
159159

160-
let mut process = command
161-
.spawn()
162-
.map_err_context(|| "failed to execute process".to_string())?;
160+
const EXEC_ERROR: &str = "failed to execute process:";
161+
let mut process = match command.spawn() {
162+
Ok(p) => p,
163+
Err(e) => {
164+
return match e.kind() {
165+
std::io::ErrorKind::PermissionDenied => Err(USimpleError::new(
166+
126,
167+
format!("{EXEC_ERROR} Permission denied"),
168+
)),
169+
std::io::ErrorKind::NotFound => Err(USimpleError::new(
170+
127,
171+
format!("{EXEC_ERROR} No such file or directory"),
172+
)),
173+
_ => Err(USimpleError::new(1, format!("{EXEC_ERROR} {}", e))),
174+
}
175+
}
176+
};
177+
163178
let status = process.wait().map_err_context(String::new)?;
164179
match status.code() {
165180
Some(i) => {

tests/by-util/test_env.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ fn test_env_version() {
8080
.stdout_contains(util_name!());
8181
}
8282

83+
#[test]
84+
fn test_env_permissions() {
85+
new_ucmd!()
86+
.arg(".")
87+
.fails()
88+
.code_is(126)
89+
.stderr_is("env: '.': Permission denied\n");
90+
}
91+
8392
#[test]
8493
fn test_echo() {
8594
#[cfg(target_os = "windows")]

tests/by-util/test_stdbuf.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ fn invalid_input() {
1010
new_ucmd!().arg("-/").fails().code_is(125);
1111
}
1212

13+
#[test]
14+
fn test_permission() {
15+
new_ucmd!()
16+
.arg("-o1")
17+
.arg(".")
18+
.fails()
19+
.code_is(126)
20+
.stderr_contains("Permission denied");
21+
}
22+
23+
#[test]
24+
fn test_no_such() {
25+
new_ucmd!()
26+
.arg("-o1")
27+
.arg("no_such")
28+
.fails()
29+
.code_is(127)
30+
.stderr_contains("No such file or directory");
31+
}
32+
1333
#[cfg(all(not(target_os = "windows"), not(target_os = "openbsd")))]
1434
#[test]
1535
fn test_stdbuf_unbuffered_stdout() {

0 commit comments

Comments
 (0)