mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
getgroups: Reuse buffer, add comment about performance
This commit is contained in:
parent
b125159535
commit
ceff2690d2
1 changed files with 4 additions and 1 deletions
|
@ -72,6 +72,7 @@ extern "C" {
|
||||||
/// > to be used in a further call to getgroups().
|
/// > to be used in a further call to getgroups().
|
||||||
#[cfg(not(target_os = "redox"))]
|
#[cfg(not(target_os = "redox"))]
|
||||||
pub fn get_groups() -> IOResult<Vec<gid_t>> {
|
pub fn get_groups() -> IOResult<Vec<gid_t>> {
|
||||||
|
let mut groups = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
let ngroups = match unsafe { getgroups(0, ptr::null_mut()) } {
|
let ngroups = match unsafe { getgroups(0, ptr::null_mut()) } {
|
||||||
-1 => return Err(IOError::last_os_error()),
|
-1 => return Err(IOError::last_os_error()),
|
||||||
|
@ -80,7 +81,9 @@ pub fn get_groups() -> IOResult<Vec<gid_t>> {
|
||||||
n => n,
|
n => n,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut groups = vec![0; ngroups.try_into().unwrap()];
|
// This is a small buffer, so we can afford to zero-initialize it and
|
||||||
|
// use safe Vec operations
|
||||||
|
groups.resize(ngroups.try_into().unwrap(), 0);
|
||||||
let res = unsafe { getgroups(ngroups, groups.as_mut_ptr()) };
|
let res = unsafe { getgroups(ngroups, groups.as_mut_ptr()) };
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
let err = IOError::last_os_error();
|
let err = IOError::last_os_error();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue