]>
git.ipfire.org Git - thirdparty/git.git/blob - wrapper.c
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
);
38 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
39 * "data" to the allocated memory, zero terminates the allocated memory,
40 * and returns a pointer to the allocated memory. If the allocation fails,
43 void *xmemdupz(const void *data
, size_t len
)
45 char *p
= xmalloc(len
+ 1);
51 char *xstrndup(const char *str
, size_t len
)
53 char *p
= memchr(str
, '\0', len
);
54 return xmemdupz(str
, p
? p
- str
: len
);
57 void *xrealloc(void *ptr
, size_t size
)
59 void *ret
= realloc(ptr
, size
);
61 ret
= realloc(ptr
, 1);
63 release_pack_memory(size
, -1);
64 ret
= realloc(ptr
, size
);
66 ret
= realloc(ptr
, 1);
68 die("Out of memory, realloc failed");
73 void *xcalloc(size_t nmemb
, size_t size
)
75 void *ret
= calloc(nmemb
, size
);
76 if (!ret
&& (!nmemb
|| !size
))
79 release_pack_memory(nmemb
* size
, -1);
80 ret
= calloc(nmemb
, size
);
81 if (!ret
&& (!nmemb
|| !size
))
84 die("Out of memory, calloc failed");
89 void *xmmap(void *start
, size_t length
,
90 int prot
, int flags
, int fd
, off_t offset
)
92 void *ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
93 if (ret
== MAP_FAILED
) {
96 release_pack_memory(length
, fd
);
97 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
98 if (ret
== MAP_FAILED
)
99 die("Out of memory? mmap failed: %s", strerror(errno
));
105 * xread() is the same a read(), but it automatically restarts read()
106 * operations with a recoverable error (EAGAIN and EINTR). xread()
107 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
109 ssize_t
xread(int fd
, void *buf
, size_t len
)
113 nr
= read(fd
, buf
, len
);
114 if ((nr
< 0) && (errno
== EAGAIN
|| errno
== EINTR
))
121 * xwrite() is the same a write(), but it automatically restarts write()
122 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
123 * GUARANTEE that "len" bytes is written even if the operation is successful.
125 ssize_t
xwrite(int fd
, const void *buf
, size_t len
)
129 nr
= write(fd
, buf
, len
);
130 if ((nr
< 0) && (errno
== EAGAIN
|| errno
== EINTR
))
136 ssize_t
read_in_full(int fd
, void *buf
, size_t count
)
142 ssize_t loaded
= xread(fd
, p
, count
);
144 return total
? total
: loaded
;
153 ssize_t
write_in_full(int fd
, const void *buf
, size_t count
)
159 ssize_t written
= xwrite(fd
, p
, count
);
178 die("dup failed: %s", strerror(errno
));
182 FILE *xfdopen(int fd
, const char *mode
)
184 FILE *stream
= fdopen(fd
, mode
);
186 die("Out of memory? fdopen failed: %s", strerror(errno
));
190 int xmkstemp(char *template)
194 fd
= mkstemp(template);
196 die("Unable to create temporary file: %s", strerror(errno
));
201 * zlib wrappers to make sure we don't silently miss errors
204 void git_inflate_init(z_streamp strm
)
208 switch (inflateInit(strm
)) {
213 err
= "out of memory";
215 case Z_VERSION_ERROR
:
216 err
= "wrong version";
221 die("inflateInit: %s (%s)", err
, strm
->msg
? strm
->msg
: "no message");
224 void git_inflate_end(z_streamp strm
)
226 if (inflateEnd(strm
) != Z_OK
)
227 error("inflateEnd: %s", strm
->msg
? strm
->msg
: "failed");
230 int git_inflate(z_streamp strm
, int flush
)
232 int ret
= inflate(strm
, flush
);
236 /* Out of memory is fatal. */
238 die("inflate: out of memory");
240 /* Data corruption errors: we may want to recover from them (fsck) */
242 err
= "needs dictionary"; break;
244 err
= "data stream error"; break;
246 err
= "stream consistency error"; break;
248 err
= "unknown error"; break;
250 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
256 error("inflate: %s (%s)", err
, strm
->msg
? strm
->msg
: "no message");
260 int odb_mkstemp(char *template, size_t limit
, const char *pattern
)
264 snprintf(template, limit
, "%s/%s",
265 get_object_directory(), pattern
);
266 fd
= mkstemp(template);
271 /* some mkstemp implementations erase template on failure */
272 snprintf(template, limit
, "%s/%s",
273 get_object_directory(), pattern
);
274 safe_create_leading_directories(template);
275 return xmkstemp(template);
278 int odb_pack_keep(char *name
, size_t namesz
, unsigned char *sha1
)
282 snprintf(name
, namesz
, "%s/pack/pack-%s.keep",
283 get_object_directory(), sha1_to_hex(sha1
));
284 fd
= open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
289 safe_create_leading_directories(name
);
290 return open(name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);