From 9f97c38d60aa75c1dc05f7d1f2d041e51f2ad8be Mon Sep 17 00:00:00 2001 From: hlsxx Date: Mon, 16 Mar 2026 19:38:36 +0100 Subject: [PATCH 1/2] chown(ref): parse_uid flattened syntax --- src/uu/chown/src/chown.rs | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/uu/chown/src/chown.rs b/src/uu/chown/src/chown.rs index 2928171b4c5..86193dd555b 100644 --- a/src/uu/chown/src/chown.rs +++ b/src/uu/chown/src/chown.rs @@ -155,28 +155,21 @@ fn parse_uid(user: &str, spec: &str, sep: char) -> UResult> { if user.is_empty() { return Ok(None); } + match Passwd::locate(user) { - Ok(u) => Ok(Some(u.uid)), // We have been able to get the uid - Err(_) => { - // we have NOT been able to find the uid - // but we could be in the case where we have user.group - if spec.contains('.') && !spec.contains(':') && sep == ':' { - // but the input contains a '.' but not a ':' - // we might have something like username.groupname - // So, try to parse it this way - parse_spec(spec, '.').map(|(uid, _)| uid) - } else { - // It's possible that the `user` string contains a - // numeric user ID, in which case, we respect that. - match user.parse() { - Ok(uid) => Ok(Some(uid)), - Err(_) => Err(USimpleError::new( - 1, - translate!("chown-error-invalid-user", "user" => spec.quote()), - )), - } - } + Ok(u) => Ok(Some(u.uid)), + // Handle `username.groupname` syntax (e.g. when sep is ':' but spec contains '.') + Err(_) if spec.contains('.') && !spec.contains(':') && sep == ':' => { + parse_spec(spec, '.').map(|(uid, _)| uid) } + // Fallback: `user` string contains a numeric user ID + Err(_) => match user.parse() { + Ok(uid) => Ok(Some(uid)), + Err(_) => Err(USimpleError::new( + 1, + translate!("chown-error-invalid-user", "user" => spec.quote()), + )), + }, } } From 2aae6179f7090da115dcdf8ca84525957196927a Mon Sep 17 00:00:00 2001 From: hlsxx Date: Wed, 18 Mar 2026 08:02:13 +0100 Subject: [PATCH 2/2] chown(ref): parse_uid early returns --- src/uu/chown/src/chown.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/uu/chown/src/chown.rs b/src/uu/chown/src/chown.rs index 86193dd555b..6f0599ce811 100644 --- a/src/uu/chown/src/chown.rs +++ b/src/uu/chown/src/chown.rs @@ -156,21 +156,22 @@ fn parse_uid(user: &str, spec: &str, sep: char) -> UResult> { return Ok(None); } - match Passwd::locate(user) { - Ok(u) => Ok(Some(u.uid)), - // Handle `username.groupname` syntax (e.g. when sep is ':' but spec contains '.') - Err(_) if spec.contains('.') && !spec.contains(':') && sep == ':' => { - parse_spec(spec, '.').map(|(uid, _)| uid) - } - // Fallback: `user` string contains a numeric user ID - Err(_) => match user.parse() { - Ok(uid) => Ok(Some(uid)), - Err(_) => Err(USimpleError::new( - 1, - translate!("chown-error-invalid-user", "user" => spec.quote()), - )), - }, + if let Ok(u) = Passwd::locate(user) { + return Ok(Some(u.uid)); } + + // Handle `username.groupname` syntax (e.g. when sep is ':' but spec contains '.') + if spec.contains('.') && !spec.contains(':') && sep == ':' { + return parse_spec(spec, '.').map(|(uid, _)| uid); + } + + // Fallback: `user` string contains a numeric user ID + user.parse().map(Some).map_err(|_| { + USimpleError::new( + 1, + translate!("chown-error-invalid-user", "user" => spec.quote()), + ) + }) } /// Parses the group string to extract the GID.