mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
chown: added UResult
This commit is contained in:
parent
f5ba8b1811
commit
3c3daf5023
1 changed files with 27 additions and 18 deletions
|
@ -7,6 +7,9 @@
|
||||||
|
|
||||||
// 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};
|
||||||
|
@ -14,6 +17,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::{UError, UResult, USimpleError};
|
||||||
|
|
||||||
use clap::{crate_version, App, Arg};
|
use clap::{crate_version, App, Arg};
|
||||||
|
|
||||||
use walkdir::WalkDir;
|
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
|
let args = args
|
||||||
.collect_str(InvalidEncodingHandling::Ignore)
|
.collect_str(InvalidEncodingHandling::Ignore)
|
||||||
.accept_any();
|
.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 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) {
|
||||||
1
|
Err(UError::from(1))
|
||||||
} else {
|
} else {
|
||||||
0
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut bit_flag = if matches.is_present(options::traverse::TRAVERSE) {
|
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);
|
let recursive = matches.is_present(options::RECURSIVE);
|
||||||
if recursive {
|
if recursive {
|
||||||
if bit_flag == FTS_PHYSICAL {
|
if bit_flag == FTS_PHYSICAL {
|
||||||
if derefer == 1 {
|
if derefer.is_err() {
|
||||||
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 = Ok(());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bit_flag = FTS_PHYSICAL;
|
bit_flag = FTS_PHYSICAL;
|
||||||
|
@ -131,10 +139,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
Ok((None, Some(gid))) => IfFrom::Group(gid),
|
Ok((None, Some(gid))) => IfFrom::Group(gid),
|
||||||
Ok((Some(uid), Some(gid))) => IfFrom::UserGroup(uid, gid),
|
Ok((Some(uid), Some(gid))) => IfFrom::UserGroup(uid, gid),
|
||||||
Ok((None, None)) => IfFrom::All,
|
Ok((None, None)) => IfFrom::All,
|
||||||
Err(e) => {
|
Err(e) => return Err(USimpleError::new(1, e)),
|
||||||
show_error!("{}", e);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
IfFrom::All
|
IfFrom::All
|
||||||
|
@ -149,8 +154,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
dest_uid = Some(meta.uid());
|
dest_uid = Some(meta.uid());
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_error!("failed to get attributes of '{}': {}", file, e);
|
return Err(USimpleError::new(
|
||||||
return 1;
|
1,
|
||||||
|
format!("failed to get attributes of '{}': {}", file, e),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -160,8 +167,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
dest_gid = g;
|
dest_gid = g;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_error!("{}", e);
|
return Err(USimpleError::new(1, e));
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,7 +177,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
dest_gid,
|
dest_gid,
|
||||||
verbosity,
|
verbosity,
|
||||||
recursive,
|
recursive,
|
||||||
dereference: derefer != 0,
|
dereference: derefer.is_ok(),
|
||||||
filter,
|
filter,
|
||||||
preserve_root,
|
preserve_root,
|
||||||
files,
|
files,
|
||||||
|
@ -330,12 +336,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