]>
Commit | Line | Data |
---|---|---|
112db553 LT |
1 | /* |
2 | * Various trivial helper wrappers around standard functions | |
3 | */ | |
4 | #include "cache.h" | |
b2141fc1 | 5 | #include "config.h" |
112db553 | 6 | |
9a498767 NS |
7 | static intmax_t count_fsync_writeout_only; |
8 | static intmax_t count_fsync_hardware_flush; | |
9 | ||
19d3f228 NS |
10 | #ifdef HAVE_RTLGENRANDOM |
11 | /* This is required to get access to RtlGenRandom. */ | |
12 | #define SystemFunction036 NTAPI SystemFunction036 | |
13 | #include <NTSecAPI.h> | |
14 | #undef SystemFunction036 | |
15 | #endif | |
16 | ||
f8bb1d94 | 17 | static int memory_limit_check(size_t size, int gentle) |
d41489a6 | 18 | { |
9927d962 SP |
19 | static size_t limit = 0; |
20 | if (!limit) { | |
21 | limit = git_env_ulong("GIT_ALLOC_LIMIT", 0); | |
22 | if (!limit) | |
23 | limit = SIZE_MAX; | |
d41489a6 | 24 | } |
f0d89001 | 25 | if (size > limit) { |
f8bb1d94 | 26 | if (gentle) { |
f0d89001 JH |
27 | error("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX, |
28 | (uintmax_t)size, (uintmax_t)limit); | |
f8bb1d94 NTND |
29 | return -1; |
30 | } else | |
f0d89001 JH |
31 | die("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX, |
32 | (uintmax_t)size, (uintmax_t)limit); | |
f8bb1d94 NTND |
33 | } |
34 | return 0; | |
d41489a6 NTND |
35 | } |
36 | ||
112db553 LT |
37 | char *xstrdup(const char *str) |
38 | { | |
39 | char *ret = strdup(str); | |
9827d4c1 JK |
40 | if (!ret) |
41 | die("Out of memory, strdup failed"); | |
112db553 LT |
42 | return ret; |
43 | } | |
44 | ||
f8bb1d94 | 45 | static void *do_xmalloc(size_t size, int gentle) |
112db553 | 46 | { |
d41489a6 NTND |
47 | void *ret; |
48 | ||
f8bb1d94 NTND |
49 | if (memory_limit_check(size, gentle)) |
50 | return NULL; | |
d41489a6 | 51 | ret = malloc(size); |
112db553 LT |
52 | if (!ret && !size) |
53 | ret = malloc(1); | |
54 | if (!ret) { | |
9827d4c1 JK |
55 | if (!gentle) |
56 | die("Out of memory, malloc failed (tried to allocate %lu bytes)", | |
57 | (unsigned long)size); | |
58 | else { | |
59 | error("Out of memory, malloc failed (tried to allocate %lu bytes)", | |
60 | (unsigned long)size); | |
61 | return NULL; | |
f8bb1d94 | 62 | } |
112db553 LT |
63 | } |
64 | #ifdef XMALLOC_POISON | |
65 | memset(ret, 0xA5, size); | |
66 | #endif | |
67 | return ret; | |
68 | } | |
69 | ||
f8bb1d94 NTND |
70 | void *xmalloc(size_t size) |
71 | { | |
72 | return do_xmalloc(size, 0); | |
73 | } | |
74 | ||
75 | static void *do_xmallocz(size_t size, int gentle) | |
5bf9219d IL |
76 | { |
77 | void *ret; | |
f8bb1d94 NTND |
78 | if (unsigned_add_overflows(size, 1)) { |
79 | if (gentle) { | |
80 | error("Data too large to fit into virtual memory space."); | |
81 | return NULL; | |
82 | } else | |
83 | die("Data too large to fit into virtual memory space."); | |
84 | } | |
85 | ret = do_xmalloc(size + 1, gentle); | |
86 | if (ret) | |
87 | ((char*)ret)[size] = 0; | |
5bf9219d IL |
88 | return ret; |
89 | } | |
90 | ||
f8bb1d94 NTND |
91 | void *xmallocz(size_t size) |
92 | { | |
93 | return do_xmallocz(size, 0); | |
94 | } | |
95 | ||
96 | void *xmallocz_gently(size_t size) | |
97 | { | |
98 | return do_xmallocz(size, 1); | |
99 | } | |
100 | ||
112db553 LT |
101 | /* |
102 | * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of | |
103 | * "data" to the allocated memory, zero terminates the allocated memory, | |
104 | * and returns a pointer to the allocated memory. If the allocation fails, | |
105 | * the program dies. | |
106 | */ | |
107 | void *xmemdupz(const void *data, size_t len) | |
108 | { | |
5bf9219d | 109 | return memcpy(xmallocz(len), data, len); |
112db553 LT |
110 | } |
111 | ||
112 | char *xstrndup(const char *str, size_t len) | |
113 | { | |
114 | char *p = memchr(str, '\0', len); | |
115 | return xmemdupz(str, p ? p - str : len); | |
116 | } | |
117 | ||
14570dc6 | 118 | int xstrncmpz(const char *s, const char *t, size_t len) |
119 | { | |
120 | int res = strncmp(s, t, len); | |
121 | if (res) | |
122 | return res; | |
123 | return s[len] == '\0' ? 0 : 1; | |
124 | } | |
125 | ||
112db553 LT |
126 | void *xrealloc(void *ptr, size_t size) |
127 | { | |
d41489a6 NTND |
128 | void *ret; |
129 | ||
6479ea4a JK |
130 | if (!size) { |
131 | free(ptr); | |
132 | return xmalloc(0); | |
133 | } | |
134 | ||
f8bb1d94 | 135 | memory_limit_check(size, 0); |
d41489a6 | 136 | ret = realloc(ptr, size); |
9827d4c1 JK |
137 | if (!ret) |
138 | die("Out of memory, realloc failed"); | |
112db553 LT |
139 | return ret; |
140 | } | |
141 | ||
142 | void *xcalloc(size_t nmemb, size_t size) | |
143 | { | |
d41489a6 NTND |
144 | void *ret; |
145 | ||
e7792a74 JK |
146 | if (unsigned_mult_overflows(nmemb, size)) |
147 | die("data too large to fit into virtual memory space"); | |
148 | ||
f8bb1d94 | 149 | memory_limit_check(size * nmemb, 0); |
d41489a6 | 150 | ret = calloc(nmemb, size); |
112db553 LT |
151 | if (!ret && (!nmemb || !size)) |
152 | ret = calloc(1, 1); | |
9827d4c1 JK |
153 | if (!ret) |
154 | die("Out of memory, calloc failed"); | |
112db553 LT |
155 | return ret; |
156 | } | |
157 | ||
3540c71e ÆAB |
158 | void xsetenv(const char *name, const char *value, int overwrite) |
159 | { | |
160 | if (setenv(name, value, overwrite)) | |
161 | die_errno(_("could not setenv '%s'"), name ? name : "(null)"); | |
162 | } | |
163 | ||
0b6806b9 SP |
164 | /* |
165 | * Limit size of IO chunks, because huge chunks only cause pain. OS X | |
166 | * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in | |
382d20e3 | 167 | * the absence of bugs, large chunks can result in bad latencies when |
0b6806b9 | 168 | * you decide to kill the process. |
a983e6ac JH |
169 | * |
170 | * We pick 8 MiB as our default, but if the platform defines SSIZE_MAX | |
171 | * that is smaller than that, clip it to SSIZE_MAX, as a call to | |
172 | * read(2) or write(2) larger than that is allowed to fail. As the last | |
173 | * resort, we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value" | |
174 | * to override this, if the definition of SSIZE_MAX given by the platform | |
175 | * is broken. | |
0b6806b9 | 176 | */ |
a983e6ac JH |
177 | #ifndef MAX_IO_SIZE |
178 | # define MAX_IO_SIZE_DEFAULT (8*1024*1024) | |
179 | # if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT) | |
180 | # define MAX_IO_SIZE SSIZE_MAX | |
181 | # else | |
182 | # define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT | |
183 | # endif | |
184 | #endif | |
0b6806b9 | 185 | |
3ff53df7 PT |
186 | /** |
187 | * xopen() is the same as open(), but it die()s if the open() fails. | |
188 | */ | |
189 | int xopen(const char *path, int oflag, ...) | |
190 | { | |
191 | mode_t mode = 0; | |
192 | va_list ap; | |
193 | ||
194 | /* | |
195 | * va_arg() will have undefined behavior if the specified type is not | |
196 | * compatible with the argument type. Since integers are promoted to | |
197 | * ints, we fetch the next argument as an int, and then cast it to a | |
198 | * mode_t to avoid undefined behavior. | |
199 | */ | |
200 | va_start(ap, oflag); | |
201 | if (oflag & O_CREAT) | |
202 | mode = va_arg(ap, int); | |
203 | va_end(ap); | |
204 | ||
205 | for (;;) { | |
206 | int fd = open(path, oflag, mode); | |
207 | if (fd >= 0) | |
208 | return fd; | |
209 | if (errno == EINTR) | |
210 | continue; | |
211 | ||
a7439d0f RS |
212 | if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
213 | die_errno(_("unable to create '%s'"), path); | |
214 | else if ((oflag & O_RDWR) == O_RDWR) | |
3ff53df7 PT |
215 | die_errno(_("could not open '%s' for reading and writing"), path); |
216 | else if ((oflag & O_WRONLY) == O_WRONLY) | |
217 | die_errno(_("could not open '%s' for writing"), path); | |
218 | else | |
219 | die_errno(_("could not open '%s' for reading"), path); | |
220 | } | |
221 | } | |
222 | ||
d751dd11 EW |
223 | static int handle_nonblock(int fd, short poll_events, int err) |
224 | { | |
225 | struct pollfd pfd; | |
226 | ||
227 | if (err != EAGAIN && err != EWOULDBLOCK) | |
228 | return 0; | |
229 | ||
230 | pfd.fd = fd; | |
231 | pfd.events = poll_events; | |
232 | ||
233 | /* | |
234 | * no need to check for errors, here; | |
235 | * a subsequent read/write will detect unrecoverable errors | |
236 | */ | |
237 | poll(&pfd, 1, -1); | |
238 | return 1; | |
239 | } | |
240 | ||
112db553 LT |
241 | /* |
242 | * xread() is the same a read(), but it automatically restarts read() | |
243 | * operations with a recoverable error (EAGAIN and EINTR). xread() | |
244 | * DOES NOT GUARANTEE that "len" bytes is read even if the data is available. | |
245 | */ | |
246 | ssize_t xread(int fd, void *buf, size_t len) | |
247 | { | |
248 | ssize_t nr; | |
0b6806b9 | 249 | if (len > MAX_IO_SIZE) |
7cd54d37 | 250 | len = MAX_IO_SIZE; |
112db553 LT |
251 | while (1) { |
252 | nr = read(fd, buf, len); | |
1079c4be SB |
253 | if (nr < 0) { |
254 | if (errno == EINTR) | |
255 | continue; | |
d751dd11 | 256 | if (handle_nonblock(fd, POLLIN, errno)) |
c22f6202 | 257 | continue; |
1079c4be | 258 | } |
112db553 LT |
259 | return nr; |
260 | } | |
261 | } | |
262 | ||
263 | /* | |
264 | * xwrite() is the same a write(), but it automatically restarts write() | |
265 | * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT | |
266 | * GUARANTEE that "len" bytes is written even if the operation is successful. | |
267 | */ | |
268 | ssize_t xwrite(int fd, const void *buf, size_t len) | |
269 | { | |
270 | ssize_t nr; | |
0b6806b9 | 271 | if (len > MAX_IO_SIZE) |
7cd54d37 | 272 | len = MAX_IO_SIZE; |
112db553 LT |
273 | while (1) { |
274 | nr = write(fd, buf, len); | |
ef1cf016 EW |
275 | if (nr < 0) { |
276 | if (errno == EINTR) | |
277 | continue; | |
d751dd11 | 278 | if (handle_nonblock(fd, POLLOUT, errno)) |
ef1cf016 | 279 | continue; |
ef1cf016 EW |
280 | } |
281 | ||
9aa91af0 YM |
282 | return nr; |
283 | } | |
284 | } | |
285 | ||
286 | /* | |
287 | * xpread() is the same as pread(), but it automatically restarts pread() | |
288 | * operations with a recoverable error (EAGAIN and EINTR). xpread() DOES | |
289 | * NOT GUARANTEE that "len" bytes is read even if the data is available. | |
290 | */ | |
291 | ssize_t xpread(int fd, void *buf, size_t len, off_t offset) | |
292 | { | |
293 | ssize_t nr; | |
294 | if (len > MAX_IO_SIZE) | |
295 | len = MAX_IO_SIZE; | |
296 | while (1) { | |
297 | nr = pread(fd, buf, len, offset); | |
298 | if ((nr < 0) && (errno == EAGAIN || errno == EINTR)) | |
112db553 LT |
299 | continue; |
300 | return nr; | |
301 | } | |
302 | } | |
303 | ||
559e840b JH |
304 | ssize_t read_in_full(int fd, void *buf, size_t count) |
305 | { | |
306 | char *p = buf; | |
307 | ssize_t total = 0; | |
308 | ||
309 | while (count > 0) { | |
310 | ssize_t loaded = xread(fd, p, count); | |
56d7c27a JK |
311 | if (loaded < 0) |
312 | return -1; | |
313 | if (loaded == 0) | |
314 | return total; | |
559e840b JH |
315 | count -= loaded; |
316 | p += loaded; | |
317 | total += loaded; | |
318 | } | |
319 | ||
320 | return total; | |
321 | } | |
322 | ||
323 | ssize_t write_in_full(int fd, const void *buf, size_t count) | |
324 | { | |
325 | const char *p = buf; | |
326 | ssize_t total = 0; | |
327 | ||
328 | while (count > 0) { | |
329 | ssize_t written = xwrite(fd, p, count); | |
330 | if (written < 0) | |
331 | return -1; | |
332 | if (!written) { | |
333 | errno = ENOSPC; | |
334 | return -1; | |
335 | } | |
336 | count -= written; | |
337 | p += written; | |
338 | total += written; | |
339 | } | |
340 | ||
341 | return total; | |
342 | } | |
343 | ||
426ddeea YM |
344 | ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset) |
345 | { | |
346 | char *p = buf; | |
347 | ssize_t total = 0; | |
348 | ||
349 | while (count > 0) { | |
350 | ssize_t loaded = xpread(fd, p, count, offset); | |
351 | if (loaded < 0) | |
352 | return -1; | |
353 | if (loaded == 0) | |
354 | return total; | |
355 | count -= loaded; | |
356 | p += loaded; | |
357 | total += loaded; | |
358 | offset += loaded; | |
359 | } | |
360 | ||
361 | return total; | |
362 | } | |
363 | ||
112db553 LT |
364 | int xdup(int fd) |
365 | { | |
366 | int ret = dup(fd); | |
367 | if (ret < 0) | |
d824cbba | 368 | die_errno("dup failed"); |
112db553 LT |
369 | return ret; |
370 | } | |
371 | ||
260eec29 PT |
372 | /** |
373 | * xfopen() is the same as fopen(), but it die()s if the fopen() fails. | |
374 | */ | |
375 | FILE *xfopen(const char *path, const char *mode) | |
376 | { | |
377 | for (;;) { | |
378 | FILE *fp = fopen(path, mode); | |
379 | if (fp) | |
380 | return fp; | |
381 | if (errno == EINTR) | |
382 | continue; | |
383 | ||
384 | if (*mode && mode[1] == '+') | |
385 | die_errno(_("could not open '%s' for reading and writing"), path); | |
386 | else if (*mode == 'w' || *mode == 'a') | |
387 | die_errno(_("could not open '%s' for writing"), path); | |
388 | else | |
389 | die_errno(_("could not open '%s' for reading"), path); | |
390 | } | |
391 | } | |
392 | ||
112db553 LT |
393 | FILE *xfdopen(int fd, const char *mode) |
394 | { | |
395 | FILE *stream = fdopen(fd, mode); | |
afe8a907 | 396 | if (!stream) |
d824cbba | 397 | die_errno("Out of memory? fdopen failed"); |
112db553 LT |
398 | return stream; |
399 | } | |
400 | ||
79d7582e JS |
401 | FILE *fopen_for_writing(const char *path) |
402 | { | |
403 | FILE *ret = fopen(path, "w"); | |
404 | ||
405 | if (!ret && errno == EPERM) { | |
406 | if (!unlink(path)) | |
407 | ret = fopen(path, "w"); | |
408 | else | |
409 | errno = EPERM; | |
410 | } | |
411 | return ret; | |
412 | } | |
413 | ||
382fb07f NTND |
414 | static void warn_on_inaccessible(const char *path) |
415 | { | |
416 | warning_errno(_("unable to access '%s'"), path); | |
417 | } | |
418 | ||
11dc1fcb NTND |
419 | int warn_on_fopen_errors(const char *path) |
420 | { | |
421 | if (errno != ENOENT && errno != ENOTDIR) { | |
422 | warn_on_inaccessible(path); | |
423 | return -1; | |
424 | } | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
e9d983f1 NTND |
429 | FILE *fopen_or_warn(const char *path, const char *mode) |
430 | { | |
431 | FILE *fp = fopen(path, mode); | |
432 | ||
433 | if (fp) | |
434 | return fp; | |
435 | ||
436 | warn_on_fopen_errors(path); | |
437 | return NULL; | |
438 | } | |
439 | ||
eb78e23f | 440 | int xmkstemp(char *filename_template) |
112db553 LT |
441 | { |
442 | int fd; | |
6cf6bb3e | 443 | char origtemplate[PATH_MAX]; |
eb78e23f | 444 | strlcpy(origtemplate, filename_template, sizeof(origtemplate)); |
112db553 | 445 | |
eb78e23f | 446 | fd = mkstemp(filename_template); |
6cf6bb3e AE |
447 | if (fd < 0) { |
448 | int saved_errno = errno; | |
449 | const char *nonrelative_template; | |
450 | ||
eb78e23f BW |
451 | if (strlen(filename_template) != strlen(origtemplate)) |
452 | filename_template = origtemplate; | |
6cf6bb3e | 453 | |
eb78e23f | 454 | nonrelative_template = absolute_path(filename_template); |
6cf6bb3e AE |
455 | errno = saved_errno; |
456 | die_errno("Unable to create temporary file '%s'", | |
457 | nonrelative_template); | |
458 | } | |
112db553 LT |
459 | return fd; |
460 | } | |
39c68542 | 461 | |
33f23936 JN |
462 | /* Adapted from libiberty's mkstemp.c. */ |
463 | ||
464 | #undef TMP_MAX | |
465 | #define TMP_MAX 16384 | |
466 | ||
467 | int git_mkstemps_mode(char *pattern, int suffix_len, int mode) | |
468 | { | |
469 | static const char letters[] = | |
470 | "abcdefghijklmnopqrstuvwxyz" | |
471 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
472 | "0123456789"; | |
53d687bf JK |
473 | static const int num_letters = ARRAY_SIZE(letters) - 1; |
474 | static const char x_pattern[] = "XXXXXX"; | |
475 | static const int num_x = ARRAY_SIZE(x_pattern) - 1; | |
eb78e23f | 476 | char *filename_template; |
33f23936 JN |
477 | size_t len; |
478 | int fd, count; | |
479 | ||
480 | len = strlen(pattern); | |
481 | ||
53d687bf | 482 | if (len < num_x + suffix_len) { |
33f23936 JN |
483 | errno = EINVAL; |
484 | return -1; | |
485 | } | |
486 | ||
53d687bf | 487 | if (strncmp(&pattern[len - num_x - suffix_len], x_pattern, num_x)) { |
33f23936 JN |
488 | errno = EINVAL; |
489 | return -1; | |
490 | } | |
491 | ||
492 | /* | |
493 | * Replace pattern's XXXXXX characters with randomness. | |
494 | * Try TMP_MAX different filenames. | |
495 | */ | |
53d687bf | 496 | filename_template = &pattern[len - num_x - suffix_len]; |
33f23936 | 497 | for (count = 0; count < TMP_MAX; ++count) { |
54a80a9a | 498 | int i; |
47efda96 | 499 | uint64_t v; |
500 | if (csprng_bytes(&v, sizeof(v)) < 0) | |
501 | return error_errno("unable to get random bytes for temporary file"); | |
502 | ||
33f23936 | 503 | /* Fill in the random bits. */ |
53d687bf | 504 | for (i = 0; i < num_x; i++) { |
54a80a9a AH |
505 | filename_template[i] = letters[v % num_letters]; |
506 | v /= num_letters; | |
507 | } | |
33f23936 JN |
508 | |
509 | fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode); | |
a2cb86c1 | 510 | if (fd >= 0) |
33f23936 JN |
511 | return fd; |
512 | /* | |
513 | * Fatal error (EPERM, ENOSPC etc). | |
514 | * It doesn't make sense to loop. | |
515 | */ | |
516 | if (errno != EEXIST) | |
517 | break; | |
33f23936 JN |
518 | } |
519 | /* We return the null string if we can't find a unique file name. */ | |
520 | pattern[0] = '\0'; | |
521 | return -1; | |
522 | } | |
523 | ||
524 | int git_mkstemp_mode(char *pattern, int mode) | |
525 | { | |
526 | /* mkstemp is just mkstemps with no suffix */ | |
527 | return git_mkstemps_mode(pattern, 0, mode); | |
528 | } | |
529 | ||
eb78e23f | 530 | int xmkstemp_mode(char *filename_template, int mode) |
b862b61c MM |
531 | { |
532 | int fd; | |
6cf6bb3e | 533 | char origtemplate[PATH_MAX]; |
eb78e23f | 534 | strlcpy(origtemplate, filename_template, sizeof(origtemplate)); |
b862b61c | 535 | |
eb78e23f | 536 | fd = git_mkstemp_mode(filename_template, mode); |
6cf6bb3e AE |
537 | if (fd < 0) { |
538 | int saved_errno = errno; | |
539 | const char *nonrelative_template; | |
540 | ||
eb78e23f BW |
541 | if (!filename_template[0]) |
542 | filename_template = origtemplate; | |
6cf6bb3e | 543 | |
eb78e23f | 544 | nonrelative_template = absolute_path(filename_template); |
6cf6bb3e AE |
545 | errno = saved_errno; |
546 | die_errno("Unable to create temporary file '%s'", | |
547 | nonrelative_template); | |
548 | } | |
b862b61c MM |
549 | return fd; |
550 | } | |
551 | ||
abf38abe NS |
552 | /* |
553 | * Some platforms return EINTR from fsync. Since fsync is invoked in some | |
554 | * cases by a wrapper that dies on failure, do not expose EINTR to callers. | |
555 | */ | |
556 | static int fsync_loop(int fd) | |
557 | { | |
558 | int err; | |
559 | ||
560 | do { | |
561 | err = fsync(fd); | |
562 | } while (err < 0 && errno == EINTR); | |
563 | return err; | |
564 | } | |
565 | ||
566 | int git_fsync(int fd, enum fsync_action action) | |
567 | { | |
568 | switch (action) { | |
569 | case FSYNC_WRITEOUT_ONLY: | |
9a498767 | 570 | count_fsync_writeout_only += 1; |
abf38abe NS |
571 | |
572 | #ifdef __APPLE__ | |
573 | /* | |
574 | * On macOS, fsync just causes filesystem cache writeback but | |
575 | * does not flush hardware caches. | |
576 | */ | |
577 | return fsync_loop(fd); | |
578 | #endif | |
579 | ||
580 | #ifdef HAVE_SYNC_FILE_RANGE | |
581 | /* | |
582 | * On linux 2.6.17 and above, sync_file_range is the way to | |
583 | * issue a writeback without a hardware flush. An offset of | |
584 | * 0 and size of 0 indicates writeout of the entire file and the | |
585 | * wait flags ensure that all dirty data is written to the disk | |
586 | * (potentially in a disk-side cache) before we continue. | |
587 | */ | |
588 | ||
589 | return sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE | | |
590 | SYNC_FILE_RANGE_WRITE | | |
591 | SYNC_FILE_RANGE_WAIT_AFTER); | |
592 | #endif | |
593 | ||
594 | #ifdef fsync_no_flush | |
595 | return fsync_no_flush(fd); | |
596 | #endif | |
597 | ||
598 | errno = ENOSYS; | |
599 | return -1; | |
600 | ||
601 | case FSYNC_HARDWARE_FLUSH: | |
9a498767 NS |
602 | count_fsync_hardware_flush += 1; |
603 | ||
abf38abe NS |
604 | /* |
605 | * On macOS, a special fcntl is required to really flush the | |
606 | * caches within the storage controller. As of this writing, | |
607 | * this is a very expensive operation on Apple SSDs. | |
608 | */ | |
609 | #ifdef __APPLE__ | |
610 | return fcntl(fd, F_FULLFSYNC); | |
611 | #else | |
612 | return fsync_loop(fd); | |
613 | #endif | |
614 | default: | |
615 | BUG("unexpected git_fsync(%d) call", action); | |
616 | } | |
617 | } | |
618 | ||
9a498767 NS |
619 | void trace_git_fsync_stats(void) |
620 | { | |
621 | trace2_data_intmax("fsync", the_repository, "fsync/writeout-only", count_fsync_writeout_only); | |
622 | trace2_data_intmax("fsync", the_repository, "fsync/hardware-flush", count_fsync_hardware_flush); | |
623 | } | |
624 | ||
10e13ec8 | 625 | static int warn_if_unremovable(const char *op, const char *file, int rc) |
fc71db39 | 626 | { |
1054af7d RS |
627 | int err; |
628 | if (!rc || errno == ENOENT) | |
629 | return 0; | |
630 | err = errno; | |
0a288d1e | 631 | warning_errno("unable to %s '%s'", op, file); |
1054af7d | 632 | errno = err; |
fc71db39 AR |
633 | return rc; |
634 | } | |
635 | ||
9ccc0c08 RS |
636 | int unlink_or_msg(const char *file, struct strbuf *err) |
637 | { | |
638 | int rc = unlink(file); | |
639 | ||
640 | assert(err); | |
641 | ||
642 | if (!rc || errno == ENOENT) | |
643 | return 0; | |
644 | ||
0a288d1e | 645 | strbuf_addf(err, "unable to unlink '%s': %s", |
9ccc0c08 RS |
646 | file, strerror(errno)); |
647 | return -1; | |
648 | } | |
649 | ||
10e13ec8 PC |
650 | int unlink_or_warn(const char *file) |
651 | { | |
652 | return warn_if_unremovable("unlink", file, unlink(file)); | |
653 | } | |
d1723296 PC |
654 | |
655 | int rmdir_or_warn(const char *file) | |
656 | { | |
657 | return warn_if_unremovable("rmdir", file, rmdir(file)); | |
658 | } | |
80d706af PC |
659 | |
660 | int remove_or_warn(unsigned int mode, const char *file) | |
661 | { | |
662 | return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file); | |
663 | } | |
2f705875 | 664 | |
4698c8fe JN |
665 | static int access_error_is_ok(int err, unsigned flag) |
666 | { | |
c7054209 JH |
667 | return (is_missing_file_error(err) || |
668 | ((flag & ACCESS_EACCES_OK) && err == EACCES)); | |
4698c8fe JN |
669 | } |
670 | ||
671 | int access_or_warn(const char *path, int mode, unsigned flag) | |
ba8bd830 JK |
672 | { |
673 | int ret = access(path, mode); | |
4698c8fe | 674 | if (ret && !access_error_is_ok(errno, flag)) |
55b38a48 | 675 | warn_on_inaccessible(path); |
ba8bd830 JK |
676 | return ret; |
677 | } | |
678 | ||
4698c8fe | 679 | int access_or_die(const char *path, int mode, unsigned flag) |
96b9e0e3 JN |
680 | { |
681 | int ret = access(path, mode); | |
4698c8fe | 682 | if (ret && !access_error_is_ok(errno, flag)) |
96b9e0e3 JN |
683 | die_errno(_("unable to access '%s'"), path); |
684 | return ret; | |
685 | } | |
686 | ||
aa14e980 RS |
687 | char *xgetcwd(void) |
688 | { | |
689 | struct strbuf sb = STRBUF_INIT; | |
690 | if (strbuf_getcwd(&sb)) | |
691 | die_errno(_("unable to get current working directory")); | |
692 | return strbuf_detach(&sb, NULL); | |
693 | } | |
316e53e6 | 694 | |
7b03c89e JK |
695 | int xsnprintf(char *dst, size_t max, const char *fmt, ...) |
696 | { | |
697 | va_list ap; | |
698 | int len; | |
699 | ||
700 | va_start(ap, fmt); | |
701 | len = vsnprintf(dst, max, fmt, ap); | |
702 | va_end(ap); | |
703 | ||
704 | if (len < 0) | |
033abf97 | 705 | BUG("your snprintf is broken"); |
7b03c89e | 706 | if (len >= max) |
033abf97 | 707 | BUG("attempt to snprintf into too-small buffer"); |
7b03c89e JK |
708 | return len; |
709 | } | |
710 | ||
52563d7e | 711 | void write_file_buf(const char *path, const char *buf, size_t len) |
316e53e6 | 712 | { |
52563d7e | 713 | int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
06f46f23 | 714 | if (write_in_full(fd, buf, len) < 0) |
0a288d1e | 715 | die_errno(_("could not write to '%s'"), path); |
52563d7e | 716 | if (close(fd)) |
0a288d1e | 717 | die_errno(_("could not close '%s'"), path); |
316e53e6 | 718 | } |
2024d317 | 719 | |
ef22318c | 720 | void write_file(const char *path, const char *fmt, ...) |
12d6ce1d | 721 | { |
12d6ce1d | 722 | va_list params; |
316e53e6 | 723 | struct strbuf sb = STRBUF_INIT; |
12d6ce1d JH |
724 | |
725 | va_start(params, fmt); | |
ef22318c | 726 | strbuf_vaddf(&sb, fmt, params); |
12d6ce1d | 727 | va_end(params); |
12d6ce1d | 728 | |
ef22318c | 729 | strbuf_complete_line(&sb); |
12d6ce1d | 730 | |
52563d7e | 731 | write_file_buf(path, sb.buf, sb.len); |
ef22318c | 732 | strbuf_release(&sb); |
12d6ce1d JH |
733 | } |
734 | ||
2024d317 JS |
735 | void sleep_millisec(int millisec) |
736 | { | |
737 | poll(NULL, 0, millisec); | |
738 | } | |
5781a9a2 DT |
739 | |
740 | int xgethostname(char *buf, size_t len) | |
741 | { | |
742 | /* | |
743 | * If the full hostname doesn't fit in buf, POSIX does not | |
744 | * specify whether the buffer will be null-terminated, so to | |
745 | * be safe, do it ourselves. | |
746 | */ | |
747 | int ret = gethostname(buf, len); | |
748 | if (!ret) | |
749 | buf[len - 1] = 0; | |
750 | return ret; | |
751 | } | |
e3b1e3bd PB |
752 | |
753 | int is_empty_or_missing_file(const char *filename) | |
754 | { | |
755 | struct stat st; | |
756 | ||
757 | if (stat(filename, &st) < 0) { | |
758 | if (errno == ENOENT) | |
759 | return 1; | |
760 | die_errno(_("could not stat %s"), filename); | |
761 | } | |
762 | ||
763 | return !st.st_size; | |
764 | } | |
00611d84 JK |
765 | |
766 | int open_nofollow(const char *path, int flags) | |
767 | { | |
768 | #ifdef O_NOFOLLOW | |
769 | return open(path, flags | O_NOFOLLOW); | |
770 | #else | |
771 | struct stat st; | |
772 | if (lstat(path, &st) < 0) | |
773 | return -1; | |
774 | if (S_ISLNK(st.st_mode)) { | |
775 | errno = ELOOP; | |
776 | return -1; | |
777 | } | |
778 | return open(path, flags); | |
779 | #endif | |
780 | } | |
05cd988d | 781 | |
782 | int csprng_bytes(void *buf, size_t len) | |
783 | { | |
784 | #if defined(HAVE_ARC4RANDOM) || defined(HAVE_ARC4RANDOM_LIBBSD) | |
785 | /* This function never returns an error. */ | |
786 | arc4random_buf(buf, len); | |
787 | return 0; | |
788 | #elif defined(HAVE_GETRANDOM) | |
789 | ssize_t res; | |
790 | char *p = buf; | |
791 | while (len) { | |
792 | res = getrandom(p, len, 0); | |
793 | if (res < 0) | |
794 | return -1; | |
795 | len -= res; | |
796 | p += res; | |
797 | } | |
798 | return 0; | |
799 | #elif defined(HAVE_GETENTROPY) | |
800 | int res; | |
801 | char *p = buf; | |
802 | while (len) { | |
803 | /* getentropy has a maximum size of 256 bytes. */ | |
804 | size_t chunk = len < 256 ? len : 256; | |
805 | res = getentropy(p, chunk); | |
806 | if (res < 0) | |
807 | return -1; | |
808 | len -= chunk; | |
809 | p += chunk; | |
810 | } | |
811 | return 0; | |
812 | #elif defined(HAVE_RTLGENRANDOM) | |
813 | if (!RtlGenRandom(buf, len)) | |
814 | return -1; | |
815 | return 0; | |
816 | #elif defined(HAVE_OPENSSL_CSPRNG) | |
817 | int res = RAND_bytes(buf, len); | |
818 | if (res == 1) | |
819 | return 0; | |
820 | if (res == -1) | |
821 | errno = ENOTSUP; | |
822 | else | |
823 | errno = EIO; | |
824 | return -1; | |
825 | #else | |
826 | ssize_t res; | |
827 | char *p = buf; | |
828 | int fd, err; | |
829 | fd = open("/dev/urandom", O_RDONLY); | |
830 | if (fd < 0) | |
831 | return -1; | |
832 | while (len) { | |
833 | res = xread(fd, p, len); | |
834 | if (res < 0) { | |
835 | err = errno; | |
836 | close(fd); | |
837 | errno = err; | |
838 | return -1; | |
839 | } | |
840 | len -= res; | |
841 | p += res; | |
842 | } | |
843 | close(fd); | |
844 | return 0; | |
845 | #endif | |
846 | } |