mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
chown: clean up parse_spec
This commit is contained in:
parent
e985131c83
commit
9e8be3093f
1 changed files with 30 additions and 33 deletions
|
@ -278,42 +278,25 @@ fn parse_spec(spec: &str) -> Result<(Option<u32>, Option<u32>), String> {
|
|||
let usr_only = args.len() == 1 && !args[0].is_empty();
|
||||
let grp_only = args.len() == 2 && args[0].is_empty();
|
||||
let usr_grp = args.len() == 2 && !args[0].is_empty() && !args[1].is_empty();
|
||||
|
||||
let r = if usr_only {
|
||||
(
|
||||
Some(
|
||||
Passwd::locate(args[0])
|
||||
.map_err(|_| format!("invalid user: ‘{}’", spec))?
|
||||
.uid(),
|
||||
),
|
||||
None,
|
||||
)
|
||||
} else if grp_only {
|
||||
(
|
||||
None,
|
||||
Some(
|
||||
Group::locate(args[1])
|
||||
.map_err(|_| format!("invalid group: ‘{}’", spec))?
|
||||
.gid(),
|
||||
),
|
||||
)
|
||||
} else if usr_grp {
|
||||
(
|
||||
Some(
|
||||
Passwd::locate(args[0])
|
||||
.map_err(|_| format!("invalid user: ‘{}’", spec))?
|
||||
.uid(),
|
||||
),
|
||||
Some(
|
||||
Group::locate(args[1])
|
||||
.map_err(|_| format!("invalid group: ‘{}’", spec))?
|
||||
.gid(),
|
||||
),
|
||||
let uid = if usr_only || usr_grp {
|
||||
Some(
|
||||
Passwd::locate(args[0])
|
||||
.map_err(|_| format!("invalid user: ‘{}’", spec))?
|
||||
.uid(),
|
||||
)
|
||||
} else {
|
||||
(None, None)
|
||||
None
|
||||
};
|
||||
Ok(r)
|
||||
let gid = if grp_only || usr_grp {
|
||||
Some(
|
||||
Group::locate(args[1])
|
||||
.map_err(|_| format!("invalid group: ‘{}’", spec))?
|
||||
.gid(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Ok((uid, gid))
|
||||
}
|
||||
|
||||
enum IfFrom {
|
||||
|
@ -502,3 +485,17 @@ impl Chowner {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_spec() {
|
||||
assert_eq!(parse_spec(":"), Ok((None, None)));
|
||||
assert!(parse_spec("::")
|
||||
.err()
|
||||
.unwrap()
|
||||
.starts_with("invalid group: "));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue