1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Merge pull request #2678 from blyxxyz/io-error-detect-os

Do not discard non-OS error messages
This commit is contained in:
Michael Debertol 2021-09-14 20:21:48 +02:00 committed by GitHub
commit 5c97c1ccc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 35 deletions

View file

@ -43,6 +43,9 @@ gibi
gibibytes gibibytes
glob glob
globbing globbing
hardcode
hardcoded
hardcoding
hardfloat hardfloat
hardlink hardlink
hardlinks hardlinks

View file

@ -490,11 +490,10 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UR
return Err(InstallError::TargetDirIsntDir(target_dir.to_path_buf()).into()); return Err(InstallError::TargetDirIsntDir(target_dir.to_path_buf()).into());
} }
for sourcepath in files.iter() { for sourcepath in files.iter() {
if !sourcepath.exists() { if let Err(err) = sourcepath
let err = UIoError::new( .metadata()
std::io::ErrorKind::NotFound, .map_err_context(|| format!("cannot stat {}", sourcepath.quote()))
format!("cannot stat {}", sourcepath.quote()), {
);
show!(err); show!(err);
continue; continue;
} }

View file

@ -42,7 +42,7 @@ use std::{
use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use uucore::{ use uucore::{
display::Quotable, display::Quotable,
error::{set_exit_code, FromIo, UError, UResult}, error::{set_exit_code, UError, UResult},
}; };
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
@ -1257,8 +1257,13 @@ fn list(locs: Vec<&Path>, config: Config) -> UResult<()> {
let path_data = PathData::new(p, None, None, &config, true); let path_data = PathData::new(p, None, None, &config, true);
if path_data.md().is_none() { if path_data.md().is_none() {
show!(std::io::ErrorKind::NotFound // FIXME: Would be nice to use the actual error instead of hardcoding it
.map_err_context(|| format!("cannot access {}", path_data.p_buf.quote()))); // Presumably other errors can happen too?
show_error!(
"cannot access {}: No such file or directory",
path_data.p_buf.quote()
);
set_exit_code(1);
// We found an error, no need to continue the execution // We found an error, no need to continue the execution
continue; continue;
} }

View file

@ -393,34 +393,56 @@ impl Display for UIoError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
use std::io::ErrorKind::*; use std::io::ErrorKind::*;
let message; let mut message;
let message = match self.inner.kind() { let message = if self.inner.raw_os_error().is_some() {
NotFound => "No such file or directory", // These are errors that come directly from the OS.
PermissionDenied => "Permission denied", // We want to normalize their messages across systems,
ConnectionRefused => "Connection refused", // and we want to strip the "(os error X)" suffix.
ConnectionReset => "Connection reset", match self.inner.kind() {
ConnectionAborted => "Connection aborted", NotFound => "No such file or directory",
NotConnected => "Not connected", PermissionDenied => "Permission denied",
AddrInUse => "Address in use", ConnectionRefused => "Connection refused",
AddrNotAvailable => "Address not available", ConnectionReset => "Connection reset",
BrokenPipe => "Broken pipe", ConnectionAborted => "Connection aborted",
AlreadyExists => "Already exists", NotConnected => "Not connected",
WouldBlock => "Would block", AddrInUse => "Address in use",
InvalidInput => "Invalid input", AddrNotAvailable => "Address not available",
InvalidData => "Invalid data", BrokenPipe => "Broken pipe",
TimedOut => "Timed out", AlreadyExists => "Already exists",
WriteZero => "Write zero", WouldBlock => "Would block",
Interrupted => "Interrupted", InvalidInput => "Invalid input",
UnexpectedEof => "Unexpected end of file", InvalidData => "Invalid data",
_ => { TimedOut => "Timed out",
// TODO: using `strip_errno()` causes the error message WriteZero => "Write zero",
// to not be capitalized. When the new error variants (https://github.com/rust-lang/rust/issues/86442) Interrupted => "Interrupted",
// are stabilized, we should add them to the match statement. UnexpectedEof => "Unexpected end of file",
message = strip_errno(&self.inner); _ => {
&message // TODO: When the new error variants
// (https://github.com/rust-lang/rust/issues/86442)
// are stabilized, we should add them to the match statement.
message = strip_errno(&self.inner);
capitalize(&mut message);
&message
}
} }
} else {
// These messages don't need as much normalization, and the above
// messages wouldn't always be a good substitute.
// For example, ErrorKind::NotFound doesn't necessarily mean it was
// a file that was not found.
// There are also errors with entirely custom messages.
message = self.inner.to_string();
capitalize(&mut message);
&message
}; };
write!(f, "{}: {}", self.context, message,) write!(f, "{}: {}", self.context, message)
}
}
/// Capitalize the first character of an ASCII string.
fn capitalize(text: &mut str) {
if let Some(first) = text.get_mut(..1) {
first.make_ascii_uppercase();
} }
} }
@ -428,7 +450,7 @@ impl Display for UIoError {
pub fn strip_errno(err: &std::io::Error) -> String { pub fn strip_errno(err: &std::io::Error) -> String {
let mut msg = err.to_string(); let mut msg = err.to_string();
if let Some(pos) = msg.find(" (os error ") { if let Some(pos) = msg.find(" (os error ") {
msg.drain(pos..); msg.truncate(pos);
} }
msg msg
} }