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

chown: code cleanup

This commit is contained in:
353fc443 2021-07-17 14:14:23 +00:00
parent 3c3daf5023
commit e9174f7d82
No known key found for this signature in database
GPG key ID: D58B14ED3D42A937

View file

@ -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,36 +144,22 @@ 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) => {
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());
}
Err(e) => {
return Err(USimpleError::new(
1,
format!("failed to get attributes of '{}': {}", file, e),
));
}
}
} else {
match parse_spec(owner) {
Ok((u, g)) => {
let (u, g) = parse_spec(owner)?;
dest_uid = u;
dest_gid = g;
}
Err(e) => {
return Err(USimpleError::new(1, e));
}
}
}
let executor = Chowner {
bit_flag,
dest_uid,
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 {