2 * Various trivial helper wrappers around standard functions
6 static void try_to_free_builtin(size_t size)
8 release_pack_memory(size, -1);
11 static void (*try_to_free_routine)(size_t size) = try_to_free_builtin;
13 try_to_free_t set_try_to_free_routine(try_to_free_t routine)
15 try_to_free_t old = try_to_free_routine;
16 try_to_free_routine = routine;
20 char *xstrdup(const char *str)
22 char *ret = strdup(str);
24 try_to_free_routine(strlen(str) + 1);
27 die("Out of memory, strdup failed");
32 void *xmalloc(size_t size)
34 void *ret = malloc(size);
38 try_to_free_routine(size);
43 die("Out of memory, malloc failed");
46 memset(ret, 0xA5, size);
51 void *xmallocz(size_t size)
55 die("Data too large to fit into virtual memory space.");
56 ret = xmalloc(size + 1);
57 ((char*)ret)[size] = 0;
62 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
63 * "data" to the allocated memory, zero terminates the allocated memory,
64 * and returns a pointer to the allocated memory. If the allocation fails,
67 void *xmemdupz(const void *data, size_t len)
69 return memcpy(xmallocz(len), data, len);
72 char *xstrndup(const char *str, size_t len)
74 char *p = memchr(str, '\0', len);
75 return xmemdupz(str, p ? p - str : len);
78 void *xrealloc(void *ptr, size_t size)
80 void *ret = realloc(ptr, size);
82 ret = realloc(ptr, 1);
84 try_to_free_routine(size);
85 ret = realloc(ptr, size);
87 ret = realloc(ptr, 1);
89 die("Out of memory, realloc failed");
94 void *xcalloc(size_t nmemb, size_t size)
96 void *ret = calloc(nmemb, size);
97 if (!ret && (!nmemb || !size))
100 try_to_free_routine(nmemb * size);
101 ret = calloc(nmemb, size);
102 if (!ret && (!nmemb || !size))
105 die("Out of memory, calloc failed");
110 void *xmmap(void *start, size_t length,
111 int prot, int flags, int fd, off_t offset)
113 void *ret = mmap(start, length, prot, flags, fd, offset);
114 if (ret == MAP_FAILED) {
117 release_pack_memory(length, fd);
118 ret = mmap(start, length, prot, flags, fd, offset);
119 if (ret == MAP_FAILED)
120 die_errno("Out of memory? mmap failed");
126 * xread() is the same a read(), but it automatically restarts read()
127 * operations with a recoverable error (EAGAIN and EINTR). xread()
128 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
130 ssize_t xread(int fd, void *buf, size_t len)
134 nr = read(fd, buf, len);
135 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
142 * xwrite() is the same a write(), but it automatically restarts write()
143 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
144 * GUARANTEE that "len" bytes is written even if the operation is successful.
146 ssize_t xwrite(int fd, const void *buf, size_t len)
150 nr = write(fd, buf, len);
151 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
157 ssize_t read_in_full(int fd, void *buf, size_t count)
163 ssize_t loaded = xread(fd, p, count);
165 return total ? total : loaded;
174 ssize_t write_in_full(int fd, const void *buf, size_t count)
180 ssize_t written = xwrite(fd, p, count);
199 die_errno("dup failed");
203 FILE *xfdopen(int fd, const char *mode)
205 FILE *stream = fdopen(fd, mode);
207 die_errno("Out of memory? fdopen failed");
211 int xmkstemp(char *template)
215 fd = mkstemp(template);
217 die_errno("Unable to create temporary file");
221 int xmkstemp_mode(char *template, int mode)
225 fd = git_mkstemp_mode(template, mode);
227 die_errno("Unable to create temporary file");
232 * zlib wrappers to make sure we don't silently miss errors
235 void git_inflate_init(z_streamp strm)
239 switch (inflateInit(strm)) {
244 err = "out of memory";
246 case Z_VERSION_ERROR:
247 err = "wrong version";
252 die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
255 void git_inflate_end(z_streamp strm)
257 if (inflateEnd(strm) != Z_OK)
258 error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
261 int git_inflate(z_streamp strm, int flush)
263 int ret = inflate(strm, flush);
267 /* Out of memory is fatal. */
269 die("inflate: out of memory");
271 /* Data corruption errors: we may want to recover from them (fsck) */
273 err = "needs dictionary"; break;
275 err = "data stream error"; break;
277 err = "stream consistency error"; break;
279 err = "unknown error"; break;
281 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
287 error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
291 int odb_mkstemp(char *template, size_t limit, const char *pattern)
295 * we let the umask do its job, don't try to be more
296 * restrictive except to remove write permission.
299 snprintf(template, limit, "%s/%s",
300 get_object_directory(), pattern);
301 fd = git_mkstemp_mode(template, mode);
306 /* some mkstemp implementations erase template on failure */
307 snprintf(template, limit, "%s/%s",
308 get_object_directory(), pattern);
309 safe_create_leading_directories(template);
310 return xmkstemp_mode(template, mode);
313 int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
317 snprintf(name, namesz, "%s/pack/pack-%s.keep",
318 get_object_directory(), sha1_to_hex(sha1));
319 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
324 safe_create_leading_directories(name);
325 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
328 static int warn_if_unremovable(const char *op, const char *file, int rc)
333 warning("unable to %s %s: %s",
334 op, file, strerror(errno));
341 int unlink_or_warn(const char *file)
343 return warn_if_unremovable("unlink", file, unlink(file));
346 int rmdir_or_warn(const char *file)
348 return warn_if_unremovable("rmdir", file, rmdir(file));
351 int remove_or_warn(unsigned int mode, const char *file)
353 return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);