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

Merge pull request #2818 from jfinkels/readlink-uresult

readlink: return UResult from uumain() function
This commit is contained in:
Sylvestre Ledru 2021-12-31 10:43:09 +01:00 committed by GitHub
commit 60f501bab3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,6 +15,7 @@ use std::fs;
use std::io::{stdout, Write}; use std::io::{stdout, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::fs::{canonicalize, MissingHandling, ResolveMode}; use uucore::fs::{canonicalize, MissingHandling, ResolveMode};
const ABOUT: &str = "Print value of a symbolic link or canonical file name."; const ABOUT: &str = "Print value of a symbolic link or canonical file name.";
@ -33,7 +34,8 @@ fn usage() -> String {
format!("{0} [OPTION]... [FILE]...", uucore::execution_phrase()) format!("{0} [OPTION]... [FILE]...", uucore::execution_phrase())
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let usage = usage(); let usage = usage();
let matches = uu_app().usage(&usage[..]).get_matches_from(args); let matches = uu_app().usage(&usage[..]).get_matches_from(args);
@ -64,11 +66,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.map(|v| v.map(ToString::to_string).collect()) .map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default(); .unwrap_or_default();
if files.is_empty() { if files.is_empty() {
crash!( return Err(UUsageError::new(1, "missing operand"));
1,
"missing operand\nTry '{} --help' for more information",
uucore::execution_phrase()
);
} }
if no_newline && files.len() > 1 && !silent { if no_newline && files.len() > 1 && !silent {
@ -78,30 +76,26 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
for f in &files { for f in &files {
let p = PathBuf::from(f); let p = PathBuf::from(f);
if res_mode == ResolveMode::None { let path_result = if res_mode == ResolveMode::None {
match fs::read_link(&p) { fs::read_link(&p)
Ok(path) => show(&path, no_newline, use_zero),
Err(err) => {
if verbose {
show_error!("{}: errno {}", f.maybe_quote(), err.raw_os_error().unwrap());
}
return 1;
}
}
} else { } else {
match canonicalize(&p, can_mode, res_mode) { canonicalize(&p, can_mode, res_mode)
Ok(path) => show(&path, no_newline, use_zero), };
Err(err) => { match path_result {
if verbose { Ok(path) => show(&path, no_newline, use_zero).map_err_context(String::new)?,
show_error!("{}: errno {}", f.maybe_quote(), err.raw_os_error().unwrap()); Err(err) => {
} if verbose {
return 1; return Err(USimpleError::new(
1,
format!("{}: errno {}", f.maybe_quote(), err.raw_os_error().unwrap()),
));
} else {
return Err(1.into());
} }
} }
} }
} }
Ok(())
0
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {
@ -168,7 +162,7 @@ pub fn uu_app() -> App<'static, 'static> {
.arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true))
} }
fn show(path: &Path, no_newline: bool, use_zero: bool) { fn show(path: &Path, no_newline: bool, use_zero: bool) -> std::io::Result<()> {
let path = path.to_str().unwrap(); let path = path.to_str().unwrap();
if use_zero { if use_zero {
print!("{}\0", path); print!("{}\0", path);
@ -177,5 +171,5 @@ fn show(path: &Path, no_newline: bool, use_zero: bool) {
} else { } else {
println!("{}", path); println!("{}", path);
} }
crash_if_err!(1, stdout().flush()); stdout().flush()
} }