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