d8b383fe6b0544e65e9633f747346f8042f2840a
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include <stdarg.h>
   7#include "cache.h"
   8
   9const char *sha1_file_directory = NULL;
  10struct cache_entry **active_cache = NULL;
  11unsigned int active_nr = 0, active_alloc = 0;
  12
  13void usage(const char *err)
  14{
  15        fprintf(stderr, "usage: %s\n", err);
  16        exit(1);
  17}
  18
  19static void report(const char *prefix, const char *err, va_list params)
  20{
  21        fputs(prefix, stderr);
  22        vfprintf(stderr, err, params);
  23        fputs("\n", stderr);
  24}
  25
  26void die(const char *err, ...)
  27{
  28        va_list params;
  29
  30        va_start(params, err);
  31        report("fatal: ", err, params);
  32        va_end(params);
  33        exit(1);
  34}
  35
  36int error(const char *err, ...)
  37{
  38        va_list params;
  39
  40        va_start(params, err);
  41        report("error: ", err, params);
  42        va_end(params);
  43        return -1;
  44}
  45
  46
  47static unsigned hexval(char c)
  48{
  49        if (c >= '0' && c <= '9')
  50                return c - '0';
  51        if (c >= 'a' && c <= 'f')
  52                return c - 'a' + 10;
  53        if (c >= 'A' && c <= 'F')
  54                return c - 'A' + 10;
  55        return ~0;
  56}
  57
  58int get_sha1_hex(const char *hex, unsigned char *sha1)
  59{
  60        int i;
  61        for (i = 0; i < 20; i++) {
  62                unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
  63                if (val & ~0xff)
  64                        return -1;
  65                *sha1++ = val;
  66                hex += 2;
  67        }
  68        return 0;
  69}
  70
  71char * sha1_to_hex(const unsigned char *sha1)
  72{
  73        static char buffer[50];
  74        static const char hex[] = "0123456789abcdef";
  75        char *buf = buffer;
  76        int i;
  77
  78        for (i = 0; i < 20; i++) {
  79                unsigned int val = *sha1++;
  80                *buf++ = hex[val >> 4];
  81                *buf++ = hex[val & 0xf];
  82        }
  83        return buffer;
  84}
  85
  86/*
  87 * NOTE! This returns a statically allocated buffer, so you have to be
  88 * careful about using it. Do a "strdup()" if you need to save the
  89 * filename.
  90 */
  91char *sha1_file_name(const unsigned char *sha1)
  92{
  93        int i;
  94        static char *name, *base;
  95
  96        if (!base) {
  97                char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
  98                int len = strlen(sha1_file_directory);
  99                base = malloc(len + 60);
 100                memcpy(base, sha1_file_directory, len);
 101                memset(base+len, 0, 60);
 102                base[len] = '/';
 103                base[len+3] = '/';
 104                name = base + len + 1;
 105        }
 106        for (i = 0; i < 20; i++) {
 107                static char hex[] = "0123456789abcdef";
 108                unsigned int val = sha1[i];
 109                char *pos = name + i*2 + (i > 0);
 110                *pos++ = hex[val >> 4];
 111                *pos = hex[val & 0xf];
 112        }
 113        return base;
 114}
 115
 116int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
 117{
 118        unsigned char real_sha1[20];
 119        SHA_CTX c;
 120
 121        SHA1_Init(&c);
 122        SHA1_Update(&c, map, size);
 123        SHA1_Final(real_sha1, &c);
 124        return memcmp(sha1, real_sha1, 20) ? -1 : 0;
 125}
 126
 127void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
 128{
 129        char *filename = sha1_file_name(sha1);
 130        int fd = open(filename, O_RDONLY);
 131        struct stat st;
 132        void *map;
 133
 134        if (fd < 0) {
 135                perror(filename);
 136                return NULL;
 137        }
 138        if (fstat(fd, &st) < 0) {
 139                close(fd);  
 140                return NULL;
 141        }
 142        map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
 143        close(fd);
 144        if (-1 == (int)(long)map)
 145                return NULL;
 146        *size = st.st_size;
 147        return map;
 148}
 149
 150void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
 151{
 152        int ret, bytes;
 153        z_stream stream;
 154        char buffer[8192];
 155        char *buf;
 156
 157        /* Get the data stream */
 158        memset(&stream, 0, sizeof(stream));
 159        stream.next_in = map;
 160        stream.avail_in = mapsize;
 161        stream.next_out = buffer;
 162        stream.avail_out = sizeof(buffer);
 163
 164        inflateInit(&stream);
 165        ret = inflate(&stream, 0);
 166        if (sscanf(buffer, "%10s %lu", type, size) != 2)
 167                return NULL;
 168
 169        bytes = strlen(buffer) + 1;
 170        buf = malloc(*size);
 171        if (!buf)
 172                return NULL;
 173
 174        memcpy(buf, buffer + bytes, stream.total_out - bytes);
 175        bytes = stream.total_out - bytes;
 176        if (bytes < *size && ret == Z_OK) {
 177                stream.next_out = buf + bytes;
 178                stream.avail_out = *size - bytes;
 179                while (inflate(&stream, Z_FINISH) == Z_OK)
 180                        /* nothing */;
 181        }
 182        inflateEnd(&stream);
 183        return buf;
 184}
 185
 186void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
 187{
 188        unsigned long mapsize;
 189        void *map, *buf;
 190
 191        map = map_sha1_file(sha1, &mapsize);
 192        if (map) {
 193                buf = unpack_sha1_file(map, mapsize, type, size);
 194                munmap(map, mapsize);
 195                return buf;
 196        }
 197        return NULL;
 198}
 199
 200int write_sha1_file(char *buf, unsigned len, unsigned char *returnsha1)
 201{
 202        int size;
 203        char *compressed;
 204        z_stream stream;
 205        unsigned char sha1[20];
 206        SHA_CTX c;
 207
 208        /* Set it up */
 209        memset(&stream, 0, sizeof(stream));
 210        deflateInit(&stream, Z_BEST_COMPRESSION);
 211        size = deflateBound(&stream, len);
 212        compressed = malloc(size);
 213
 214        /* Compress it */
 215        stream.next_in = buf;
 216        stream.avail_in = len;
 217        stream.next_out = compressed;
 218        stream.avail_out = size;
 219        while (deflate(&stream, Z_FINISH) == Z_OK)
 220                /* nothing */;
 221        deflateEnd(&stream);
 222        size = stream.total_out;
 223
 224        /* Sha1.. */
 225        SHA1_Init(&c);
 226        SHA1_Update(&c, compressed, size);
 227        SHA1_Final(sha1, &c);
 228
 229        if (write_sha1_buffer(sha1, compressed, size) < 0)
 230                return -1;
 231        if (returnsha1)
 232                memcpy(returnsha1, sha1, 20);
 233        return 0;
 234}
 235
 236int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
 237{
 238        char *filename = sha1_file_name(sha1);
 239        int fd;
 240
 241        fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 242        if (fd < 0) {
 243                void *map;
 244
 245                if (errno != EEXIST)
 246                        return -1;
 247#ifndef COLLISION_CHECK
 248                fd = open(filename, O_RDONLY);
 249                if (fd < 0)
 250                        return -1;
 251                map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 252                if (map == MAP_FAILED)
 253                        return -1;
 254                if (memcmp(buf, map, size))
 255                        return error("SHA1 collision detected!"
 256                                        " This is bad, bad, BAD!\a\n");
 257#endif
 258                return 0;
 259        }
 260        write(fd, buf, size);
 261        close(fd);
 262        return 0;
 263}
 264
 265int cache_match_stat(struct cache_entry *ce, struct stat *st)
 266{
 267        unsigned int changed = 0;
 268
 269        /* nsec seems unreliable - not all filesystems support it, so
 270         * as long as it is in the inode cache you get right nsec
 271         * but after it gets flushed, you get zero nsec. */
 272        if (ce->mtime.sec  != (unsigned int)st->st_mtim.tv_sec
 273#ifdef NSEC
 274            || ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec
 275#endif
 276            )
 277                changed |= MTIME_CHANGED;
 278        if (ce->ctime.sec  != (unsigned int)st->st_ctim.tv_sec
 279#ifdef NSEC
 280            || ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec
 281#endif
 282            )
 283                changed |= CTIME_CHANGED;
 284        if (ce->st_uid != (unsigned int)st->st_uid ||
 285            ce->st_gid != (unsigned int)st->st_gid)
 286                changed |= OWNER_CHANGED;
 287        if (ce->st_mode != (unsigned int)st->st_mode)
 288                changed |= MODE_CHANGED;
 289        if (ce->st_dev != (unsigned int)st->st_dev ||
 290            ce->st_ino != (unsigned int)st->st_ino)
 291                changed |= INODE_CHANGED;
 292        if (ce->st_size != (unsigned int)st->st_size)
 293                changed |= DATA_CHANGED;
 294        return changed;
 295}
 296
 297int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
 298{
 299        int len = len1 < len2 ? len1 : len2;
 300        int cmp;
 301
 302        cmp = memcmp(name1, name2, len);
 303        if (cmp)
 304                return cmp;
 305        if (len1 < len2)
 306                return -1;
 307        if (len1 > len2)
 308                return 1;
 309        return 0;
 310}
 311
 312int cache_name_pos(const char *name, int namelen)
 313{
 314        int first, last;
 315
 316        first = 0;
 317        last = active_nr;
 318        while (last > first) {
 319                int next = (last + first) >> 1;
 320                struct cache_entry *ce = active_cache[next];
 321                int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
 322                if (!cmp)
 323                        return next;
 324                if (cmp < 0) {
 325                        last = next;
 326                        continue;
 327                }
 328                first = next+1;
 329        }
 330        return -first-1;
 331}
 332
 333int remove_file_from_cache(char *path)
 334{
 335        int pos = cache_name_pos(path, strlen(path));
 336        if (pos >= 0) {
 337                active_nr--;
 338                if (pos < active_nr)
 339                        memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
 340        }
 341        return 0;
 342}
 343
 344int add_cache_entry(struct cache_entry *ce, int ok_to_add)
 345{
 346        int pos;
 347
 348        pos = cache_name_pos(ce->name, ce->namelen);
 349
 350        /* existing match? Just replace it */
 351        if (pos >= 0) {
 352                active_cache[pos] = ce;
 353                return 0;
 354        }
 355        pos = -pos-1;
 356
 357        if (!ok_to_add)
 358                return -1;
 359
 360        /* Make sure the array is big enough .. */
 361        if (active_nr == active_alloc) {
 362                active_alloc = alloc_nr(active_alloc);
 363                active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
 364        }
 365
 366        /* Add it in.. */
 367        active_nr++;
 368        if (active_nr > pos)
 369                memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
 370        active_cache[pos] = ce;
 371        return 0;
 372}
 373
 374static int verify_hdr(struct cache_header *hdr, unsigned long size)
 375{
 376        SHA_CTX c;
 377        unsigned char sha1[20];
 378
 379        if (hdr->signature != CACHE_SIGNATURE)
 380                return error("bad signature");
 381        if (hdr->version != 1)
 382                return error("bad version");
 383        SHA1_Init(&c);
 384        SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
 385        SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
 386        SHA1_Final(sha1, &c);
 387        if (memcmp(sha1, hdr->sha1, 20))
 388                return error("bad header sha1");
 389        return 0;
 390}
 391
 392int read_cache(void)
 393{
 394        int fd, i;
 395        struct stat st;
 396        unsigned long size, offset;
 397        void *map;
 398        struct cache_header *hdr;
 399
 400        errno = EBUSY;
 401        if (active_cache)
 402                return error("more than one cachefile");
 403        errno = ENOENT;
 404        sha1_file_directory = getenv(DB_ENVIRONMENT);
 405        if (!sha1_file_directory)
 406                sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
 407        if (access(sha1_file_directory, X_OK) < 0)
 408                return error("no access to SHA1 file directory");
 409        fd = open(".git/index", O_RDONLY);
 410        if (fd < 0)
 411                return (errno == ENOENT) ? 0 : error("open failed");
 412
 413        size = 0; // avoid gcc warning
 414        map = (void *)-1;
 415        if (!fstat(fd, &st)) {
 416                size = st.st_size;
 417                errno = EINVAL;
 418                if (size >= sizeof(struct cache_header))
 419                        map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 420        }
 421        close(fd);
 422        if (-1 == (int)(long)map)
 423                return error("mmap failed");
 424
 425        hdr = map;
 426        if (verify_hdr(hdr, size) < 0)
 427                goto unmap;
 428
 429        active_nr = hdr->entries;
 430        active_alloc = alloc_nr(active_nr);
 431        active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
 432
 433        offset = sizeof(*hdr);
 434        for (i = 0; i < hdr->entries; i++) {
 435                struct cache_entry *ce = map + offset;
 436                offset = offset + ce_size(ce);
 437                active_cache[i] = ce;
 438        }
 439        return active_nr;
 440
 441unmap:
 442        munmap(map, size);
 443        errno = EINVAL;
 444        return error("verify header failed");
 445}
 446
 447int write_cache(int newfd, struct cache_entry **cache, int entries)
 448{
 449        SHA_CTX c;
 450        struct cache_header hdr;
 451        int i;
 452
 453        hdr.signature = CACHE_SIGNATURE;
 454        hdr.version = 1;
 455        hdr.entries = entries;
 456
 457        SHA1_Init(&c);
 458        SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
 459        for (i = 0; i < entries; i++) {
 460                struct cache_entry *ce = cache[i];
 461                int size = ce_size(ce);
 462                SHA1_Update(&c, ce, size);
 463        }
 464        SHA1_Final(hdr.sha1, &c);
 465
 466        if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
 467                return -1;
 468
 469        for (i = 0; i < entries; i++) {
 470                struct cache_entry *ce = cache[i];
 471                int size = ce_size(ce);
 472                if (write(newfd, ce, size) != size)
 473                        return -1;
 474        }
 475        return 0;
 476}