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 void memory_limit_check(size_t size)
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)
20 die("attempting to allocate %"PRIuMAX" over limit %d",
21 (intmax_t)size, limit);
24 try_to_free_t set_try_to_free_routine(try_to_free_t routine)
26 try_to_free_t old = try_to_free_routine;
29 try_to_free_routine = routine;
33 char *xstrdup(const char *str)
35 char *ret = strdup(str);
37 try_to_free_routine(strlen(str) + 1);
40 die("Out of memory, strdup failed");
45 void *xmalloc(size_t size)
49 memory_limit_check(size);
54 try_to_free_routine(size);
59 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
63 memset(ret, 0xA5, size);
68 void *xmallocz(size_t size)
71 if (unsigned_add_overflows(size, 1))
72 die("Data too large to fit into virtual memory space.");
73 ret = xmalloc(size + 1);
74 ((char*)ret)[size] = 0;
79 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
80 * "data" to the allocated memory, zero terminates the allocated memory,
81 * and returns a pointer to the allocated memory. If the allocation fails,
84 void *xmemdupz(const void *data, size_t len)
86 return memcpy(xmallocz(len), data, len);
89 char *xstrndup(const char *str, size_t len)
91 char *p = memchr(str, '\0', len);
92 return xmemdupz(str, p ? p - str : len);
95 void *xrealloc(void *ptr, size_t size)
99 memory_limit_check(size);
100 ret = realloc(ptr, size);
102 ret = realloc(ptr, 1);
104 try_to_free_routine(size);
105 ret = realloc(ptr, size);
107 ret = realloc(ptr, 1);
109 die("Out of memory, realloc failed");
114 void *xcalloc(size_t nmemb, size_t size)
118 memory_limit_check(size * nmemb);
119 ret = calloc(nmemb, size);
120 if (!ret && (!nmemb || !size))
123 try_to_free_routine(nmemb * size);
124 ret = calloc(nmemb, size);
125 if (!ret && (!nmemb || !size))
128 die("Out of memory, calloc failed");
134 * Limit size of IO chunks, because huge chunks only cause pain. OS X
135 * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
136 * the absense of bugs, large chunks can result in bad latencies when
137 * you decide to kill the process.
139 * We pick 8 MiB as our default, but if the platform defines SSIZE_MAX
140 * that is smaller than that, clip it to SSIZE_MAX, as a call to
141 * read(2) or write(2) larger than that is allowed to fail. As the last
142 * resort, we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value"
143 * to override this, if the definition of SSIZE_MAX given by the platform
147 # define MAX_IO_SIZE_DEFAULT (8*1024*1024)
148 # if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
149 # define MAX_IO_SIZE SSIZE_MAX
151 # define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
156 * xread() is the same a read(), but it automatically restarts read()
157 * operations with a recoverable error (EAGAIN and EINTR). xread()
158 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
160 ssize_t xread(int fd, void *buf, size_t len)
163 if (len > MAX_IO_SIZE)
166 nr = read(fd, buf, len);
167 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
174 * xwrite() is the same a write(), but it automatically restarts write()
175 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
176 * GUARANTEE that "len" bytes is written even if the operation is successful.
178 ssize_t xwrite(int fd, const void *buf, size_t len)
181 if (len > MAX_IO_SIZE)
184 nr = write(fd, buf, len);
185 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
191 ssize_t read_in_full(int fd, void *buf, size_t count)
197 ssize_t loaded = xread(fd, p, count);
210 ssize_t write_in_full(int fd, const void *buf, size_t count)
216 ssize_t written = xwrite(fd, p, count);
235 die_errno("dup failed");
239 FILE *xfdopen(int fd, const char *mode)
241 FILE *stream = fdopen(fd, mode);
243 die_errno("Out of memory? fdopen failed");
247 int xmkstemp(char *template)
250 char origtemplate[PATH_MAX];
251 strlcpy(origtemplate, template, sizeof(origtemplate));
253 fd = mkstemp(template);
255 int saved_errno = errno;
256 const char *nonrelative_template;
258 if (strlen(template) != strlen(origtemplate))
259 template = origtemplate;
261 nonrelative_template = absolute_path(template);
263 die_errno("Unable to create temporary file '%s'",
264 nonrelative_template);
269 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
270 int git_mkstemp(char *path, size_t len, const char *template)
275 tmp = getenv("TMPDIR");
278 n = snprintf(path, len, "%s/%s", tmp, template);
280 errno = ENAMETOOLONG;
283 return mkstemp(path);
286 /* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
287 int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
292 tmp = getenv("TMPDIR");
295 n = snprintf(path, len, "%s/%s", tmp, template);
297 errno = ENAMETOOLONG;
300 return mkstemps(path, suffix_len);
303 /* Adapted from libiberty's mkstemp.c. */
306 #define TMP_MAX 16384
308 int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
310 static const char letters[] =
311 "abcdefghijklmnopqrstuvwxyz"
312 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
314 static const int num_letters = 62;
321 len = strlen(pattern);
323 if (len < 6 + suffix_len) {
328 if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
334 * Replace pattern's XXXXXX characters with randomness.
335 * Try TMP_MAX different filenames.
337 gettimeofday(&tv, NULL);
338 value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
339 template = &pattern[len - 6 - suffix_len];
340 for (count = 0; count < TMP_MAX; ++count) {
342 /* Fill in the random bits. */
343 template[0] = letters[v % num_letters]; v /= num_letters;
344 template[1] = letters[v % num_letters]; v /= num_letters;
345 template[2] = letters[v % num_letters]; v /= num_letters;
346 template[3] = letters[v % num_letters]; v /= num_letters;
347 template[4] = letters[v % num_letters]; v /= num_letters;
348 template[5] = letters[v % num_letters]; v /= num_letters;
350 fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
354 * Fatal error (EPERM, ENOSPC etc).
355 * It doesn't make sense to loop.
360 * This is a random value. It is only necessary that
361 * the next TMP_MAX values generated by adding 7777 to
362 * VALUE are different with (module 2^32).
366 /* We return the null string if we can't find a unique file name. */
371 int git_mkstemp_mode(char *pattern, int mode)
373 /* mkstemp is just mkstemps with no suffix */
374 return git_mkstemps_mode(pattern, 0, mode);
377 int gitmkstemps(char *pattern, int suffix_len)
379 return git_mkstemps_mode(pattern, suffix_len, 0600);
382 int xmkstemp_mode(char *template, int mode)
385 char origtemplate[PATH_MAX];
386 strlcpy(origtemplate, template, sizeof(origtemplate));
388 fd = git_mkstemp_mode(template, mode);
390 int saved_errno = errno;
391 const char *nonrelative_template;
394 template = origtemplate;
396 nonrelative_template = absolute_path(template);
398 die_errno("Unable to create temporary file '%s'",
399 nonrelative_template);
404 static int warn_if_unremovable(const char *op, const char *file, int rc)
409 warning("unable to %s %s: %s",
410 op, file, strerror(errno));
417 int unlink_or_warn(const char *file)
419 return warn_if_unremovable("unlink", file, unlink(file));
422 int rmdir_or_warn(const char *file)
424 return warn_if_unremovable("rmdir", file, rmdir(file));
427 int remove_or_warn(unsigned int mode, const char *file)
429 return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
432 void warn_on_inaccessible(const char *path)
434 warning(_("unable to access '%s': %s"), path, strerror(errno));
437 static int access_error_is_ok(int err, unsigned flag)
439 return err == ENOENT || err == ENOTDIR ||
440 ((flag & ACCESS_EACCES_OK) && err == EACCES);
443 int access_or_warn(const char *path, int mode, unsigned flag)
445 int ret = access(path, mode);
446 if (ret && !access_error_is_ok(errno, flag))
447 warn_on_inaccessible(path);
451 int access_or_die(const char *path, int mode, unsigned flag)
453 int ret = access(path, mode);
454 if (ret && !access_error_is_ok(errno, flag))
455 die_errno(_("unable to access '%s'"), path);
459 struct passwd *xgetpwuid_self(void)
464 pw = getpwuid(getuid());
466 die(_("unable to look up current user in the passwd file: %s"),
467 errno ? strerror(errno) : _("no such user"));