d2f38f0cdc78d81ffb5a601eb603a81775586368
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 *
   6 * This handles basic git sha1 object files - packing, unpacking,
   7 * creation etc.
   8 */
   9#include <stdarg.h>
  10#include "cache.h"
  11
  12const char *sha1_file_directory = NULL;
  13
  14#ifndef O_NOATIME
  15#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
  16#define O_NOATIME 01000000
  17#else
  18#define O_NOATIME 0
  19#endif
  20#endif
  21
  22static unsigned int sha1_file_open_flag = O_NOATIME;
  23
  24static unsigned hexval(char c)
  25{
  26        if (c >= '0' && c <= '9')
  27                return c - '0';
  28        if (c >= 'a' && c <= 'f')
  29                return c - 'a' + 10;
  30        if (c >= 'A' && c <= 'F')
  31                return c - 'A' + 10;
  32        return ~0;
  33}
  34
  35int get_sha1_hex(const char *hex, unsigned char *sha1)
  36{
  37        int i;
  38        for (i = 0; i < 20; i++) {
  39                unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
  40                if (val & ~0xff)
  41                        return -1;
  42                *sha1++ = val;
  43                hex += 2;
  44        }
  45        return 0;
  46}
  47
  48char * sha1_to_hex(const unsigned char *sha1)
  49{
  50        static char buffer[50];
  51        static const char hex[] = "0123456789abcdef";
  52        char *buf = buffer;
  53        int i;
  54
  55        for (i = 0; i < 20; i++) {
  56                unsigned int val = *sha1++;
  57                *buf++ = hex[val >> 4];
  58                *buf++ = hex[val & 0xf];
  59        }
  60        return buffer;
  61}
  62
  63/*
  64 * NOTE! This returns a statically allocated buffer, so you have to be
  65 * careful about using it. Do a "strdup()" if you need to save the
  66 * filename.
  67 */
  68char *sha1_file_name(const unsigned char *sha1)
  69{
  70        int i;
  71        static char *name, *base;
  72
  73        if (!base) {
  74                char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
  75                int len = strlen(sha1_file_directory);
  76                base = malloc(len + 60);
  77                memcpy(base, sha1_file_directory, len);
  78                memset(base+len, 0, 60);
  79                base[len] = '/';
  80                base[len+3] = '/';
  81                name = base + len + 1;
  82        }
  83        for (i = 0; i < 20; i++) {
  84                static char hex[] = "0123456789abcdef";
  85                unsigned int val = sha1[i];
  86                char *pos = name + i*2 + (i > 0);
  87                *pos++ = hex[val >> 4];
  88                *pos = hex[val & 0xf];
  89        }
  90        return base;
  91}
  92
  93int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)
  94{
  95        char header[100];
  96        unsigned char real_sha1[20];
  97        SHA_CTX c;
  98
  99        SHA1_Init(&c);
 100        SHA1_Update(&c, header, 1+sprintf(header, "%s %lu", type, size));
 101        SHA1_Update(&c, map, size);
 102        SHA1_Final(real_sha1, &c);
 103        return memcmp(sha1, real_sha1, 20) ? -1 : 0;
 104}
 105
 106void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
 107{
 108        char *filename = sha1_file_name(sha1);
 109        struct stat st;
 110        void *map;
 111        int fd;
 112
 113        fd = open(filename, O_RDONLY | sha1_file_open_flag);
 114        if (fd < 0) {
 115                /* See if it works without O_NOATIME */
 116                switch (sha1_file_open_flag) {
 117                default:
 118                        fd = open(filename, O_RDONLY);
 119                        if (fd >= 0)
 120                                break;
 121                /* Fallthrough */
 122                case 0:
 123                        perror(filename);
 124                        return NULL;
 125                }
 126
 127                /* If it failed once, it will probably fail again. Stop using O_NOATIME */
 128                sha1_file_open_flag = 0;
 129        }
 130        if (fstat(fd, &st) < 0) {
 131                close(fd);
 132                return NULL;
 133        }
 134        map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
 135        close(fd);
 136        if (-1 == (int)(long)map)
 137                return NULL;
 138        *size = st.st_size;
 139        return map;
 140}
 141
 142void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
 143{
 144        int ret, bytes;
 145        z_stream stream;
 146        char buffer[8192];
 147        char *buf;
 148
 149        /* Get the data stream */
 150        memset(&stream, 0, sizeof(stream));
 151        stream.next_in = map;
 152        stream.avail_in = mapsize;
 153        stream.next_out = buffer;
 154        stream.avail_out = sizeof(buffer);
 155
 156        inflateInit(&stream);
 157        ret = inflate(&stream, 0);
 158        if (ret < Z_OK)
 159                return NULL;
 160        if (sscanf(buffer, "%10s %lu", type, size) != 2)
 161                return NULL;
 162
 163        bytes = strlen(buffer) + 1;
 164        buf = malloc(*size);
 165        if (!buf)
 166                return NULL;
 167
 168        memcpy(buf, buffer + bytes, stream.total_out - bytes);
 169        bytes = stream.total_out - bytes;
 170        if (bytes < *size && ret == Z_OK) {
 171                stream.next_out = buf + bytes;
 172                stream.avail_out = *size - bytes;
 173                while (inflate(&stream, Z_FINISH) == Z_OK)
 174                        /* nothing */;
 175        }
 176        inflateEnd(&stream);
 177        return buf;
 178}
 179
 180void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
 181{
 182        unsigned long mapsize;
 183        void *map, *buf;
 184
 185        map = map_sha1_file(sha1, &mapsize);
 186        if (map) {
 187                buf = unpack_sha1_file(map, mapsize, type, size);
 188                munmap(map, mapsize);
 189                return buf;
 190        }
 191        return NULL;
 192}
 193
 194void *read_tree_with_tree_or_commit_sha1(const unsigned char *sha1,
 195                                         unsigned long *size,
 196                                         unsigned char *tree_sha1_return)
 197{
 198        char type[20];
 199        void *buffer;
 200        unsigned long isize;
 201        int was_commit = 0;
 202        unsigned char tree_sha1[20];
 203
 204        buffer = read_sha1_file(sha1, type, &isize);
 205
 206        /* 
 207         * We might have read a commit instead of a tree, in which case
 208         * we parse out the tree_sha1 and attempt to read from there.
 209         * (buffer + 5) is because the tree sha1 is always at offset 5
 210         * in a commit record ("tree ").
 211         */
 212        if (buffer &&
 213            !strcmp(type, "commit") &&
 214            !get_sha1_hex(buffer + 5, tree_sha1)) {
 215                free(buffer);
 216                buffer = read_sha1_file(tree_sha1, type, &isize);
 217                was_commit = 1;
 218        }
 219
 220        /*
 221         * Now do we have something and if so is it a tree?
 222         */
 223        if (!buffer || strcmp(type, "tree")) {
 224                free(buffer);
 225                return NULL;
 226        }
 227
 228        *size = isize;
 229        if (tree_sha1_return)
 230                memcpy(tree_sha1_return, was_commit ? tree_sha1 : sha1, 20);
 231        return buffer;
 232}
 233
 234int write_sha1_file(char *buf, unsigned len, const char *type, unsigned char *returnsha1)
 235{
 236        int size;
 237        char *compressed;
 238        z_stream stream;
 239        unsigned char sha1[20];
 240        SHA_CTX c;
 241        char *filename;
 242        char hdr[50];
 243        int fd, hdrlen;
 244
 245        /* Generate the header */
 246        hdrlen = sprintf(hdr, "%s %d", type, len)+1;
 247
 248        /* Sha1.. */
 249        SHA1_Init(&c);
 250        SHA1_Update(&c, hdr, hdrlen);
 251        SHA1_Update(&c, buf, len);
 252        SHA1_Final(sha1, &c);
 253
 254        if (returnsha1)
 255                memcpy(returnsha1, sha1, 20);
 256
 257        filename = sha1_file_name(sha1);
 258        fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 259        if (fd < 0) {
 260                if (errno != EEXIST)
 261                        return -1;
 262
 263                /*
 264                 * We might do collision checking here, but we'd need to
 265                 * uncompress the old file and check it. Later.
 266                 */
 267                return 0;
 268        }
 269
 270        /* Set it up */
 271        memset(&stream, 0, sizeof(stream));
 272        deflateInit(&stream, Z_BEST_COMPRESSION);
 273        size = deflateBound(&stream, len+hdrlen);
 274        compressed = malloc(size);
 275
 276        /* Compress it */
 277        stream.next_out = compressed;
 278        stream.avail_out = size;
 279
 280        /* First header.. */
 281        stream.next_in = hdr;
 282        stream.avail_in = hdrlen;
 283        while (deflate(&stream, 0) == Z_OK)
 284                /* nothing */
 285
 286        /* Then the data itself.. */
 287        stream.next_in = buf;
 288        stream.avail_in = len;
 289        while (deflate(&stream, Z_FINISH) == Z_OK)
 290                /* nothing */;
 291        deflateEnd(&stream);
 292        size = stream.total_out;
 293
 294        if (write(fd, compressed, size) != size)
 295                die("unable to write file");
 296        close(fd);
 297                
 298        return 0;
 299}
 300
 301static inline int collision_check(char *filename, void *buf, unsigned int size)
 302{
 303#ifdef COLLISION_CHECK
 304        void *map;
 305        int fd = open(filename, O_RDONLY);
 306        struct stat st;
 307        int cmp;
 308
 309        /* Unreadable object, or object went away? Strange. */
 310        if (fd < 0)
 311                return -1;
 312
 313        if (fstat(fd, &st) < 0 || size != st.st_size)
 314                return -1;
 315
 316        map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 317        close(fd);
 318        if (map == MAP_FAILED)
 319                return -1;
 320        cmp = memcmp(buf, map, size);
 321        munmap(map, size);
 322        if (cmp)
 323                return -1;
 324#endif
 325        return 0;
 326}
 327
 328int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
 329{
 330        char *filename = sha1_file_name(sha1);
 331        int fd;
 332
 333        fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 334        if (fd < 0) {
 335                if (errno != EEXIST)
 336                        return -1;
 337                if (collision_check(filename, buf, size))
 338                        return error("SHA1 collision detected!"
 339                                        " This is bad, bad, BAD!\a\n");
 340                return 0;
 341        }
 342        write(fd, buf, size);
 343        close(fd);
 344        return 0;
 345}
 346
 347int write_sha1_from_fd(const unsigned char *sha1, int fd)
 348{
 349        char *filename = sha1_file_name(sha1);
 350
 351        int local;
 352        z_stream stream;
 353        unsigned char real_sha1[20];
 354        char buf[4096];
 355        char discard[4096];
 356        int ret;
 357        SHA_CTX c;
 358
 359        local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 360
 361        if (local < 0)
 362                return error("Couldn't open %s\n", filename);
 363
 364        memset(&stream, 0, sizeof(stream));
 365
 366        inflateInit(&stream);
 367
 368        SHA1_Init(&c);
 369
 370        do {
 371                ssize_t size;
 372                size = read(fd, buf, 4096);
 373                if (size <= 0) {
 374                        close(local);
 375                        unlink(filename);
 376                        if (!size)
 377                                return error("Connection closed?");
 378                        perror("Reading from connection");
 379                        return -1;
 380                }
 381                write(local, buf, size);
 382                stream.avail_in = size;
 383                stream.next_in = buf;
 384                do {
 385                        stream.next_out = discard;
 386                        stream.avail_out = sizeof(discard);
 387                        ret = inflate(&stream, Z_SYNC_FLUSH);
 388                        SHA1_Update(&c, discard, sizeof(discard) -
 389                                    stream.avail_out);
 390                } while (stream.avail_in && ret == Z_OK);
 391                
 392        } while (ret == Z_OK);
 393        inflateEnd(&stream);
 394
 395        close(local);
 396        SHA1_Final(real_sha1, &c);
 397        if (ret != Z_STREAM_END) {
 398                unlink(filename);
 399                return error("File %s corrupted", sha1_to_hex(sha1));
 400        }
 401        if (memcmp(sha1, real_sha1, 20)) {
 402                unlink(filename);
 403                return error("File %s has bad hash\n", sha1_to_hex(sha1));
 404        }
 405        
 406        return 0;
 407}
 408
 409int has_sha1_file(const unsigned char *sha1)
 410{
 411        char *filename = sha1_file_name(sha1);
 412        struct stat st;
 413
 414        if (!stat(filename, &st))
 415                return 1;
 416        return 0;
 417}