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