streaming: make streaming-write-entry to be more reusable
[git/git.git] / wrapper.c
CommitLineData
112db553
LT
1/*
2 * Various trivial helper wrappers around standard functions
3 */
4#include "cache.h"
5
e0500293 6static void do_nothing(size_t size)
a9a74636 7{
a9a74636
NP
8}
9
e0500293 10static void (*try_to_free_routine)(size_t size) = do_nothing;
a9a74636 11
851c34b0 12try_to_free_t set_try_to_free_routine(try_to_free_t routine)
a9a74636 13{
851c34b0 14 try_to_free_t old = try_to_free_routine;
00b0d7f7
JH
15 if (!routine)
16 routine = do_nothing;
851c34b0
JS
17 try_to_free_routine = routine;
18 return old;
a9a74636
NP
19}
20
112db553
LT
21char *xstrdup(const char *str)
22{
23 char *ret = strdup(str);
24 if (!ret) {
a9a74636 25 try_to_free_routine(strlen(str) + 1);
112db553
LT
26 ret = strdup(str);
27 if (!ret)
28 die("Out of memory, strdup failed");
29 }
30 return ret;
31}
32
33void *xmalloc(size_t size)
34{
35 void *ret = malloc(size);
36 if (!ret && !size)
37 ret = malloc(1);
38 if (!ret) {
a9a74636 39 try_to_free_routine(size);
112db553
LT
40 ret = malloc(size);
41 if (!ret && !size)
42 ret = malloc(1);
43 if (!ret)
8bd9fd50
MM
44 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
45 (unsigned long)size);
112db553
LT
46 }
47#ifdef XMALLOC_POISON
48 memset(ret, 0xA5, size);
49#endif
50 return ret;
51}
52
5bf9219d
IL
53void *xmallocz(size_t size)
54{
55 void *ret;
1368f650 56 if (unsigned_add_overflows(size, 1))
5bf9219d
IL
57 die("Data too large to fit into virtual memory space.");
58 ret = xmalloc(size + 1);
59 ((char*)ret)[size] = 0;
60 return ret;
61}
62
112db553
LT
63/*
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.
68 */
69void *xmemdupz(const void *data, size_t len)
70{
5bf9219d 71 return memcpy(xmallocz(len), data, len);
112db553
LT
72}
73
74char *xstrndup(const char *str, size_t len)
75{
76 char *p = memchr(str, '\0', len);
77 return xmemdupz(str, p ? p - str : len);
78}
79
80void *xrealloc(void *ptr, size_t size)
81{
82 void *ret = realloc(ptr, size);
83 if (!ret && !size)
84 ret = realloc(ptr, 1);
85 if (!ret) {
a9a74636 86 try_to_free_routine(size);
112db553
LT
87 ret = realloc(ptr, size);
88 if (!ret && !size)
89 ret = realloc(ptr, 1);
90 if (!ret)
91 die("Out of memory, realloc failed");
92 }
93 return ret;
94}
95
96void *xcalloc(size_t nmemb, size_t size)
97{
98 void *ret = calloc(nmemb, size);
99 if (!ret && (!nmemb || !size))
100 ret = calloc(1, 1);
101 if (!ret) {
a9a74636 102 try_to_free_routine(nmemb * size);
112db553
LT
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");
108 }
109 return ret;
110}
111
112db553
LT
112/*
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.
116 */
117ssize_t xread(int fd, void *buf, size_t len)
118{
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;
125 }
126}
127
128/*
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.
132 */
133ssize_t xwrite(int fd, const void *buf, size_t len)
134{
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;
141 }
142}
143
559e840b
JH
144ssize_t read_in_full(int fd, void *buf, size_t count)
145{
146 char *p = buf;
147 ssize_t total = 0;
148
149 while (count > 0) {
150 ssize_t loaded = xread(fd, p, count);
56d7c27a
JK
151 if (loaded < 0)
152 return -1;
153 if (loaded == 0)
154 return total;
559e840b
JH
155 count -= loaded;
156 p += loaded;
157 total += loaded;
158 }
159
160 return total;
161}
162
163ssize_t write_in_full(int fd, const void *buf, size_t count)
164{
165 const char *p = buf;
166 ssize_t total = 0;
167
168 while (count > 0) {
169 ssize_t written = xwrite(fd, p, count);
170 if (written < 0)
171 return -1;
172 if (!written) {
173 errno = ENOSPC;
174 return -1;
175 }
176 count -= written;
177 p += written;
178 total += written;
179 }
180
181 return total;
182}
183
112db553
LT
184int xdup(int fd)
185{
186 int ret = dup(fd);
187 if (ret < 0)
d824cbba 188 die_errno("dup failed");
112db553
LT
189 return ret;
190}
191
192FILE *xfdopen(int fd, const char *mode)
193{
194 FILE *stream = fdopen(fd, mode);
195 if (stream == NULL)
d824cbba 196 die_errno("Out of memory? fdopen failed");
112db553
LT
197 return stream;
198}
199
200int xmkstemp(char *template)
201{
202 int fd;
6cf6bb3e
AE
203 char origtemplate[PATH_MAX];
204 strlcpy(origtemplate, template, sizeof(origtemplate));
112db553
LT
205
206 fd = mkstemp(template);
6cf6bb3e
AE
207 if (fd < 0) {
208 int saved_errno = errno;
209 const char *nonrelative_template;
210
211 if (!template[0])
212 template = origtemplate;
213
e2a57aac 214 nonrelative_template = absolute_path(template);
6cf6bb3e
AE
215 errno = saved_errno;
216 die_errno("Unable to create temporary file '%s'",
217 nonrelative_template);
218 }
112db553
LT
219 return fd;
220}
39c68542 221
33f23936
JN
222/* git_mkstemp() - create tmp file honoring TMPDIR variable */
223int git_mkstemp(char *path, size_t len, const char *template)
224{
225 const char *tmp;
226 size_t n;
227
228 tmp = getenv("TMPDIR");
229 if (!tmp)
230 tmp = "/tmp";
231 n = snprintf(path, len, "%s/%s", tmp, template);
232 if (len <= n) {
233 errno = ENAMETOOLONG;
234 return -1;
235 }
236 return mkstemp(path);
237}
238
239/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
240int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
241{
242 const char *tmp;
243 size_t n;
244
245 tmp = getenv("TMPDIR");
246 if (!tmp)
247 tmp = "/tmp";
248 n = snprintf(path, len, "%s/%s", tmp, template);
249 if (len <= n) {
250 errno = ENAMETOOLONG;
251 return -1;
252 }
253 return mkstemps(path, suffix_len);
254}
255
256/* Adapted from libiberty's mkstemp.c. */
257
258#undef TMP_MAX
259#define TMP_MAX 16384
260
261int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
262{
263 static const char letters[] =
264 "abcdefghijklmnopqrstuvwxyz"
265 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
266 "0123456789";
267 static const int num_letters = 62;
268 uint64_t value;
269 struct timeval tv;
270 char *template;
271 size_t len;
272 int fd, count;
273
274 len = strlen(pattern);
275
276 if (len < 6 + suffix_len) {
277 errno = EINVAL;
278 return -1;
279 }
280
281 if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
282 errno = EINVAL;
283 return -1;
284 }
285
286 /*
287 * Replace pattern's XXXXXX characters with randomness.
288 * Try TMP_MAX different filenames.
289 */
290 gettimeofday(&tv, NULL);
291 value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
292 template = &pattern[len - 6 - suffix_len];
293 for (count = 0; count < TMP_MAX; ++count) {
294 uint64_t v = value;
295 /* Fill in the random bits. */
296 template[0] = letters[v % num_letters]; v /= num_letters;
297 template[1] = letters[v % num_letters]; v /= num_letters;
298 template[2] = letters[v % num_letters]; v /= num_letters;
299 template[3] = letters[v % num_letters]; v /= num_letters;
300 template[4] = letters[v % num_letters]; v /= num_letters;
301 template[5] = letters[v % num_letters]; v /= num_letters;
302
303 fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
304 if (fd > 0)
305 return fd;
306 /*
307 * Fatal error (EPERM, ENOSPC etc).
308 * It doesn't make sense to loop.
309 */
310 if (errno != EEXIST)
311 break;
312 /*
313 * This is a random value. It is only necessary that
314 * the next TMP_MAX values generated by adding 7777 to
315 * VALUE are different with (module 2^32).
316 */
317 value += 7777;
318 }
319 /* We return the null string if we can't find a unique file name. */
320 pattern[0] = '\0';
321 return -1;
322}
323
324int git_mkstemp_mode(char *pattern, int mode)
325{
326 /* mkstemp is just mkstemps with no suffix */
327 return git_mkstemps_mode(pattern, 0, mode);
328}
329
330int gitmkstemps(char *pattern, int suffix_len)
331{
332 return git_mkstemps_mode(pattern, suffix_len, 0600);
333}
334
b862b61c
MM
335int xmkstemp_mode(char *template, int mode)
336{
337 int fd;
6cf6bb3e
AE
338 char origtemplate[PATH_MAX];
339 strlcpy(origtemplate, template, sizeof(origtemplate));
b862b61c
MM
340
341 fd = git_mkstemp_mode(template, mode);
6cf6bb3e
AE
342 if (fd < 0) {
343 int saved_errno = errno;
344 const char *nonrelative_template;
345
346 if (!template[0])
347 template = origtemplate;
348
e2a57aac 349 nonrelative_template = absolute_path(template);
6cf6bb3e
AE
350 errno = saved_errno;
351 die_errno("Unable to create temporary file '%s'",
352 nonrelative_template);
353 }
b862b61c
MM
354 return fd;
355}
356
10e13ec8 357static int warn_if_unremovable(const char *op, const char *file, int rc)
fc71db39 358{
fc71db39
AR
359 if (rc < 0) {
360 int err = errno;
361 if (ENOENT != err) {
10e13ec8
PC
362 warning("unable to %s %s: %s",
363 op, file, strerror(errno));
fc71db39
AR
364 errno = err;
365 }
366 }
367 return rc;
368}
369
10e13ec8
PC
370int unlink_or_warn(const char *file)
371{
372 return warn_if_unremovable("unlink", file, unlink(file));
373}
d1723296
PC
374
375int rmdir_or_warn(const char *file)
376{
377 return warn_if_unremovable("rmdir", file, rmdir(file));
378}
80d706af
PC
379
380int remove_or_warn(unsigned int mode, const char *file)
381{
382 return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
383}