Source file
src/os/user/cgo_listgroups_unix.go
1
2
3
4
5
6
7 package user
8
9 import (
10 "fmt"
11 "strconv"
12 "unsafe"
13 )
14
15
19 import "C"
20
21 const maxGroups = 2048
22
23 func listGroups(u *User) ([]string, error) {
24 ug, err := strconv.Atoi(u.Gid)
25 if err != nil {
26 return nil, fmt.Errorf("user: list groups for %s: invalid gid %q", u.Username, u.Gid)
27 }
28 userGID := C.gid_t(ug)
29 nameC := make([]byte, len(u.Username)+1)
30 copy(nameC, u.Username)
31
32 n := C.int(256)
33 gidsC := make([]C.gid_t, n)
34 rv := getGroupList((*C.char)(unsafe.Pointer(&nameC[0])), userGID, &gidsC[0], &n)
35 if rv == -1 {
36
37
38 if err := groupRetry(u.Username, nameC, userGID, &gidsC, &n); err != nil {
39 return nil, err
40 }
41 }
42 gidsC = gidsC[:n]
43 gids := make([]string, 0, n)
44 for _, g := range gidsC[:n] {
45 gids = append(gids, strconv.Itoa(int(g)))
46 }
47 return gids, nil
48 }
49
View as plain text