sha1_file.con commit Fix up d_type handling - we need to include <dirent.h> before (4b18242)
   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 = xmalloc(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 = xmalloc(*size);
 165
 166        memcpy(buf, buffer + bytes, stream.total_out - bytes);
 167        bytes = stream.total_out - bytes;
 168        if (bytes < *size && ret == Z_OK) {
 169                stream.next_out = buf + bytes;
 170                stream.avail_out = *size - bytes;
 171                while (inflate(&stream, Z_FINISH) == Z_OK)
 172                        /* nothing */;
 173        }
 174        inflateEnd(&stream);
 175        return buf;
 176}
 177
 178void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
 179{
 180        unsigned long mapsize;
 181        void *map, *buf;
 182
 183        map = map_sha1_file(sha1, &mapsize);
 184        if (map) {
 185                buf = unpack_sha1_file(map, mapsize, type, size);
 186                munmap(map, mapsize);
 187                return buf;
 188        }
 189        return NULL;
 190}
 191
 192void *read_object_with_reference(const unsigned char *sha1,
 193                                 const unsigned char *required_type,
 194                                 unsigned long *size,
 195                                 unsigned char *actual_sha1_return)
 196{
 197        char type[20];
 198        void *buffer;
 199        unsigned long isize;
 200        unsigned char actual_sha1[20];
 201
 202        memcpy(actual_sha1, sha1, 20);
 203        while (1) {
 204                int ref_length = -1;
 205                const char *ref_type = NULL;
 206
 207                buffer = read_sha1_file(actual_sha1, type, &isize);
 208                if (!buffer)
 209                        return NULL;
 210                if (!strcmp(type, required_type)) {
 211                        *size = isize;
 212                        if (actual_sha1_return)
 213                                memcpy(actual_sha1_return, actual_sha1, 20);
 214                        return buffer;
 215                }
 216                /* Handle references */
 217                else if (!strcmp(type, "commit"))
 218                        ref_type = "tree ";
 219                else if (!strcmp(type, "tag"))
 220                        ref_type = "object ";
 221                else {
 222                        free(buffer);
 223                        return NULL;
 224                }
 225                ref_length = strlen(ref_type);
 226
 227                if (memcmp(buffer, ref_type, ref_length) ||
 228                    get_sha1_hex(buffer + ref_length, actual_sha1)) {
 229                        free(buffer);
 230                        return NULL;
 231                }
 232                /* Now we have the ID of the referred-to object in
 233                 * actual_sha1.  Check again. */
 234        }
 235}
 236
 237int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *returnsha1)
 238{
 239        int size;
 240        char *compressed;
 241        z_stream stream;
 242        unsigned char sha1[20];
 243        SHA_CTX c;
 244        char *filename;
 245        char hdr[50];
 246        int fd, hdrlen;
 247
 248        /* Generate the header */
 249        hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
 250
 251        /* Sha1.. */
 252        SHA1_Init(&c);
 253        SHA1_Update(&c, hdr, hdrlen);
 254        SHA1_Update(&c, buf, len);
 255        SHA1_Final(sha1, &c);
 256
 257        if (returnsha1)
 258                memcpy(returnsha1, sha1, 20);
 259
 260        filename = sha1_file_name(sha1);
 261        fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 262        if (fd < 0) {
 263                if (errno != EEXIST)
 264                        return -1;
 265
 266                /*
 267                 * We might do collision checking here, but we'd need to
 268                 * uncompress the old file and check it. Later.
 269                 */
 270                return 0;
 271        }
 272
 273        /* Set it up */
 274        memset(&stream, 0, sizeof(stream));
 275        deflateInit(&stream, Z_BEST_COMPRESSION);
 276        size = deflateBound(&stream, len+hdrlen);
 277        compressed = xmalloc(size);
 278
 279        /* Compress it */
 280        stream.next_out = compressed;
 281        stream.avail_out = size;
 282
 283        /* First header.. */
 284        stream.next_in = hdr;
 285        stream.avail_in = hdrlen;
 286        while (deflate(&stream, 0) == Z_OK)
 287                /* nothing */
 288
 289        /* Then the data itself.. */
 290        stream.next_in = buf;
 291        stream.avail_in = len;
 292        while (deflate(&stream, Z_FINISH) == Z_OK)
 293                /* nothing */;
 294        deflateEnd(&stream);
 295        size = stream.total_out;
 296
 297        if (write(fd, compressed, size) != size)
 298                die("unable to write file");
 299        close(fd);
 300                
 301        return 0;
 302}
 303
 304static inline int collision_check(char *filename, void *buf, unsigned int size)
 305{
 306#ifdef COLLISION_CHECK
 307        void *map;
 308        int fd = open(filename, O_RDONLY);
 309        struct stat st;
 310        int cmp;
 311
 312        /* Unreadable object, or object went away? Strange. */
 313        if (fd < 0)
 314                return -1;
 315
 316        if (fstat(fd, &st) < 0 || size != st.st_size)
 317                return -1;
 318
 319        map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 320        close(fd);
 321        if (map == MAP_FAILED)
 322                return -1;
 323        cmp = memcmp(buf, map, size);
 324        munmap(map, size);
 325        if (cmp)
 326                return -1;
 327#endif
 328        return 0;
 329}
 330
 331int write_sha1_buffer(const unsigned char *sha1, void *buf, unsigned int size)
 332{
 333        char *filename = sha1_file_name(sha1);
 334        int fd;
 335
 336        fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 337        if (fd < 0) {
 338                if (errno != EEXIST)
 339                        return -1;
 340                if (collision_check(filename, buf, size))
 341                        return error("SHA1 collision detected!"
 342                                        " This is bad, bad, BAD!\a\n");
 343                return 0;
 344        }
 345        write(fd, buf, size);
 346        close(fd);
 347        return 0;
 348}
 349
 350int write_sha1_from_fd(const unsigned char *sha1, int fd)
 351{
 352        char *filename = sha1_file_name(sha1);
 353
 354        int local;
 355        z_stream stream;
 356        unsigned char real_sha1[20];
 357        char buf[4096];
 358        char discard[4096];
 359        int ret;
 360        SHA_CTX c;
 361
 362        local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 363
 364        if (local < 0)
 365                return error("Couldn't open %s\n", filename);
 366
 367        memset(&stream, 0, sizeof(stream));
 368
 369        inflateInit(&stream);
 370
 371        SHA1_Init(&c);
 372
 373        do {
 374                ssize_t size;
 375                size = read(fd, buf, 4096);
 376                if (size <= 0) {
 377                        close(local);
 378                        unlink(filename);
 379                        if (!size)
 380                                return error("Connection closed?");
 381                        perror("Reading from connection");
 382                        return -1;
 383                }
 384                write(local, buf, size);
 385                stream.avail_in = size;
 386                stream.next_in = buf;
 387                do {
 388                        stream.next_out = discard;
 389                        stream.avail_out = sizeof(discard);
 390                        ret = inflate(&stream, Z_SYNC_FLUSH);
 391                        SHA1_Update(&c, discard, sizeof(discard) -
 392                                    stream.avail_out);
 393                } while (stream.avail_in && ret == Z_OK);
 394                
 395        } while (ret == Z_OK);
 396        inflateEnd(&stream);
 397
 398        close(local);
 399        SHA1_Final(real_sha1, &c);
 400        if (ret != Z_STREAM_END) {
 401                unlink(filename);
 402                return error("File %s corrupted", sha1_to_hex(sha1));
 403        }
 404        if (memcmp(sha1, real_sha1, 20)) {
 405                unlink(filename);
 406                return error("File %s has bad hash\n", sha1_to_hex(sha1));
 407        }
 408        
 409        return 0;
 410}
 411
 412int has_sha1_file(const unsigned char *sha1)
 413{
 414        char *filename = sha1_file_name(sha1);
 415        struct stat st;
 416
 417        if (!stat(filename, &st))
 418                return 1;
 419        return 0;
 420}