wrapper.con commit perl: use "use warnings" instead of -w (3328ace)
   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 (tried to allocate %lu bytes)",
  44                            (unsigned long)size);
  45        }
  46#ifdef XMALLOC_POISON
  47        memset(ret, 0xA5, size);
  48#endif
  49        return ret;
  50}
  51
  52void *xmallocz(size_t size)
  53{
  54        void *ret;
  55        if (size + 1 < size)
  56                die("Data too large to fit into virtual memory space.");
  57        ret = xmalloc(size + 1);
  58        ((char*)ret)[size] = 0;
  59        return ret;
  60}
  61
  62/*
  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,
  66 * the program dies.
  67 */
  68void *xmemdupz(const void *data, size_t len)
  69{
  70        return memcpy(xmallocz(len), data, len);
  71}
  72
  73char *xstrndup(const char *str, size_t len)
  74{
  75        char *p = memchr(str, '\0', len);
  76        return xmemdupz(str, p ? p - str : len);
  77}
  78
  79void *xrealloc(void *ptr, size_t size)
  80{
  81        void *ret = realloc(ptr, size);
  82        if (!ret && !size)
  83                ret = realloc(ptr, 1);
  84        if (!ret) {
  85                try_to_free_routine(size);
  86                ret = realloc(ptr, size);
  87                if (!ret && !size)
  88                        ret = realloc(ptr, 1);
  89                if (!ret)
  90                        die("Out of memory, realloc failed");
  91        }
  92        return ret;
  93}
  94
  95void *xcalloc(size_t nmemb, size_t size)
  96{
  97        void *ret = calloc(nmemb, size);
  98        if (!ret && (!nmemb || !size))
  99                ret = calloc(1, 1);
 100        if (!ret) {
 101                try_to_free_routine(nmemb * size);
 102                ret = calloc(nmemb, size);
 103                if (!ret && (!nmemb || !size))
 104                        ret = calloc(1, 1);
 105                if (!ret)
 106                        die("Out of memory, calloc failed");
 107        }
 108        return ret;
 109}
 110
 111void *xmmap(void *start, size_t length,
 112        int prot, int flags, int fd, off_t offset)
 113{
 114        void *ret = mmap(start, length, prot, flags, fd, offset);
 115        if (ret == MAP_FAILED) {
 116                if (!length)
 117                        return NULL;
 118                release_pack_memory(length, fd);
 119                ret = mmap(start, length, prot, flags, fd, offset);
 120                if (ret == MAP_FAILED)
 121                        die_errno("Out of memory? mmap failed");
 122        }
 123        return ret;
 124}
 125
 126/*
 127 * xread() is the same a read(), but it automatically restarts read()
 128 * operations with a recoverable error (EAGAIN and EINTR). xread()
 129 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
 130 */
 131ssize_t xread(int fd, void *buf, size_t len)
 132{
 133        ssize_t nr;
 134        while (1) {
 135                nr = read(fd, buf, len);
 136                if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
 137                        continue;
 138                return nr;
 139        }
 140}
 141
 142/*
 143 * xwrite() is the same a write(), but it automatically restarts write()
 144 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
 145 * GUARANTEE that "len" bytes is written even if the operation is successful.
 146 */
 147ssize_t xwrite(int fd, const void *buf, size_t len)
 148{
 149        ssize_t nr;
 150        while (1) {
 151                nr = write(fd, buf, len);
 152                if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
 153                        continue;
 154                return nr;
 155        }
 156}
 157
 158ssize_t read_in_full(int fd, void *buf, size_t count)
 159{
 160        char *p = buf;
 161        ssize_t total = 0;
 162
 163        while (count > 0) {
 164                ssize_t loaded = xread(fd, p, count);
 165                if (loaded <= 0)
 166                        return total ? total : loaded;
 167                count -= loaded;
 168                p += loaded;
 169                total += loaded;
 170        }
 171
 172        return total;
 173}
 174
 175ssize_t write_in_full(int fd, const void *buf, size_t count)
 176{
 177        const char *p = buf;
 178        ssize_t total = 0;
 179
 180        while (count > 0) {
 181                ssize_t written = xwrite(fd, p, count);
 182                if (written < 0)
 183                        return -1;
 184                if (!written) {
 185                        errno = ENOSPC;
 186                        return -1;
 187                }
 188                count -= written;
 189                p += written;
 190                total += written;
 191        }
 192
 193        return total;
 194}
 195
 196int xdup(int fd)
 197{
 198        int ret = dup(fd);
 199        if (ret < 0)
 200                die_errno("dup failed");
 201        return ret;
 202}
 203
 204FILE *xfdopen(int fd, const char *mode)
 205{
 206        FILE *stream = fdopen(fd, mode);
 207        if (stream == NULL)
 208                die_errno("Out of memory? fdopen failed");
 209        return stream;
 210}
 211
 212int xmkstemp(char *template)
 213{
 214        int fd;
 215
 216        fd = mkstemp(template);
 217        if (fd < 0)
 218                die_errno("Unable to create temporary file");
 219        return fd;
 220}
 221
 222int xmkstemp_mode(char *template, int mode)
 223{
 224        int fd;
 225
 226        fd = git_mkstemp_mode(template, mode);
 227        if (fd < 0)
 228                die_errno("Unable to create temporary file");
 229        return fd;
 230}
 231
 232/*
 233 * zlib wrappers to make sure we don't silently miss errors
 234 * at init time.
 235 */
 236void git_inflate_init(z_streamp strm)
 237{
 238        const char *err;
 239
 240        switch (inflateInit(strm)) {
 241        case Z_OK:
 242                return;
 243
 244        case Z_MEM_ERROR:
 245                err = "out of memory";
 246                break;
 247        case Z_VERSION_ERROR:
 248                err = "wrong version";
 249                break;
 250        default:
 251                err = "error";
 252        }
 253        die("inflateInit: %s (%s)", err, strm->msg ? strm->msg : "no message");
 254}
 255
 256void git_inflate_end(z_streamp strm)
 257{
 258        if (inflateEnd(strm) != Z_OK)
 259                error("inflateEnd: %s", strm->msg ? strm->msg : "failed");
 260}
 261
 262int git_inflate(z_streamp strm, int flush)
 263{
 264        int ret = inflate(strm, flush);
 265        const char *err;
 266
 267        switch (ret) {
 268        /* Out of memory is fatal. */
 269        case Z_MEM_ERROR:
 270                die("inflate: out of memory");
 271
 272        /* Data corruption errors: we may want to recover from them (fsck) */
 273        case Z_NEED_DICT:
 274                err = "needs dictionary"; break;
 275        case Z_DATA_ERROR:
 276                err = "data stream error"; break;
 277        case Z_STREAM_ERROR:
 278                err = "stream consistency error"; break;
 279        default:
 280                err = "unknown error"; break;
 281
 282        /* Z_BUF_ERROR: normal, needs more space in the output buffer */
 283        case Z_BUF_ERROR:
 284        case Z_OK:
 285        case Z_STREAM_END:
 286                return ret;
 287        }
 288        error("inflate: %s (%s)", err, strm->msg ? strm->msg : "no message");
 289        return ret;
 290}
 291
 292int odb_mkstemp(char *template, size_t limit, const char *pattern)
 293{
 294        int fd;
 295        /*
 296         * we let the umask do its job, don't try to be more
 297         * restrictive except to remove write permission.
 298         */
 299        int mode = 0444;
 300        snprintf(template, limit, "%s/%s",
 301                 get_object_directory(), pattern);
 302        fd = git_mkstemp_mode(template, mode);
 303        if (0 <= fd)
 304                return fd;
 305
 306        /* slow path */
 307        /* some mkstemp implementations erase template on failure */
 308        snprintf(template, limit, "%s/%s",
 309                 get_object_directory(), pattern);
 310        safe_create_leading_directories(template);
 311        return xmkstemp_mode(template, mode);
 312}
 313
 314int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
 315{
 316        int fd;
 317
 318        snprintf(name, namesz, "%s/pack/pack-%s.keep",
 319                 get_object_directory(), sha1_to_hex(sha1));
 320        fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 321        if (0 <= fd)
 322                return fd;
 323
 324        /* slow path */
 325        safe_create_leading_directories(name);
 326        return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 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}