Skip to content

Commit 91070e6

Browse files
authored
chown(ref): parse_uid flattened syntax (#11351)
* chown(ref): parse_uid flattened syntax * chown(ref): parse_uid early returns
1 parent 1616015 commit 91070e6

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

src/uu/chown/src/chown.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,23 @@ fn parse_uid(user: &str, spec: &str, sep: char) -> UResult<Option<u32>> {
155155
if user.is_empty() {
156156
return Ok(None);
157157
}
158-
match Passwd::locate(user) {
159-
Ok(u) => Ok(Some(u.uid)), // We have been able to get the uid
160-
Err(_) => {
161-
// we have NOT been able to find the uid
162-
// but we could be in the case where we have user.group
163-
if spec.contains('.') && !spec.contains(':') && sep == ':' {
164-
// but the input contains a '.' but not a ':'
165-
// we might have something like username.groupname
166-
// So, try to parse it this way
167-
parse_spec(spec, '.').map(|(uid, _)| uid)
168-
} else {
169-
// It's possible that the `user` string contains a
170-
// numeric user ID, in which case, we respect that.
171-
match user.parse() {
172-
Ok(uid) => Ok(Some(uid)),
173-
Err(_) => Err(USimpleError::new(
174-
1,
175-
translate!("chown-error-invalid-user", "user" => spec.quote()),
176-
)),
177-
}
178-
}
179-
}
158+
159+
if let Ok(u) = Passwd::locate(user) {
160+
return Ok(Some(u.uid));
161+
}
162+
163+
// Handle `username.groupname` syntax (e.g. when sep is ':' but spec contains '.')
164+
if spec.contains('.') && !spec.contains(':') && sep == ':' {
165+
return parse_spec(spec, '.').map(|(uid, _)| uid);
180166
}
167+
168+
// Fallback: `user` string contains a numeric user ID
169+
user.parse().map(Some).map_err(|_| {
170+
USimpleError::new(
171+
1,
172+
translate!("chown-error-invalid-user", "user" => spec.quote()),
173+
)
174+
})
181175
}
182176

183177
/// Parses the group string to extract the GID.

0 commit comments

Comments
 (0)