Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Various trivial helper wrappers around standard functions |
| 3 | */ |
| 4 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 5 | #include "config.h" |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 6 | |
Neeraj Singh | 9a49876 | 2022-03-30 05:06:40 +0000 | [diff] [blame] | 7 | static intmax_t count_fsync_writeout_only; |
| 8 | static intmax_t count_fsync_hardware_flush; |
| 9 | |
Neeraj Singh | 19d3f22 | 2022-03-10 22:43:19 +0000 | [diff] [blame] | 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 | |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 17 | static int memory_limit_check(size_t size, int gentle) |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 18 | { |
Steffen Prohaska | 9927d96 | 2014-08-26 17:23:22 +0200 | [diff] [blame] | 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; |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 24 | } |
Junio C Hamano | f0d8900 | 2014-10-08 13:05:32 -0700 | [diff] [blame] | 25 | if (size > limit) { |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 26 | if (gentle) { |
Junio C Hamano | f0d8900 | 2014-10-08 13:05:32 -0700 | [diff] [blame] | 27 | error("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX, |
| 28 | (uintmax_t)size, (uintmax_t)limit); |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 29 | return -1; |
| 30 | } else |
Junio C Hamano | f0d8900 | 2014-10-08 13:05:32 -0700 | [diff] [blame] | 31 | die("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX, |
| 32 | (uintmax_t)size, (uintmax_t)limit); |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 33 | } |
| 34 | return 0; |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 35 | } |
| 36 | |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 37 | char *xstrdup(const char *str) |
| 38 | { |
| 39 | char *ret = strdup(str); |
Jeff King | 9827d4c | 2019-08-12 16:50:21 -0400 | [diff] [blame] | 40 | if (!ret) |
| 41 | die("Out of memory, strdup failed"); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 42 | return ret; |
| 43 | } |
| 44 | |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 45 | static void *do_xmalloc(size_t size, int gentle) |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 46 | { |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 47 | void *ret; |
| 48 | |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 49 | if (memory_limit_check(size, gentle)) |
| 50 | return NULL; |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 51 | ret = malloc(size); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 52 | if (!ret && !size) |
| 53 | ret = malloc(1); |
| 54 | if (!ret) { |
Jeff King | 9827d4c | 2019-08-12 16:50:21 -0400 | [diff] [blame] | 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; |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 62 | } |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 63 | } |
| 64 | #ifdef XMALLOC_POISON |
| 65 | memset(ret, 0xA5, size); |
| 66 | #endif |
| 67 | return ret; |
| 68 | } |
| 69 | |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 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) |
Ilari Liusvaara | 5bf9219 | 2010-01-26 20:24:12 +0200 | [diff] [blame] | 76 | { |
| 77 | void *ret; |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 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; |
Ilari Liusvaara | 5bf9219 | 2010-01-26 20:24:12 +0200 | [diff] [blame] | 88 | return ret; |
| 89 | } |
| 90 | |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 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 | |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 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 | { |
Ilari Liusvaara | 5bf9219 | 2010-01-26 20:24:12 +0200 | [diff] [blame] | 109 | return memcpy(xmallocz(len), data, len); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 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 | |
brian m. carlson | 14570dc | 2020-05-25 19:58:50 +0000 | [diff] [blame] | 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 | |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 126 | void *xrealloc(void *ptr, size_t size) |
| 127 | { |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 128 | void *ret; |
| 129 | |
Jeff King | 6479ea4 | 2020-09-02 03:54:39 -0400 | [diff] [blame] | 130 | if (!size) { |
| 131 | free(ptr); |
| 132 | return xmalloc(0); |
| 133 | } |
| 134 | |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 135 | memory_limit_check(size, 0); |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 136 | ret = realloc(ptr, size); |
Jeff King | 9827d4c | 2019-08-12 16:50:21 -0400 | [diff] [blame] | 137 | if (!ret) |
| 138 | die("Out of memory, realloc failed"); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | void *xcalloc(size_t nmemb, size_t size) |
| 143 | { |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 144 | void *ret; |
| 145 | |
Jeff King | e7792a7 | 2016-02-22 17:43:18 -0500 | [diff] [blame] | 146 | if (unsigned_mult_overflows(nmemb, size)) |
| 147 | die("data too large to fit into virtual memory space"); |
| 148 | |
Nguyễn Thái Ngọc Duy | f8bb1d9 | 2014-08-16 10:08:02 +0700 | [diff] [blame] | 149 | memory_limit_check(size * nmemb, 0); |
Nguyễn Thái Ngọc Duy | d41489a | 2012-03-07 17:54:16 +0700 | [diff] [blame] | 150 | ret = calloc(nmemb, size); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 151 | if (!ret && (!nmemb || !size)) |
| 152 | ret = calloc(1, 1); |
Jeff King | 9827d4c | 2019-08-12 16:50:21 -0400 | [diff] [blame] | 153 | if (!ret) |
| 154 | die("Out of memory, calloc failed"); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
Ævar Arnfjörð Bjarmason | 3540c71 | 2021-09-21 15:12:59 +0200 | [diff] [blame] | 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 | |
Paul Tan | 3ff53df | 2015-08-04 21:51:22 +0800 | [diff] [blame] | 164 | /** |
| 165 | * xopen() is the same as open(), but it die()s if the open() fails. |
| 166 | */ |
| 167 | int xopen(const char *path, int oflag, ...) |
| 168 | { |
| 169 | mode_t mode = 0; |
| 170 | va_list ap; |
| 171 | |
| 172 | /* |
| 173 | * va_arg() will have undefined behavior if the specified type is not |
| 174 | * compatible with the argument type. Since integers are promoted to |
| 175 | * ints, we fetch the next argument as an int, and then cast it to a |
| 176 | * mode_t to avoid undefined behavior. |
| 177 | */ |
| 178 | va_start(ap, oflag); |
| 179 | if (oflag & O_CREAT) |
| 180 | mode = va_arg(ap, int); |
| 181 | va_end(ap); |
| 182 | |
| 183 | for (;;) { |
| 184 | int fd = open(path, oflag, mode); |
| 185 | if (fd >= 0) |
| 186 | return fd; |
| 187 | if (errno == EINTR) |
| 188 | continue; |
| 189 | |
René Scharfe | a7439d0 | 2021-08-25 22:14:09 +0200 | [diff] [blame] | 190 | if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
| 191 | die_errno(_("unable to create '%s'"), path); |
| 192 | else if ((oflag & O_RDWR) == O_RDWR) |
Paul Tan | 3ff53df | 2015-08-04 21:51:22 +0800 | [diff] [blame] | 193 | die_errno(_("could not open '%s' for reading and writing"), path); |
| 194 | else if ((oflag & O_WRONLY) == O_WRONLY) |
| 195 | die_errno(_("could not open '%s' for writing"), path); |
| 196 | else |
| 197 | die_errno(_("could not open '%s' for reading"), path); |
| 198 | } |
| 199 | } |
| 200 | |
Eric Wong | d751dd1 | 2016-07-10 08:20:46 +0000 | [diff] [blame] | 201 | static int handle_nonblock(int fd, short poll_events, int err) |
| 202 | { |
| 203 | struct pollfd pfd; |
| 204 | |
| 205 | if (err != EAGAIN && err != EWOULDBLOCK) |
| 206 | return 0; |
| 207 | |
| 208 | pfd.fd = fd; |
| 209 | pfd.events = poll_events; |
| 210 | |
| 211 | /* |
| 212 | * no need to check for errors, here; |
| 213 | * a subsequent read/write will detect unrecoverable errors |
| 214 | */ |
| 215 | poll(&pfd, 1, -1); |
| 216 | return 1; |
| 217 | } |
| 218 | |
Steffen Prohaska | 0b6806b | 2013-08-20 08:43:54 +0200 | [diff] [blame] | 219 | /* |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 220 | * xread() is the same a read(), but it automatically restarts read() |
| 221 | * operations with a recoverable error (EAGAIN and EINTR). xread() |
| 222 | * DOES NOT GUARANTEE that "len" bytes is read even if the data is available. |
| 223 | */ |
| 224 | ssize_t xread(int fd, void *buf, size_t len) |
| 225 | { |
| 226 | ssize_t nr; |
Steffen Prohaska | 0b6806b | 2013-08-20 08:43:54 +0200 | [diff] [blame] | 227 | if (len > MAX_IO_SIZE) |
Denton Liu | 7cd54d3 | 2020-03-27 22:57:34 -0400 | [diff] [blame] | 228 | len = MAX_IO_SIZE; |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 229 | while (1) { |
| 230 | nr = read(fd, buf, len); |
Stefan Beller | 1079c4b | 2015-12-15 16:04:07 -0800 | [diff] [blame] | 231 | if (nr < 0) { |
| 232 | if (errno == EINTR) |
| 233 | continue; |
Eric Wong | d751dd1 | 2016-07-10 08:20:46 +0000 | [diff] [blame] | 234 | if (handle_nonblock(fd, POLLIN, errno)) |
Eric Wong | c22f620 | 2016-06-27 03:56:35 +0000 | [diff] [blame] | 235 | continue; |
Stefan Beller | 1079c4b | 2015-12-15 16:04:07 -0800 | [diff] [blame] | 236 | } |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 237 | return nr; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * xwrite() is the same a write(), but it automatically restarts write() |
| 243 | * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT |
| 244 | * GUARANTEE that "len" bytes is written even if the operation is successful. |
| 245 | */ |
| 246 | ssize_t xwrite(int fd, const void *buf, size_t len) |
| 247 | { |
| 248 | ssize_t nr; |
Steffen Prohaska | 0b6806b | 2013-08-20 08:43:54 +0200 | [diff] [blame] | 249 | if (len > MAX_IO_SIZE) |
Denton Liu | 7cd54d3 | 2020-03-27 22:57:34 -0400 | [diff] [blame] | 250 | len = MAX_IO_SIZE; |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 251 | while (1) { |
| 252 | nr = write(fd, buf, len); |
Eric Wong | ef1cf01 | 2016-06-26 23:21:12 +0000 | [diff] [blame] | 253 | if (nr < 0) { |
| 254 | if (errno == EINTR) |
| 255 | continue; |
Eric Wong | d751dd1 | 2016-07-10 08:20:46 +0000 | [diff] [blame] | 256 | if (handle_nonblock(fd, POLLOUT, errno)) |
Eric Wong | ef1cf01 | 2016-06-26 23:21:12 +0000 | [diff] [blame] | 257 | continue; |
Eric Wong | ef1cf01 | 2016-06-26 23:21:12 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 260 | return nr; |
| 261 | } |
| 262 | } |
| 263 | |
Yiannis Marangos | 9aa91af | 2014-04-10 21:54:12 +0300 | [diff] [blame] | 264 | /* |
| 265 | * xpread() is the same as pread(), but it automatically restarts pread() |
| 266 | * operations with a recoverable error (EAGAIN and EINTR). xpread() DOES |
| 267 | * NOT GUARANTEE that "len" bytes is read even if the data is available. |
| 268 | */ |
| 269 | ssize_t xpread(int fd, void *buf, size_t len, off_t offset) |
| 270 | { |
| 271 | ssize_t nr; |
| 272 | if (len > MAX_IO_SIZE) |
| 273 | len = MAX_IO_SIZE; |
| 274 | while (1) { |
| 275 | nr = pread(fd, buf, len, offset); |
| 276 | if ((nr < 0) && (errno == EAGAIN || errno == EINTR)) |
| 277 | continue; |
| 278 | return nr; |
| 279 | } |
| 280 | } |
| 281 | |
Junio C Hamano | 559e840 | 2008-07-20 16:13:05 -0700 | [diff] [blame] | 282 | ssize_t read_in_full(int fd, void *buf, size_t count) |
| 283 | { |
| 284 | char *p = buf; |
| 285 | ssize_t total = 0; |
| 286 | |
| 287 | while (count > 0) { |
| 288 | ssize_t loaded = xread(fd, p, count); |
Jeff King | 56d7c27 | 2011-05-26 12:30:27 -0400 | [diff] [blame] | 289 | if (loaded < 0) |
| 290 | return -1; |
| 291 | if (loaded == 0) |
| 292 | return total; |
Junio C Hamano | 559e840 | 2008-07-20 16:13:05 -0700 | [diff] [blame] | 293 | count -= loaded; |
| 294 | p += loaded; |
| 295 | total += loaded; |
| 296 | } |
| 297 | |
| 298 | return total; |
| 299 | } |
| 300 | |
| 301 | ssize_t write_in_full(int fd, const void *buf, size_t count) |
| 302 | { |
| 303 | const char *p = buf; |
| 304 | ssize_t total = 0; |
| 305 | |
| 306 | while (count > 0) { |
| 307 | ssize_t written = xwrite(fd, p, count); |
| 308 | if (written < 0) |
| 309 | return -1; |
| 310 | if (!written) { |
| 311 | errno = ENOSPC; |
| 312 | return -1; |
| 313 | } |
| 314 | count -= written; |
| 315 | p += written; |
| 316 | total += written; |
| 317 | } |
| 318 | |
| 319 | return total; |
| 320 | } |
| 321 | |
Yiannis Marangos | 426ddee | 2014-04-10 21:31:21 +0300 | [diff] [blame] | 322 | ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset) |
| 323 | { |
| 324 | char *p = buf; |
| 325 | ssize_t total = 0; |
| 326 | |
| 327 | while (count > 0) { |
| 328 | ssize_t loaded = xpread(fd, p, count, offset); |
| 329 | if (loaded < 0) |
| 330 | return -1; |
| 331 | if (loaded == 0) |
| 332 | return total; |
| 333 | count -= loaded; |
| 334 | p += loaded; |
| 335 | total += loaded; |
| 336 | offset += loaded; |
| 337 | } |
| 338 | |
| 339 | return total; |
| 340 | } |
| 341 | |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 342 | int xdup(int fd) |
| 343 | { |
| 344 | int ret = dup(fd); |
| 345 | if (ret < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 346 | die_errno("dup failed"); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 347 | return ret; |
| 348 | } |
| 349 | |
Paul Tan | 260eec2 | 2015-08-04 21:51:23 +0800 | [diff] [blame] | 350 | /** |
| 351 | * xfopen() is the same as fopen(), but it die()s if the fopen() fails. |
| 352 | */ |
| 353 | FILE *xfopen(const char *path, const char *mode) |
| 354 | { |
| 355 | for (;;) { |
| 356 | FILE *fp = fopen(path, mode); |
| 357 | if (fp) |
| 358 | return fp; |
| 359 | if (errno == EINTR) |
| 360 | continue; |
| 361 | |
| 362 | if (*mode && mode[1] == '+') |
| 363 | die_errno(_("could not open '%s' for reading and writing"), path); |
| 364 | else if (*mode == 'w' || *mode == 'a') |
| 365 | die_errno(_("could not open '%s' for writing"), path); |
| 366 | else |
| 367 | die_errno(_("could not open '%s' for reading"), path); |
| 368 | } |
| 369 | } |
| 370 | |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 371 | FILE *xfdopen(int fd, const char *mode) |
| 372 | { |
| 373 | FILE *stream = fdopen(fd, mode); |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 374 | if (!stream) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 375 | die_errno("Out of memory? fdopen failed"); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 376 | return stream; |
| 377 | } |
| 378 | |
Johannes Schindelin | 79d7582 | 2016-01-06 14:09:43 +0100 | [diff] [blame] | 379 | FILE *fopen_for_writing(const char *path) |
| 380 | { |
| 381 | FILE *ret = fopen(path, "w"); |
| 382 | |
| 383 | if (!ret && errno == EPERM) { |
| 384 | if (!unlink(path)) |
| 385 | ret = fopen(path, "w"); |
| 386 | else |
| 387 | errno = EPERM; |
| 388 | } |
| 389 | return ret; |
| 390 | } |
| 391 | |
Nguyễn Thái Ngọc Duy | 382fb07 | 2017-05-08 17:40:37 +0700 | [diff] [blame] | 392 | static void warn_on_inaccessible(const char *path) |
| 393 | { |
| 394 | warning_errno(_("unable to access '%s'"), path); |
| 395 | } |
| 396 | |
Nguyễn Thái Ngọc Duy | 11dc1fc | 2017-05-03 17:16:49 +0700 | [diff] [blame] | 397 | int warn_on_fopen_errors(const char *path) |
| 398 | { |
| 399 | if (errno != ENOENT && errno != ENOTDIR) { |
| 400 | warn_on_inaccessible(path); |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 407 | FILE *fopen_or_warn(const char *path, const char *mode) |
| 408 | { |
| 409 | FILE *fp = fopen(path, mode); |
| 410 | |
| 411 | if (fp) |
| 412 | return fp; |
| 413 | |
| 414 | warn_on_fopen_errors(path); |
| 415 | return NULL; |
| 416 | } |
| 417 | |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 418 | int xmkstemp(char *filename_template) |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 419 | { |
| 420 | int fd; |
Arnout Engelen | 6cf6bb3 | 2010-12-18 22:28:00 +0100 | [diff] [blame] | 421 | char origtemplate[PATH_MAX]; |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 422 | strlcpy(origtemplate, filename_template, sizeof(origtemplate)); |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 423 | |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 424 | fd = mkstemp(filename_template); |
Arnout Engelen | 6cf6bb3 | 2010-12-18 22:28:00 +0100 | [diff] [blame] | 425 | if (fd < 0) { |
| 426 | int saved_errno = errno; |
| 427 | const char *nonrelative_template; |
| 428 | |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 429 | if (strlen(filename_template) != strlen(origtemplate)) |
| 430 | filename_template = origtemplate; |
Arnout Engelen | 6cf6bb3 | 2010-12-18 22:28:00 +0100 | [diff] [blame] | 431 | |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 432 | nonrelative_template = absolute_path(filename_template); |
Arnout Engelen | 6cf6bb3 | 2010-12-18 22:28:00 +0100 | [diff] [blame] | 433 | errno = saved_errno; |
| 434 | die_errno("Unable to create temporary file '%s'", |
| 435 | nonrelative_template); |
| 436 | } |
Linus Torvalds | 112db55 | 2008-06-22 12:19:25 -0700 | [diff] [blame] | 437 | return fd; |
| 438 | } |
Linus Torvalds | 39c6854 | 2009-01-07 19:54:47 -0800 | [diff] [blame] | 439 | |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 440 | /* Adapted from libiberty's mkstemp.c. */ |
| 441 | |
| 442 | #undef TMP_MAX |
| 443 | #define TMP_MAX 16384 |
| 444 | |
| 445 | int git_mkstemps_mode(char *pattern, int suffix_len, int mode) |
| 446 | { |
| 447 | static const char letters[] = |
| 448 | "abcdefghijklmnopqrstuvwxyz" |
| 449 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 450 | "0123456789"; |
Jeff King | 53d687b | 2019-10-02 11:32:07 -0400 | [diff] [blame] | 451 | static const int num_letters = ARRAY_SIZE(letters) - 1; |
| 452 | static const char x_pattern[] = "XXXXXX"; |
| 453 | static const int num_x = ARRAY_SIZE(x_pattern) - 1; |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 454 | char *filename_template; |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 455 | size_t len; |
| 456 | int fd, count; |
| 457 | |
| 458 | len = strlen(pattern); |
| 459 | |
Jeff King | 53d687b | 2019-10-02 11:32:07 -0400 | [diff] [blame] | 460 | if (len < num_x + suffix_len) { |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 461 | errno = EINVAL; |
| 462 | return -1; |
| 463 | } |
| 464 | |
Jeff King | 53d687b | 2019-10-02 11:32:07 -0400 | [diff] [blame] | 465 | if (strncmp(&pattern[len - num_x - suffix_len], x_pattern, num_x)) { |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 466 | errno = EINVAL; |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Replace pattern's XXXXXX characters with randomness. |
| 472 | * Try TMP_MAX different filenames. |
| 473 | */ |
Jeff King | 53d687b | 2019-10-02 11:32:07 -0400 | [diff] [blame] | 474 | filename_template = &pattern[len - num_x - suffix_len]; |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 475 | for (count = 0; count < TMP_MAX; ++count) { |
Alex Henrie | 54a80a9 | 2019-09-30 20:29:36 -0600 | [diff] [blame] | 476 | int i; |
brian m. carlson | 47efda9 | 2022-01-17 21:56:17 +0000 | [diff] [blame] | 477 | uint64_t v; |
| 478 | if (csprng_bytes(&v, sizeof(v)) < 0) |
| 479 | return error_errno("unable to get random bytes for temporary file"); |
| 480 | |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 481 | /* Fill in the random bits. */ |
Jeff King | 53d687b | 2019-10-02 11:32:07 -0400 | [diff] [blame] | 482 | for (i = 0; i < num_x; i++) { |
Alex Henrie | 54a80a9 | 2019-09-30 20:29:36 -0600 | [diff] [blame] | 483 | filename_template[i] = letters[v % num_letters]; |
| 484 | v /= num_letters; |
| 485 | } |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 486 | |
| 487 | fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode); |
Dale R. Worley | a2cb86c | 2013-07-12 10:58:35 +0200 | [diff] [blame] | 488 | if (fd >= 0) |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 489 | return fd; |
| 490 | /* |
| 491 | * Fatal error (EPERM, ENOSPC etc). |
| 492 | * It doesn't make sense to loop. |
| 493 | */ |
| 494 | if (errno != EEXIST) |
| 495 | break; |
Jonathan Nieder | 33f2393 | 2010-11-06 06:46:31 -0500 | [diff] [blame] | 496 | } |
| 497 | /* We return the null string if we can't find a unique file name. */ |
| 498 | pattern[0] = '\0'; |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | int git_mkstemp_mode(char *pattern, int mode) |
| 503 | { |
| 504 | /* mkstemp is just mkstemps with no suffix */ |
| 505 | return git_mkstemps_mode(pattern, 0, mode); |
| 506 | } |
| 507 | |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 508 | int xmkstemp_mode(char *filename_template, int mode) |
Matthieu Moy | b862b61 | 2010-02-22 23:32:13 +0100 | [diff] [blame] | 509 | { |
| 510 | int fd; |
Arnout Engelen | 6cf6bb3 | 2010-12-18 22:28:00 +0100 | [diff] [blame] | 511 | char origtemplate[PATH_MAX]; |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 512 | strlcpy(origtemplate, filename_template, sizeof(origtemplate)); |
Matthieu Moy | b862b61 | 2010-02-22 23:32:13 +0100 | [diff] [blame] | 513 | |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 514 | fd = git_mkstemp_mode(filename_template, mode); |
Arnout Engelen | 6cf6bb3 | 2010-12-18 22:28:00 +0100 | [diff] [blame] | 515 | if (fd < 0) { |
| 516 | int saved_errno = errno; |
| 517 | const char *nonrelative_template; |
| 518 | |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 519 | if (!filename_template[0]) |
| 520 | filename_template = origtemplate; |
Arnout Engelen | 6cf6bb3 | 2010-12-18 22:28:00 +0100 | [diff] [blame] | 521 | |
Brandon Williams | eb78e23 | 2018-02-14 10:59:56 -0800 | [diff] [blame] | 522 | nonrelative_template = absolute_path(filename_template); |
Arnout Engelen | 6cf6bb3 | 2010-12-18 22:28:00 +0100 | [diff] [blame] | 523 | errno = saved_errno; |
| 524 | die_errno("Unable to create temporary file '%s'", |
| 525 | nonrelative_template); |
| 526 | } |
Matthieu Moy | b862b61 | 2010-02-22 23:32:13 +0100 | [diff] [blame] | 527 | return fd; |
| 528 | } |
| 529 | |
Neeraj Singh | abf38ab | 2022-03-10 22:43:20 +0000 | [diff] [blame] | 530 | /* |
| 531 | * Some platforms return EINTR from fsync. Since fsync is invoked in some |
| 532 | * cases by a wrapper that dies on failure, do not expose EINTR to callers. |
| 533 | */ |
| 534 | static int fsync_loop(int fd) |
| 535 | { |
| 536 | int err; |
| 537 | |
| 538 | do { |
| 539 | err = fsync(fd); |
| 540 | } while (err < 0 && errno == EINTR); |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | int git_fsync(int fd, enum fsync_action action) |
| 545 | { |
| 546 | switch (action) { |
| 547 | case FSYNC_WRITEOUT_ONLY: |
Neeraj Singh | 9a49876 | 2022-03-30 05:06:40 +0000 | [diff] [blame] | 548 | count_fsync_writeout_only += 1; |
Neeraj Singh | abf38ab | 2022-03-10 22:43:20 +0000 | [diff] [blame] | 549 | |
| 550 | #ifdef __APPLE__ |
| 551 | /* |
| 552 | * On macOS, fsync just causes filesystem cache writeback but |
| 553 | * does not flush hardware caches. |
| 554 | */ |
| 555 | return fsync_loop(fd); |
| 556 | #endif |
| 557 | |
| 558 | #ifdef HAVE_SYNC_FILE_RANGE |
| 559 | /* |
| 560 | * On linux 2.6.17 and above, sync_file_range is the way to |
| 561 | * issue a writeback without a hardware flush. An offset of |
| 562 | * 0 and size of 0 indicates writeout of the entire file and the |
| 563 | * wait flags ensure that all dirty data is written to the disk |
| 564 | * (potentially in a disk-side cache) before we continue. |
| 565 | */ |
| 566 | |
| 567 | return sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE | |
| 568 | SYNC_FILE_RANGE_WRITE | |
| 569 | SYNC_FILE_RANGE_WAIT_AFTER); |
| 570 | #endif |
| 571 | |
| 572 | #ifdef fsync_no_flush |
| 573 | return fsync_no_flush(fd); |
| 574 | #endif |
| 575 | |
| 576 | errno = ENOSYS; |
| 577 | return -1; |
| 578 | |
| 579 | case FSYNC_HARDWARE_FLUSH: |
Neeraj Singh | 9a49876 | 2022-03-30 05:06:40 +0000 | [diff] [blame] | 580 | count_fsync_hardware_flush += 1; |
| 581 | |
Neeraj Singh | abf38ab | 2022-03-10 22:43:20 +0000 | [diff] [blame] | 582 | /* |
| 583 | * On macOS, a special fcntl is required to really flush the |
| 584 | * caches within the storage controller. As of this writing, |
| 585 | * this is a very expensive operation on Apple SSDs. |
| 586 | */ |
| 587 | #ifdef __APPLE__ |
| 588 | return fcntl(fd, F_FULLFSYNC); |
| 589 | #else |
| 590 | return fsync_loop(fd); |
| 591 | #endif |
| 592 | default: |
| 593 | BUG("unexpected git_fsync(%d) call", action); |
| 594 | } |
| 595 | } |
| 596 | |
Neeraj Singh | 9a49876 | 2022-03-30 05:06:40 +0000 | [diff] [blame] | 597 | void trace_git_fsync_stats(void) |
| 598 | { |
| 599 | trace2_data_intmax("fsync", the_repository, "fsync/writeout-only", count_fsync_writeout_only); |
| 600 | trace2_data_intmax("fsync", the_repository, "fsync/hardware-flush", count_fsync_hardware_flush); |
| 601 | } |
| 602 | |
Peter Collingbourne | 10e13ec | 2010-03-26 15:25:32 +0000 | [diff] [blame] | 603 | static int warn_if_unremovable(const char *op, const char *file, int rc) |
Alex Riesen | fc71db3 | 2009-04-29 23:21:46 +0200 | [diff] [blame] | 604 | { |
Ronnie Sahlberg | 1054af7 | 2014-07-16 11:01:18 -0700 | [diff] [blame] | 605 | int err; |
| 606 | if (!rc || errno == ENOENT) |
| 607 | return 0; |
| 608 | err = errno; |
Simon Ruderich | 0a288d1 | 2017-11-01 15:44:44 +0100 | [diff] [blame] | 609 | warning_errno("unable to %s '%s'", op, file); |
Ronnie Sahlberg | 1054af7 | 2014-07-16 11:01:18 -0700 | [diff] [blame] | 610 | errno = err; |
Alex Riesen | fc71db3 | 2009-04-29 23:21:46 +0200 | [diff] [blame] | 611 | return rc; |
| 612 | } |
| 613 | |
Ronnie Sahlberg | 9ccc0c0 | 2014-07-16 11:20:36 -0700 | [diff] [blame] | 614 | int unlink_or_msg(const char *file, struct strbuf *err) |
| 615 | { |
| 616 | int rc = unlink(file); |
| 617 | |
| 618 | assert(err); |
| 619 | |
| 620 | if (!rc || errno == ENOENT) |
| 621 | return 0; |
| 622 | |
Simon Ruderich | 0a288d1 | 2017-11-01 15:44:44 +0100 | [diff] [blame] | 623 | strbuf_addf(err, "unable to unlink '%s': %s", |
Ronnie Sahlberg | 9ccc0c0 | 2014-07-16 11:20:36 -0700 | [diff] [blame] | 624 | file, strerror(errno)); |
| 625 | return -1; |
| 626 | } |
| 627 | |
Peter Collingbourne | 10e13ec | 2010-03-26 15:25:32 +0000 | [diff] [blame] | 628 | int unlink_or_warn(const char *file) |
| 629 | { |
| 630 | return warn_if_unremovable("unlink", file, unlink(file)); |
| 631 | } |
Peter Collingbourne | d172329 | 2010-03-26 15:25:33 +0000 | [diff] [blame] | 632 | |
| 633 | int rmdir_or_warn(const char *file) |
| 634 | { |
| 635 | return warn_if_unremovable("rmdir", file, rmdir(file)); |
| 636 | } |
Peter Collingbourne | 80d706a | 2010-03-26 15:25:34 +0000 | [diff] [blame] | 637 | |
| 638 | int remove_or_warn(unsigned int mode, const char *file) |
| 639 | { |
| 640 | return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file); |
| 641 | } |
Jeff King | 2f70587 | 2012-05-21 19:10:20 -0400 | [diff] [blame] | 642 | |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 643 | static int access_error_is_ok(int err, unsigned flag) |
| 644 | { |
Junio C Hamano | c705420 | 2017-05-30 09:23:33 +0900 | [diff] [blame] | 645 | return (is_missing_file_error(err) || |
| 646 | ((flag & ACCESS_EACCES_OK) && err == EACCES)); |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | int access_or_warn(const char *path, int mode, unsigned flag) |
Jeff King | ba8bd83 | 2012-08-21 02:10:59 -0400 | [diff] [blame] | 650 | { |
| 651 | int ret = access(path, mode); |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 652 | if (ret && !access_error_is_ok(errno, flag)) |
Junio C Hamano | 55b38a4 | 2012-08-21 14:52:07 -0700 | [diff] [blame] | 653 | warn_on_inaccessible(path); |
Jeff King | ba8bd83 | 2012-08-21 02:10:59 -0400 | [diff] [blame] | 654 | return ret; |
| 655 | } |
| 656 | |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 657 | int access_or_die(const char *path, int mode, unsigned flag) |
Jonathan Nieder | 96b9e0e | 2012-10-13 17:04:02 -0700 | [diff] [blame] | 658 | { |
| 659 | int ret = access(path, mode); |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 660 | if (ret && !access_error_is_ok(errno, flag)) |
Jonathan Nieder | 96b9e0e | 2012-10-13 17:04:02 -0700 | [diff] [blame] | 661 | die_errno(_("unable to access '%s'"), path); |
| 662 | return ret; |
| 663 | } |
| 664 | |
René Scharfe | aa14e98 | 2014-07-28 20:29:50 +0200 | [diff] [blame] | 665 | char *xgetcwd(void) |
| 666 | { |
| 667 | struct strbuf sb = STRBUF_INIT; |
| 668 | if (strbuf_getcwd(&sb)) |
| 669 | die_errno(_("unable to get current working directory")); |
| 670 | return strbuf_detach(&sb, NULL); |
| 671 | } |
Nguyễn Thái Ngọc Duy | 316e53e | 2014-11-30 15:24:45 +0700 | [diff] [blame] | 672 | |
Jeff King | 7b03c89 | 2015-09-24 17:05:37 -0400 | [diff] [blame] | 673 | int xsnprintf(char *dst, size_t max, const char *fmt, ...) |
| 674 | { |
| 675 | va_list ap; |
| 676 | int len; |
| 677 | |
| 678 | va_start(ap, fmt); |
| 679 | len = vsnprintf(dst, max, fmt, ap); |
| 680 | va_end(ap); |
| 681 | |
| 682 | if (len < 0) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 683 | BUG("your snprintf is broken"); |
Jeff King | 7b03c89 | 2015-09-24 17:05:37 -0400 | [diff] [blame] | 684 | if (len >= max) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 685 | BUG("attempt to snprintf into too-small buffer"); |
Jeff King | 7b03c89 | 2015-09-24 17:05:37 -0400 | [diff] [blame] | 686 | return len; |
| 687 | } |
| 688 | |
Jeff King | 52563d7 | 2016-07-08 05:12:22 -0400 | [diff] [blame] | 689 | void write_file_buf(const char *path, const char *buf, size_t len) |
Nguyễn Thái Ngọc Duy | 316e53e | 2014-11-30 15:24:45 +0700 | [diff] [blame] | 690 | { |
Jeff King | 52563d7 | 2016-07-08 05:12:22 -0400 | [diff] [blame] | 691 | int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Jeff King | 06f46f2 | 2017-09-13 13:16:03 -0400 | [diff] [blame] | 692 | if (write_in_full(fd, buf, len) < 0) |
Simon Ruderich | 0a288d1 | 2017-11-01 15:44:44 +0100 | [diff] [blame] | 693 | die_errno(_("could not write to '%s'"), path); |
Jeff King | 52563d7 | 2016-07-08 05:12:22 -0400 | [diff] [blame] | 694 | if (close(fd)) |
Simon Ruderich | 0a288d1 | 2017-11-01 15:44:44 +0100 | [diff] [blame] | 695 | die_errno(_("could not close '%s'"), path); |
Jeff King | 52563d7 | 2016-07-08 05:12:22 -0400 | [diff] [blame] | 696 | } |
| 697 | |
Jeff King | ef22318 | 2016-07-08 05:09:34 -0400 | [diff] [blame] | 698 | void write_file(const char *path, const char *fmt, ...) |
Nguyễn Thái Ngọc Duy | 316e53e | 2014-11-30 15:24:45 +0700 | [diff] [blame] | 699 | { |
Jeff King | ef22318 | 2016-07-08 05:09:34 -0400 | [diff] [blame] | 700 | va_list params; |
Nguyễn Thái Ngọc Duy | 316e53e | 2014-11-30 15:24:45 +0700 | [diff] [blame] | 701 | struct strbuf sb = STRBUF_INIT; |
Jeff King | ef22318 | 2016-07-08 05:09:34 -0400 | [diff] [blame] | 702 | |
| 703 | va_start(params, fmt); |
Nguyễn Thái Ngọc Duy | 316e53e | 2014-11-30 15:24:45 +0700 | [diff] [blame] | 704 | strbuf_vaddf(&sb, fmt, params); |
Jeff King | ef22318 | 2016-07-08 05:09:34 -0400 | [diff] [blame] | 705 | va_end(params); |
| 706 | |
Junio C Hamano | e7ffa38 | 2015-08-24 09:39:48 -0700 | [diff] [blame] | 707 | strbuf_complete_line(&sb); |
Jeff King | 52563d7 | 2016-07-08 05:12:22 -0400 | [diff] [blame] | 708 | |
| 709 | write_file_buf(path, sb.buf, sb.len); |
Nguyễn Thái Ngọc Duy | 316e53e | 2014-11-30 15:24:45 +0700 | [diff] [blame] | 710 | strbuf_release(&sb); |
Junio C Hamano | 12d6ce1 | 2015-08-24 13:03:07 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Johannes Sixt | 2024d31 | 2015-06-05 21:45:05 +0200 | [diff] [blame] | 713 | void sleep_millisec(int millisec) |
| 714 | { |
| 715 | poll(NULL, 0, millisec); |
| 716 | } |
David Turner | 5781a9a | 2017-04-18 17:57:43 -0400 | [diff] [blame] | 717 | |
| 718 | int xgethostname(char *buf, size_t len) |
| 719 | { |
| 720 | /* |
| 721 | * If the full hostname doesn't fit in buf, POSIX does not |
| 722 | * specify whether the buffer will be null-terminated, so to |
| 723 | * be safe, do it ourselves. |
| 724 | */ |
| 725 | int ret = gethostname(buf, len); |
| 726 | if (!ret) |
| 727 | buf[len - 1] = 0; |
| 728 | return ret; |
| 729 | } |
Pranit Bauva | e3b1e3b | 2019-01-02 07:38:32 -0800 | [diff] [blame] | 730 | |
| 731 | int is_empty_or_missing_file(const char *filename) |
| 732 | { |
| 733 | struct stat st; |
| 734 | |
| 735 | if (stat(filename, &st) < 0) { |
| 736 | if (errno == ENOENT) |
| 737 | return 1; |
| 738 | die_errno(_("could not stat %s"), filename); |
| 739 | } |
| 740 | |
| 741 | return !st.st_size; |
| 742 | } |
Jeff King | 00611d8 | 2021-02-16 09:44:22 -0500 | [diff] [blame] | 743 | |
| 744 | int open_nofollow(const char *path, int flags) |
| 745 | { |
| 746 | #ifdef O_NOFOLLOW |
| 747 | return open(path, flags | O_NOFOLLOW); |
| 748 | #else |
| 749 | struct stat st; |
| 750 | if (lstat(path, &st) < 0) |
| 751 | return -1; |
| 752 | if (S_ISLNK(st.st_mode)) { |
| 753 | errno = ELOOP; |
| 754 | return -1; |
| 755 | } |
| 756 | return open(path, flags); |
| 757 | #endif |
| 758 | } |
brian m. carlson | 05cd988 | 2022-01-17 21:56:16 +0000 | [diff] [blame] | 759 | |
| 760 | int csprng_bytes(void *buf, size_t len) |
| 761 | { |
| 762 | #if defined(HAVE_ARC4RANDOM) || defined(HAVE_ARC4RANDOM_LIBBSD) |
| 763 | /* This function never returns an error. */ |
| 764 | arc4random_buf(buf, len); |
| 765 | return 0; |
| 766 | #elif defined(HAVE_GETRANDOM) |
| 767 | ssize_t res; |
| 768 | char *p = buf; |
| 769 | while (len) { |
| 770 | res = getrandom(p, len, 0); |
| 771 | if (res < 0) |
| 772 | return -1; |
| 773 | len -= res; |
| 774 | p += res; |
| 775 | } |
| 776 | return 0; |
| 777 | #elif defined(HAVE_GETENTROPY) |
| 778 | int res; |
| 779 | char *p = buf; |
| 780 | while (len) { |
| 781 | /* getentropy has a maximum size of 256 bytes. */ |
| 782 | size_t chunk = len < 256 ? len : 256; |
| 783 | res = getentropy(p, chunk); |
| 784 | if (res < 0) |
| 785 | return -1; |
| 786 | len -= chunk; |
| 787 | p += chunk; |
| 788 | } |
| 789 | return 0; |
| 790 | #elif defined(HAVE_RTLGENRANDOM) |
| 791 | if (!RtlGenRandom(buf, len)) |
| 792 | return -1; |
| 793 | return 0; |
| 794 | #elif defined(HAVE_OPENSSL_CSPRNG) |
| 795 | int res = RAND_bytes(buf, len); |
| 796 | if (res == 1) |
| 797 | return 0; |
| 798 | if (res == -1) |
| 799 | errno = ENOTSUP; |
| 800 | else |
| 801 | errno = EIO; |
| 802 | return -1; |
| 803 | #else |
| 804 | ssize_t res; |
| 805 | char *p = buf; |
| 806 | int fd, err; |
| 807 | fd = open("/dev/urandom", O_RDONLY); |
| 808 | if (fd < 0) |
| 809 | return -1; |
| 810 | while (len) { |
| 811 | res = xread(fd, p, len); |
| 812 | if (res < 0) { |
| 813 | err = errno; |
| 814 | close(fd); |
| 815 | errno = err; |
| 816 | return -1; |
| 817 | } |
| 818 | len -= res; |
| 819 | p += res; |
| 820 | } |
| 821 | close(fd); |
| 822 | return 0; |
| 823 | #endif |
| 824 | } |