From a761144e4fcadb375b68124da7cfec1dbe7b5f3d Mon Sep 17 00:00:00 2001 From: Rajpal_14 Date: Tue, 17 Nov 2026 14:00:48 +0530 Subject: [PATCH 1/5] chown: print parsed IDs and use 'group' when only group is given on broken symlinks (#16500) --- src/uucore/src/lib/features/perms.rs | 55 ++++++++++++++++++++++++++-- tests/by-util/test_chown.rs | 1 - 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index 3ed9b462891..8abcff9d442 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -281,10 +281,59 @@ impl ChownExecutor { let path = root.as_ref(); let Some(meta) = self.obtain_meta(path, self.dereference) else { if self.verbosity.level == VerbosityLevel::Verbose { + let action = if self.verbosity.groups_only + || (self.dest_uid.is_none() && self.dest_gid.is_some()) + { + "group" + } else { + "ownership" + }; + + let dest = if let Some((u, g)) = self.raw_owner.split_once(':') { + let user = if let Some(uid) = self.dest_uid { + if u.starts_with('+') { + uid.to_string() + } else { + u.to_string() + } + } else { + String::new() + }; + let group = if let Some(gid) = self.dest_gid { + if g.starts_with('+') { + gid.to_string() + } else { + g.to_string() + } + } else { + String::new() + }; + if user.is_empty() { + group + } else if group.is_empty() { + user + } else { + format!("{user}:{group}") + } + } else if let Some(uid) = self.dest_uid { + if self.raw_owner.starts_with('+') { + uid.to_string() + } else { + self.raw_owner.clone() + } + } else if let Some(gid) = self.dest_gid { + if self.raw_owner.starts_with('+') { + gid.to_string() + } else { + self.raw_owner.clone() + } + } else { + self.raw_owner.clone() + }; + self.write_verbose_line(&format!( - "failed to change ownership of {} to {}", - path.quote(), - self.raw_owner + "failed to change {action} of {} to {dest}", + path.quote() )); } return 1; diff --git a/tests/by-util/test_chown.rs b/tests/by-util/test_chown.rs index 3cd10be823b..443c68a4810 100644 --- a/tests/by-util/test_chown.rs +++ b/tests/by-util/test_chown.rs @@ -1202,7 +1202,6 @@ fn test_repeated_flags_are_accepted() { #[cfg(unix)] #[test] -#[ignore = "chown -v must print +ID specs as plain IDs"] fn test_chown_verbose_broken_symlink_plus_ids() { let scene = TestScenario::new(util_name!()); scene.fixtures.symlink_file("absent", "ghost"); From 490df93c201600255195767edc127c639b6ab5dc Mon Sep 17 00:00:00 2001 From: Rajpal_14 Date: Tue, 17 Nov 2026 15:00:36 +0530 Subject: [PATCH 2/5] refactor: extract format_target_ownership into helper function --- src/uucore/src/lib/features/perms.rs | 105 ++++++++++++++------------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index 8abcff9d442..1377e5a3120 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -276,61 +276,66 @@ impl ChownExecutor { Ok(()) } + fn format_target_ownership(&self) -> (&'static str, String) { + let action = if self.verbosity.groups_only + || (self.dest_uid.is_none() && self.dest_gid.is_some()) + { + "group" + } else { + "ownership" + }; + + let dest = if let Some((u, g)) = self.raw_owner.split_once(':') { + let user = if let Some(uid) = self.dest_uid { + if u.starts_with('+') { + uid.to_string() + } else { + u.to_string() + } + } else { + String::new() + }; + let group = if let Some(gid) = self.dest_gid { + if g.starts_with('+') { + gid.to_string() + } else { + g.to_string() + } + } else { + String::new() + }; + if user.is_empty() { + group + } else if group.is_empty() { + user + } else { + format!("{user}:{group}") + } + } else if let Some(uid) = self.dest_uid { + if self.raw_owner.starts_with('+') { + uid.to_string() + } else { + self.raw_owner.clone() + } + } else if let Some(gid) = self.dest_gid { + if self.raw_owner.starts_with('+') { + gid.to_string() + } else { + self.raw_owner.clone() + } + } else { + self.raw_owner.clone() + }; + + (action, dest) + } + #[allow(clippy::cognitive_complexity)] fn traverse>(&self, root: P) -> i32 { let path = root.as_ref(); let Some(meta) = self.obtain_meta(path, self.dereference) else { if self.verbosity.level == VerbosityLevel::Verbose { - let action = if self.verbosity.groups_only - || (self.dest_uid.is_none() && self.dest_gid.is_some()) - { - "group" - } else { - "ownership" - }; - - let dest = if let Some((u, g)) = self.raw_owner.split_once(':') { - let user = if let Some(uid) = self.dest_uid { - if u.starts_with('+') { - uid.to_string() - } else { - u.to_string() - } - } else { - String::new() - }; - let group = if let Some(gid) = self.dest_gid { - if g.starts_with('+') { - gid.to_string() - } else { - g.to_string() - } - } else { - String::new() - }; - if user.is_empty() { - group - } else if group.is_empty() { - user - } else { - format!("{user}:{group}") - } - } else if let Some(uid) = self.dest_uid { - if self.raw_owner.starts_with('+') { - uid.to_string() - } else { - self.raw_owner.clone() - } - } else if let Some(gid) = self.dest_gid { - if self.raw_owner.starts_with('+') { - gid.to_string() - } else { - self.raw_owner.clone() - } - } else { - self.raw_owner.clone() - }; - + let (action, dest) = self.format_target_ownership(); self.write_verbose_line(&format!( "failed to change {action} of {} to {dest}", path.quote() From b79c980b3eda0d87e7a586bdebd903ae6045d080 Mon Sep 17 00:00:00 2001 From: Rajpal14 Date: Tue, 17 Nov 2026 15:27:30 +0000 Subject: [PATCH 3/5] refactor(chown): extract resolve_part helper and handle dot separator in format_target_ownership Address two review comments from @sylvestre: - Extract a small resolve_part(spec, id) helper to eliminate four identical `if spec.starts_with('+') { id } else { spec }` blocks. - Handle '.' as a user/group separator in addition to ':' so that `chown -v +1000.+1000 ghost` correctly prints`1000:1000` instead of dropping the group. - Add a test case for the dot-separator form in test_chown_verbose_broken_symlink_plus_ids. --- src/uucore/src/lib/features/perms.rs | 49 +++++++++++++--------------- tests/by-util/test_chown.rs | 6 +++- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index 1377e5a3120..165c48599a6 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -1,4 +1,4 @@ -// This file is part of the uutils coreutils package. +// This file is part of the uutils coreutils package. // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. @@ -277,6 +277,14 @@ impl ChownExecutor { } fn format_target_ownership(&self) -> (&'static str, String) { + // Returns the numeric ID if the spec part starts with '+', otherwise the spec itself. + fn resolve_part(spec: &str, id: Option) -> String { + match id { + Some(n) if spec.starts_with('+') => n.to_string(), + _ => spec.to_string(), + } + } + let action = if self.verbosity.groups_only || (self.dest_uid.is_none() && self.dest_gid.is_some()) { @@ -285,22 +293,19 @@ impl ChownExecutor { "ownership" }; - let dest = if let Some((u, g)) = self.raw_owner.split_once(':') { - let user = if let Some(uid) = self.dest_uid { - if u.starts_with('+') { - uid.to_string() - } else { - u.to_string() - } + // Handle both ':' and '.' as user/group separators (e.g. "user:group" or "user.group"). + let dest = if let Some((u, g)) = self + .raw_owner + .split_once(':') + .or_else(|| self.raw_owner.split_once('.')) + { + let user = if self.dest_uid.is_some() { + resolve_part(u, self.dest_uid) } else { String::new() }; - let group = if let Some(gid) = self.dest_gid { - if g.starts_with('+') { - gid.to_string() - } else { - g.to_string() - } + let group = if self.dest_gid.is_some() { + resolve_part(g, self.dest_gid) } else { String::new() }; @@ -311,18 +316,10 @@ impl ChownExecutor { } else { format!("{user}:{group}") } - } else if let Some(uid) = self.dest_uid { - if self.raw_owner.starts_with('+') { - uid.to_string() - } else { - self.raw_owner.clone() - } - } else if let Some(gid) = self.dest_gid { - if self.raw_owner.starts_with('+') { - gid.to_string() - } else { - self.raw_owner.clone() - } + } else if self.dest_uid.is_some() { + resolve_part(&self.raw_owner, self.dest_uid) + } else if self.dest_gid.is_some() { + resolve_part(&self.raw_owner, self.dest_gid) } else { self.raw_owner.clone() }; diff --git a/tests/by-util/test_chown.rs b/tests/by-util/test_chown.rs index 443c68a4810..9ef3d1463d5 100644 --- a/tests/by-util/test_chown.rs +++ b/tests/by-util/test_chown.rs @@ -1,4 +1,4 @@ -// This file is part of the uutils coreutils package. +// This file is part of the uutils coreutils package. // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. @@ -1216,6 +1216,10 @@ fn test_chown_verbose_broken_symlink_plus_ids() { format!(":+{gid}"), format!("failed to change group of 'ghost' to {gid}\n"), ), + ( + format!("+{uid}.+{gid}"), + format!("failed to change ownership of 'ghost' to {uid}:{gid}\n"), + ), ] { scene .ucmd() From 717f6a173853be911badbe0763568995b2660cd1 Mon Sep 17 00:00:00 2001 From: Rajpal14 Date: Wed, 18 Nov 2026 09:48:11 +0000 Subject: [PATCH 4/5] fix(chown): remove BOM, fix dotted-username bug, clean up resolve_part signature Address three review comments from @sylvestre: - Remove UTF-8 BOM that was inadvertently added to perms.rs and test_chown.rs. - Change resolve_part to take u32 instead of Option and use it as self.dest_uid.map(|id| resolve_part(u, id)).unwrap_or_default() so the call sites are cleaner (suggested at line 302). - Only attempt to split on '.' when dest_gid is set, so that dotted user names like 'john.doe' (a real user with no group) are not incorrectly split at the dot (bug reported at line 300). - Add a colon-separator test case (+UID:GID) alongside the dot-separator case in test_chown_verbose_broken_symlink_plus_ids. --- src/uucore/src/lib/features/perms.rs | 44 +++++++++++++--------------- tests/by-util/test_chown.rs | 6 +++- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index 165c48599a6..aeca278a566 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -1,4 +1,4 @@ -// This file is part of the uutils coreutils package. +// This file is part of the uutils coreutils package. // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. @@ -278,10 +278,11 @@ impl ChownExecutor { fn format_target_ownership(&self) -> (&'static str, String) { // Returns the numeric ID if the spec part starts with '+', otherwise the spec itself. - fn resolve_part(spec: &str, id: Option) -> String { - match id { - Some(n) if spec.starts_with('+') => n.to_string(), - _ => spec.to_string(), + fn resolve_part(spec: &str, id: u32) -> String { + if spec.starts_with('+') { + id.to_string() + } else { + spec.to_string() } } @@ -293,22 +294,17 @@ impl ChownExecutor { "ownership" }; - // Handle both ':' and '.' as user/group separators (e.g. "user:group" or "user.group"). - let dest = if let Some((u, g)) = self - .raw_owner - .split_once(':') - .or_else(|| self.raw_owner.split_once('.')) - { - let user = if self.dest_uid.is_some() { - resolve_part(u, self.dest_uid) - } else { - String::new() - }; - let group = if self.dest_gid.is_some() { - resolve_part(g, self.dest_gid) + // Split on ':' always; only split on '.' when a group is expected, to avoid + // misidentifying dotted user names like "john.doe" as "user.group" specs. + let dest = if let Some((u, g)) = self.raw_owner.split_once(':').or_else(|| { + if self.dest_gid.is_some() { + self.raw_owner.split_once('.') } else { - String::new() - }; + None + } + }) { + let user = self.dest_uid.map(|id| resolve_part(u, id)).unwrap_or_default(); + let group = self.dest_gid.map(|id| resolve_part(g, id)).unwrap_or_default(); if user.is_empty() { group } else if group.is_empty() { @@ -316,10 +312,10 @@ impl ChownExecutor { } else { format!("{user}:{group}") } - } else if self.dest_uid.is_some() { - resolve_part(&self.raw_owner, self.dest_uid) - } else if self.dest_gid.is_some() { - resolve_part(&self.raw_owner, self.dest_gid) + } else if let Some(id) = self.dest_uid { + resolve_part(&self.raw_owner, id) + } else if let Some(id) = self.dest_gid { + resolve_part(&self.raw_owner, id) } else { self.raw_owner.clone() }; diff --git a/tests/by-util/test_chown.rs b/tests/by-util/test_chown.rs index 9ef3d1463d5..bd935b860b5 100644 --- a/tests/by-util/test_chown.rs +++ b/tests/by-util/test_chown.rs @@ -1,4 +1,4 @@ -// This file is part of the uutils coreutils package. +// This file is part of the uutils coreutils package. // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. @@ -1220,6 +1220,10 @@ fn test_chown_verbose_broken_symlink_plus_ids() { format!("+{uid}.+{gid}"), format!("failed to change ownership of 'ghost' to {uid}:{gid}\n"), ), + ( + format!("+{uid}:{gid}"), + format!("failed to change ownership of 'ghost' to {uid}:{gid}\n"), + ), ] { scene .ucmd() From a12cfbbcfa7724e81713139102d57aa7c76a8267 Mon Sep 17 00:00:00 2001 From: Rajpal14 Date: Wed, 18 Nov 2026 17:30:28 +0000 Subject: [PATCH 5/5] style(chown): apply cargo fmt formatting to format_target_ownership Fix rustfmt formatting as requested by @sylvestre: reformat the action= conditional to fit on fewer lines per rustfmt style. --- src/uucore/src/lib/features/perms.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index aeca278a566..8aa51b463c4 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -286,13 +286,12 @@ impl ChownExecutor { } } - let action = if self.verbosity.groups_only - || (self.dest_uid.is_none() && self.dest_gid.is_some()) - { - "group" - } else { - "ownership" - }; + let action = + if self.verbosity.groups_only || (self.dest_uid.is_none() && self.dest_gid.is_some()) { + "group" + } else { + "ownership" + }; // Split on ':' always; only split on '.' when a group is expected, to avoid // misidentifying dotted user names like "john.doe" as "user.group" specs.