@@ -995,17 +995,18 @@ pub fn getgroups() -> Result<Vec<Gid>> {
995
995
// First get the number of groups so we can size our Vec
996
996
use std:: ptr;
997
997
let ret = unsafe { libc:: getgroups ( 0 , ptr:: null_mut ( ) ) } ;
998
- let mut size = Errno :: result ( ret) ?;
999
998
1000
999
// Now actually get the groups. We try multiple times in case the number of
1001
1000
// groups has changed since the first call to getgroups() and the buffer is
1002
- // now too small
1003
- let mut groups = Vec :: < Gid > :: with_capacity ( size as usize ) ;
1001
+ // now too small.
1002
+ let mut groups = Vec :: < Gid > :: with_capacity ( Errno :: result ( ret ) ? as usize ) ;
1004
1003
loop {
1005
1004
// FIXME: On the platforms we currently support, the `Gid` struct has
1006
1005
// the same representation in memory as a bare `gid_t`. This is not
1007
1006
// necessarily the case on all Rust platforms, though. See RFC 1785.
1008
- let ret = unsafe { libc:: getgroups ( size, groups. as_mut_ptr ( ) as * mut gid_t ) } ;
1007
+ let ret = unsafe {
1008
+ libc:: getgroups ( groups. capacity ( ) as c_int , groups. as_mut_ptr ( ) as * mut gid_t )
1009
+ } ;
1009
1010
1010
1011
match Errno :: result ( ret) {
1011
1012
Ok ( s) => {
@@ -1018,7 +1019,6 @@ pub fn getgroups() -> Result<Vec<Gid>> {
1018
1019
let cap = groups. capacity ( ) ;
1019
1020
unsafe { groups. set_len ( cap) } ;
1020
1021
groups. reserve ( 1 ) ;
1021
- size = groups. capacity ( ) as c_int ;
1022
1022
} ,
1023
1023
Err ( e) => return Err ( e)
1024
1024
}
@@ -1070,9 +1070,10 @@ pub fn setgroups(groups: &[Gid]) -> Result<()> {
1070
1070
Errno :: result ( res) . map ( |_| ( ) )
1071
1071
}
1072
1072
1073
- /// Calculate the supplementary group access list. Gets the group IDs of all
1074
- /// groups that `user` is a member of. The additional group `group` is also
1075
- /// added to the list.
1073
+ /// Calculate the supplementary group access list.
1074
+ ///
1075
+ /// Gets the group IDs of all groups that `user` is a member of. The additional
1076
+ /// group `group` is also added to the list.
1076
1077
///
1077
1078
/// [Further reading](http://man7.org/linux/man-pages/man3/getgrouplist.3.html)
1078
1079
///
@@ -1108,6 +1109,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
1108
1109
groups. as_mut_ptr ( ) as * mut getgrouplist_group_t ,
1109
1110
& mut ngroups)
1110
1111
} ;
1112
+
1111
1113
// BSD systems only return 0 or -1, Linux returns ngroups on success.
1112
1114
if ret >= 0 {
1113
1115
unsafe { groups. set_len ( ngroups as usize ) } ;
@@ -1134,9 +1136,11 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
1134
1136
}
1135
1137
}
1136
1138
1137
- /// Initialize the supplementary group access list. Sets the supplementary
1138
- /// group IDs for the calling process using all groups that `user` is a member
1139
- /// of. The additional group `group` is also added to the list.
1139
+ /// Initialize the supplementary group access list.
1140
+ ///
1141
+ /// Sets the supplementary group IDs for the calling process using all groups
1142
+ /// that `user` is a member of. The additional group `group` is also added to
1143
+ /// the list.
1140
1144
///
1141
1145
/// [Further reading](http://man7.org/linux/man-pages/man3/initgroups.3.html)
1142
1146
///
@@ -1146,7 +1150,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
1146
1150
/// another user. For example, given the user `www-data`, we could look up the
1147
1151
/// UID and GID for the user in the system's password database (usually found
1148
1152
/// in `/etc/passwd`). If the `www-data` user's UID and GID were `33` and `33`,
1149
- /// respectively, one could switch user as follows:
1153
+ /// respectively, one could switch the user as follows:
1150
1154
/// ```
1151
1155
/// let user = CString::new("www-data").unwrap();
1152
1156
/// let uid = Uid::from_raw(33);
0 commit comments