wrapper.c: add xpread() similar to xread()
[git/git.git] / wrapper.c
CommitLineData
112db553
LT
1/*
2 * Various trivial helper wrappers around standard functions
3 */
4#include "cache.h"
5
e0500293 6static void do_nothing(size_t size)
a9a74636 7{
a9a74636
NP
8}
9
e0500293 10static void (*try_to_free_routine)(size_t size) = do_nothing;
a9a74636 11
d41489a6
NTND
12static 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
851c34b0 24try_to_free_t set_try_to_free_routine(try_to_free_t routine)
a9a74636 25{
851c34b0 26 try_to_free_t old = try_to_free_routine;
00b0d7f7
JH
27 if (!routine)
28 routine = do_nothing;
851c34b0
JS
29 try_to_free_routine = routine;
30 return old;
a9a74636
NP
31}
32
112db553
LT
33char *xstrdup(const char *str)
34{
35 char *ret = strdup(str);
36 if (!ret) {
a9a74636 37 try_to_free_routine(strlen(str) + 1);
112db553
LT
38 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{
d41489a6
NTND
47 void *ret;
48
49 memory_limit_check(size);
50 ret = malloc(size);
112db553
LT
51 if (!ret && !size)
52 ret = malloc(1);
53 if (!ret) {
a9a74636 54 try_to_free_routine(size);
112db553
LT
55 ret = malloc(size);
56 if (!ret && !size)
57 ret = malloc(1);
58 if (!ret)
8bd9fd50
MM
59 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
60 (unsigned long)size);
112db553
LT
61 }
62#ifdef XMALLOC_POISON
63 memset(ret, 0xA5, size);
64#endif
65 return ret;
66}
67
5bf9219d
IL
68void *xmallocz(size_t size)
69{
70 void *ret;
1368f650 71 if (unsigned_add_overflows(size, 1))
5bf9219d
IL
72 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
112db553
LT
78/*
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{
5bf9219d 86 return memcpy(xmallocz(len), data, len);
112db553
LT
87}
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{
d41489a6
NTND
97 void *ret;
98
99 memory_limit_check(size);
100 ret = realloc(ptr, size);
112db553
LT
101 if (!ret && !size)
102 ret = realloc(ptr, 1);
103 if (!ret) {
a9a74636 104 try_to_free_routine(size);
112db553
LT
105 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{
d41489a6
NTND
116 void *ret;
117
118 memory_limit_check(size * nmemb);
119 ret = calloc(nmemb, size);
112db553
LT
120 if (!ret && (!nmemb || !size))
121 ret = calloc(1, 1);
122 if (!ret) {
a9a74636 123 try_to_free_routine(nmemb * size);
112db553
LT
124 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
0b6806b9
SP
133/*
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
382d20e3 136 * the absence of bugs, large chunks can result in bad latencies when
0b6806b9
SP
137 * you decide to kill the process.
138 */
139#define MAX_IO_SIZE (8*1024*1024)
140
112db553
LT
141/*
142 * 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;
0b6806b9
SP
149 if (len > MAX_IO_SIZE)
150 len = MAX_IO_SIZE;
112db553
LT
151 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;
0b6806b9
SP
167 if (len > MAX_IO_SIZE)
168 len = MAX_IO_SIZE;
112db553
LT
169 while (1) {
170 nr = write(fd, buf, len);
171 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
9aa91af0
YM
172 continue;
173 return nr;
174 }
175}
176
177/*
178 * xpread() is the same as pread(), but it automatically restarts pread()
179 * operations with a recoverable error (EAGAIN and EINTR). xpread() DOES
180 * NOT GUARANTEE that "len" bytes is read even if the data is available.
181 */
182ssize_t xpread(int fd, void *buf, size_t len, off_t offset)
183{
184 ssize_t nr;
185 if (len > MAX_IO_SIZE)
186 len = MAX_IO_SIZE;
187 while (1) {
188 nr = pread(fd, buf, len, offset);
189 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
112db553
LT
190 continue;
191 return nr;
192 }
193}
194
559e840b
JH
195ssize_t read_in_full(int fd, void *buf, size_t count)
196{
197 char *p = buf;
198 ssize_t total = 0;
199
200 while (count > 0) {
201 ssize_t loaded = xread(fd, p, count);
56d7c27a
JK
202 if (loaded < 0)
203 return -1;
204 if (loaded == 0)
205 return total;
559e840b
JH
206 count -= loaded;
207 p += loaded;
208 total += loaded;
209 }
210
211 return total;
212}
213
214ssize_t write_in_full(int fd, const void *buf, size_t count)
215{
216 const char *p = buf;
217 ssize_t total = 0;
218
219 while (count > 0) {
220 ssize_t written = xwrite(fd, p, count);
221 if (written < 0)
222 return -1;
223 if (!written) {
224 errno = ENOSPC;
225 return -1;
226 }
227 count -= written;
228 p += written;
229 total += written;
230 }
231
232 return total;
233}
234
112db553
LT
235int xdup(int fd)
236{
237 int ret = dup(fd);
238 if (ret < 0)
d824cbba 239 die_errno("dup failed");
112db553
LT
240 return ret;
241}
242
243FILE *xfdopen(int fd, const char *mode)
244{
245 FILE *stream = fdopen(fd, mode);
246 if (stream == NULL)
d824cbba 247 die_errno("Out of memory? fdopen failed");
112db553
LT
248 return stream;
249}
250
251int xmkstemp(char *template)
252{
253 int fd;
6cf6bb3e
AE
254 char origtemplate[PATH_MAX];
255 strlcpy(origtemplate, template, sizeof(origtemplate));
112db553
LT
256
257 fd = mkstemp(template);
6cf6bb3e
AE
258 if (fd < 0) {
259 int saved_errno = errno;
260 const char *nonrelative_template;
261
f7be59b4 262 if (strlen(template) != strlen(origtemplate))
6cf6bb3e
AE
263 template = origtemplate;
264
e2a57aac 265 nonrelative_template = absolute_path(template);
6cf6bb3e
AE
266 errno = saved_errno;
267 die_errno("Unable to create temporary file '%s'",
268 nonrelative_template);
269 }
112db553
LT
270 return fd;
271}
39c68542 272
33f23936
JN
273/* git_mkstemp() - create tmp file honoring TMPDIR variable */
274int git_mkstemp(char *path, size_t len, const char *template)
275{
276 const char *tmp;
277 size_t n;
278
279 tmp = getenv("TMPDIR");
280 if (!tmp)
281 tmp = "/tmp";
282 n = snprintf(path, len, "%s/%s", tmp, template);
283 if (len <= n) {
284 errno = ENAMETOOLONG;
285 return -1;
286 }
287 return mkstemp(path);
288}
289
290/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
291int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
292{
293 const char *tmp;
294 size_t n;
295
296 tmp = getenv("TMPDIR");
297 if (!tmp)
298 tmp = "/tmp";
299 n = snprintf(path, len, "%s/%s", tmp, template);
300 if (len <= n) {
301 errno = ENAMETOOLONG;
302 return -1;
303 }
304 return mkstemps(path, suffix_len);
305}
306
307/* Adapted from libiberty's mkstemp.c. */
308
309#undef TMP_MAX
310#define TMP_MAX 16384
311
312int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
313{
314 static const char letters[] =
315 "abcdefghijklmnopqrstuvwxyz"
316 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
317 "0123456789";
318 static const int num_letters = 62;
319 uint64_t value;
320 struct timeval tv;
321 char *template;
322 size_t len;
323 int fd, count;
324
325 len = strlen(pattern);
326
327 if (len < 6 + suffix_len) {
328 errno = EINVAL;
329 return -1;
330 }
331
332 if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
333 errno = EINVAL;
334 return -1;
335 }
336
337 /*
338 * Replace pattern's XXXXXX characters with randomness.
339 * Try TMP_MAX different filenames.
340 */
341 gettimeofday(&tv, NULL);
342 value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
343 template = &pattern[len - 6 - suffix_len];
344 for (count = 0; count < TMP_MAX; ++count) {
345 uint64_t v = value;
346 /* Fill in the random bits. */
347 template[0] = letters[v % num_letters]; v /= num_letters;
348 template[1] = letters[v % num_letters]; v /= num_letters;
349 template[2] = letters[v % num_letters]; v /= num_letters;
350 template[3] = letters[v % num_letters]; v /= num_letters;
351 template[4] = letters[v % num_letters]; v /= num_letters;
352 template[5] = letters[v % num_letters]; v /= num_letters;
353
354 fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
a2cb86c1 355 if (fd >= 0)
33f23936
JN
356 return fd;
357 /*
358 * Fatal error (EPERM, ENOSPC etc).
359 * It doesn't make sense to loop.
360 */
361 if (errno != EEXIST)
362 break;
363 /*
364 * This is a random value. It is only necessary that
365 * the next TMP_MAX values generated by adding 7777 to
366 * VALUE are different with (module 2^32).
367 */
368 value += 7777;
369 }
370 /* We return the null string if we can't find a unique file name. */
371 pattern[0] = '\0';
372 return -1;
373}
374
375int git_mkstemp_mode(char *pattern, int mode)
376{
377 /* mkstemp is just mkstemps with no suffix */
378 return git_mkstemps_mode(pattern, 0, mode);
379}
380
ec145c9c 381#ifdef NO_MKSTEMPS
33f23936
JN
382int gitmkstemps(char *pattern, int suffix_len)
383{
384 return git_mkstemps_mode(pattern, suffix_len, 0600);
385}
ec145c9c 386#endif
33f23936 387
b862b61c
MM
388int xmkstemp_mode(char *template, int mode)
389{
390 int fd;
6cf6bb3e
AE
391 char origtemplate[PATH_MAX];
392 strlcpy(origtemplate, template, sizeof(origtemplate));
b862b61c
MM
393
394 fd = git_mkstemp_mode(template, mode);
6cf6bb3e
AE
395 if (fd < 0) {
396 int saved_errno = errno;
397 const char *nonrelative_template;
398
399 if (!template[0])
400 template = origtemplate;
401
e2a57aac 402 nonrelative_template = absolute_path(template);
6cf6bb3e
AE
403 errno = saved_errno;
404 die_errno("Unable to create temporary file '%s'",
405 nonrelative_template);
406 }
b862b61c
MM
407 return fd;
408}
409
10e13ec8 410static int warn_if_unremovable(const char *op, const char *file, int rc)
fc71db39 411{
fc71db39
AR
412 if (rc < 0) {
413 int err = errno;
414 if (ENOENT != err) {
10e13ec8
PC
415 warning("unable to %s %s: %s",
416 op, file, strerror(errno));
fc71db39
AR
417 errno = err;
418 }
419 }
420 return rc;
421}
422
10e13ec8
PC
423int unlink_or_warn(const char *file)
424{
425 return warn_if_unremovable("unlink", file, unlink(file));
426}
d1723296
PC
427
428int rmdir_or_warn(const char *file)
429{
430 return warn_if_unremovable("rmdir", file, rmdir(file));
431}
80d706af
PC
432
433int remove_or_warn(unsigned int mode, const char *file)
434{
435 return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
436}
2f705875 437
55b38a48
JH
438void warn_on_inaccessible(const char *path)
439{
440 warning(_("unable to access '%s': %s"), path, strerror(errno));
441}
442
4698c8fe
JN
443static int access_error_is_ok(int err, unsigned flag)
444{
445 return err == ENOENT || err == ENOTDIR ||
446 ((flag & ACCESS_EACCES_OK) && err == EACCES);
447}
448
449int access_or_warn(const char *path, int mode, unsigned flag)
ba8bd830
JK
450{
451 int ret = access(path, mode);
4698c8fe 452 if (ret && !access_error_is_ok(errno, flag))
55b38a48 453 warn_on_inaccessible(path);
ba8bd830
JK
454 return ret;
455}
456
4698c8fe 457int access_or_die(const char *path, int mode, unsigned flag)
96b9e0e3
JN
458{
459 int ret = access(path, mode);
4698c8fe 460 if (ret && !access_error_is_ok(errno, flag))
96b9e0e3
JN
461 die_errno(_("unable to access '%s'"), path);
462 return ret;
463}
464
2f705875
JK
465struct passwd *xgetpwuid_self(void)
466{
467 struct passwd *pw;
468
469 errno = 0;
470 pw = getpwuid(getuid());
471 if (!pw)
472 die(_("unable to look up current user in the passwd file: %s"),
473 errno ? strerror(errno) : _("no such user"));
474 return pw;
475}