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