2 * Various trivial helper wrappers around standard functions
6 static void do_nothing(size_t size
)
10 static void (*try_to_free_routine
)(size_t size
) = do_nothing
;
12 try_to_free_t
set_try_to_free_routine(try_to_free_t routine
)
14 try_to_free_t old
= try_to_free_routine
;
15 try_to_free_routine
= routine
;
19 char *xstrdup(const char *str
)
21 char *ret
= strdup(str
);
23 try_to_free_routine(strlen(str
) + 1);
26 die("Out of memory, strdup failed");
31 void *xmalloc(size_t size
)
33 void *ret
= malloc(size
);
37 try_to_free_routine(size
);
42 die("Out of memory, malloc failed (tried to allocate %lu bytes)",
46 memset(ret
, 0xA5, size
);
51 void *xmallocz(size_t size
)
55 die("Data too large to fit into virtual memory space.");
56 ret
= xmalloc(size
+ 1);
57 ((char*)ret
)[size
] = 0;
62 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
63 * "data" to the allocated memory, zero terminates the allocated memory,
64 * and returns a pointer to the allocated memory. If the allocation fails,
67 void *xmemdupz(const void *data
, size_t len
)
69 return memcpy(xmallocz(len
), data
, len
);
72 char *xstrndup(const char *str
, size_t len
)
74 char *p
= memchr(str
, '\0', len
);
75 return xmemdupz(str
, p
? p
- str
: len
);
78 void *xrealloc(void *ptr
, size_t size
)
80 void *ret
= realloc(ptr
, size
);
82 ret
= realloc(ptr
, 1);
84 try_to_free_routine(size
);
85 ret
= realloc(ptr
, size
);
87 ret
= realloc(ptr
, 1);
89 die("Out of memory, realloc failed");
94 void *xcalloc(size_t nmemb
, size_t size
)
96 void *ret
= calloc(nmemb
, size
);
97 if (!ret
&& (!nmemb
|| !size
))
100 try_to_free_routine(nmemb
* size
);
101 ret
= calloc(nmemb
, size
);
102 if (!ret
&& (!nmemb
|| !size
))
105 die("Out of memory, calloc failed");
111 * xread() is the same a read(), but it automatically restarts read()
112 * operations with a recoverable error (EAGAIN and EINTR). xread()
113 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
115 ssize_t
xread(int fd
, void *buf
, size_t len
)
119 nr
= read(fd
, buf
, len
);
120 if ((nr
< 0) && (errno
== EAGAIN
|| errno
== EINTR
))
127 * xwrite() is the same a write(), but it automatically restarts write()
128 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
129 * GUARANTEE that "len" bytes is written even if the operation is successful.
131 ssize_t
xwrite(int fd
, const void *buf
, size_t len
)
135 nr
= write(fd
, buf
, len
);
136 if ((nr
< 0) && (errno
== EAGAIN
|| errno
== EINTR
))
142 ssize_t
read_in_full(int fd
, void *buf
, size_t count
)
148 ssize_t loaded
= xread(fd
, p
, count
);
150 return total
? total
: loaded
;
159 ssize_t
write_in_full(int fd
, const void *buf
, size_t count
)
165 ssize_t written
= xwrite(fd
, p
, count
);
184 die_errno("dup failed");
188 FILE *xfdopen(int fd
, const char *mode
)
190 FILE *stream
= fdopen(fd
, mode
);
192 die_errno("Out of memory? fdopen failed");
196 int xmkstemp(char *template)
200 fd
= mkstemp(template);
202 die_errno("Unable to create temporary file");
206 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
207 int git_mkstemp(char *path
, size_t len
, const char *template)
212 tmp
= getenv("TMPDIR");
215 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
217 errno
= ENAMETOOLONG
;
220 return mkstemp(path
);
223 /* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
224 int git_mkstemps(char *path
, size_t len
, const char *template, int suffix_len
)
229 tmp
= getenv("TMPDIR");
232 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
234 errno
= ENAMETOOLONG
;
237 return mkstemps(path
, suffix_len
);
240 /* Adapted from libiberty's mkstemp.c. */
243 #define TMP_MAX 16384
245 int git_mkstemps_mode(char *pattern
, int suffix_len
, int mode
)
247 static const char letters
[] =
248 "abcdefghijklmnopqrstuvwxyz"
249 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
251 static const int num_letters
= 62;
258 len
= strlen(pattern
);
260 if (len
< 6 + suffix_len
) {
265 if (strncmp(&pattern
[len
- 6 - suffix_len
], "XXXXXX", 6)) {
271 * Replace pattern's XXXXXX characters with randomness.
272 * Try TMP_MAX different filenames.
274 gettimeofday(&tv
, NULL
);
275 value
= ((size_t)(tv
.tv_usec
<< 16)) ^ tv
.tv_sec
^ getpid();
276 template = &pattern
[len
- 6 - suffix_len
];
277 for (count
= 0; count
< TMP_MAX
; ++count
) {
279 /* Fill in the random bits. */
280 template[0] = letters
[v
% num_letters
]; v
/= num_letters
;
281 template[1] = letters
[v
% num_letters
]; v
/= num_letters
;
282 template[2] = letters
[v
% num_letters
]; v
/= num_letters
;
283 template[3] = letters
[v
% num_letters
]; v
/= num_letters
;
284 template[4] = letters
[v
% num_letters
]; v
/= num_letters
;
285 template[5] = letters
[v
% num_letters
]; v
/= num_letters
;
287 fd
= open(pattern
, O_CREAT
| O_EXCL
| O_RDWR
, mode
);
291 * Fatal error (EPERM, ENOSPC etc).
292 * It doesn't make sense to loop.
297 * This is a random value. It is only necessary that
298 * the next TMP_MAX values generated by adding 7777 to
299 * VALUE are different with (module 2^32).
303 /* We return the null string if we can't find a unique file name. */
308 int git_mkstemp_mode(char *pattern
, int mode
)
310 /* mkstemp is just mkstemps with no suffix */
311 return git_mkstemps_mode(pattern
, 0, mode
);
314 int gitmkstemps(char *pattern
, int suffix_len
)
316 return git_mkstemps_mode(pattern
, suffix_len
, 0600);
319 int xmkstemp_mode(char *template, int mode
)
323 fd
= git_mkstemp_mode(template, mode
);
325 die_errno("Unable to create temporary file");
329 static int warn_if_unremovable(const char *op
, const char *file
, int rc
)
334 warning("unable to %s %s: %s",
335 op
, file
, strerror(errno
));
342 int unlink_or_warn(const char *file
)
344 return warn_if_unremovable("unlink", file
, unlink(file
));
347 int rmdir_or_warn(const char *file
)
349 return warn_if_unremovable("rmdir", file
, rmdir(file
));
352 int remove_or_warn(unsigned int mode
, const char *file
)
354 return S_ISGITLINK(mode
) ? rmdir_or_warn(file
) : unlink_or_warn(file
);