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