wrapper.con commit t/t6035-merge-dir-to-symlink.sh: Remove TODO on passing test (2fd1ea1)
   1/*
   2 * Various trivial helper wrappers around standard functions
   3 */
   4#include "cache.h"
   5
   6char *xstrdup(const char *str)
   7{
   8        char *ret = strdup(str);
   9        if (!ret) {
  10                release_pack_memory(strlen(str) + 1, -1);
  11                ret = strdup(str);
  12                if (!ret)
  13                        die("Out of memory, strdup failed");
  14        }
  15        return ret;
  16}
  17
  18void *xmalloc(size_t size)
  19{
  20        void *ret = malloc(size);
  21        if (!ret && !size)
  22                ret = malloc(1);
  23        if (!ret) {
  24                release_pack_memory(size, -1);
  25                ret = malloc(size);
  26                if (!ret && !size)
  27                        ret = malloc(1);
  28                if (!ret)
  29                        die("Out of memory, malloc failed");
  30        }
  31#ifdef XMALLOC_POISON
  32        memset(ret, 0xA5, size);
  33#endif
  34        return ret;
  35}
  36
  37void *xmallocz(size_t size)
  38{
  39        void *ret;
  40        if (size + 1 < size)
  41                die("Data too large to fit into virtual memory space.");
  42        ret = xmalloc(size + 1);
  43        ((char*)ret)[size] = 0;
  44        return ret;
  45}
  46
  47/*
  48 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
  49 * "data" to the allocated memory, zero terminates the allocated memory,
  50 * and returns a pointer to the allocated memory. If the allocation fails,
  51 * the program dies.
  52 */
  53void *xmemdupz(const void *data, size_t len)
  54{
  55        return memcpy(xmallocz(len), data, len);
  56}
  57
  58char *xstrndup(const char *str, size_t len)
  59{
  60        char *p = memchr(str, '\0', len);
  61        return xmemdupz(str, p ? p - str : len);
  62}
  63
  64void *xrealloc(void *ptr, size_t size)
  65{
  66        void *ret = realloc(ptr, size);
  67        if (!ret && !size)
  68                ret = realloc(ptr, 1);
  69        if (!ret) {
  70                release_pack_memory(size, -1);
  71                ret = realloc(ptr, size);
  72                if (!ret && !size)
  73                        ret = realloc(ptr, 1);
  74                if (!ret)
  75                        die("Out of memory, realloc failed");
  76        }
  77        return ret;
  78}
  79
  80void *xcalloc(size_t nmemb, size_t size)
  81{
  82        void *ret = calloc(nmemb, size);
  83        if (!ret && (!nmemb || !size))
  84                ret = calloc(1, 1);
  85        if (!ret) {
  86                release_pack_memory(nmemb * size, -1);
  87                ret = calloc(nmemb, size);
  88                if (!ret && (!nmemb || !size))
  89                        ret = calloc(1, 1);
  90                if (!ret)
  91                        die("Out of memory, calloc failed");
  92        }
  93        return ret;
  94}
  95
  96void *xmmap(void *start, size_t length,
  97        int prot, int flags, int fd, off_t offset)
  98{
  99        void *ret = mmap(start, length, prot, flags, fd, offset);
 100        if (ret == MAP_FAILED) {
 101                if (!length)
 102                        return NULL;
 103                release_pack_memory(length, fd);
 104                ret = mmap(start, length, prot, flags, fd, offset);
 105                if (ret == MAP_FAILED)
 106                        die_errno("Out of memory? mmap failed");
 107        }
 108        return ret;
 109}
 110
 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
 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
 181int xdup(int fd)
 182{
 183        int ret = dup(fd);
 184        if (ret < 0)
 185                die_errno("dup failed");
 186        return ret;
 187}
 188
 189FILE *xfdopen(int fd, const char *mode)
 190{
 191        FILE *stream = fdopen(fd, mode);
 192        if (stream == NULL)
 193                die_errno("Out of memory? fdopen failed");
 194        return stream;
 195}
 196
 197int xmkstemp(char *template)
 198{
 199        int fd;
 200
 201        fd = mkstemp(template);
 202        if (fd < 0)
 203                die_errno("Unable to create temporary file");
 204        return fd;
 205}
 206
 207int xmkstemp_mode(char *template, int mode)
 208{
 209        int fd;
 210
 211        fd = git_mkstemp_mode(template, mode);
 212        if (fd < 0)
 213                die_errno("Unable to create temporary file");
 214        return fd;
 215}
 216
 217/*
 218 * zlib wrappers to make sure we don't silently miss errors
 219 * at init time.
 220 */
 221void git_inflate_init(z_streamp strm)
 222{
 223        const char *err;
 224
 225        switch (inflateInit(strm)) {
 226        case Z_OK:
 227                return;
 228
 229        case Z_MEM_ERROR:
 230                err = "out of memory";
 231                break;
 232        case Z_VERSION_ERROR:
 233                err = "wrong version";
 234                break;
 235        default:
 236                err = "error";
 237        }
 238        die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
 239}
 240
 241void git_inflate_end(z_streamp strm)
 242{
 243        if (inflateEnd(strm) != Z_OK)
 244                error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
 245}
 246
 247int git_inflate(z_streamp strm, int flush)
 248{
 249        int ret = inflate(strm, flush);
 250        const char *err;
 251
 252        switch (ret) {
 253        /* Out of memory is fatal. */
 254        case Z_MEM_ERROR:
 255                die("inflate: out of memory");
 256
 257        /* Data corruption errors: we may want to recover from them (fsck) */
 258        case Z_NEED_DICT:
 259                err = "needs dictionary"; break;
 260        case Z_DATA_ERROR:
 261                err = "data stream error"; break;
 262        case Z_STREAM_ERROR:
 263                err = "stream consistency error"; break;
 264        default:
 265                err = "unknown error"; break;
 266
 267        /* Z_BUF_ERROR: normal, needs more space in the output buffer */
 268        case Z_BUF_ERROR:
 269        case Z_OK:
 270        case Z_STREAM_END:
 271                return ret;
 272        }
 273        error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
 274        return ret;
 275}
 276
 277int odb_mkstemp(char *template, size_t limit, const char *pattern)
 278{
 279        int fd;
 280        /*
 281         * we let the umask do its job, don't try to be more
 282         * restrictive except to remove write permission.
 283         */
 284        int mode = 0444;
 285        snprintf(template, limit, "%s/%s",
 286                 get_object_directory(), pattern);
 287        fd = git_mkstemp_mode(template, mode);
 288        if (0 <= fd)
 289                return fd;
 290
 291        /* slow path */
 292        /* some mkstemp implementations erase template on failure */
 293        snprintf(template, limit, "%s/%s",
 294                 get_object_directory(), pattern);
 295        safe_create_leading_directories(template);
 296        return xmkstemp_mode(template, mode);
 297}
 298
 299int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
 300{
 301        int fd;
 302
 303        snprintf(name, namesz, "%s/pack/pack-%s.keep",
 304                 get_object_directory(), sha1_to_hex(sha1));
 305        fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 306        if (0 <= fd)
 307                return fd;
 308
 309        /* slow path */
 310        safe_create_leading_directories(name);
 311        return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 312}
 313
 314int unlink_or_warn(const char *file)
 315{
 316        int rc = unlink(file);
 317
 318        if (rc < 0) {
 319                int err = errno;
 320                if (ENOENT != err) {
 321                        warning("unable to unlink %s: %s",
 322                                file, strerror(errno));
 323                        errno = err;
 324                }
 325        }
 326        return rc;
 327}
 328