t2021: mark a test as fixed
[git/dscho.git] / wrapper.c
blob8d7dd31c4ba5439652d11e5ada06e0d52bc04e4f
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 try_to_free_t set_try_to_free_routine(try_to_free_t routine)
14 try_to_free_t old = try_to_free_routine;
15 if (!routine)
16 routine = do_nothing;
17 try_to_free_routine = routine;
18 return old;
21 char *xstrdup(const char *str)
23 char *ret = strdup(str);
24 if (!ret) {
25 try_to_free_routine(strlen(str) + 1);
26 ret = strdup(str);
27 if (!ret)
28 die("Out of memory, strdup failed");
30 return ret;
33 void *xmalloc(size_t size)
35 void *ret = malloc(size);
36 if (!ret && !size)
37 ret = malloc(1);
38 if (!ret) {
39 try_to_free_routine(size);
40 ret = malloc(size);
41 if (!ret && !size)
42 ret = malloc(1);
43 if (!ret)
44 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
45 (unsigned long)size);
47 #ifdef XMALLOC_POISON
48 memset(ret, 0xA5, size);
49 #endif
50 return ret;
53 void *xmallocz(size_t size)
55 void *ret;
56 if (size + 1 < size)
57 die("Data too large to fit into virtual memory space.");
58 ret = xmalloc(size + 1);
59 ((char*)ret)[size] = 0;
60 return ret;
64 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
65 * "data" to the allocated memory, zero terminates the allocated memory,
66 * and returns a pointer to the allocated memory. If the allocation fails,
67 * the program dies.
69 void *xmemdupz(const void *data, size_t len)
71 return memcpy(xmallocz(len), data, len);
74 char *xstrndup(const char *str, size_t len)
76 char *p = memchr(str, '\0', len);
77 return xmemdupz(str, p ? p - str : len);
80 void *xrealloc(void *ptr, size_t size)
82 void *ret = realloc(ptr, size);
83 if (!ret && !size)
84 ret = realloc(ptr, 1);
85 if (!ret) {
86 try_to_free_routine(size);
87 ret = realloc(ptr, size);
88 if (!ret && !size)
89 ret = realloc(ptr, 1);
90 if (!ret)
91 die("Out of memory, realloc failed");
93 return ret;
96 void *xcalloc(size_t nmemb, size_t size)
98 void *ret = calloc(nmemb, size);
99 if (!ret && (!nmemb || !size))
100 ret = calloc(1, 1);
101 if (!ret) {
102 try_to_free_routine(nmemb * size);
103 ret = calloc(nmemb, size);
104 if (!ret && (!nmemb || !size))
105 ret = calloc(1, 1);
106 if (!ret)
107 die("Out of memory, calloc failed");
109 return ret;
113 * xread() is the same a read(), but it automatically restarts read()
114 * operations with a recoverable error (EAGAIN and EINTR). xread()
115 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
117 ssize_t xread(int fd, void *buf, size_t len)
119 ssize_t nr;
120 while (1) {
121 nr = read(fd, buf, len);
122 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
123 continue;
124 return nr;
129 * xwrite() is the same a write(), but it automatically restarts write()
130 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
131 * GUARANTEE that "len" bytes is written even if the operation is successful.
133 ssize_t xwrite(int fd, const void *buf, size_t len)
135 ssize_t nr;
136 while (1) {
137 nr = write(fd, buf, len);
138 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
139 continue;
140 return nr;
144 ssize_t read_in_full(int fd, void *buf, size_t count)
146 char *p = buf;
147 ssize_t total = 0;
149 while (count > 0) {
150 ssize_t loaded = xread(fd, p, count);
151 if (loaded <= 0)
152 return total ? total : loaded;
153 count -= loaded;
154 p += loaded;
155 total += loaded;
158 return total;
161 ssize_t write_in_full(int fd, const void *buf, size_t count)
163 const char *p = buf;
164 ssize_t total = 0;
166 while (count > 0) {
167 ssize_t written = xwrite(fd, p, count);
168 if (written < 0)
169 return -1;
170 if (!written) {
171 errno = ENOSPC;
172 return -1;
174 count -= written;
175 p += written;
176 total += written;
179 return total;
182 int xdup(int fd)
184 int ret = dup(fd);
185 if (ret < 0)
186 die_errno("dup failed");
187 return ret;
190 FILE *xfdopen(int fd, const char *mode)
192 FILE *stream = fdopen(fd, mode);
193 if (stream == NULL)
194 die_errno("Out of memory? fdopen failed");
195 return stream;
198 int xmkstemp(char *template)
200 int fd;
202 fd = mkstemp(template);
203 if (fd < 0)
204 die_errno("Unable to create temporary file");
205 return fd;
208 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
209 int git_mkstemp(char *path, size_t len, const char *template)
211 const char *tmp;
212 size_t n;
214 tmp = getenv("TMPDIR");
215 if (!tmp)
216 tmp = "/tmp";
217 n = snprintf(path, len, "%s/%s", tmp, template);
218 if (len <= n) {
219 errno = ENAMETOOLONG;
220 return -1;
222 return mkstemp(path);
225 /* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
226 int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
228 const char *tmp;
229 size_t n;
231 tmp = getenv("TMPDIR");
232 if (!tmp)
233 tmp = "/tmp";
234 n = snprintf(path, len, "%s/%s", tmp, template);
235 if (len <= n) {
236 errno = ENAMETOOLONG;
237 return -1;
239 return mkstemps(path, suffix_len);
242 /* Adapted from libiberty's mkstemp.c. */
244 #undef TMP_MAX
245 #define TMP_MAX 16384
247 int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
249 static const char letters[] =
250 "abcdefghijklmnopqrstuvwxyz"
251 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
252 "0123456789";
253 static const int num_letters = 62;
254 uint64_t value;
255 struct timeval tv;
256 char *template;
257 size_t len;
258 int fd, count;
260 len = strlen(pattern);
262 if (len < 6 + suffix_len) {
263 errno = EINVAL;
264 return -1;
267 if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
268 errno = EINVAL;
269 return -1;
273 * Replace pattern's XXXXXX characters with randomness.
274 * Try TMP_MAX different filenames.
276 gettimeofday(&tv, NULL);
277 value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
278 template = &pattern[len - 6 - suffix_len];
279 for (count = 0; count < TMP_MAX; ++count) {
280 uint64_t v = value;
281 /* Fill in the random bits. */
282 template[0] = letters[v % num_letters]; v /= num_letters;
283 template[1] = letters[v % num_letters]; v /= num_letters;
284 template[2] = letters[v % num_letters]; v /= num_letters;
285 template[3] = letters[v % num_letters]; v /= num_letters;
286 template[4] = letters[v % num_letters]; v /= num_letters;
287 template[5] = letters[v % num_letters]; v /= num_letters;
289 fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
290 if (fd > 0)
291 return fd;
293 * Fatal error (EPERM, ENOSPC etc).
294 * It doesn't make sense to loop.
296 if (errno != EEXIST)
297 break;
299 * This is a random value. It is only necessary that
300 * the next TMP_MAX values generated by adding 7777 to
301 * VALUE are different with (module 2^32).
303 value += 7777;
305 /* We return the null string if we can't find a unique file name. */
306 pattern[0] = '\0';
307 return -1;
310 int git_mkstemp_mode(char *pattern, int mode)
312 /* mkstemp is just mkstemps with no suffix */
313 return git_mkstemps_mode(pattern, 0, mode);
316 int gitmkstemps(char *pattern, int suffix_len)
318 return git_mkstemps_mode(pattern, suffix_len, 0600);
321 int xmkstemp_mode(char *template, int mode)
323 int fd;
325 fd = git_mkstemp_mode(template, mode);
326 if (fd < 0)
327 die_errno("Unable to create temporary file");
328 return fd;
331 static int warn_if_unremovable(const char *op, const char *file, int rc)
333 if (rc < 0) {
334 int err = errno;
335 if (ENOENT != err) {
336 warning("unable to %s %s: %s",
337 op, file, strerror(errno));
338 errno = err;
341 return rc;
344 int unlink_or_warn(const char *file)
346 return warn_if_unremovable("unlink", file, unlink(file));
349 int rmdir_or_warn(const char *file)
351 return warn_if_unremovable("rmdir", file, rmdir(file));
354 int remove_or_warn(unsigned int mode, const char *file)
356 return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);