1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

chgrp: handle empty group

This commit is contained in:
Michael Debertol 2021-08-10 23:31:42 +02:00
parent 13b6d003bb
commit 1c30fb42d2
4 changed files with 23 additions and 16 deletions

View file

@ -161,12 +161,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
Verbosity::Normal Verbosity::Normal
}; };
let dest_gid: u32; let dest_gid = if let Some(file) = matches.value_of(options::REFERENCE) {
if let Some(file) = matches.value_of(options::REFERENCE) {
match fs::metadata(&file) { match fs::metadata(&file) {
Ok(meta) => { Ok(meta) => Some(meta.gid()),
dest_gid = meta.gid();
}
Err(e) => { Err(e) => {
show_error!("failed to get attributes of '{}': {}", file, e); show_error!("failed to get attributes of '{}': {}", file, e);
return 1; return 1;
@ -174,16 +171,18 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
} else { } else {
let group = matches.value_of(options::ARG_GROUP).unwrap_or_default(); let group = matches.value_of(options::ARG_GROUP).unwrap_or_default();
match entries::grp2gid(group) { if group.is_empty() {
Ok(g) => { None
dest_gid = g; } else {
} match entries::grp2gid(group) {
_ => { Ok(g) => Some(g),
show_error!("invalid group: {}", group); _ => {
return 1; show_error!("invalid group: {}", group);
return 1;
}
} }
} }
} };
let executor = Chgrper { let executor = Chgrper {
bit_flag, bit_flag,
@ -278,7 +277,7 @@ pub fn uu_app() -> App<'static, 'static> {
} }
struct Chgrper { struct Chgrper {
dest_gid: gid_t, dest_gid: Option<gid_t>,
bit_flag: u8, bit_flag: u8,
verbosity: Verbosity, verbosity: Verbosity,
files: Vec<String>, files: Vec<String>,

View file

@ -660,7 +660,7 @@ fn copy(from: &Path, to: &Path, b: &Behavior) -> UResult<()> {
Ok(g) => g, Ok(g) => g,
_ => return Err(InstallError::NoSuchGroup(b.group.clone()).into()), _ => return Err(InstallError::NoSuchGroup(b.group.clone()).into()),
}; };
match wrap_chgrp(to, &meta, group_id, false, Verbosity::Normal) { match wrap_chgrp(to, &meta, Some(group_id), false, Verbosity::Normal) {
Ok(n) => { Ok(n) => {
if !n.is_empty() { if !n.is_empty() {
show_error!("{}", n); show_error!("{}", n);

View file

@ -49,13 +49,14 @@ fn chgrp<P: AsRef<Path>>(path: P, gid: gid_t, follow: bool) -> IOResult<()> {
pub fn wrap_chgrp<P: AsRef<Path>>( pub fn wrap_chgrp<P: AsRef<Path>>(
path: P, path: P,
meta: &Metadata, meta: &Metadata,
dest_gid: gid_t, dest_gid: Option<gid_t>,
follow: bool, follow: bool,
verbosity: Verbosity, verbosity: Verbosity,
) -> Result<String, String> { ) -> Result<String, String> {
use self::Verbosity::*; use self::Verbosity::*;
let path = path.as_ref(); let path = path.as_ref();
let mut out: String = String::new(); let mut out: String = String::new();
let dest_gid = dest_gid.unwrap_or_else(|| meta.gid());
if let Err(e) = chgrp(path, dest_gid, follow) { if let Err(e) = chgrp(path, dest_gid, follow) {
match verbosity { match verbosity {

View file

@ -228,3 +228,10 @@ fn test_big_h() {
); );
} }
} }
#[test]
fn test_no_change() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("file");
ucmd.arg("").arg(at.plus("file")).succeeds();
}