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:
parent
3c3daf5023
commit
e9174f7d82
1 changed files with 21 additions and 39 deletions
|
@ -7,9 +7,6 @@
|
||||||
|
|
||||||
// spell-checker:ignore (ToDO) COMFOLLOW Chowner Passwd RFILE RFILE's derefer dgid duid
|
// 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]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
pub use uucore::entries::{self, Group, Locate, Passwd};
|
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::libc::{gid_t, uid_t};
|
||||||
use uucore::perms::{wrap_chown, Verbosity};
|
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};
|
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 preserve_root = matches.is_present(options::preserve_root::PRESERVE);
|
||||||
|
|
||||||
let mut derefer = if matches.is_present(options::dereference::NO_DEREFERENCE) {
|
let mut derefer = if matches.is_present(options::dereference::NO_DEREFERENCE) {
|
||||||
Err(UError::from(1))
|
1
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut bit_flag = if matches.is_present(options::traverse::TRAVERSE) {
|
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);
|
let recursive = matches.is_present(options::RECURSIVE);
|
||||||
if recursive {
|
if recursive {
|
||||||
if bit_flag == FTS_PHYSICAL {
|
if bit_flag == FTS_PHYSICAL {
|
||||||
if derefer.is_err() {
|
if derefer == 1 {
|
||||||
return Err(USimpleError::new(
|
return Err(USimpleError::new(
|
||||||
1,
|
1,
|
||||||
"-R --dereference requires -H or -L".to_string(),
|
"-R --dereference requires -H or -L".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
derefer = Ok(());
|
derefer = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bit_flag = FTS_PHYSICAL;
|
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) {
|
let filter = if let Some(spec) = matches.value_of(options::FROM) {
|
||||||
match parse_spec(spec) {
|
match parse_spec(spec)? {
|
||||||
Ok((Some(uid), None)) => IfFrom::User(uid),
|
(Some(uid), None) => IfFrom::User(uid),
|
||||||
Ok((None, Some(gid))) => IfFrom::Group(gid),
|
(None, Some(gid)) => IfFrom::Group(gid),
|
||||||
Ok((Some(uid), Some(gid))) => IfFrom::UserGroup(uid, gid),
|
(Some(uid), Some(gid)) => IfFrom::UserGroup(uid, gid),
|
||||||
Ok((None, None)) => IfFrom::All,
|
(None, None) => IfFrom::All,
|
||||||
Err(e) => return Err(USimpleError::new(1, e)),
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
IfFrom::All
|
IfFrom::All
|
||||||
|
@ -148,28 +144,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let dest_uid: Option<u32>;
|
let dest_uid: Option<u32>;
|
||||||
let dest_gid: Option<u32>;
|
let dest_gid: Option<u32>;
|
||||||
if let Some(file) = matches.value_of(options::REFERENCE) {
|
if let Some(file) = matches.value_of(options::REFERENCE) {
|
||||||
match fs::metadata(&file) {
|
let meta = fs::metadata(&file)
|
||||||
Ok(meta) => {
|
.map_err_context(|| format!("failed to get attributes of '{}'", file))?;
|
||||||
dest_gid = Some(meta.gid());
|
dest_gid = Some(meta.gid());
|
||||||
dest_uid = Some(meta.uid());
|
dest_uid = Some(meta.uid());
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
return Err(USimpleError::new(
|
|
||||||
1,
|
|
||||||
format!("failed to get attributes of '{}': {}", file, e),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
match parse_spec(owner) {
|
let (u, g) = parse_spec(owner)?;
|
||||||
Ok((u, g)) => {
|
dest_uid = u;
|
||||||
dest_uid = u;
|
dest_gid = g;
|
||||||
dest_gid = g;
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
return Err(USimpleError::new(1, e));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let executor = Chowner {
|
let executor = Chowner {
|
||||||
bit_flag,
|
bit_flag,
|
||||||
|
@ -177,7 +159,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
dest_gid,
|
dest_gid,
|
||||||
verbosity,
|
verbosity,
|
||||||
recursive,
|
recursive,
|
||||||
dereference: derefer.is_ok(),
|
dereference: derefer != 0,
|
||||||
filter,
|
filter,
|
||||||
preserve_root,
|
preserve_root,
|
||||||
files,
|
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 args = spec.split_terminator(':').collect::<Vec<_>>();
|
||||||
let usr_only = args.len() == 1 && !args[0].is_empty();
|
let usr_only = args.len() == 1 && !args[0].is_empty();
|
||||||
let grp_only = args.len() == 2 && 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 {
|
let uid = if usr_only || usr_grp {
|
||||||
Some(
|
Some(
|
||||||
Passwd::locate(args[0])
|
Passwd::locate(args[0])
|
||||||
.map_err(|_| format!("invalid user: '{}'", spec))?
|
.map_err(|_| USimpleError::new(1, format!("invalid user: '{}'", spec)))?
|
||||||
.uid(),
|
.uid(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
@ -298,7 +280,7 @@ fn parse_spec(spec: &str) -> Result<(Option<u32>, Option<u32>), String> {
|
||||||
let gid = if grp_only || usr_grp {
|
let gid = if grp_only || usr_grp {
|
||||||
Some(
|
Some(
|
||||||
Group::locate(args[1])
|
Group::locate(args[1])
|
||||||
.map_err(|_| format!("invalid group: '{}'", spec))?
|
.map_err(|_| USimpleError::new(1, format!("invalid group: '{}'", spec)))?
|
||||||
.gid(),
|
.gid(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue