1/*
2 * Various trivial helper wrappers around standard functions
3 */
4#include "cache.h"
5
6char *xstrdup(const char *str)
7{
8 char *ret = strdup(str);
9 if (!ret) {
10 release_pack_memory(strlen(str) + 1, -1);
11 ret = strdup(str);
12 if (!ret)
13 die("Out of memory, strdup failed");
14 }
15 return ret;
16}
17
18void *xmalloc(size_t size)
19{
20 void *ret = malloc(size);
21 if (!ret && !size)
22 ret = malloc(1);
23 if (!ret) {
24 release_pack_memory(size, -1);
25 ret = malloc(size);
26 if (!ret && !size)
27 ret = malloc(1);
28 if (!ret)
29 die("Out of memory, malloc failed");
30 }
31#ifdef XMALLOC_POISON
32 memset(ret, 0xA5, size);
33#endif
34 return ret;
35}
36
37/*
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,
41 * the program dies.
42 */
43void *xmemdupz(const void *data, size_t len)
44{
45 char *p = xmalloc(len + 1);
46 memcpy(p, data, len);
47 p[len] = '\0';
48 return p;
49}
50
51char *xstrndup(const char *str, size_t len)
52{
53 char *p = memchr(str, '\0', len);
54 return xmemdupz(str, p ? p - str : len);
55}
56
57void *xrealloc(void *ptr, size_t size)
58{
59 void *ret = realloc(ptr, size);
60 if (!ret && !size)
61 ret = realloc(ptr, 1);
62 if (!ret) {
63 release_pack_memory(size, -1);
64 ret = realloc(ptr, size);
65 if (!ret && !size)
66 ret = realloc(ptr, 1);
67 if (!ret)
68 die("Out of memory, realloc failed");
69 }
70 return ret;
71}
72
73void *xcalloc(size_t nmemb, size_t size)
74{
75 void *ret = calloc(nmemb, size);
76 if (!ret && (!nmemb || !size))
77 ret = calloc(1, 1);
78 if (!ret) {
79 release_pack_memory(nmemb * size, -1);
80 ret = calloc(nmemb, size);
81 if (!ret && (!nmemb || !size))
82 ret = calloc(1, 1);
83 if (!ret)
84 die("Out of memory, calloc failed");
85 }
86 return ret;
87}
88
89void *xmmap(void *start, size_t length,
90 int prot, int flags, int fd, off_t offset)
91{
92 void *ret = mmap(start, length, prot, flags, fd, offset);
93 if (ret == MAP_FAILED) {
94 if (!length)
95 return NULL;
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));
100 }
101 return ret;
102}
103
104/*
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.
108 */
109ssize_t xread(int fd, void *buf, size_t len)
110{
111 ssize_t nr;
112 while (1) {
113 nr = read(fd, buf, len);
114 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
115 continue;
116 return nr;
117 }
118}
119
120/*
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.
124 */
125ssize_t xwrite(int fd, const void *buf, size_t len)
126{
127 ssize_t nr;
128 while (1) {
129 nr = write(fd, buf, len);
130 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
131 continue;
132 return nr;
133 }
134}
135
136ssize_t read_in_full(int fd, void *buf, size_t count)
137{
138 char *p = buf;
139 ssize_t total = 0;
140
141 while (count > 0) {
142 ssize_t loaded = xread(fd, p, count);
143 if (loaded <= 0)
144 return total ? total : loaded;
145 count -= loaded;
146 p += loaded;
147 total += loaded;
148 }
149
150 return total;
151}
152
153ssize_t write_in_full(int fd, const void *buf, size_t count)
154{
155 const char *p = buf;
156 ssize_t total = 0;
157
158 while (count > 0) {
159 ssize_t written = xwrite(fd, p, count);
160 if (written < 0)
161 return -1;
162 if (!written) {
163 errno = ENOSPC;
164 return -1;
165 }
166 count -= written;
167 p += written;
168 total += written;
169 }
170
171 return total;
172}
173
174int xdup(int fd)
175{
176 int ret = dup(fd);
177 if (ret < 0)
178 die("dup failed: %s", strerror(errno));
179 return ret;
180}
181
182FILE *xfdopen(int fd, const char *mode)
183{
184 FILE *stream = fdopen(fd, mode);
185 if (stream == NULL)
186 die("Out of memory? fdopen failed: %s", strerror(errno));
187 return stream;
188}
189
190int xmkstemp(char *template)
191{
192 int fd;
193
194 fd = mkstemp(template);
195 if (fd < 0)
196 die("Unable to create temporary file: %s", strerror(errno));
197 return fd;
198}
199
200/*
201 * zlib wrappers to make sure we don't silently miss errors
202 * at init time.
203 */
204void git_inflate_init(z_streamp strm)
205{
206 const char *err;
207
208 switch (inflateInit(strm)) {
209 case Z_OK:
210 return;
211
212 case Z_MEM_ERROR:
213 err = "out of memory";
214 break;
215 case Z_VERSION_ERROR:
216 err = "wrong version";
217 break;
218 default:
219 err = "error";
220 }
221 die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
222}
223
224void git_inflate_end(z_streamp strm)
225{
226 if (inflateEnd(strm) != Z_OK)
227 error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
228}
229
230int git_inflate(z_streamp strm, int flush)
231{
232 int ret = inflate(strm, flush);
233 const char *err;
234
235 switch (ret) {
236 /* Out of memory is fatal. */
237 case Z_MEM_ERROR:
238 die("inflate: out of memory");
239
240 /* Data corruption errors: we may want to recover from them (fsck) */
241 case Z_NEED_DICT:
242 err = "needs dictionary"; break;
243 case Z_DATA_ERROR:
244 err = "data stream error"; break;
245 case Z_STREAM_ERROR:
246 err = "stream consistency error"; break;
247 default:
248 err = "unknown error"; break;
249
250 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
251 case Z_BUF_ERROR:
252 case Z_OK:
253 case Z_STREAM_END:
254 return ret;
255 }
256 error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
257 return ret;
258}
259
260int odb_mkstemp(char *template, size_t limit, const char *pattern)
261{
262 int fd;
263
264 snprintf(template, limit, "%s/%s",
265 get_object_directory(), pattern);
266 fd = mkstemp(template);
267 if (0 <= fd)
268 return fd;
269
270 /* slow path */
271 safe_create_leading_directories(template);
272 snprintf(template, limit, "%s/%s",
273 get_object_directory(), pattern);
274 return xmkstemp(template);
275}
276
277int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
278{
279 int fd;
280
281 snprintf(name, namesz, "%s/pack/pack-%s.keep",
282 get_object_directory(), sha1_to_hex(sha1));
283 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
284 if (0 <= fd)
285 return fd;
286
287 /* slow path */
288 safe_create_leading_directories(name);
289 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
290}