4d750506e97af68013aa25e192c569da557701fd
   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
  87void *map_sha1_file(unsigned char *sha1, unsigned long *size)
  88{
  89        char *filename = sha1_file_name(sha1);
  90        int fd = open(filename, O_RDONLY);
  91        struct stat st;
  92        void *map;
  93
  94        if (fd < 0) {
  95                perror(filename);
  96                return NULL;
  97        }
  98        if (fstat(fd, &st) < 0) {
  99                close(fd);  
 100                return NULL;
 101        }
 102        map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
 103        close(fd);
 104        if (-1 == (int)(long)map)
 105                return NULL;
 106        *size = st.st_size;
 107        return map;
 108}
 109
 110void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)
 111{
 112        int ret, bytes;
 113        z_stream stream;
 114        char buffer[8192];
 115        char *buf;
 116
 117        /* Get the data stream */
 118        memset(&stream, 0, sizeof(stream));
 119        stream.next_in = map;
 120        stream.avail_in = mapsize;
 121        stream.next_out = buffer;
 122        stream.avail_out = sizeof(buffer);
 123
 124        inflateInit(&stream);
 125        ret = inflate(&stream, 0);
 126        if (sscanf(buffer, "%10s %lu", type, size) != 2)
 127                return NULL;
 128
 129        bytes = strlen(buffer) + 1;
 130        buf = malloc(*size);
 131        if (!buf)
 132                return NULL;
 133
 134        memcpy(buf, buffer + bytes, stream.total_out - bytes);
 135        bytes = stream.total_out - bytes;
 136        if (bytes < *size && ret == Z_OK) {
 137                stream.next_out = buf + bytes;
 138                stream.avail_out = *size - bytes;
 139                while (inflate(&stream, Z_FINISH) == Z_OK)
 140                        /* nothing */;
 141        }
 142        inflateEnd(&stream);
 143        return buf;
 144}
 145
 146void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size)
 147{
 148        unsigned long mapsize;
 149        void *map, *buf;
 150
 151        map = map_sha1_file(sha1, &mapsize);
 152        if (map) {
 153                buf = unpack_sha1_file(map, mapsize, type, size);
 154                munmap(map, mapsize);
 155                return buf;
 156        }
 157        return NULL;
 158}
 159
 160int write_sha1_file(char *buf, unsigned len)
 161{
 162        int size;
 163        char *compressed;
 164        z_stream stream;
 165        unsigned char sha1[20];
 166        SHA_CTX c;
 167
 168        /* Set it up */
 169        memset(&stream, 0, sizeof(stream));
 170        deflateInit(&stream, Z_BEST_COMPRESSION);
 171        size = deflateBound(&stream, len);
 172        compressed = malloc(size);
 173
 174        /* Compress it */
 175        stream.next_in = buf;
 176        stream.avail_in = len;
 177        stream.next_out = compressed;
 178        stream.avail_out = size;
 179        while (deflate(&stream, Z_FINISH) == Z_OK)
 180                /* nothing */;
 181        deflateEnd(&stream);
 182        size = stream.total_out;
 183
 184        /* Sha1.. */
 185        SHA1_Init(&c);
 186        SHA1_Update(&c, compressed, size);
 187        SHA1_Final(sha1, &c);
 188
 189        if (write_sha1_buffer(sha1, compressed, size) < 0)
 190                return -1;
 191        printf("%s\n", sha1_to_hex(sha1));
 192        return 0;
 193}
 194
 195int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size)
 196{
 197        char *filename = sha1_file_name(sha1);
 198        int fd;
 199
 200        fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
 201        if (fd < 0)
 202                return (errno == EEXIST) ? 0 : -1;
 203        write(fd, buf, size);
 204        close(fd);
 205        return 0;
 206}
 207
 208static int error(const char * string)
 209{
 210        fprintf(stderr, "error: %s\n", string);
 211        return -1;
 212}
 213
 214static int verify_hdr(struct cache_header *hdr, unsigned long size)
 215{
 216        SHA_CTX c;
 217        unsigned char sha1[20];
 218
 219        if (hdr->signature != CACHE_SIGNATURE)
 220                return error("bad signature");
 221        if (hdr->version != 1)
 222                return error("bad version");
 223        SHA1_Init(&c);
 224        SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1));
 225        SHA1_Update(&c, hdr+1, size - sizeof(*hdr));
 226        SHA1_Final(sha1, &c);
 227        if (memcmp(sha1, hdr->sha1, 20))
 228                return error("bad header sha1");
 229        return 0;
 230}
 231
 232int read_cache(void)
 233{
 234        int fd, i;
 235        struct stat st;
 236        unsigned long size, offset;
 237        void *map;
 238        struct cache_header *hdr;
 239
 240        errno = EBUSY;
 241        if (active_cache)
 242                return error("more than one cachefile");
 243        errno = ENOENT;
 244        sha1_file_directory = getenv(DB_ENVIRONMENT);
 245        if (!sha1_file_directory)
 246                sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
 247        if (access(sha1_file_directory, X_OK) < 0)
 248                return error("no access to SHA1 file directory");
 249        fd = open(".dircache/index", O_RDONLY);
 250        if (fd < 0)
 251                return (errno == ENOENT) ? 0 : error("open failed");
 252
 253        size = 0; // avoid gcc warning
 254        map = (void *)-1;
 255        if (!fstat(fd, &st)) {
 256                map = NULL;
 257                size = st.st_size;
 258                errno = EINVAL;
 259                if (size > sizeof(struct cache_header))
 260                        map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
 261        }
 262        close(fd);
 263        if (-1 == (int)(long)map)
 264                return error("mmap failed");
 265
 266        hdr = map;
 267        if (verify_hdr(hdr, size) < 0)
 268                goto unmap;
 269
 270        active_nr = hdr->entries;
 271        active_alloc = alloc_nr(active_nr);
 272        active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
 273
 274        offset = sizeof(*hdr);
 275        for (i = 0; i < hdr->entries; i++) {
 276                struct cache_entry *ce = map + offset;
 277                offset = offset + ce_size(ce);
 278                active_cache[i] = ce;
 279        }
 280        return active_nr;
 281
 282unmap:
 283        munmap(map, size);
 284        errno = EINVAL;
 285        return error("verify header failed");
 286}
 287