Skip to content

Commit 09635ec

Browse files
authored
Update error strings (#1343)
2 parents b24de8a + e9847a3 commit 09635ec

File tree

12 files changed

+68
-29
lines changed

12 files changed

+68
-29
lines changed

src/sudo/cli/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::os::unix::ffi::OsStrExt;
44
use std::{borrow::Cow, mem};
55

66
use crate::common::{SudoPath, SudoString};
7+
use crate::log::user_warn;
78

89
pub mod help;
910
pub mod help_edit;
@@ -649,8 +650,8 @@ impl SudoOptions {
649650
options.bell = true;
650651
}
651652
"-E" | "--preserve-env" => {
652-
eprintln_ignore_io_error!(
653-
"warning: preserving the entire environment is not supported, `{flag}` is ignored"
653+
user_warn!(
654+
"preserving the entire environment is not supported, `{flag}` is ignored"
654655
)
655656
}
656657
"-e" | "--edit" if !invoked_as_sudoedit => {
@@ -740,7 +741,7 @@ impl SudoOptions {
740741
&& !is_dir
741742
&& (cmd.ends_with("sudoedit") || cmd.ends_with("sudoedit-rs"))
742743
{
743-
eprintln_ignore_io_error!("sudoedit doesn't need to be run via sudo");
744+
user_warn!("sudoedit doesn't need to be run via sudo");
744745
options.edit = true;
745746
rest.remove(0);
746747
}

src/sudo/diagnostic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ macro_rules! diagnostic {
4343
if let Some(range) = $pos {
4444
$crate::sudo::diagnostic::cited_error(&format!($str), range, $path);
4545
} else {
46-
eprintln_ignore_io_error!("sudo-rs: {}", format!($str));
46+
eprintln_ignore_io_error!("sudo: {}", format!($str));
4747
}
4848
};
4949
($str:expr) => {{
50-
eprintln_ignore_io_error!("sudo-rs: {}", format!($str));
50+
eprintln_ignore_io_error!("sudo: {}", format!($str));
5151
}};
5252
}
5353

src/sudo/edit.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{io, process};
1010

1111
use crate::common::SudoPath;
1212
use crate::exec::ExitReason;
13-
use crate::log::user_info;
13+
use crate::log::{user_error, user_info};
1414
use crate::system::file::{create_temporary_dir, FileLock};
1515
use crate::system::wait::{Wait, WaitError, WaitOptions};
1616
use crate::system::{fork, ForkResult};
@@ -157,8 +157,8 @@ struct TempDirDropGuard(PathBuf);
157157
impl Drop for TempDirDropGuard {
158158
fn drop(&mut self) {
159159
if let Err(e) = std::fs::remove_dir_all(&self.0) {
160-
eprintln_ignore_io_error!(
161-
"Failed to remove temporary directory {}: {e}",
160+
user_error!(
161+
"failed to remove temporary directory {}: {e}",
162162
self.0.display(),
163163
);
164164
};
@@ -169,7 +169,7 @@ fn handle_child(editor: &Path, file: Vec<ChildFileInfo<'_>>) -> ! {
169169
match handle_child_inner(editor, file) {
170170
Ok(()) => process::exit(0),
171171
Err(err) => {
172-
eprintln_ignore_io_error!("{err}");
172+
user_error!("{err}");
173173
process::exit(1);
174174
}
175175
}
@@ -184,15 +184,15 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
184184
}
185185

186186
let tempdir = TempDirDropGuard(
187-
create_temporary_dir().map_err(|e| format!("Failed to create temporary directory: {e}"))?,
187+
create_temporary_dir().map_err(|e| format!("failed to create temporary directory: {e}"))?,
188188
);
189189

190190
for (i, file) in files.iter_mut().enumerate() {
191191
// Create temp file
192192
let dir = tempdir.0.join(format!("{i}"));
193193
std::fs::create_dir(&dir).map_err(|e| {
194194
format!(
195-
"Failed to create temporary directory {}: {e}",
195+
"failed to create temporary directory {}: {e}",
196196
dir.display(),
197197
)
198198
})?;
@@ -205,15 +205,15 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
205205
.open(&tempfile_path)
206206
.map_err(|e| {
207207
format!(
208-
"Failed to create temporary file {}: {e}",
208+
"failed to create temporary file {}: {e}",
209209
tempfile_path.display(),
210210
)
211211
})?;
212212

213213
// Write to temp file
214214
tempfile.write_all(&file.old_data).map_err(|e| {
215215
format!(
216-
"Failed to write to temporary file {}: {e}",
216+
"failed to write to temporary file {}: {e}",
217217
tempfile_path.display(),
218218
)
219219
})?;
@@ -229,7 +229,7 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
229229
.map(|file| file.tempfile_path.as_ref().expect("filled in above")),
230230
)
231231
.status()
232-
.map_err(|e| format!("Failed to run editor {}: {e}", editor.display()))?;
232+
.map_err(|e| format!("failed to run editor {}: {e}", editor.display()))?;
233233

234234
if !status.success() {
235235
drop(tempdir);
@@ -246,15 +246,15 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
246246
// Read from temp file
247247
let new_data = std::fs::read(tempfile_path).map_err(|e| {
248248
format!(
249-
"Failed to read from temporary file {}: {e}",
249+
"failed to read from temporary file {}: {e}",
250250
tempfile_path.display(),
251251
)
252252
})?;
253253

254254
// FIXME preserve temporary file if the original couldn't be written to
255255
std::fs::remove_file(tempfile_path).map_err(|e| {
256256
format!(
257-
"Failed to remove temporary file {}: {e}",
257+
"failed to remove temporary file {}: {e}",
258258
tempfile_path.display(),
259259
)
260260
})?;
@@ -271,11 +271,11 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
271271
) {
272272
Ok(b'y') => {}
273273
_ => {
274-
eprintln_ignore_io_error!("Not overwriting {}", file.path.display());
274+
user_info!("not overwriting {}", file.path.display());
275275

276276
// Parent ignores write when new data matches old data
277277
write_stream(&mut file.new_data_tx, &file.old_data)
278-
.map_err(|e| format!("Failed to write data to parent: {e}"))?;
278+
.map_err(|e| format!("failed to write data to parent: {e}"))?;
279279

280280
continue;
281281
}
@@ -284,7 +284,7 @@ fn handle_child_inner(editor: &Path, mut files: Vec<ChildFileInfo<'_>>) -> Resul
284284

285285
// Write to socket
286286
write_stream(&mut file.new_data_tx, &new_data)
287-
.map_err(|e| format!("Failed to write data to parent: {e}"))?;
287+
.map_err(|e| format!("failed to write data to parent: {e}"))?;
288288
}
289289

290290
process::exit(0);

src/sudo/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ fn sudo_process() -> Result<(), Error> {
8181
match SudoAction::from_env() {
8282
Ok(action) => match action {
8383
SudoAction::Help(_) => {
84-
eprintln_ignore_io_error!("{}", long_help());
84+
println_ignore_io_error!("{}", long_help());
8585
std::process::exit(0);
8686
}
8787
SudoAction::Version(_) => {
88-
eprintln_ignore_io_error!("sudo-rs {VERSION}");
88+
println_ignore_io_error!("sudo-rs {VERSION}");
8989
std::process::exit(0);
9090
}
9191
SudoAction::RemoveTimestamp(_) => {

src/sudoers/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ impl Parse for MetaOrTag {
397397
// this is less fatal
398398
"LOG_INPUT" | "NOLOG_INPUT" | "LOG_OUTPUT" | "NOLOG_OUTPUT" | "MAIL" | "NOMAIL"
399399
| "FOLLOW" => {
400-
eprintln_ignore_io_error!(
401-
"warning: {} tags are ignored by sudo-rs",
400+
crate::log::user_warn!(
401+
"{} tags in the sudoers policy are ignored by sudo-rs",
402402
keyword.as_str()
403403
);
404404
switch(|_| {})?

test-framework/e2e-tests/src/regression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn no_permissions_should_not_violate_io_safety() {
2727

2828
assert_eq!(
2929
stderr,
30-
"sudo-rs: cannot execute '/usr/bin/foo': Permission denied (os error 13)"
30+
"sudo: cannot execute '/usr/bin/foo': Permission denied (os error 13)"
3131
);
3232
}
3333

test-framework/sudo-compliance-tests/src/sudo/flag_help.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ fn does_not_panic_on_io_errors() -> Result<()> {
1616

1717
Ok(())
1818
}
19+
20+
#[test]
21+
fn prints_on_stdout() -> Result<()> {
22+
let env = Env("").build();
23+
24+
let output = Command::new("sudo").args(["--help"]).output(&env);
25+
26+
let output = output.stdout();
27+
assert_starts_with!(
28+
output,
29+
if sudo_test::is_original_sudo() {
30+
"sudo - execute a command as another user"
31+
} else {
32+
"sudo - run commands as another user"
33+
}
34+
);
35+
36+
Ok(())
37+
}

test-framework/sudo-compliance-tests/src/sudo/flag_list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ fn resolves_command_in_invoking_users_path_fail() {
404404
let diagnostic = if sudo_test::is_original_sudo() {
405405
"sudo: true: command not found"
406406
} else {
407-
"sudo-rs: 'true': command not found"
407+
"sudo: 'true': command not found"
408408
};
409409
assert_eq!(output.stderr(), diagnostic);
410410
}
@@ -453,7 +453,7 @@ fn relative_path_does_not_exist() {
453453
let diagnostic = if sudo_test::is_original_sudo() {
454454
format!("sudo: {prog_rel_path}: command not found")
455455
} else {
456-
format!("sudo-rs: '{prog_rel_path}': command not found")
456+
format!("sudo: '{prog_rel_path}': command not found")
457457
};
458458
assert_contains!(output.stderr(), diagnostic);
459459
}

test-framework/sudo-compliance-tests/src/sudo/flag_list/flag_other_user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn other_user_does_not_exist() {
1616
let diagnostic = if sudo_test::is_original_sudo() {
1717
format!("sudo: unknown user {USERNAME}")
1818
} else {
19-
format!("sudo-rs: user '{USERNAME}' not found")
19+
format!("sudo: user '{USERNAME}' not found")
2020
};
2121
assert_contains!(output.stderr(), diagnostic);
2222
}

test-framework/sudo-compliance-tests/src/sudo/flag_version.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,22 @@ fn does_not_panic_on_io_errors() -> Result<()> {
1919

2020
Ok(())
2121
}
22+
23+
#[test]
24+
fn prints_on_stdout() -> Result<()> {
25+
let env = Env("").build();
26+
27+
let output = Command::new("sudo").args(["--version"]).output(&env);
28+
29+
let output = output.stdout();
30+
assert_starts_with!(
31+
output,
32+
if sudo_test::is_original_sudo() {
33+
"Sudo version"
34+
} else {
35+
"sudo-rs"
36+
}
37+
);
38+
39+
Ok(())
40+
}

0 commit comments

Comments
 (0)