blob: 0d5108bab68bce63f54fe7321442f531d9af39b3 [file] [log] [blame]
Linus Torvalds112db552008-06-22 12:19:25 -07001/*
2 * Various trivial helper wrappers around standard functions
3 */
4#include "cache.h"
5
Jonathan Niedere0500292010-11-06 14:00:38 -05006static void do_nothing(size_t size)
Nicolas Pitrea9a74632010-03-24 16:22:34 -04007{
Nicolas Pitrea9a74632010-03-24 16:22:34 -04008}
9
Jonathan Niedere0500292010-11-06 14:00:38 -050010static void (*try_to_free_routine)(size_t size) = do_nothing;
Nicolas Pitrea9a74632010-03-24 16:22:34 -040011
Nguyễn Thái Ngọc Duyd41489a2012-03-07 17:54:16 +070012static void memory_limit_check(size_t size)
13{
14 static int limit = -1;
15 if (limit == -1) {
16 const char *env = getenv("GIT_ALLOC_LIMIT");
17 limit = env ? atoi(env) * 1024 : 0;
18 }
19 if (limit && size > limit)
20 die("attempting to allocate %"PRIuMAX" over limit %d",
21 (intmax_t)size, limit);
22}
23
Johannes Sixt851c34b2010-05-08 17:13:49 +020024try_to_free_t set_try_to_free_routine(try_to_free_t routine)
Nicolas Pitrea9a74632010-03-24 16:22:34 -040025{
Johannes Sixt851c34b2010-05-08 17:13:49 +020026 try_to_free_t old = try_to_free_routine;
Junio C Hamano00b0d7f2010-12-21 09:24:18 -080027 if (!routine)
28 routine = do_nothing;
Johannes Sixt851c34b2010-05-08 17:13:49 +020029 try_to_free_routine = routine;
30 return old;
Nicolas Pitrea9a74632010-03-24 16:22:34 -040031}
32
Linus Torvalds112db552008-06-22 12:19:25 -070033char *xstrdup(const char *str)
34{
35 char *ret = strdup(str);
36 if (!ret) {
Nicolas Pitrea9a74632010-03-24 16:22:34 -040037 try_to_free_routine(strlen(str) + 1);
Linus Torvalds112db552008-06-22 12:19:25 -070038 ret = strdup(str);
39 if (!ret)
40 die("Out of memory, strdup failed");
41 }
42 return ret;
43}
44
45void *xmalloc(size_t size)
46{
Nguyễn Thái Ngọc Duyd41489a2012-03-07 17:54:16 +070047 void *ret;
48
49 memory_limit_check(size);
50 ret = malloc(size);
Linus Torvalds112db552008-06-22 12:19:25 -070051 if (!ret && !size)
52 ret = malloc(1);
53 if (!ret) {
Nicolas Pitrea9a74632010-03-24 16:22:34 -040054 try_to_free_routine(size);
Linus Torvalds112db552008-06-22 12:19:25 -070055 ret = malloc(size);
56 if (!ret && !size)
57 ret = malloc(1);
58 if (!ret)
Matthieu Moy8bd9fd52010-08-20 17:09:51 +020059 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
60 (unsigned long)size);
Linus Torvalds112db552008-06-22 12:19:25 -070061 }
62#ifdef XMALLOC_POISON
63 memset(ret, 0xA5, size);
64#endif
65 return ret;
66}
67
Ilari Liusvaara5bf92192010-01-26 20:24:12 +020068void *xmallocz(size_t size)
69{
70 void *ret;
Jonathan Nieder1368f652010-10-10 21:59:26 -050071 if (unsigned_add_overflows(size, 1))
Ilari Liusvaara5bf92192010-01-26 20:24:12 +020072 die("Data too large to fit into virtual memory space.");
73 ret = xmalloc(size + 1);
74 ((char*)ret)[size] = 0;
75 return ret;
76}
77
Linus Torvalds112db552008-06-22 12:19:25 -070078/*
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,
82 * the program dies.
83 */
84void *xmemdupz(const void *data, size_t len)
85{
Ilari Liusvaara5bf92192010-01-26 20:24:12 +020086 return memcpy(xmallocz(len), data, len);
Linus Torvalds112db552008-06-22 12:19:25 -070087}
88
89char *xstrndup(const char *str, size_t len)
90{
91 char *p = memchr(str, '\0', len);
92 return xmemdupz(str, p ? p - str : len);
93}
94
95void *xrealloc(void *ptr, size_t size)
96{
Nguyễn Thái Ngọc Duyd41489a2012-03-07 17:54:16 +070097 void *ret;
98
99 memory_limit_check(size);
100 ret = realloc(ptr, size);
Linus Torvalds112db552008-06-22 12:19:25 -0700101 if (!ret && !size)
102 ret = realloc(ptr, 1);
103 if (!ret) {
Nicolas Pitrea9a74632010-03-24 16:22:34 -0400104 try_to_free_routine(size);
Linus Torvalds112db552008-06-22 12:19:25 -0700105 ret = realloc(ptr, size);
106 if (!ret && !size)
107 ret = realloc(ptr, 1);
108 if (!ret)
109 die("Out of memory, realloc failed");
110 }
111 return ret;
112}
113
114void *xcalloc(size_t nmemb, size_t size)
115{
Nguyễn Thái Ngọc Duyd41489a2012-03-07 17:54:16 +0700116 void *ret;
117
118 memory_limit_check(size * nmemb);
119 ret = calloc(nmemb, size);
Linus Torvalds112db552008-06-22 12:19:25 -0700120 if (!ret && (!nmemb || !size))
121 ret = calloc(1, 1);
122 if (!ret) {
Nicolas Pitrea9a74632010-03-24 16:22:34 -0400123 try_to_free_routine(nmemb * size);
Linus Torvalds112db552008-06-22 12:19:25 -0700124 ret = calloc(nmemb, size);
125 if (!ret && (!nmemb || !size))
126 ret = calloc(1, 1);
127 if (!ret)
128 die("Out of memory, calloc failed");
129 }
130 return ret;
131}
132
Linus Torvalds112db552008-06-22 12:19:25 -0700133/*
Steffen Prohaska0b6806b2013-08-20 08:43:54 +0200134 * 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.
138 */
139#define MAX_IO_SIZE (8*1024*1024)
140
141/*
Linus Torvalds112db552008-06-22 12:19:25 -0700142 * xread() is the same a read(), but it automatically restarts read()
143 * operations with a recoverable error (EAGAIN and EINTR). xread()
144 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
145 */
146ssize_t xread(int fd, void *buf, size_t len)
147{
148 ssize_t nr;
Steffen Prohaska0b6806b2013-08-20 08:43:54 +0200149 if (len > MAX_IO_SIZE)
Denton Liu7cd54d32020-03-27 22:57:34 -0400150 len = MAX_IO_SIZE;
Linus Torvalds112db552008-06-22 12:19:25 -0700151 while (1) {
152 nr = read(fd, buf, len);
153 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
154 continue;
155 return nr;
156 }
157}
158
159/*
160 * xwrite() is the same a write(), but it automatically restarts write()
161 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
162 * GUARANTEE that "len" bytes is written even if the operation is successful.
163 */
164ssize_t xwrite(int fd, const void *buf, size_t len)
165{
166 ssize_t nr;
Steffen Prohaska0b6806b2013-08-20 08:43:54 +0200167 if (len > MAX_IO_SIZE)
Denton Liu7cd54d32020-03-27 22:57:34 -0400168 len = MAX_IO_SIZE;
Linus Torvalds112db552008-06-22 12:19:25 -0700169 while (1) {
170 nr = write(fd, buf, len);
171 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
172 continue;
173 return nr;
174 }
175}
176
Junio C Hamano559e8402008-07-20 16:13:05 -0700177ssize_t read_in_full(int fd, void *buf, size_t count)
178{
179 char *p = buf;
180 ssize_t total = 0;
181
182 while (count > 0) {
183 ssize_t loaded = xread(fd, p, count);
Jeff King56d7c272011-05-26 12:30:27 -0400184 if (loaded < 0)
185 return -1;
186 if (loaded == 0)
187 return total;
Junio C Hamano559e8402008-07-20 16:13:05 -0700188 count -= loaded;
189 p += loaded;
190 total += loaded;
191 }
192
193 return total;
194}
195
196ssize_t write_in_full(int fd, const void *buf, size_t count)
197{
198 const char *p = buf;
199 ssize_t total = 0;
200
201 while (count > 0) {
202 ssize_t written = xwrite(fd, p, count);
203 if (written < 0)
204 return -1;
205 if (!written) {
206 errno = ENOSPC;
207 return -1;
208 }
209 count -= written;
210 p += written;
211 total += written;
212 }
213
214 return total;
215}
216
Linus Torvalds112db552008-06-22 12:19:25 -0700217int xdup(int fd)
218{
219 int ret = dup(fd);
220 if (ret < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200221 die_errno("dup failed");
Linus Torvalds112db552008-06-22 12:19:25 -0700222 return ret;
223}
224
225FILE *xfdopen(int fd, const char *mode)
226{
227 FILE *stream = fdopen(fd, mode);
228 if (stream == NULL)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200229 die_errno("Out of memory? fdopen failed");
Linus Torvalds112db552008-06-22 12:19:25 -0700230 return stream;
231}
232
233int xmkstemp(char *template)
234{
235 int fd;
Arnout Engelen6cf6bb32010-12-18 22:28:00 +0100236 char origtemplate[PATH_MAX];
237 strlcpy(origtemplate, template, sizeof(origtemplate));
Linus Torvalds112db552008-06-22 12:19:25 -0700238
239 fd = mkstemp(template);
Arnout Engelen6cf6bb32010-12-18 22:28:00 +0100240 if (fd < 0) {
241 int saved_errno = errno;
242 const char *nonrelative_template;
243
Junio C Hamanof7be59b2012-12-18 12:51:35 -0800244 if (strlen(template) != strlen(origtemplate))
Arnout Engelen6cf6bb32010-12-18 22:28:00 +0100245 template = origtemplate;
246
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100247 nonrelative_template = absolute_path(template);
Arnout Engelen6cf6bb32010-12-18 22:28:00 +0100248 errno = saved_errno;
249 die_errno("Unable to create temporary file '%s'",
250 nonrelative_template);
251 }
Linus Torvalds112db552008-06-22 12:19:25 -0700252 return fd;
253}
Linus Torvalds39c68542009-01-07 19:54:47 -0800254
Jonathan Nieder33f23932010-11-06 06:46:31 -0500255/* git_mkstemp() - create tmp file honoring TMPDIR variable */
256int git_mkstemp(char *path, size_t len, const char *template)
257{
258 const char *tmp;
259 size_t n;
260
261 tmp = getenv("TMPDIR");
262 if (!tmp)
263 tmp = "/tmp";
264 n = snprintf(path, len, "%s/%s", tmp, template);
265 if (len <= n) {
266 errno = ENAMETOOLONG;
267 return -1;
268 }
269 return mkstemp(path);
270}
271
272/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
273int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
274{
275 const char *tmp;
276 size_t n;
277
278 tmp = getenv("TMPDIR");
279 if (!tmp)
280 tmp = "/tmp";
281 n = snprintf(path, len, "%s/%s", tmp, template);
282 if (len <= n) {
283 errno = ENAMETOOLONG;
284 return -1;
285 }
286 return mkstemps(path, suffix_len);
287}
288
289/* Adapted from libiberty's mkstemp.c. */
290
291#undef TMP_MAX
292#define TMP_MAX 16384
293
294int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
295{
296 static const char letters[] =
297 "abcdefghijklmnopqrstuvwxyz"
298 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
299 "0123456789";
300 static const int num_letters = 62;
301 uint64_t value;
302 struct timeval tv;
303 char *template;
304 size_t len;
305 int fd, count;
306
307 len = strlen(pattern);
308
309 if (len < 6 + suffix_len) {
310 errno = EINVAL;
311 return -1;
312 }
313
314 if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
315 errno = EINVAL;
316 return -1;
317 }
318
319 /*
320 * Replace pattern's XXXXXX characters with randomness.
321 * Try TMP_MAX different filenames.
322 */
323 gettimeofday(&tv, NULL);
324 value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
325 template = &pattern[len - 6 - suffix_len];
326 for (count = 0; count < TMP_MAX; ++count) {
327 uint64_t v = value;
328 /* Fill in the random bits. */
329 template[0] = letters[v % num_letters]; v /= num_letters;
330 template[1] = letters[v % num_letters]; v /= num_letters;
331 template[2] = letters[v % num_letters]; v /= num_letters;
332 template[3] = letters[v % num_letters]; v /= num_letters;
333 template[4] = letters[v % num_letters]; v /= num_letters;
334 template[5] = letters[v % num_letters]; v /= num_letters;
335
336 fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
Dale R. Worleya2cb86c2013-07-12 10:58:35 +0200337 if (fd >= 0)
Jonathan Nieder33f23932010-11-06 06:46:31 -0500338 return fd;
339 /*
340 * Fatal error (EPERM, ENOSPC etc).
341 * It doesn't make sense to loop.
342 */
343 if (errno != EEXIST)
344 break;
345 /*
346 * This is a random value. It is only necessary that
347 * the next TMP_MAX values generated by adding 7777 to
348 * VALUE are different with (module 2^32).
349 */
350 value += 7777;
351 }
352 /* We return the null string if we can't find a unique file name. */
353 pattern[0] = '\0';
354 return -1;
355}
356
357int git_mkstemp_mode(char *pattern, int mode)
358{
359 /* mkstemp is just mkstemps with no suffix */
360 return git_mkstemps_mode(pattern, 0, mode);
361}
362
363int gitmkstemps(char *pattern, int suffix_len)
364{
365 return git_mkstemps_mode(pattern, suffix_len, 0600);
366}
367
Matthieu Moyb862b612010-02-22 23:32:13 +0100368int xmkstemp_mode(char *template, int mode)
369{
370 int fd;
Arnout Engelen6cf6bb32010-12-18 22:28:00 +0100371 char origtemplate[PATH_MAX];
372 strlcpy(origtemplate, template, sizeof(origtemplate));
Matthieu Moyb862b612010-02-22 23:32:13 +0100373
374 fd = git_mkstemp_mode(template, mode);
Arnout Engelen6cf6bb32010-12-18 22:28:00 +0100375 if (fd < 0) {
376 int saved_errno = errno;
377 const char *nonrelative_template;
378
379 if (!template[0])
380 template = origtemplate;
381
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100382 nonrelative_template = absolute_path(template);
Arnout Engelen6cf6bb32010-12-18 22:28:00 +0100383 errno = saved_errno;
384 die_errno("Unable to create temporary file '%s'",
385 nonrelative_template);
386 }
Matthieu Moyb862b612010-02-22 23:32:13 +0100387 return fd;
388}
389
Peter Collingbourne10e13ec2010-03-26 15:25:32 +0000390static int warn_if_unremovable(const char *op, const char *file, int rc)
Alex Riesenfc71db32009-04-29 23:21:46 +0200391{
Alex Riesenfc71db32009-04-29 23:21:46 +0200392 if (rc < 0) {
393 int err = errno;
394 if (ENOENT != err) {
Peter Collingbourne10e13ec2010-03-26 15:25:32 +0000395 warning("unable to %s %s: %s",
396 op, file, strerror(errno));
Alex Riesenfc71db32009-04-29 23:21:46 +0200397 errno = err;
398 }
399 }
400 return rc;
401}
402
Peter Collingbourne10e13ec2010-03-26 15:25:32 +0000403int unlink_or_warn(const char *file)
404{
405 return warn_if_unremovable("unlink", file, unlink(file));
406}
Peter Collingbourned1723292010-03-26 15:25:33 +0000407
408int rmdir_or_warn(const char *file)
409{
410 return warn_if_unremovable("rmdir", file, rmdir(file));
411}
Peter Collingbourne80d706a2010-03-26 15:25:34 +0000412
413int remove_or_warn(unsigned int mode, const char *file)
414{
415 return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
416}
Jeff King2f705872012-05-21 19:10:20 -0400417
Junio C Hamano55b38a42012-08-21 14:52:07 -0700418void warn_on_inaccessible(const char *path)
419{
420 warning(_("unable to access '%s': %s"), path, strerror(errno));
421}
422
Jonathan Nieder4698c8f2013-04-12 14:03:18 -0700423static int access_error_is_ok(int err, unsigned flag)
424{
425 return err == ENOENT || err == ENOTDIR ||
426 ((flag & ACCESS_EACCES_OK) && err == EACCES);
427}
428
429int access_or_warn(const char *path, int mode, unsigned flag)
Jeff Kingba8bd832012-08-21 02:10:59 -0400430{
431 int ret = access(path, mode);
Jonathan Nieder4698c8f2013-04-12 14:03:18 -0700432 if (ret && !access_error_is_ok(errno, flag))
Junio C Hamano55b38a42012-08-21 14:52:07 -0700433 warn_on_inaccessible(path);
Jeff Kingba8bd832012-08-21 02:10:59 -0400434 return ret;
435}
436
Jonathan Nieder4698c8f2013-04-12 14:03:18 -0700437int access_or_die(const char *path, int mode, unsigned flag)
Jonathan Nieder96b9e0e2012-10-13 17:04:02 -0700438{
439 int ret = access(path, mode);
Jonathan Nieder4698c8f2013-04-12 14:03:18 -0700440 if (ret && !access_error_is_ok(errno, flag))
Jonathan Nieder96b9e0e2012-10-13 17:04:02 -0700441 die_errno(_("unable to access '%s'"), path);
442 return ret;
443}
444
Jeff King2f705872012-05-21 19:10:20 -0400445struct passwd *xgetpwuid_self(void)
446{
447 struct passwd *pw;
448
449 errno = 0;
450 pw = getpwuid(getuid());
451 if (!pw)
452 die(_("unable to look up current user in the passwd file: %s"),
453 errno ? strerror(errno) : _("no such user"));
454 return pw;
455}