2 * Various trivial helper wrappers around standard functions
6 static void try_to_free_builtin(size_t size
)
8 release_pack_memory(size
, -1);
11 static void (*try_to_free_routine
)(size_t size
) = try_to_free_builtin
;
13 try_to_free_t
set_try_to_free_routine(try_to_free_t routine
)
15 try_to_free_t old
= try_to_free_routine
;
16 try_to_free_routine
= routine
;
20 char *xstrdup(const char *str
)
22 char *ret
= strdup(str
);
24 try_to_free_routine(strlen(str
) + 1);
27 die("Out of memory, strdup failed");
32 void *xmalloc(size_t size
)
34 void *ret
= malloc(size
);
38 try_to_free_routine(size
);
43 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
47 memset(ret
, 0xA5, size
);
52 void *xmallocz(size_t size
)
56 die("Data too large to fit into virtual memory space.");
57 ret
= xmalloc(size
+ 1);
58 ((char*)ret
)[size
] = 0;
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,
68 void *xmemdupz(const void *data
, size_t len
)
70 return memcpy(xmallocz(len
), data
, len
);
73 char *xstrndup(const char *str
, size_t len
)
75 char *p
= memchr(str
, '\0', len
);
76 return xmemdupz(str
, p
? p
- str
: len
);
79 void *xrealloc(void *ptr
, size_t size
)
81 void *ret
= realloc(ptr
, size
);
83 ret
= realloc(ptr
, 1);
85 try_to_free_routine(size
);
86 ret
= realloc(ptr
, size
);
88 ret
= realloc(ptr
, 1);
90 die("Out of memory, realloc failed");
95 void *xcalloc(size_t nmemb
, size_t size
)
97 void *ret
= calloc(nmemb
, size
);
98 if (!ret
&& (!nmemb
|| !size
))
101 try_to_free_routine(nmemb
* size
);
102 ret
= calloc(nmemb
, size
);
103 if (!ret
&& (!nmemb
|| !size
))
106 die("Out of memory, calloc 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 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
208 int git_mkstemp(char *path
, size_t len
, const char *template)
213 tmp
= getenv("TMPDIR");
216 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
218 errno
= ENAMETOOLONG
;
221 return mkstemp(path
);
224 /* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
225 int git_mkstemps(char *path
, size_t len
, const char *template, int suffix_len
)
230 tmp
= getenv("TMPDIR");
233 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
235 errno
= ENAMETOOLONG
;
238 return mkstemps(path
, suffix_len
);
241 /* Adapted from libiberty's mkstemp.c. */
244 #define TMP_MAX 16384
246 int git_mkstemps_mode(char *pattern
, int suffix_len
, int mode
)
248 static const char letters
[] =
249 "abcdefghijklmnopqrstuvwxyz"
250 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
252 static const int num_letters
= 62;
259 len
= strlen(pattern
);
261 if (len
< 6 + suffix_len
) {
266 if (strncmp(&pattern
[len
- 6 - suffix_len
], "XXXXXX", 6)) {
272 * Replace pattern's XXXXXX characters with randomness.
273 * Try TMP_MAX different filenames.
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
) {
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
;
288 fd
= open(pattern
, O_CREAT
| O_EXCL
| O_RDWR
, mode
);
292 * Fatal error (EPERM, ENOSPC etc).
293 * It doesn't make sense to loop.
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).
304 /* We return the null string if we can't find a unique file name. */
309 int git_mkstemp_mode(char *pattern
, int mode
)
311 /* mkstemp is just mkstemps with no suffix */
312 return git_mkstemps_mode(pattern
, 0, mode
);
315 int gitmkstemps(char *pattern
, int suffix_len
)
317 return git_mkstemps_mode(pattern
, suffix_len
, 0600);
320 int xmkstemp_mode(char *template, int mode
)
324 fd
= git_mkstemp_mode(template, mode
);
326 die_errno("Unable to create temporary file");
331 * zlib wrappers to make sure we don't silently miss errors
334 void git_inflate_init(z_streamp strm
)
338 switch (inflateInit(strm
)) {
343 err
= "out of memory";
345 case Z_VERSION_ERROR
:
346 err
= "wrong version";
351 die("inflateInit: %s (%s)", err
, strm
->msg
? strm
->msg
: "no message");
354 void git_inflate_end(z_streamp strm
)
356 if (inflateEnd(strm
) != Z_OK
)
357 error("inflateEnd: %s", strm
->msg
? strm
->msg
: "failed");
360 int git_inflate(z_streamp strm
, int flush
)
362 int ret
= inflate(strm
, flush
);
366 /* Out of memory is fatal. */
368 die("inflate: out of memory");
370 /* Data corruption errors: we may want to recover from them (fsck) */
372 err
= "needs dictionary"; break;
374 err
= "data stream error"; break;
376 err
= "stream consistency error"; break;
378 err
= "unknown error"; break;
380 /* Z_BUF_ERROR: normal, needs more space in the output buffer */
386 error("inflate: %s (%s)", err
, strm
->msg
? strm
->msg
: "no message");
390 static int warn_if_unremovable(const char *op
, const char *file
, int rc
)
395 warning("unable to %s %s: %s",
396 op
, file
, strerror(errno
));
403 int unlink_or_warn(const char *file
)
405 return warn_if_unremovable("unlink", file
, unlink(file
));
408 int rmdir_or_warn(const char *file
)
410 return warn_if_unremovable("rmdir", file
, rmdir(file
));
413 int remove_or_warn(unsigned int mode
, const char *file
)
415 return S_ISGITLINK(mode
) ? rmdir_or_warn(file
) : unlink_or_warn(file
);