1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2023 The rust-ggstd authors. All rights reserved.
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
use User;
use crateerrors;
use cratelibc_;
use cratestrings;
// //go:build (cgo || darwin) && !osusergo && unix && !android
// import (
// "fmt"
// "runtime"
// "strconv"
// "strings"
// "syscall"
// "unsafe"
// )
// fn lookupUser(username string) (*User, error) {
// var pwd _C_struct_passwd
// var found bool
// nameC := make([]byte, len(username)+1)
// copy(nameC, username)
// err := retryWithBuffer(userBuffer, fn(buf []byte) syscall.Errno {
// var errno syscall.Errno
// pwd, found, errno = _C_getpwnam_r((*_C_char)(unsafe.Pointer(&nameC[0])),
// (*_C_char)(unsafe.Pointer(&buf[0])), _C_size_t(len(buf)))
// return errno
// })
// if err != nil {
// return nil, fmt.Errorf("user: lookup username %s: %v", username, err)
// }
// if !found {
// return nil, UnknownUserError(username)
// }
// return build_user(&pwd), err
// }
// fn lookupUserId(uid string) -> std::io::Result<User> {
// i, e := strconv.Atoi(uid)
// if e != nil {
// return nil, e
// }
// return lookup_unix_uid(i)
// }
pub
// fn lookupGroup(groupname string) (*Group, error) {
// var grp _C_struct_group
// var found bool
// cname := make([]byte, len(groupname)+1)
// copy(cname, groupname)
// err := retryWithBuffer(groupBuffer, fn(buf []byte) syscall.Errno {
// var errno syscall.Errno
// grp, found, errno = _C_getgrnam_r((*_C_char)(unsafe.Pointer(&cname[0])),
// (*_C_char)(unsafe.Pointer(&buf[0])), _C_size_t(len(buf)))
// return errno
// })
// if err != nil {
// return nil, fmt.Errorf("user: lookup groupname %s: %v", groupname, err)
// }
// if !found {
// return nil, UnknownGroupError(groupname)
// }
// return buildGroup(&grp), nil
// }
// fn lookupGroupId(gid string) (*Group, error) {
// i, e := strconv.Atoi(gid)
// if e != nil {
// return nil, e
// }
// return lookupUnixGid(i)
// }
// fn lookupUnixGid(gid isize) (*Group, error) {
// var grp _C_struct_group
// var found bool
// err := retryWithBuffer(groupBuffer, fn(buf []byte) syscall.Errno {
// var errno syscall.Errno
// grp, found, errno = _C_getgrgid_r(_C_gid_t(gid),
// (*_C_char)(unsafe.Pointer(&buf[0])), _C_size_t(len(buf)))
// return syscall.Errno(errno)
// })
// if err != nil {
// return nil, fmt.Errorf("user: lookup groupid %d: %v", gid, err)
// }
// if !found {
// return nil, UnknownGroupIdError(strconv.Itoa(gid))
// }
// return buildGroup(&grp), nil
// }
// fn buildGroup(grp *_C_struct_group) *Group {
// g := &Group{
// Gid: strconv.Itoa(isize(_C_gr_gid(grp))),
// Name: _C_GoString(_C_gr_name(grp)),
// }
// return g
// }
// type bufferKind _C_int
// var (
// userBuffer = bufferKind(_C__SC_GETPW_R_SIZE_MAX)
// groupBuffer = bufferKind(_C__SC_GETGR_R_SIZE_MAX)
// )
// fn (k bufferKind) initialSize() _C_size_t {
// sz := _C_sysconf(_C_int(k))
// if sz == -1 {
// // DragonFly and FreeBSD do not have _SC_GETPW_R_SIZE_MAX.
// // Additionally, not all Linux systems have it, either. For
// // example, the musl libc returns -1.
// return 1024
// }
// if !is_size_reasonable(int64(sz)) {
// // Truncate. If this truly isn't enough, retryWithBuffer will error on the first run.
// return MAX_BUFFER_SIZE
// }
// return _C_size_t(sz)
// }
// // retryWithBuffer repeatedly calls f(), increasing the size of the
// // buffer each time, until f succeeds, fails with a non-ERANGE error,
// // or the buffer exceeds a reasonable limit.
// fn retryWithBuffer(startSize bufferKind, f fn([]byte) syscall.Errno) error {
// buf := make([]byte, startSize)
// for {
// errno := f(buf)
// if errno == 0 {
// return nil
// } else if runtime.GOOS == "aix" && errno+1 == 0 {
// // On AIX getpwuid_r appears to return -1,
// // not ERANGE, on buffer overflow.
// } else if errno != syscall.ERANGE {
// return errno
// }
// newSize := len(buf) * 2
// if !is_size_reasonable(int64(newSize)) {
// return fmt.Errorf("internal buffer exceeds %d bytes", MAX_BUFFER_SIZE)
// }
// buf = make([]byte, newSize)
// }
// }
const MAX_BUFFER_SIZE: usize = 1 << 20;