2 * Various trivial helper wrappers around standard functions
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;
16 const char *env = getenv("GIT_ALLOC_LIMIT");
17 limit = env ? atoi(env) * 1024 : 0;
19 if (limit && size > limit) {
21 error("attempting to allocate %"PRIuMAX" over limit %d",
22 (intmax_t)size, limit);
25 die("attempting to allocate %"PRIuMAX" over limit %d",
26 (intmax_t)size, limit);
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;
36 try_to_free_routine = routine;
40 char *xstrdup(const char *str)
42 char *ret = strdup(str);
44 try_to_free_routine(strlen(str) + 1);
47 die("Out of memory, strdup failed");
52 static void *do_xmalloc(size_t size, int gentle)
56 if (memory_limit_check(size, gentle))
62 try_to_free_routine(size);
68 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
71 error("Out of memory, malloc failed (tried to allocate %lu bytes)",
78 memset(ret, 0xA5, size);
83 void *xmalloc(size_t size)
85 return do_xmalloc(size, 0);
88 static void *do_xmallocz(size_t size, int gentle)
91 if (unsigned_add_overflows(size, 1)) {
93 error("Data too large to fit into virtual memory space.");
96 die("Data too large to fit into virtual memory space.");
98 ret = do_xmalloc(size + 1, gentle);
100 ((char*)ret)[size] = 0;
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,
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)
135 memory_limit_check(size, 0);
136 ret = realloc(ptr, size);
138 ret = realloc(ptr, 1);
140 try_to_free_routine(size);
141 ret = realloc(ptr, size);
143 ret = realloc(ptr, 1);
145 die("Out of memory, realloc failed");
150 void *xcalloc(size_t nmemb, size_t size)
154 memory_limit_check(size * nmemb, 0);
155 ret = calloc(nmemb, size);
156 if (!ret && (!nmemb || !size))
159 try_to_free_routine(nmemb * size);
160 ret = calloc(nmemb, size);
161 if (!ret && (!nmemb || !size))
164 die("Out of memory, calloc failed");
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)
185 if (len > MAX_IO_SIZE)
188 nr = read(fd, buf, len);
189 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
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)
203 if (len > MAX_IO_SIZE)
206 nr = write(fd, buf, len);
207 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
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)
221 if (len > MAX_IO_SIZE)
224 nr = pread(fd, buf, len, offset);
225 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
231 ssize_t read_in_full(int fd, void *buf, size_t count)
237 ssize_t loaded = xread(fd, p, count);
250 ssize_t write_in_full(int fd, const void *buf, size_t count)
256 ssize_t written = xwrite(fd, p, count);
271 ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset)
277 ssize_t loaded = xpread(fd, p, count, offset);
295 die_errno("dup failed");
299 FILE *xfdopen(int fd, const char *mode)
301 FILE *stream = fdopen(fd, mode);
303 die_errno("Out of memory? fdopen failed");
307 int xmkstemp(char *template)
310 char origtemplate[PATH_MAX];
311 strlcpy(origtemplate, template, sizeof(origtemplate));
313 fd = mkstemp(template);
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);
323 die_errno("Unable to create temporary file '%s'",
324 nonrelative_template);
329 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
330 int git_mkstemp(char *path, size_t len, const char *template)
335 tmp = getenv("TMPDIR");
338 n = snprintf(path, len, "%s/%s", tmp, template);
340 errno = ENAMETOOLONG;
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)
352 tmp = getenv("TMPDIR");
355 n = snprintf(path, len, "%s/%s", tmp, template);
357 errno = ENAMETOOLONG;
360 return mkstemps(path, suffix_len);
363 /* Adapted from libiberty's mkstemp.c. */
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"
374 static const int num_letters = 62;
381 len = strlen(pattern);
383 if (len < 6 + suffix_len) {
388 if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
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) {
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);
414 * Fatal error (EPERM, ENOSPC etc).
415 * It doesn't make sense to loop.
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).
426 /* We return the null string if we can't find a unique file name. */
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);
438 int gitmkstemps(char *pattern, int suffix_len)
440 return git_mkstemps_mode(pattern, suffix_len, 0600);
444 int xmkstemp_mode(char *template, int mode)
447 char origtemplate[PATH_MAX];
448 strlcpy(origtemplate, template, sizeof(origtemplate));
450 fd = git_mkstemp_mode(template, mode);
452 int saved_errno = errno;
453 const char *nonrelative_template;
456 template = origtemplate;
458 nonrelative_template = absolute_path(template);
460 die_errno("Unable to create temporary file '%s'",
461 nonrelative_template);
466 static int warn_if_unremovable(const char *op, const char *file, int rc)
471 warning("unable to %s %s: %s",
472 op, file, strerror(errno));
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);
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);
521 struct passwd *xgetpwuid_self(void)
526 pw = getpwuid(getuid());
528 die(_("unable to look up current user in the passwd file: %s"),
529 errno ? strerror(errno) : _("no such user"));
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);