mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
Merge pull request #2504 from 353fc443/uresult-chown
chown: added UResult
This commit is contained in:
commit
45d2e09de0
1 changed files with 28 additions and 37 deletions
|
@ -14,6 +14,8 @@ 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::{FromIo, UError, UResult, USimpleError};
|
||||||
|
|
||||||
use clap::{crate_version, App, Arg};
|
use clap::{crate_version, App, Arg};
|
||||||
|
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
@ -66,7 +68,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
|
let args = args
|
||||||
.collect_str(InvalidEncodingHandling::Ignore)
|
.collect_str(InvalidEncodingHandling::Ignore)
|
||||||
.accept_any();
|
.accept_any();
|
||||||
|
@ -104,8 +107,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
if recursive {
|
if recursive {
|
||||||
if bit_flag == FTS_PHYSICAL {
|
if bit_flag == FTS_PHYSICAL {
|
||||||
if derefer == 1 {
|
if derefer == 1 {
|
||||||
show_error!("-R --dereference requires -H or -L");
|
return Err(USimpleError::new(
|
||||||
return 1;
|
1,
|
||||||
|
"-R --dereference requires -H or -L".to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
derefer = 0;
|
derefer = 0;
|
||||||
}
|
}
|
||||||
|
@ -126,15 +131,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
};
|
};
|
||||||
|
|
||||||
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) => {
|
|
||||||
show_error!("{}", e);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
IfFrom::All
|
IfFrom::All
|
||||||
|
@ -143,27 +144,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
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) => {
|
|
||||||
show_error!("failed to get attributes of '{}': {}", file, e);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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) => {
|
|
||||||
show_error!("{}", e);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let executor = Chowner {
|
let executor = Chowner {
|
||||||
bit_flag,
|
bit_flag,
|
||||||
|
@ -275,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();
|
||||||
|
@ -283,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 {
|
||||||
|
@ -292,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 {
|
||||||
|
@ -330,12 +318,15 @@ macro_rules! unwrap {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Chowner {
|
impl Chowner {
|
||||||
fn exec(&self) -> i32 {
|
fn exec(&self) -> UResult<()> {
|
||||||
let mut ret = 0;
|
let mut ret = 0;
|
||||||
for f in &self.files {
|
for f in &self.files {
|
||||||
ret |= self.traverse(f);
|
ret |= self.traverse(f);
|
||||||
}
|
}
|
||||||
ret
|
if ret != 0 {
|
||||||
|
return Err(UError::from(ret));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn traverse<P: AsRef<Path>>(&self, root: P) -> i32 {
|
fn traverse<P: AsRef<Path>>(&self, root: P) -> i32 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue