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