@@ -1060,17 +1060,18 @@ pub fn getgroups() -> Result<Vec<Gid>> {
1060
1060
// First get the number of groups so we can size our Vec
1061
1061
use std:: ptr;
1062
1062
let ret = unsafe { libc:: getgroups ( 0 , ptr:: null_mut ( ) ) } ;
1063
- let mut size = Errno :: result ( ret) ?;
1064
1063
1065
1064
// Now actually get the groups. We try multiple times in case the number of
1066
1065
// groups has changed since the first call to getgroups() and the buffer is
1067
- // now too small
1068
- let mut groups = Vec :: < Gid > :: with_capacity ( size as usize ) ;
1066
+ // now too small.
1067
+ let mut groups = Vec :: < Gid > :: with_capacity ( Errno :: result ( ret ) ? as usize ) ;
1069
1068
loop {
1070
1069
// FIXME: On the platforms we currently support, the `Gid` struct has
1071
1070
// the same representation in memory as a bare `gid_t`. This is not
1072
1071
// necessarily the case on all Rust platforms, though. See RFC 1785.
1073
- let ret = unsafe { libc:: getgroups ( size, groups. as_mut_ptr ( ) as * mut gid_t ) } ;
1072
+ let ret = unsafe {
1073
+ libc:: getgroups ( groups. capacity ( ) as c_int , groups. as_mut_ptr ( ) as * mut gid_t )
1074
+ } ;
1074
1075
1075
1076
match Errno :: result ( ret) {
1076
1077
Ok ( s) => {
@@ -1083,7 +1084,6 @@ pub fn getgroups() -> Result<Vec<Gid>> {
1083
1084
let cap = groups. capacity ( ) ;
1084
1085
unsafe { groups. set_len ( cap) } ;
1085
1086
groups. reserve ( 1 ) ;
1086
- size = groups. capacity ( ) as c_int ;
1087
1087
} ,
1088
1088
Err ( e) => return Err ( e)
1089
1089
}
@@ -1135,9 +1135,10 @@ pub fn setgroups(groups: &[Gid]) -> Result<()> {
1135
1135
Errno :: result ( res) . map ( |_| ( ) )
1136
1136
}
1137
1137
1138
- /// Calculate the supplementary group access list. Gets the group IDs of all
1139
- /// groups that `user` is a member of. The additional group `group` is also
1140
- /// added to the list.
1138
+ /// Calculate the supplementary group access list.
1139
+ ///
1140
+ /// Gets the group IDs of all groups that `user` is a member of. The additional
1141
+ /// group `group` is also added to the list.
1141
1142
///
1142
1143
/// [Further reading](http://man7.org/linux/man-pages/man3/getgrouplist.3.html)
1143
1144
///
@@ -1173,6 +1174,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
1173
1174
groups. as_mut_ptr ( ) as * mut getgrouplist_group_t ,
1174
1175
& mut ngroups)
1175
1176
} ;
1177
+
1176
1178
// BSD systems only return 0 or -1, Linux returns ngroups on success.
1177
1179
if ret >= 0 {
1178
1180
unsafe { groups. set_len ( ngroups as usize ) } ;
@@ -1199,9 +1201,11 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
1199
1201
}
1200
1202
}
1201
1203
1202
- /// Initialize the supplementary group access list. Sets the supplementary
1203
- /// group IDs for the calling process using all groups that `user` is a member
1204
- /// of. The additional group `group` is also added to the list.
1204
+ /// Initialize the supplementary group access list.
1205
+ ///
1206
+ /// Sets the supplementary group IDs for the calling process using all groups
1207
+ /// that `user` is a member of. The additional group `group` is also added to
1208
+ /// the list.
1205
1209
///
1206
1210
/// [Further reading](http://man7.org/linux/man-pages/man3/initgroups.3.html)
1207
1211
///
@@ -1211,7 +1215,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
1211
1215
/// another user. For example, given the user `www-data`, we could look up the
1212
1216
/// UID and GID for the user in the system's password database (usually found
1213
1217
/// in `/etc/passwd`). If the `www-data` user's UID and GID were `33` and `33`,
1214
- /// respectively, one could switch user as follows:
1218
+ /// respectively, one could switch the user as follows:
1215
1219
/// ```
1216
1220
/// let user = CString::new("www-data").unwrap();
1217
1221
/// let uid = Uid::from_raw(33);
0 commit comments