mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 23:02:38 +00:00
head: use uucore error handling instead of custom
Use `show!()` and `USimpleError` to handle I/O errors instead of using custom code.
This commit is contained in:
parent
ec386fa460
commit
ab4036297b
1 changed files with 15 additions and 24 deletions
|
@ -10,8 +10,8 @@ use std::convert::TryFrom;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::io::{self, BufWriter, ErrorKind, Read, Seek, SeekFrom, Write};
|
use std::io::{self, BufWriter, ErrorKind, Read, Seek, SeekFrom, Write};
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::{UResult, USimpleError};
|
use uucore::error::{FromIo, UError, UResult, USimpleError};
|
||||||
use uucore::show_error_custom_description;
|
use uucore::show;
|
||||||
|
|
||||||
const BUF_SIZE: usize = 65536;
|
const BUF_SIZE: usize = 65536;
|
||||||
|
|
||||||
|
@ -367,7 +367,6 @@ fn head_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Resul
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uu_head(options: &HeadOptions) -> UResult<()> {
|
fn uu_head(options: &HeadOptions) -> UResult<()> {
|
||||||
let mut error_count = 0;
|
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for file in &options.files {
|
for file in &options.files {
|
||||||
let res = match file.as_str() {
|
let res = match file.as_str() {
|
||||||
|
@ -401,19 +400,10 @@ fn uu_head(options: &HeadOptions) -> UResult<()> {
|
||||||
let mut file = match std::fs::File::open(name) {
|
let mut file = match std::fs::File::open(name) {
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let prefix = format!("cannot open {} for reading", name.quote());
|
show!(err.map_err_context(|| format!(
|
||||||
match err.kind() {
|
"cannot open {} for reading",
|
||||||
ErrorKind::NotFound => {
|
name.quote()
|
||||||
show_error_custom_description!(prefix, "No such file or directory");
|
)));
|
||||||
}
|
|
||||||
ErrorKind::PermissionDenied => {
|
|
||||||
show_error_custom_description!(prefix, "Permission denied");
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
show_error_custom_description!(prefix, "{}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
error_count += 1;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -432,17 +422,18 @@ fn uu_head(options: &HeadOptions) -> UResult<()> {
|
||||||
} else {
|
} else {
|
||||||
file
|
file
|
||||||
};
|
};
|
||||||
let prefix = format!("error reading {}", name);
|
show!(USimpleError::new(
|
||||||
show_error_custom_description!(prefix, "Input/output error");
|
1,
|
||||||
error_count += 1;
|
format!("error reading {}: Input/output error", name)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
if error_count > 0 {
|
// Even though this is returning `Ok`, it is possible that a call
|
||||||
Err(USimpleError::new(1, ""))
|
// to `show!()` and thus a call to `set_exit_code()` has been
|
||||||
} else {
|
// called above. If that happens, then this process will exit with
|
||||||
Ok(())
|
// a non-zero exit code.
|
||||||
}
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[uucore_procs::gen_uumain]
|
#[uucore_procs::gen_uumain]
|
||||||
|
|
Loading…
Reference in a new issue