wrapper.con commit Teach fast-import to import subtrees named by tree id (334fba6)
   1/*
   2 * Various trivial helper wrappers around standard functions
   3 */
   4#include "cache.h"
   5
   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
  13try_to_free_t set_try_to_free_routine(try_to_free_t routine)
  14{
  15        try_to_free_t old = try_to_free_routine;
  16        try_to_free_routine = routine;
  17        return old;
  18}
  19
  20char *xstrdup(const char *str)
  21{
  22        char *ret = strdup(str);
  23        if (!ret) {
  24                try_to_free_routine(strlen(str) + 1);
  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) {
  38                try_to_free_routine(size);
  39                ret = malloc(size);
  40                if (!ret && !size)
  41                        ret = malloc(1);
  42                if (!ret)
  43                        die("Out of memory, malloc failed");
  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
 110void *xmmap(void *start, size_t length,
 111        int prot, int flags, int fd, off_t offset)
 112{
 113        void *ret = mmap(start, length, prot, flags, fd, offset);
 114        if (ret == MAP_FAILED) {
 115                if (!length)
 116                        return NULL;
 117                release_pack_memory(length, fd);
 118                ret = mmap(start, length, prot, flags, fd, offset);
 119                if (ret == MAP_FAILED)
 120                        die_errno("Out of memory? mmap failed");
 121        }
 122        return ret;
 123}
 124
 125/*
 126 * xread() is the same a read(), but it automatically restarts read()
 127 * operations with a recoverable error (EAGAIN and EINTR). xread()
 128 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
 129 */
 130ssize_t xread(int fd, void *buf, size_t len)
 131{
 132        ssize_t nr;
 133        while (1) {
 134                nr = read(fd, buf, len);
 135                if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
 136                        continue;
 137                return nr;
 138        }
 139}
 140
 141/*
 142 * xwrite() is the same a write(), but it automatically restarts write()
 143 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
 144 * GUARANTEE that "len" bytes is written even if the operation is successful.
 145 */
 146ssize_t xwrite(int fd, const void *buf, size_t len)
 147{
 148        ssize_t nr;
 149        while (1) {
 150                nr = write(fd, buf, len);
 151                if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
 152                        continue;
 153                return nr;
 154        }
 155}
 156
 157ssize_t read_in_full(int fd, void *buf, size_t count)
 158{
 159        char *p = buf;
 160        ssize_t total = 0;
 161
 162        while (count > 0) {
 163                ssize_t loaded = xread(fd, p, count);
 164                if (loaded <= 0)
 165                        return total ? total : loaded;
 166                count -= loaded;
 167                p += loaded;
 168                total += loaded;
 169        }
 170
 171        return total;
 172}
 173
 174ssize_t write_in_full(int fd, const void *buf, size_t count)
 175{
 176        const char *p = buf;
 177        ssize_t total = 0;
 178
 179        while (count > 0) {
 180                ssize_t written = xwrite(fd, p, count);
 181                if (written < 0)
 182                        return -1;
 183                if (!written) {
 184                        errno = ENOSPC;
 185                        return -1;
 186                }
 187                count -= written;
 188                p += written;
 189                total += written;
 190        }
 191
 192        return total;
 193}
 194
 195int xdup(int fd)
 196{
 197        int ret = dup(fd);
 198        if (ret < 0)
 199                die_errno("dup failed");
 200        return ret;
 201}
 202
 203FILE *xfdopen(int fd, const char *mode)
 204{
 205        FILE *stream = fdopen(fd, mode);
 206        if (stream == NULL)
 207                die_errno("Out of memory? fdopen failed");
 208        return stream;
 209}
 210
 211int xmkstemp(char *template)
 212{
 213        int fd;
 214
 215        fd = mkstemp(template);
 216        if (fd < 0)
 217                die_errno("Unable to create temporary file");
 218        return fd;
 219}
 220
 221int xmkstemp_mode(char *template, int mode)
 222{
 223        int fd;
 224
 225        fd = git_mkstemp_mode(template, mode);
 226        if (fd < 0)
 227                die_errno("Unable to create temporary file");
 228        return fd;
 229}
 230
 231/*
 232 * zlib wrappers to make sure we don't silently miss errors
 233 * at init time.
 234 */
 235void git_inflate_init(z_streamp strm)
 236{
 237        const char *err;
 238
 239        switch (inflateInit(strm)) {
 240        case Z_OK:
 241                return;
 242
 243        case Z_MEM_ERROR:
 244                err = "out of memory";
 245                break;
 246        case Z_VERSION_ERROR:
 247                err = "wrong version";
 248                break;
 249        default:
 250                err = "error";
 251        }
 252        die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
 253}
 254
 255void git_inflate_end(z_streamp strm)
 256{
 257        if (inflateEnd(strm) != Z_OK)
 258                error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
 259}
 260
 261int git_inflate(z_streamp strm, int flush)
 262{
 263        int ret = inflate(strm, flush);
 264        const char *err;
 265
 266        switch (ret) {
 267        /* Out of memory is fatal. */
 268        case Z_MEM_ERROR:
 269                die("inflate: out of memory");
 270
 271        /* Data corruption errors: we may want to recover from them (fsck) */
 272        case Z_NEED_DICT:
 273                err = "needs dictionary"; break;
 274        case Z_DATA_ERROR:
 275                err = "data stream error"; break;
 276        case Z_STREAM_ERROR:
 277                err = "stream consistency error"; break;
 278        default:
 279                err = "unknown error"; break;
 280
 281        /* Z_BUF_ERROR: normal, needs more space in the output buffer */
 282        case Z_BUF_ERROR:
 283        case Z_OK:
 284        case Z_STREAM_END:
 285                return ret;
 286        }
 287        error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
 288        return ret;
 289}
 290
 291int odb_mkstemp(char *template, size_t limit, const char *pattern)
 292{
 293        int fd;
 294        /*
 295         * we let the umask do its job, don't try to be more
 296         * restrictive except to remove write permission.
 297         */
 298        int mode = 0444;
 299        snprintf(template, limit, "%s/%s",
 300                 get_object_directory(), pattern);
 301        fd = git_mkstemp_mode(template, mode);
 302        if (0 <= fd)
 303                return fd;
 304
 305        /* slow path */
 306        /* some mkstemp implementations erase template on failure */
 307        snprintf(template, limit, "%s/%s",
 308                 get_object_directory(), pattern);
 309        safe_create_leading_directories(template);
 310        return xmkstemp_mode(template, mode);
 311}
 312
 313int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
 314{
 315        int fd;
 316
 317        snprintf(name, namesz, "%s/pack/pack-%s.keep",
 318                 get_object_directory(), sha1_to_hex(sha1));
 319        fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 320        if (0 <= fd)
 321                return fd;
 322
 323        /* slow path */
 324        safe_create_leading_directories(name);
 325        return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 326}
 327
 328static int warn_if_unremovable(const char *op, const char *file, int rc)
 329{
 330        if (rc < 0) {
 331                int err = errno;
 332                if (ENOENT != err) {
 333                        warning("unable to %s %s: %s",
 334                                op, file, strerror(errno));
 335                        errno = err;
 336                }
 337        }
 338        return rc;
 339}
 340
 341int unlink_or_warn(const char *file)
 342{
 343        return warn_if_unremovable("unlink", file, unlink(file));
 344}
 345
 346int rmdir_or_warn(const char *file)
 347{
 348        return warn_if_unremovable("rmdir", file, rmdir(file));
 349}
 350
 351int remove_or_warn(unsigned int mode, const char *file)
 352{
 353        return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
 354}