mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
refactor(chgrp, install): Show the error in the program instead of the lib
This commit is contained in:
parent
55c660b986
commit
3024ade071
5 changed files with 101 additions and 30 deletions
|
@ -239,13 +239,24 @@ impl Chgrper {
|
|||
}
|
||||
}
|
||||
|
||||
let ret = wrap_chgrp(
|
||||
let ret = match wrap_chgrp(
|
||||
path,
|
||||
&meta,
|
||||
self.dest_gid,
|
||||
follow_arg,
|
||||
self.verbosity.clone(),
|
||||
);
|
||||
) {
|
||||
Ok(n) => {
|
||||
show_info!("{}", n);
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
if self.verbosity != Verbosity::Silent {
|
||||
show_info!("{}", e);
|
||||
}
|
||||
1
|
||||
}
|
||||
};
|
||||
|
||||
if !self.recursive {
|
||||
ret
|
||||
|
@ -273,8 +284,22 @@ impl Chgrper {
|
|||
}
|
||||
};
|
||||
|
||||
ret = wrap_chgrp(path, &meta, self.dest_gid, follow, self.verbosity.clone());
|
||||
ret = match wrap_chgrp(path, &meta, self.dest_gid, follow, self.verbosity.clone()) {
|
||||
Ok(n) => {
|
||||
if n != "" {
|
||||
show_info!("{}", n);
|
||||
}
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
if self.verbosity != Verbosity::Silent {
|
||||
show_info!("{}", e);
|
||||
}
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
|
|
|
@ -375,14 +375,28 @@ impl Chowner {
|
|||
}
|
||||
|
||||
let ret = if self.matched(meta.uid(), meta.gid()) {
|
||||
wrap_chown(
|
||||
match wrap_chown(
|
||||
path,
|
||||
&meta,
|
||||
self.dest_uid,
|
||||
self.dest_gid,
|
||||
follow_arg,
|
||||
self.verbosity.clone(),
|
||||
)
|
||||
) {
|
||||
Ok(n) => {
|
||||
if n != "" {
|
||||
show_info!("{}", n);
|
||||
}
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
if self.verbosity != Verbosity::Silent {
|
||||
show_info!("{}", e);
|
||||
}
|
||||
1
|
||||
}
|
||||
}
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
@ -417,14 +431,27 @@ impl Chowner {
|
|||
continue;
|
||||
}
|
||||
|
||||
ret = wrap_chown(
|
||||
ret = match wrap_chown(
|
||||
path,
|
||||
&meta,
|
||||
self.dest_uid,
|
||||
self.dest_gid,
|
||||
follow,
|
||||
self.verbosity.clone(),
|
||||
)
|
||||
) {
|
||||
Ok(n) => {
|
||||
if n != "" {
|
||||
show_info!("{}", n);
|
||||
}
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
if self.verbosity != Verbosity::Silent {
|
||||
show_info!("{}", e);
|
||||
}
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
|
|
@ -516,14 +516,21 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> {
|
|||
_ => crash!(1, "no such user: {}", b.owner),
|
||||
};
|
||||
let gid = meta.gid();
|
||||
wrap_chown(
|
||||
match wrap_chown(
|
||||
to.as_path(),
|
||||
&meta,
|
||||
Some(owner_id),
|
||||
Some(gid),
|
||||
false,
|
||||
Verbosity::Normal,
|
||||
);
|
||||
) {
|
||||
Ok(n) => {
|
||||
if n != "" {
|
||||
show_info!("{}", n);
|
||||
}
|
||||
}
|
||||
Err(e) => show_info!("{}", e),
|
||||
}
|
||||
}
|
||||
|
||||
if b.group != "" {
|
||||
|
@ -536,7 +543,14 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> {
|
|||
Ok(g) => g,
|
||||
_ => crash!(1, "no such group: {}", b.group),
|
||||
};
|
||||
wrap_chgrp(to.as_path(), &meta, group_id, false, Verbosity::Normal);
|
||||
match wrap_chgrp(to.as_path(), &meta, group_id, false, Verbosity::Normal) {
|
||||
Ok(n) => {
|
||||
if n != "" {
|
||||
show_info!("{}", n);
|
||||
}
|
||||
}
|
||||
Err(e) => show_info!("{}", e),
|
||||
}
|
||||
}
|
||||
|
||||
if b.verbose {
|
||||
|
|
|
@ -19,6 +19,8 @@ use std::os::unix::fs::MetadataExt;
|
|||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::path::Path;
|
||||
|
||||
//type PermResult<T> = Result<T, IOError>;
|
||||
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
pub enum Verbosity {
|
||||
Silent,
|
||||
|
@ -50,18 +52,20 @@ pub fn wrap_chgrp<P: AsRef<Path>>(
|
|||
dest_gid: gid_t,
|
||||
follow: bool,
|
||||
verbosity: Verbosity,
|
||||
) -> i32 {
|
||||
) -> Result<String, String> {
|
||||
use self::Verbosity::*;
|
||||
let mut ret = 0;
|
||||
let path = path.as_ref();
|
||||
let mut out: String = String::new();
|
||||
|
||||
if let Err(e) = chgrp(path, dest_gid, follow) {
|
||||
match verbosity {
|
||||
Silent => (),
|
||||
_ => {
|
||||
show_info!("changing group of '{}': {}", path.display(), e);
|
||||
out = format!("changing group of '{}': {}", path.display(), e);
|
||||
if verbosity == Verbose {
|
||||
println!(
|
||||
"failed to change group of {} from {} to {}",
|
||||
out = format!(
|
||||
"{}\nfailed to change group of {} from {} to {}",
|
||||
out,
|
||||
path.display(),
|
||||
entries::gid2grp(meta.gid()).unwrap(),
|
||||
entries::gid2grp(dest_gid).unwrap()
|
||||
|
@ -69,13 +73,13 @@ pub fn wrap_chgrp<P: AsRef<Path>>(
|
|||
};
|
||||
}
|
||||
}
|
||||
ret = 1;
|
||||
return Err(out);
|
||||
} else {
|
||||
let changed = dest_gid != meta.gid();
|
||||
if changed {
|
||||
match verbosity {
|
||||
Changes | Verbose => {
|
||||
println!(
|
||||
out = format!(
|
||||
"changed group of {} from {} to {}",
|
||||
path.display(),
|
||||
entries::gid2grp(meta.gid()).unwrap(),
|
||||
|
@ -85,14 +89,14 @@ pub fn wrap_chgrp<P: AsRef<Path>>(
|
|||
_ => (),
|
||||
};
|
||||
} else if verbosity == Verbose {
|
||||
println!(
|
||||
out = format!(
|
||||
"group of {} retained as {}",
|
||||
path.display(),
|
||||
entries::gid2grp(dest_gid).unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
ret
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn chown<P: AsRef<Path>>(path: P, duid: uid_t, dgid: gid_t, follow: bool) -> IOResult<()> {
|
||||
|
@ -119,20 +123,22 @@ pub fn wrap_chown<P: AsRef<Path>>(
|
|||
dest_gid: Option<u32>,
|
||||
follow: bool,
|
||||
verbosity: Verbosity,
|
||||
) -> i32 {
|
||||
) -> Result<String, String> {
|
||||
use self::Verbosity::*;
|
||||
let mut ret = 0;
|
||||
let dest_uid = dest_uid.unwrap_or_else(|| meta.uid());
|
||||
let dest_gid = dest_gid.unwrap_or_else(|| meta.gid());
|
||||
let path = path.as_ref();
|
||||
let mut out: String = String::new();
|
||||
|
||||
if let Err(e) = chown(path, dest_uid, dest_gid, follow) {
|
||||
match verbosity {
|
||||
Silent => (),
|
||||
_ => {
|
||||
show_info!("changing ownership of '{}': {}", path.display(), e);
|
||||
out = format!("changing ownership of '{}': {}", path.display(), e);
|
||||
if verbosity == Verbose {
|
||||
println!(
|
||||
"failed to change ownership of {} from {}:{} to {}:{}",
|
||||
out = format!(
|
||||
"{}\nfailed to change ownership of {} from {}:{} to {}:{}",
|
||||
out,
|
||||
path.display(),
|
||||
entries::uid2usr(meta.uid()).unwrap(),
|
||||
entries::gid2grp(meta.gid()).unwrap(),
|
||||
|
@ -142,13 +148,13 @@ pub fn wrap_chown<P: AsRef<Path>>(
|
|||
};
|
||||
}
|
||||
}
|
||||
ret = 1;
|
||||
return Err(out);
|
||||
} else {
|
||||
let changed = dest_uid != meta.uid() || dest_gid != meta.gid();
|
||||
if changed {
|
||||
match verbosity {
|
||||
Changes | Verbose => {
|
||||
println!(
|
||||
out = format!(
|
||||
"changed ownership of {} from {}:{} to {}:{}",
|
||||
path.display(),
|
||||
entries::uid2usr(meta.uid()).unwrap(),
|
||||
|
@ -160,7 +166,7 @@ pub fn wrap_chown<P: AsRef<Path>>(
|
|||
_ => (),
|
||||
};
|
||||
} else if verbosity == Verbose {
|
||||
println!(
|
||||
out = format!(
|
||||
"ownership of {} retained as {}:{}",
|
||||
path.display(),
|
||||
entries::uid2usr(dest_uid).unwrap(),
|
||||
|
@ -168,5 +174,5 @@ pub fn wrap_chown<P: AsRef<Path>>(
|
|||
);
|
||||
}
|
||||
}
|
||||
ret
|
||||
Ok(out)
|
||||
}
|
||||
|
|
|
@ -110,8 +110,7 @@ fn test_reference() {
|
|||
.arg("--reference=/etc/passwd")
|
||||
.arg("/etc")
|
||||
.fails()
|
||||
.stderr_is("chgrp: changing group of '/etc': Operation not permitted (os error 1)\n")
|
||||
.stdout_is("failed to change group of /etc from root to root\n");
|
||||
.stderr_is("chgrp: changing group of '/etc': Operation not permitted (os error 1)\nfailed to change group of /etc from root to root");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue