git-compat-util.hon commit Support delimited data regions in fast-import. (3b4dce0)
   1#ifndef GIT_COMPAT_UTIL_H
   2#define GIT_COMPAT_UTIL_H
   3
   4#ifndef FLEX_ARRAY
   5#if defined(__GNUC__) && (__GNUC__ < 3)
   6#define FLEX_ARRAY 0
   7#else
   8#define FLEX_ARRAY /* empty */
   9#endif
  10#endif
  11
  12#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  13
  14#if !defined(__APPLE__) && !defined(__FreeBSD__)
  15#define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
  16#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
  17#endif
  18#define _GNU_SOURCE
  19#define _BSD_SOURCE
  20
  21#include <unistd.h>
  22#include <stdio.h>
  23#include <sys/stat.h>
  24#include <fcntl.h>
  25#include <stddef.h>
  26#include <stdlib.h>
  27#include <stdarg.h>
  28#include <string.h>
  29#include <errno.h>
  30#include <limits.h>
  31#include <inttypes.h>
  32#include <sys/param.h>
  33#include <sys/types.h>
  34#include <dirent.h>
  35#include <sys/time.h>
  36#include <time.h>
  37#include <signal.h>
  38#include <sys/wait.h>
  39#include <fnmatch.h>
  40#include <sys/poll.h>
  41#include <sys/socket.h>
  42#include <assert.h>
  43#include <regex.h>
  44#include <netinet/in.h>
  45#include <netinet/tcp.h>
  46#include <arpa/inet.h>
  47#include <netdb.h>
  48#include <pwd.h>
  49#include <stdint.h>
  50#include <grp.h>
  51
  52#ifndef NO_ICONV
  53#include <iconv.h>
  54#endif
  55
  56/* On most systems <limits.h> would have given us this, but
  57 * not on some systems (e.g. GNU/Hurd).
  58 */
  59#ifndef PATH_MAX
  60#define PATH_MAX 4096
  61#endif
  62
  63#ifdef __GNUC__
  64#define NORETURN __attribute__((__noreturn__))
  65#else
  66#define NORETURN
  67#ifndef __attribute__
  68#define __attribute__(x)
  69#endif
  70#endif
  71
  72/* General helper functions */
  73extern void usage(const char *err) NORETURN;
  74extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
  75extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  76extern void warn(const char *err, ...) __attribute__((format (printf, 1, 2)));
  77
  78extern void set_usage_routine(void (*routine)(const char *err) NORETURN);
  79extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
  80extern void set_error_routine(void (*routine)(const char *err, va_list params));
  81extern void set_warn_routine(void (*routine)(const char *warn, va_list params));
  82
  83#ifdef NO_MMAP
  84
  85#ifndef PROT_READ
  86#define PROT_READ 1
  87#define PROT_WRITE 2
  88#define MAP_PRIVATE 1
  89#define MAP_FAILED ((void*)-1)
  90#endif
  91
  92#define mmap git_mmap
  93#define munmap git_munmap
  94extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
  95extern int git_munmap(void *start, size_t length);
  96
  97#define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
  98
  99#else /* NO_MMAP */
 100
 101#include <sys/mman.h>
 102#define DEFAULT_PACKED_GIT_WINDOW_SIZE \
 103        (sizeof(void*) >= 8 \
 104                ?  1 * 1024 * 1024 * 1024 \
 105                : 32 * 1024 * 1024)
 106
 107#endif /* NO_MMAP */
 108
 109#define DEFAULT_PACKED_GIT_LIMIT \
 110        ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
 111
 112#ifdef NO_PREAD
 113#define pread git_pread
 114extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
 115#endif
 116
 117#ifdef NO_SETENV
 118#define setenv gitsetenv
 119extern int gitsetenv(const char *, const char *, int);
 120#endif
 121
 122#ifdef NO_UNSETENV
 123#define unsetenv gitunsetenv
 124extern void gitunsetenv(const char *);
 125#endif
 126
 127#ifdef NO_STRCASESTR
 128#define strcasestr gitstrcasestr
 129extern char *gitstrcasestr(const char *haystack, const char *needle);
 130#endif
 131
 132#ifdef NO_STRLCPY
 133#define strlcpy gitstrlcpy
 134extern size_t gitstrlcpy(char *, const char *, size_t);
 135#endif
 136
 137extern void release_pack_memory(size_t);
 138
 139static inline char* xstrdup(const char *str)
 140{
 141        char *ret = strdup(str);
 142        if (!ret) {
 143                release_pack_memory(strlen(str) + 1);
 144                ret = strdup(str);
 145                if (!ret)
 146                        die("Out of memory, strdup failed");
 147        }
 148        return ret;
 149}
 150
 151static inline void *xmalloc(size_t size)
 152{
 153        void *ret = malloc(size);
 154        if (!ret && !size)
 155                ret = malloc(1);
 156        if (!ret) {
 157                release_pack_memory(size);
 158                ret = malloc(size);
 159                if (!ret && !size)
 160                        ret = malloc(1);
 161                if (!ret)
 162                        die("Out of memory, malloc failed");
 163        }
 164#ifdef XMALLOC_POISON
 165        memset(ret, 0xA5, size);
 166#endif
 167        return ret;
 168}
 169
 170static inline void *xrealloc(void *ptr, size_t size)
 171{
 172        void *ret = realloc(ptr, size);
 173        if (!ret && !size)
 174                ret = realloc(ptr, 1);
 175        if (!ret) {
 176                release_pack_memory(size);
 177                ret = realloc(ptr, size);
 178                if (!ret && !size)
 179                        ret = realloc(ptr, 1);
 180                if (!ret)
 181                        die("Out of memory, realloc failed");
 182        }
 183        return ret;
 184}
 185
 186static inline void *xcalloc(size_t nmemb, size_t size)
 187{
 188        void *ret = calloc(nmemb, size);
 189        if (!ret && (!nmemb || !size))
 190                ret = calloc(1, 1);
 191        if (!ret) {
 192                release_pack_memory(nmemb * size);
 193                ret = calloc(nmemb, size);
 194                if (!ret && (!nmemb || !size))
 195                        ret = calloc(1, 1);
 196                if (!ret)
 197                        die("Out of memory, calloc failed");
 198        }
 199        return ret;
 200}
 201
 202static inline void *xmmap(void *start, size_t length,
 203        int prot, int flags, int fd, off_t offset)
 204{
 205        void *ret = mmap(start, length, prot, flags, fd, offset);
 206        if (ret == MAP_FAILED) {
 207                if (!length)
 208                        return NULL;
 209                release_pack_memory(length);
 210                ret = mmap(start, length, prot, flags, fd, offset);
 211                if (ret == MAP_FAILED)
 212                        die("Out of memory? mmap failed: %s", strerror(errno));
 213        }
 214        return ret;
 215}
 216
 217static inline ssize_t xread(int fd, void *buf, size_t len)
 218{
 219        ssize_t nr;
 220        while (1) {
 221                nr = read(fd, buf, len);
 222                if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
 223                        continue;
 224                return nr;
 225        }
 226}
 227
 228static inline ssize_t xwrite(int fd, const void *buf, size_t len)
 229{
 230        ssize_t nr;
 231        while (1) {
 232                nr = write(fd, buf, len);
 233                if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
 234                        continue;
 235                return nr;
 236        }
 237}
 238
 239static inline int has_extension(const char *filename, const char *ext)
 240{
 241        size_t len = strlen(filename);
 242        size_t extlen = strlen(ext);
 243        return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
 244}
 245
 246/* Sane ctype - no locale, and works with signed chars */
 247#undef isspace
 248#undef isdigit
 249#undef isalpha
 250#undef isalnum
 251#undef tolower
 252#undef toupper
 253extern unsigned char sane_ctype[256];
 254#define GIT_SPACE 0x01
 255#define GIT_DIGIT 0x02
 256#define GIT_ALPHA 0x04
 257#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
 258#define isspace(x) sane_istest(x,GIT_SPACE)
 259#define isdigit(x) sane_istest(x,GIT_DIGIT)
 260#define isalpha(x) sane_istest(x,GIT_ALPHA)
 261#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
 262#define tolower(x) sane_case((unsigned char)(x), 0x20)
 263#define toupper(x) sane_case((unsigned char)(x), 0)
 264
 265static inline int sane_case(int x, int high)
 266{
 267        if (sane_istest(x, GIT_ALPHA))
 268                x = (x & ~0x20) | high;
 269        return x;
 270}
 271
 272#endif