This source file includes following definitions.
- mix_random_values
- random_bits
- try_file
- try_dir
- try_nocreate
- gen_tempname_len
- try_tempname_len
- __gen_tempname
- try_tempname
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #if !_LIBC
19 # include <libc-config.h>
20 # include "tempname.h"
21 #endif
22
23 #include <errno.h>
24
25 #include <stdio.h>
26 #ifndef TMP_MAX
27 # define TMP_MAX 238328
28 #endif
29 #ifndef __GT_FILE
30 # define __GT_FILE 0
31 # define __GT_DIR 1
32 # define __GT_NOCREATE 2
33 #endif
34 #if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR \
35 || GT_NOCREATE != __GT_NOCREATE)
36 # error report this to bug-gnulib@gnu.org
37 #endif
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <fcntl.h>
43 #include <stdint.h>
44 #include <sys/random.h>
45 #include <sys/stat.h>
46 #include <time.h>
47
48 #if _LIBC
49 # define struct_stat64 struct __stat64_t64
50 #else
51 # define struct_stat64 struct stat
52 # define __gen_tempname gen_tempname
53 # define __mkdir mkdir
54 # define __open open
55 # define __lstat64_time64(file, buf) lstat (file, buf)
56 # define __getrandom getrandom
57 # define __clock_gettime64 clock_gettime
58 # define __timespec64 timespec
59 #endif
60
61
62
63
64 typedef uint_fast64_t random_value;
65 #define RANDOM_VALUE_MAX UINT_FAST64_MAX
66 #define BASE_62_DIGITS 10
67 #define BASE_62_POWER (62LL * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62)
68
69
70
71
72
73 static random_value
74 mix_random_values (random_value r, random_value s)
75 {
76
77
78
79 return (2862933555777941757 * r + 3037000493) ^ s;
80 }
81
82
83
84
85
86
87
88
89
90
91
92 static bool
93 random_bits (random_value *r, random_value s)
94 {
95
96 if (__getrandom (r, sizeof *r, GRND_NONBLOCK) == sizeof *r)
97 return true;
98
99
100
101
102
103
104
105
106
107 random_value v = s;
108
109 #if _LIBC || (defined CLOCK_REALTIME && HAVE_CLOCK_GETTIME)
110 struct __timespec64 tv;
111 __clock_gettime64 (CLOCK_REALTIME, &tv);
112 v = mix_random_values (v, tv.tv_sec);
113 v = mix_random_values (v, tv.tv_nsec);
114 #endif
115
116 *r = mix_random_values (v, clock ());
117 return false;
118 }
119
120 #if _LIBC
121 static int try_tempname_len (char *, int, void *, int (*) (char *, void *),
122 size_t);
123 #endif
124
125 static int
126 try_file (char *tmpl, void *flags)
127 {
128 int *openflags = flags;
129 return __open (tmpl,
130 (*openflags & ~O_ACCMODE)
131 | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
132 }
133
134 static int
135 try_dir (char *tmpl, _GL_UNUSED void *flags)
136 {
137 return __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
138 }
139
140 static int
141 try_nocreate (char *tmpl, _GL_UNUSED void *flags)
142 {
143 struct_stat64 st;
144
145 if (__lstat64_time64 (tmpl, &st) == 0 || errno == EOVERFLOW)
146 __set_errno (EEXIST);
147 return errno == ENOENT ? 0 : -1;
148 }
149
150
151 static const char letters[] =
152 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 #ifdef _LIBC
169 static
170 #endif
171 int
172 gen_tempname_len (char *tmpl, int suffixlen, int flags, int kind,
173 size_t x_suffix_len)
174 {
175 static int (*const tryfunc[]) (char *, void *) =
176 {
177 [__GT_FILE] = try_file,
178 [__GT_DIR] = try_dir,
179 [__GT_NOCREATE] = try_nocreate
180 };
181 return try_tempname_len (tmpl, suffixlen, &flags, tryfunc[kind],
182 x_suffix_len);
183 }
184
185 #ifdef _LIBC
186 static
187 #endif
188 int
189 try_tempname_len (char *tmpl, int suffixlen, void *args,
190 int (*tryfunc) (char *, void *), size_t x_suffix_len)
191 {
192 size_t len;
193 char *XXXXXX;
194 unsigned int count;
195 int fd = -1;
196 int save_errno = errno;
197
198
199
200
201
202
203
204
205 #define ATTEMPTS_MIN (62 * 62 * 62)
206
207
208
209 #if ATTEMPTS_MIN < TMP_MAX
210 unsigned int attempts = TMP_MAX;
211 #else
212 unsigned int attempts = ATTEMPTS_MIN;
213 #endif
214
215
216 random_value v = 0;
217
218
219
220 random_value vdigbuf;
221 int vdigits = 0;
222
223
224
225 random_value const biased_min
226 = RANDOM_VALUE_MAX - RANDOM_VALUE_MAX % BASE_62_POWER;
227
228 len = strlen (tmpl);
229 if (len < x_suffix_len + suffixlen
230 || strspn (&tmpl[len - x_suffix_len - suffixlen], "X") < x_suffix_len)
231 {
232 __set_errno (EINVAL);
233 return -1;
234 }
235
236
237 XXXXXX = &tmpl[len - x_suffix_len - suffixlen];
238
239 for (count = 0; count < attempts; ++count)
240 {
241 for (size_t i = 0; i < x_suffix_len; i++)
242 {
243 if (vdigits == 0)
244 {
245
246 while (random_bits (&v, v) && biased_min <= v)
247 continue;
248
249 vdigbuf = v;
250 vdigits = BASE_62_DIGITS;
251 }
252
253 XXXXXX[i] = letters[vdigbuf % 62];
254 vdigbuf /= 62;
255 vdigits--;
256 }
257
258 fd = tryfunc (tmpl, args);
259 if (fd >= 0)
260 {
261 __set_errno (save_errno);
262 return fd;
263 }
264 else if (errno != EEXIST)
265 return -1;
266 }
267
268
269 __set_errno (EEXIST);
270 return -1;
271 }
272
273 int
274 __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
275 {
276 return gen_tempname_len (tmpl, suffixlen, flags, kind, 6);
277 }
278
279 #if !_LIBC
280 int
281 try_tempname (char *tmpl, int suffixlen, void *args,
282 int (*tryfunc) (char *, void *))
283 {
284 return try_tempname_len (tmpl, suffixlen, args, tryfunc, 6);
285 }
286 #endif