summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
author353fc443 <353fc443@pm.me>2021-07-17 14:14:23 +0000
committer353fc443 <353fc443@pm.me>2021-07-17 18:37:53 +0000
commite9174f7d820c6ead4dcb18e74069d9e30921b8b9 (patch)
treea87992bd5547fb45c28e6996bf618fe06fad9a3f
parent3c3daf502317b741e7ceae3ccfeeff118b0ee907 (diff)
chown: code cleanup
-rw-r--r--src/uu/chown/src/chown.rs60
1 files 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<u32>;
let dest_gid: Option<u32>;
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<u32>, Option<u32>), String> {
+fn parse_spec(spec: &str) -> UResult<(Option<u32>, Option<u32>)> {
let args = spec.split_terminator(':').collect::<Vec<_>>();
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<u32>, Option<u32>), 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<u32>, Option<u32>), 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 {