From 3c3daf502317b741e7ceae3ccfeeff118b0ee907 Mon Sep 17 00:00:00 2001 From: 353fc443 <353fc443@pm.me> Date: Sat, 17 Jul 2021 08:40:27 +0000 Subject: [PATCH 1/2] chown: added UResult --- src/uu/chown/src/chown.rs | 45 +++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/uu/chown/src/chown.rs b/src/uu/chown/src/chown.rs index e1d3ff22b..68e7e24aa 100644 --- a/src/uu/chown/src/chown.rs +++ b/src/uu/chown/src/chown.rs @@ -7,6 +7,9 @@ // spell-checker:ignore (ToDO) COMFOLLOW Chowner Passwd RFILE RFILE's derefer dgid duid +// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 +#![allow(clippy::nonstandard_macro_braces)] + #[macro_use] extern crate uucore; pub use uucore::entries::{self, Group, Locate, Passwd}; @@ -14,6 +17,8 @@ use uucore::fs::resolve_relative_path; use uucore::libc::{gid_t, uid_t}; use uucore::perms::{wrap_chown, Verbosity}; +use uucore::error::{UError, UResult, USimpleError}; + use clap::{crate_version, App, Arg}; use walkdir::WalkDir; @@ -66,7 +71,8 @@ fn get_usage() -> String { ) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = args .collect_str(InvalidEncodingHandling::Ignore) .accept_any(); @@ -87,9 +93,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let preserve_root = matches.is_present(options::preserve_root::PRESERVE); let mut derefer = if matches.is_present(options::dereference::NO_DEREFERENCE) { - 1 + Err(UError::from(1)) } else { - 0 + Ok(()) }; let mut bit_flag = if matches.is_present(options::traverse::TRAVERSE) { @@ -103,11 +109,13 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let recursive = matches.is_present(options::RECURSIVE); if recursive { if bit_flag == FTS_PHYSICAL { - if derefer == 1 { - show_error!("-R --dereference requires -H or -L"); - return 1; + if derefer.is_err() { + return Err(USimpleError::new( + 1, + "-R --dereference requires -H or -L".to_string(), + )); } - derefer = 0; + derefer = Ok(()); } } else { bit_flag = FTS_PHYSICAL; @@ -131,10 +139,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { Ok((None, Some(gid))) => IfFrom::Group(gid), Ok((Some(uid), Some(gid))) => IfFrom::UserGroup(uid, gid), Ok((None, None)) => IfFrom::All, - Err(e) => { - show_error!("{}", e); - return 1; - } + Err(e) => return Err(USimpleError::new(1, e)), } } else { IfFrom::All @@ -149,8 +154,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { dest_uid = Some(meta.uid()); } Err(e) => { - show_error!("failed to get attributes of '{}': {}", file, e); - return 1; + return Err(USimpleError::new( + 1, + format!("failed to get attributes of '{}': {}", file, e), + )); } } } else { @@ -160,8 +167,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { dest_gid = g; } Err(e) => { - show_error!("{}", e); - return 1; + return Err(USimpleError::new(1, e)); } } } @@ -171,7 +177,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { dest_gid, verbosity, recursive, - dereference: derefer != 0, + dereference: derefer.is_ok(), filter, preserve_root, files, @@ -330,12 +336,15 @@ macro_rules! unwrap { } impl Chowner { - fn exec(&self) -> i32 { + fn exec(&self) -> UResult<()> { let mut ret = 0; for f in &self.files { ret |= self.traverse(f); } - ret + if ret != 0 { + return Err(UError::from(ret)); + } + Ok(()) } fn traverse>(&self, root: P) -> i32 { From e9174f7d820c6ead4dcb18e74069d9e30921b8b9 Mon Sep 17 00:00:00 2001 From: 353fc443 <353fc443@pm.me> Date: Sat, 17 Jul 2021 14:14:23 +0000 Subject: [PATCH 2/2] chown: code cleanup --- src/uu/chown/src/chown.rs | 60 ++++++++++++++------------------------- 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/src/uu/chown/src/chown.rs b/src/uu/chown/src/chown.rs index 68e7e24aa..081d3a148 100644 --- a/src/uu/chown/src/chown.rs +++ b/src/uu/chown/src/chown.rs @@ -7,9 +7,6 @@ // spell-checker:ignore (ToDO) COMFOLLOW Chowner Passwd RFILE RFILE's derefer dgid duid -// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 -#![allow(clippy::nonstandard_macro_braces)] - #[macro_use] extern crate uucore; pub use uucore::entries::{self, Group, Locate, Passwd}; @@ -17,7 +14,7 @@ use uucore::fs::resolve_relative_path; use uucore::libc::{gid_t, uid_t}; use uucore::perms::{wrap_chown, Verbosity}; -use uucore::error::{UError, UResult, USimpleError}; +use uucore::error::{FromIo, UError, UResult, USimpleError}; use clap::{crate_version, App, Arg}; @@ -93,9 +90,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let preserve_root = matches.is_present(options::preserve_root::PRESERVE); let mut derefer = if matches.is_present(options::dereference::NO_DEREFERENCE) { - Err(UError::from(1)) + 1 } else { - Ok(()) + 0 }; let mut bit_flag = if matches.is_present(options::traverse::TRAVERSE) { @@ -109,13 +106,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let recursive = matches.is_present(options::RECURSIVE); if recursive { if bit_flag == FTS_PHYSICAL { - if derefer.is_err() { + if derefer == 1 { return Err(USimpleError::new( 1, "-R --dereference requires -H or -L".to_string(), )); } - derefer = Ok(()); + derefer = 0; } } else { bit_flag = FTS_PHYSICAL; @@ -134,12 +131,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { }; let filter = if let Some(spec) = matches.value_of(options::FROM) { - match parse_spec(spec) { - Ok((Some(uid), None)) => IfFrom::User(uid), - Ok((None, Some(gid))) => IfFrom::Group(gid), - Ok((Some(uid), Some(gid))) => IfFrom::UserGroup(uid, gid), - Ok((None, None)) => IfFrom::All, - Err(e) => return Err(USimpleError::new(1, e)), + match parse_spec(spec)? { + (Some(uid), None) => IfFrom::User(uid), + (None, Some(gid)) => IfFrom::Group(gid), + (Some(uid), Some(gid)) => IfFrom::UserGroup(uid, gid), + (None, None) => IfFrom::All, } } else { IfFrom::All @@ -148,28 +144,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let dest_uid: Option; let dest_gid: Option; if let Some(file) = matches.value_of(options::REFERENCE) { - match fs::metadata(&file) { - Ok(meta) => { - dest_gid = Some(meta.gid()); - dest_uid = Some(meta.uid()); - } - Err(e) => { - return Err(USimpleError::new( - 1, - format!("failed to get attributes of '{}': {}", file, e), - )); - } - } + let meta = fs::metadata(&file) + .map_err_context(|| format!("failed to get attributes of '{}'", file))?; + dest_gid = Some(meta.gid()); + dest_uid = Some(meta.uid()); } else { - match parse_spec(owner) { - Ok((u, g)) => { - dest_uid = u; - dest_gid = g; - } - Err(e) => { - return Err(USimpleError::new(1, e)); - } - } + let (u, g) = parse_spec(owner)?; + dest_uid = u; + dest_gid = g; } let executor = Chowner { bit_flag, @@ -177,7 +159,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { dest_gid, verbosity, recursive, - dereference: derefer.is_ok(), + dereference: derefer != 0, filter, preserve_root, files, @@ -281,7 +263,7 @@ pub fn uu_app() -> App<'static, 'static> { ) } -fn parse_spec(spec: &str) -> Result<(Option, Option), String> { +fn parse_spec(spec: &str) -> UResult<(Option, Option)> { let args = spec.split_terminator(':').collect::>(); let usr_only = args.len() == 1 && !args[0].is_empty(); let grp_only = args.len() == 2 && args[0].is_empty(); @@ -289,7 +271,7 @@ fn parse_spec(spec: &str) -> Result<(Option, Option), String> { let uid = if usr_only || usr_grp { Some( Passwd::locate(args[0]) - .map_err(|_| format!("invalid user: '{}'", spec))? + .map_err(|_| USimpleError::new(1, format!("invalid user: '{}'", spec)))? .uid(), ) } else { @@ -298,7 +280,7 @@ fn parse_spec(spec: &str) -> Result<(Option, Option), String> { let gid = if grp_only || usr_grp { Some( Group::locate(args[1]) - .map_err(|_| format!("invalid group: '{}'", spec))? + .map_err(|_| USimpleError::new(1, format!("invalid group: '{}'", spec)))? .gid(), ) } else {