pre-push.sample: Write error message to stderr
[git/debian.git] / wrapper.c
blob25074d71b6ce72066efc02abda74ddfb10f71d6c
1 /*
2 * Various trivial helper wrappers around standard functions
3 */
4 #include "cache.h"
6 static void do_nothing(size_t size)
10 static void (*try_to_free_routine)(size_t size) = do_nothing;
12 static int memory_limit_check(size_t size, int gentle)
14 static int limit = -1;
15 if (limit == -1) {
16 const char *env = getenv("GIT_ALLOC_LIMIT");
17 limit = env ? atoi(env) * 1024 : 0;
19 if (limit && size > limit) {
20 if (gentle) {
21 error("attempting to allocate %"PRIuMAX" over limit %d",
22 (intmax_t)size, limit);
23 return -1;
24 } else
25 die("attempting to allocate %"PRIuMAX" over limit %d",
26 (intmax_t)size, limit);
28 return 0;
31 try_to_free_t set_try_to_free_routine(try_to_free_t routine)
33 try_to_free_t old = try_to_free_routine;
34 if (!routine)
35 routine = do_nothing;
36 try_to_free_routine = routine;
37 return old;
40 char *xstrdup(const char *str)
42 char *ret = strdup(str);
43 if (!ret) {
44 try_to_free_routine(strlen(str) + 1);
45 ret = strdup(str);
46 if (!ret)
47 die("Out of memory, strdup failed");
49 return ret;
52 static void *do_xmalloc(size_t size, int gentle)
54 void *ret;
56 if (memory_limit_check(size, gentle))
57 return NULL;
58 ret = malloc(size);
59 if (!ret && !size)
60 ret = malloc(1);
61 if (!ret) {
62 try_to_free_routine(size);
63 ret = malloc(size);
64 if (!ret && !size)
65 ret = malloc(1);
66 if (!ret) {
67 if (!gentle)
68 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
69 (unsigned long)size);
70 else {
71 error("Out of memory, malloc failed (tried to allocate %lu bytes)",
72 (unsigned long)size);
73 return NULL;
77 #ifdef XMALLOC_POISON
78 memset(ret, 0xA5, size);
79 #endif
80 return ret;
83 void *xmalloc(size_t size)
85 return do_xmalloc(size, 0);
88 static void *do_xmallocz(size_t size, int gentle)
90 void *ret;
91 if (unsigned_add_overflows(size, 1)) {
92 if (gentle) {
93 error("Data too large to fit into virtual memory space.");
94 return NULL;
95 } else
96 die("Data too large to fit into virtual memory space.");
98 ret = do_xmalloc(size + 1, gentle);
99 if (ret)
100 ((char*)ret)[size] = 0;
101 return ret;
104 void *xmallocz(size_t size)
106 return do_xmallocz(size, 0);
109 void *xmallocz_gently(size_t size)
111 return do_xmallocz(size, 1);
115 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
116 * "data" to the allocated memory, zero terminates the allocated memory,
117 * and returns a pointer to the allocated memory. If the allocation fails,
118 * the program dies.
120 void *xmemdupz(const void *data, size_t len)
122 return memcpy(xmallocz(len), data, len);
125 char *xstrndup(const char *str, size_t len)
127 char *p = memchr(str, '\0', len);
128 return xmemdupz(str, p ? p - str : len);
131 void *xrealloc(void *ptr, size_t size)
133 void *ret;
135 memory_limit_check(size, 0);
136 ret = realloc(ptr, size);
137 if (!ret && !size)
138 ret = realloc(ptr, 1);
139 if (!ret) {
140 try_to_free_routine(size);
141 ret = realloc(ptr, size);
142 if (!ret && !size)
143 ret = realloc(ptr, 1);
144 if (!ret)
145 die("Out of memory, realloc failed");
147 return ret;
150 void *xcalloc(size_t nmemb, size_t size)
152 void *ret;
154 memory_limit_check(size * nmemb, 0);
155 ret = calloc(nmemb, size);
156 if (!ret && (!nmemb || !size))
157 ret = calloc(1, 1);
158 if (!ret) {
159 try_to_free_routine(nmemb * size);
160 ret = calloc(nmemb, size);
161 if (!ret && (!nmemb || !size))
162 ret = calloc(1, 1);
163 if (!ret)
164 die("Out of memory, calloc failed");
166 return ret;
170 * Limit size of IO chunks, because huge chunks only cause pain. OS X
171 * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
172 * the absence of bugs, large chunks can result in bad latencies when
173 * you decide to kill the process.
175 #define MAX_IO_SIZE (8*1024*1024)
178 * xread() is the same a read(), but it automatically restarts read()
179 * operations with a recoverable error (EAGAIN and EINTR). xread()
180 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
182 ssize_t xread(int fd, void *buf, size_t len)
184 ssize_t nr;
185 if (len > MAX_IO_SIZE)
186 len = MAX_IO_SIZE;
187 while (1) {
188 nr = read(fd, buf, len);
189 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
190 continue;
191 return nr;
196 * xwrite() is the same a write(), but it automatically restarts write()
197 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
198 * GUARANTEE that "len" bytes is written even if the operation is successful.
200 ssize_t xwrite(int fd, const void *buf, size_t len)
202 ssize_t nr;
203 if (len > MAX_IO_SIZE)
204 len = MAX_IO_SIZE;
205 while (1) {
206 nr = write(fd, buf, len);
207 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
208 continue;
209 return nr;
214 * xpread() is the same as pread(), but it automatically restarts pread()
215 * operations with a recoverable error (EAGAIN and EINTR). xpread() DOES
216 * NOT GUARANTEE that "len" bytes is read even if the data is available.
218 ssize_t xpread(int fd, void *buf, size_t len, off_t offset)
220 ssize_t nr;
221 if (len > MAX_IO_SIZE)
222 len = MAX_IO_SIZE;
223 while (1) {
224 nr = pread(fd, buf, len, offset);
225 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
226 continue;
227 return nr;
231 ssize_t read_in_full(int fd, void *buf, size_t count)
233 char *p = buf;
234 ssize_t total = 0;
236 while (count > 0) {
237 ssize_t loaded = xread(fd, p, count);
238 if (loaded < 0)
239 return -1;
240 if (loaded == 0)
241 return total;
242 count -= loaded;
243 p += loaded;
244 total += loaded;
247 return total;
250 ssize_t write_in_full(int fd, const void *buf, size_t count)
252 const char *p = buf;
253 ssize_t total = 0;
255 while (count > 0) {
256 ssize_t written = xwrite(fd, p, count);
257 if (written < 0)
258 return -1;
259 if (!written) {
260 errno = ENOSPC;
261 return -1;
263 count -= written;
264 p += written;
265 total += written;
268 return total;
271 ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset)
273 char *p = buf;
274 ssize_t total = 0;
276 while (count > 0) {
277 ssize_t loaded = xpread(fd, p, count, offset);
278 if (loaded < 0)
279 return -1;
280 if (loaded == 0)
281 return total;
282 count -= loaded;
283 p += loaded;
284 total += loaded;
285 offset += loaded;
288 return total;
291 int xdup(int fd)
293 int ret = dup(fd);
294 if (ret < 0)
295 die_errno("dup failed");
296 return ret;
299 FILE *xfdopen(int fd, const char *mode)
301 FILE *stream = fdopen(fd, mode);
302 if (stream == NULL)
303 die_errno("Out of memory? fdopen failed");
304 return stream;
307 int xmkstemp(char *template)
309 int fd;
310 char origtemplate[PATH_MAX];
311 strlcpy(origtemplate, template, sizeof(origtemplate));
313 fd = mkstemp(template);
314 if (fd < 0) {
315 int saved_errno = errno;
316 const char *nonrelative_template;
318 if (strlen(template) != strlen(origtemplate))
319 template = origtemplate;
321 nonrelative_template = absolute_path(template);
322 errno = saved_errno;
323 die_errno("Unable to create temporary file '%s'",
324 nonrelative_template);
326 return fd;
329 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
330 int git_mkstemp(char *path, size_t len, const char *template)
332 const char *tmp;
333 size_t n;
335 tmp = getenv("TMPDIR");
336 if (!tmp)
337 tmp = "/tmp";
338 n = snprintf(path, len, "%s/%s", tmp, template);
339 if (len <= n) {
340 errno = ENAMETOOLONG;
341 return -1;
343 return mkstemp(path);
346 /* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
347 int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
349 const char *tmp;
350 size_t n;
352 tmp = getenv("TMPDIR");
353 if (!tmp)
354 tmp = "/tmp";
355 n = snprintf(path, len, "%s/%s", tmp, template);
356 if (len <= n) {
357 errno = ENAMETOOLONG;
358 return -1;
360 return mkstemps(path, suffix_len);
363 /* Adapted from libiberty's mkstemp.c. */
365 #undef TMP_MAX
366 #define TMP_MAX 16384
368 int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
370 static const char letters[] =
371 "abcdefghijklmnopqrstuvwxyz"
372 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
373 "0123456789";
374 static const int num_letters = 62;
375 uint64_t value;
376 struct timeval tv;
377 char *template;
378 size_t len;
379 int fd, count;
381 len = strlen(pattern);
383 if (len < 6 + suffix_len) {
384 errno = EINVAL;
385 return -1;
388 if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
389 errno = EINVAL;
390 return -1;
394 * Replace pattern's XXXXXX characters with randomness.
395 * Try TMP_MAX different filenames.
397 gettimeofday(&tv, NULL);
398 value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
399 template = &pattern[len - 6 - suffix_len];
400 for (count = 0; count < TMP_MAX; ++count) {
401 uint64_t v = value;
402 /* Fill in the random bits. */
403 template[0] = letters[v % num_letters]; v /= num_letters;
404 template[1] = letters[v % num_letters]; v /= num_letters;
405 template[2] = letters[v % num_letters]; v /= num_letters;
406 template[3] = letters[v % num_letters]; v /= num_letters;
407 template[4] = letters[v % num_letters]; v /= num_letters;
408 template[5] = letters[v % num_letters]; v /= num_letters;
410 fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
411 if (fd >= 0)
412 return fd;
414 * Fatal error (EPERM, ENOSPC etc).
415 * It doesn't make sense to loop.
417 if (errno != EEXIST)
418 break;
420 * This is a random value. It is only necessary that
421 * the next TMP_MAX values generated by adding 7777 to
422 * VALUE are different with (module 2^32).
424 value += 7777;
426 /* We return the null string if we can't find a unique file name. */
427 pattern[0] = '\0';
428 return -1;
431 int git_mkstemp_mode(char *pattern, int mode)
433 /* mkstemp is just mkstemps with no suffix */
434 return git_mkstemps_mode(pattern, 0, mode);
437 #ifdef NO_MKSTEMPS
438 int gitmkstemps(char *pattern, int suffix_len)
440 return git_mkstemps_mode(pattern, suffix_len, 0600);
442 #endif
444 int xmkstemp_mode(char *template, int mode)
446 int fd;
447 char origtemplate[PATH_MAX];
448 strlcpy(origtemplate, template, sizeof(origtemplate));
450 fd = git_mkstemp_mode(template, mode);
451 if (fd < 0) {
452 int saved_errno = errno;
453 const char *nonrelative_template;
455 if (!template[0])
456 template = origtemplate;
458 nonrelative_template = absolute_path(template);
459 errno = saved_errno;
460 die_errno("Unable to create temporary file '%s'",
461 nonrelative_template);
463 return fd;
466 static int warn_if_unremovable(const char *op, const char *file, int rc)
468 if (rc < 0) {
469 int err = errno;
470 if (ENOENT != err) {
471 warning("unable to %s %s: %s",
472 op, file, strerror(errno));
473 errno = err;
476 return rc;
479 int unlink_or_warn(const char *file)
481 return warn_if_unremovable("unlink", file, unlink(file));
484 int rmdir_or_warn(const char *file)
486 return warn_if_unremovable("rmdir", file, rmdir(file));
489 int remove_or_warn(unsigned int mode, const char *file)
491 return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
494 void warn_on_inaccessible(const char *path)
496 warning(_("unable to access '%s': %s"), path, strerror(errno));
499 static int access_error_is_ok(int err, unsigned flag)
501 return err == ENOENT || err == ENOTDIR ||
502 ((flag & ACCESS_EACCES_OK) && err == EACCES);
505 int access_or_warn(const char *path, int mode, unsigned flag)
507 int ret = access(path, mode);
508 if (ret && !access_error_is_ok(errno, flag))
509 warn_on_inaccessible(path);
510 return ret;
513 int access_or_die(const char *path, int mode, unsigned flag)
515 int ret = access(path, mode);
516 if (ret && !access_error_is_ok(errno, flag))
517 die_errno(_("unable to access '%s'"), path);
518 return ret;
521 struct passwd *xgetpwuid_self(void)
523 struct passwd *pw;
525 errno = 0;
526 pw = getpwuid(getuid());
527 if (!pw)
528 die(_("unable to look up current user in the passwd file: %s"),
529 errno ? strerror(errno) : _("no such user"));
530 return pw;
533 char *xgetcwd(void)
535 struct strbuf sb = STRBUF_INIT;
536 if (strbuf_getcwd(&sb))
537 die_errno(_("unable to get current working directory"));
538 return strbuf_detach(&sb, NULL);