44b4b0fc69cef727b2a35c41b202e3a6bb424869
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8const char *sha1_file_directory = NULL;
   9struct cache_entry **active_cache = NULL;
  10unsigned int active_nr = 0, active_alloc = 0;
  11
  12void usage(const char *err)
  13{
  14        fprintf(stderr, "read-tree: %s\n", err);
  15        exit(1);
  16}
  17
  18static unsigned hexval(char c)
  19{
  20        if (c >= '0' && c <= '9')
  21                return c - '0';
  22        if (c >= 'a' && c <= 'f')
  23                return c - 'a' + 10;
  24        if (c >= 'A' && c <= 'F')
  25                return c - 'A' + 10;
  26        return ~0;
  27}
  28
  29int get_sha1_hex(char *hex, unsigned char *sha1)
  30{
  31        int i;
  32        for (i = 0; i < 20; i++) {
  33                unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
  34                if (val & ~0xff)
  35                        return -1;
  36                *sha1++ = val;
  37                hex += 2;
  38        }
  39        return 0;
  40}
  41
  42char * sha1_to_hex(unsigned char *sha1)
  43{
  44        static char buffer[50];
  45        static const char hex[] = "0123456789abcdef";
  46        char *buf = buffer;
  47        int i;
  48
  49        for (i = 0; i < 20; i++) {
  50                unsigned int val = *sha1++;
  51                *buf++ = hex[val >> 4];
  52                *buf++ = hex[val & 0xf];
  53        }
  54        return buffer;
  55}
  56
  57/*
  58 * NOTE! This returns a statically allocated buffer, so you have to be
  59 * careful about using it. Do a "strdup()" if you need to save the
  60 * filename.
  61 */
  62char *sha1_file_name(unsigned char *sha1)
  63{
  64        int i;
  65        static char *name, *base;
  66
  67        if (!base) {
  68                char *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
  69                int len = strlen(sha1_file_directory);
  70                base = malloc(len + 60);
  71                memcpy(base, sha1_file_directory, len);
  72                memset(base+len, 0, 60);
  73                base[len] = '/';
  74                base[len+3] = '/';
  75                name = base + len + 1;
  76        }
  77        for (i = 0; i < 20; i++) {
  78                static char hex[] = "0123456789abcdef";
  79                unsigned int val = sha1[i];
  80                char *pos = name + i*2 + (i > 0);
  81                *pos++ = hex[val >> 4];
  82                *pos = hex[val & 0xf];
  83        }
  84        return base;
  85}
  86
  87int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size)
  88{
  89        unsigned char real_sha1[20];
  90        SHA_CTX c;
  91
  92        SHA1_Init(&c);
  93        SHA1_Update(&c, map, size);
  94        SHA1_Final(real_sha1, &c);
  95        return memcmp(sha1, real_sha1, 20) ? -1 : 0;
  96}
  97
  98void *map_sha1_file(unsigned char *sha1, unsigned long *size)
  99{
 100        char *filename = sha1_file_name(sha1);
 101        int fd = open(filename, O_RDONLY);
 102        struct stat st;
 103        void *map;
 104
 105        if (fd < 0) {
 106                perror(filename);
 107                return NULL;
 108        }
 109        if (fstat(fd, &st) < 0) {
 110                close(fd);  
 111                return NULL;
 112        }
 113        map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
 114        close(fd);
 115        if (-1 == (int)(long)map)
 116                return NULL;
 117        *size = st.st_size;
 118        return map;
 119}
 120
 121void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
 122{
 123        int ret, bytes;
 124        z_stream stream;
 125        char buffer[8192];
 126        char *buf;
 127
 128        /* Get the data stream */
 129        memset(&stream, 0, sizeof(stream));
 130        stream.next_in = map;
 131        stream.avail_in = mapsize;
 132        stream.next_out = buffer;
 133        stream.avail_out = sizeof(buffer);
 134
 135        inflateInit(&stream);
 136        ret = inflate(&stream, 0);
 137        if (sscanf(buffer, "%10s %lu", type, size) != 2)
 138                return NULL;
 139
 140        bytes = strlen(buffer) + 1;
 141        buf = malloc(*size);
 142        if (!buf)
 143                return NULL;
 144
 145        memcpy(buf, buffer + bytes, stream.total_out - bytes);
 146        bytes = stream.total_out - bytes;
 147        if (bytes < *size && ret == Z_OK) {
 148                stream.next_out = buf + bytes;
 149                stream.avail_out = *size - bytes;
 150                while (inflate(&stream, Z_FINISH) == Z_OK)
 151                        /* nothing */;
 152        }
 153        inflateEnd(&stream);
 154        return buf;
 155}
 156
 157void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
 158{
 159        unsigned long mapsize;
 160        void *map, *buf;
 161
 162        map = map_sha1_file(sha1, &mapsize);
 163        if (map) {
 164                buf = unpack_sha1_file(map, mapsize, type, size);
 165                munmap(map, mapsize);
 166                return buf;
 167        }
 168        return NULL;
 169}
 170
 171int write_sha1_file(char *buf, unsigned len)
 172{
 173        int size;
 174        char *compressed;
 175        z_stream stream;
 176        unsigned char sha1[20];
 177        SHA_CTX c;
 178
 179        /* Set it up */
 180        memset(&stream, 0, sizeof(stream));
 181        deflateInit(&stream, Z_BEST_COMPRESSION);
 182        size = deflateBound(&stream, len);
 183        compressed = malloc(size);
 184
 185        /* Compress it */
 186        stream.next_in = buf;
 187        stream.avail_in = len;
 188        stream.next_out = compressed;
 189        stream.avail_out = size;
 190        while (deflate(&stream, Z_FINISH) == Z_OK)
 191                /* nothing */;
 192        deflateEnd(&stream);
 193        size = stream.total_out;
 194
 195        /* Sha1.. */
 196        SHA1_Init(&c);
 197        SHA1_Update(&c, compressed, size);
 198        SHA1_Final(sha1, &c);
 199
 200        if (write_sha1_buffer(sha1, compressed, size) < 0)
 201                return -1;
 202        printf("%s\n", sha1_to_hex(sha1));
 203        return 0;
 204}
 205
 206int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
 207{
 208        char *filename = sha1_file_name(sha1);
 209        int fd;
 210
 211        fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 212        if (fd < 0)
 213                return (errno == EEXIST) ? 0 : -1;
 214        write(fd, buf, size);
 215        close(fd);
 216        return 0;
 217}
 218
 219static int error(const char * string)
 220{
 221        fprintf(stderr, "error: %s\n", string);
 222        return -1;
 223}
 224
 225static int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
 226{
 227        int len = len1 < len2 ? len1 : len2;
 228        int cmp;
 229
 230        cmp = memcmp(name1, name2, len);
 231        if (cmp)
 232                return cmp;
 233        if (len1 < len2)
 234                return -1;
 235        if (len1 > len2)
 236                return 1;
 237        return 0;
 238}
 239
 240int cache_name_pos(const char *name, int namelen)
 241{
 242        int first, last;
 243
 244        first = 0;
 245        last = active_nr;
 246        while (last > first) {
 247                int next = (last + first) >> 1;
 248                struct cache_entry *ce = active_cache[next];
 249                int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
 250                if (!cmp)
 251                        return -next-1;
 252                if (cmp < 0) {
 253                        last = next;
 254                        continue;
 255                }
 256                first = next+1;
 257        }
 258        return first;
 259}
 260
 261static int verify_hdr(struct cache_header *hdr, unsigned long size)
 262{
 263        SHA_CTX c;
 264        unsigned char sha1[20];
 265
 266        if (hdr->signature != CACHE_SIGNATURE)
 267                return error("bad signature");
 268        if (hdr->version != 1)
 269                return error("bad version");
 270        SHA1_Init(&c);
 271        SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
 272        SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
 273        SHA1_Final(sha1, &c);
 274        if (memcmp(sha1, hdr->sha1, 20))
 275                return error("bad header sha1");
 276        return 0;
 277}
 278
 279int read_cache(void)
 280{
 281        int fd, i;
 282        struct stat st;
 283        unsigned long size, offset;
 284        void *map;
 285        struct cache_header *hdr;
 286
 287        errno = EBUSY;
 288        if (active_cache)
 289                return error("more than one cachefile");
 290        errno = ENOENT;
 291        sha1_file_directory = getenv(DB_ENVIRONMENT);
 292        if (!sha1_file_directory)
 293                sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
 294        if (access(sha1_file_directory, X_OK) < 0)
 295                return error("no access to SHA1 file directory");
 296        fd = open(".dircache/index", O_RDONLY);
 297        if (fd < 0)
 298                return (errno == ENOENT) ? 0 : error("open failed");
 299
 300        size = 0; // avoid gcc warning
 301        map = (void *)-1;
 302        if (!fstat(fd, &st)) {
 303                size = st.st_size;
 304                errno = EINVAL;
 305                if (size >= sizeof(struct cache_header))
 306                        map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 307        }
 308        close(fd);
 309        if (-1 == (int)(long)map)
 310                return error("mmap failed");
 311
 312        hdr = map;
 313        if (verify_hdr(hdr, size) < 0)
 314                goto unmap;
 315
 316        active_nr = hdr->entries;
 317        active_alloc = alloc_nr(active_nr);
 318        active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
 319
 320        offset = sizeof(*hdr);
 321        for (i = 0; i < hdr->entries; i++) {
 322                struct cache_entry *ce = map + offset;
 323                offset = offset + ce_size(ce);
 324                active_cache[i] = ce;
 325        }
 326        return active_nr;
 327
 328unmap:
 329        munmap(map, size);
 330        errno = EINVAL;
 331        return error("verify header failed");
 332}
 333