mirror of
https://github.com/uutils/coreutils
synced 2024-11-17 10:18:11 +00:00
Merge pull request #2379 from youknowone/rotate
get_groups_gnu sort with rotate_right
This commit is contained in:
commit
3e8c009a50
1 changed files with 19 additions and 7 deletions
|
@ -96,15 +96,18 @@ pub fn get_groups() -> IOResult<Vec<gid_t>> {
|
|||
/// the result.)
|
||||
#[cfg(all(unix, feature = "process"))]
|
||||
pub fn get_groups_gnu(arg_id: Option<u32>) -> IOResult<Vec<gid_t>> {
|
||||
let mut groups = get_groups()?;
|
||||
let groups = get_groups()?;
|
||||
let egid = arg_id.unwrap_or_else(crate::features::process::getegid);
|
||||
if !groups.is_empty() && *groups.first().unwrap() == egid {
|
||||
return Ok(groups);
|
||||
} else if let Some(index) = groups.iter().position(|&x| x == egid) {
|
||||
groups.remove(index);
|
||||
Ok(sort_groups(groups, egid))
|
||||
}
|
||||
|
||||
fn sort_groups(mut groups: Vec<gid_t>, egid: gid_t) -> Vec<gid_t> {
|
||||
if let Some(index) = groups.iter().position(|&x| x == egid) {
|
||||
groups[..=index].rotate_right(1);
|
||||
} else {
|
||||
groups.insert(0, egid);
|
||||
Ok(groups)
|
||||
}
|
||||
groups
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
|
@ -309,6 +312,15 @@ pub fn grp2gid(name: &str) -> IOResult<gid_t> {
|
|||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_sort_groups() {
|
||||
assert_eq!(sort_groups(vec![1, 2, 3], 4), vec![4, 1, 2, 3]);
|
||||
assert_eq!(sort_groups(vec![1, 2, 3], 3), vec![3, 1, 2]);
|
||||
assert_eq!(sort_groups(vec![1, 2, 3], 2), vec![2, 1, 3]);
|
||||
assert_eq!(sort_groups(vec![1, 2, 3], 1), vec![1, 2, 3]);
|
||||
assert_eq!(sort_groups(vec![1, 2, 3], 0), vec![0, 1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entries_get_groups_gnu() {
|
||||
if let Ok(mut groups) = get_groups() {
|
||||
|
|
Loading…
Reference in a new issue