2 * Various trivial helper wrappers around standard functions
6 char *xstrdup(const char *str)
8 char *ret = strdup(str);
10 release_pack_memory(strlen(str) + 1, -1);
13 die("Out of memory, strdup failed");
18 void *xmalloc(size_t size)
20 void *ret = malloc(size);
24 release_pack_memory(size, -1);
29 die("Out of memory, malloc failed");
32 memset(ret, 0xA5, size);
37 void *xmallocz(size_t size)
41 die("Data too large to fit into virtual memory space.");
42 ret = xmalloc(size + 1);
43 ((char*)ret)[size] = 0;
48 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
49 * "data" to the allocated memory, zero terminates the allocated memory,
50 * and returns a pointer to the allocated memory. If the allocation fails,
53 void *xmemdupz(const void *data, size_t len)
55 return memcpy(xmallocz(len), data, len);
58 char *xstrndup(const char *str, size_t len)
60 char *p = memchr(str, '\0', len);
61 return xmemdupz(str, p ? p - str : len);
64 void *xrealloc(void *ptr, size_t size)
66 void *ret = realloc(ptr, size);
68 ret = realloc(ptr, 1);
70 release_pack_memory(size, -1);
71 ret = realloc(ptr, size);
73 ret = realloc(ptr, 1);
75 die("Out of memory, realloc failed");
80 void *xcalloc(size_t nmemb, size_t size)
82 void *ret = calloc(nmemb, size);
83 if (!ret && (!nmemb || !size))
86 release_pack_memory(nmemb * size, -1);
87 ret = calloc(nmemb, size);
88 if (!ret && (!nmemb || !size))
91 die("Out of memory, calloc failed");
96 void *xmmap(void *start, size_t length,
97 int prot, int flags, int fd, off_t offset)
99 void *ret = mmap(start, length, prot, flags, fd, offset);
100 if (ret == MAP_FAILED) {
103 release_pack_memory(length, fd);
104 ret = mmap(start, length, prot, flags, fd, offset);
105 if (ret == MAP_FAILED)
106 die_errno("Out of memory? mmap failed");
112 * xread() is the same a read(), but it automatically restarts read()
113 * operations with a recoverable error (EAGAIN and EINTR). xread()
114 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
116 ssize_t xread(int fd, void *buf, size_t len)
120 nr = read(fd, buf, len);
121 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
128 * xwrite() is the same a write(), but it automatically restarts write()
129 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
130 * GUARANTEE that "len" bytes is written even if the operation is successful.
132 ssize_t xwrite(int fd, const void *buf, size_t len)
136 nr = write(fd, buf, len);
137 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
143 ssize_t read_in_full(int fd, void *buf, size_t count)
149 ssize_t loaded = xread(fd, p, count);
151 return total ? total : loaded;
160 ssize_t write_in_full(int fd, const void *buf, size_t count)
166 ssize_t written = xwrite(fd, p, count);
185 die_errno("dup failed");
189 FILE *xfdopen(int fd, const char *mode)
191 FILE *stream = fdopen(fd, mode);
193 die_errno("Out of memory? fdopen failed");
197 int xmkstemp(char *template)
201 fd = mkstemp(template);
203 die_errno("Unable to create temporary file");
207 int xmkstemp_mode(char *template, int mode)
211 fd = git_mkstemp_mode(template, mode);
213 die_errno("Unable to create temporary file");
218 * zlib wrappers to make sure we don't silently miss errors
221 void git_inflate_init(z_streamp strm)
225 switch (inflateInit(strm)) {
230 err = "out of memory";
232 case Z_VERSION_ERROR:
233 err = "wrong version";
238 die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
241 void git_inflate_end(z_streamp strm)
243 if (inflateEnd(strm) != Z_OK)
244 error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
247 int git_inflate(z_streamp strm, int flush)
249 int ret = inflate(strm, flush);
253 /* Out of memory is fatal. */
255 die("inflate: out of memory");
257 /* Data corruption errors: we may want to recover from them (fsck) */
259 err = "needs dictionary"; break;
261 err = "data stream error"; break;
263 err = "stream consistency error"; break;
265 err = "unknown error"; break;
267 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
273 error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
277 int odb_mkstemp(char *template, size_t limit, const char *pattern)
281 * we let the umask do its job, don't try to be more
282 * restrictive except to remove write permission.
285 snprintf(template, limit, "%s/%s",
286 get_object_directory(), pattern);
287 fd = git_mkstemp_mode(template, mode);
292 /* some mkstemp implementations erase template on failure */
293 snprintf(template, limit, "%s/%s",
294 get_object_directory(), pattern);
295 safe_create_leading_directories(template);
296 return xmkstemp_mode(template, mode);
299 int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
303 snprintf(name, namesz, "%s/pack/pack-%s.keep",
304 get_object_directory(), sha1_to_hex(sha1));
305 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
310 safe_create_leading_directories(name);
311 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
314 int unlink_or_warn(const char *file)
316 int rc = unlink(file);
321 warning("unable to unlink %s: %s",
322 file, strerror(errno));