0892d2b920cbf5b3456867aaaabe45f7599edcc0
   1#include "cache.h"
   2#include "object.h"
   3#include "delta.h"
   4#include "pack.h"
   5
   6static int dry_run;
   7static const char unpack_usage[] = "git-unpack-objects < pack-file";
   8
   9/* We always read in 4kB chunks. */
  10static unsigned char buffer[4096];
  11static unsigned long offset, len, eof;
  12static SHA_CTX ctx;
  13
  14/*
  15 * Make sure at least "min" bytes are available in the buffer, and
  16 * return the pointer to the buffer.
  17 */
  18static void * fill(int min)
  19{
  20        if (min <= len)
  21                return buffer + offset;
  22        if (eof)
  23                die("unable to fill input");
  24        if (min > sizeof(buffer))
  25                die("cannot fill %d bytes", min);
  26        if (offset) {
  27                SHA1_Update(&ctx, buffer, offset);
  28                memcpy(buffer, buffer + offset, len);
  29                offset = 0;
  30        }
  31        do {
  32                int ret = read(0, buffer + len, sizeof(buffer) - len);
  33                if (ret <= 0) {
  34                        if (!ret)
  35                                die("early EOF");
  36                        if (errno == EAGAIN || errno == EINTR)
  37                                continue;
  38                        die("read error on input: %s", strerror(errno));
  39                }
  40                len += ret;
  41        } while (len < min);
  42        return buffer;
  43}
  44
  45static void use(int bytes)
  46{
  47        if (bytes > len)
  48                die("used more bytes than were available");
  49        len -= bytes;
  50        offset += bytes;
  51}
  52
  53static void *get_data(unsigned long size)
  54{
  55        z_stream stream;
  56        void *buf = xmalloc(size);
  57
  58        if (!size)
  59                return buf;
  60        memset(&stream, 0, sizeof(stream));
  61
  62        stream.next_out = buf;
  63        stream.avail_out = size;
  64        stream.next_in = fill(1);
  65        stream.avail_in = len;
  66        inflateInit(&stream);
  67
  68        for (;;) {
  69                int ret = inflate(&stream, 0);
  70                use(len - stream.avail_in);
  71                if (stream.total_out == size && ret == Z_STREAM_END)
  72                        break;
  73                if (ret != Z_OK)
  74                        die("inflate returned %d\n", ret);
  75                stream.next_in = fill(1);
  76                stream.avail_in = len;
  77        }
  78        return buf;
  79}
  80
  81struct delta_info {
  82        unsigned char base_sha1[20];
  83        unsigned long size;
  84        void *delta;
  85        struct delta_info *next;
  86};
  87
  88static struct delta_info *delta_list;
  89
  90static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
  91{
  92        struct delta_info *info = xmalloc(sizeof(*info));
  93
  94        memcpy(info->base_sha1, base_sha1, 20);
  95        info->size = size;
  96        info->delta = delta;
  97        info->next = delta_list;
  98        delta_list = info;
  99}
 100
 101static void added_object(unsigned char *sha1, char *type, void *data, unsigned long size);
 102
 103static int resolve_delta(char *type,
 104        void *base, unsigned long base_size, 
 105        void *delta, unsigned long delta_size)
 106{
 107        void *result;
 108        unsigned long result_size;
 109        unsigned char sha1[20];
 110
 111        result = patch_delta(base, base_size,
 112                             delta, delta_size,
 113                             &result_size);
 114        if (!result)
 115                die("failed to apply delta");
 116        free(delta);
 117
 118        if (write_sha1_file(result, result_size, type, sha1) < 0)
 119                die("failed to write object");
 120        added_object(sha1, type, result, result_size);
 121        free(result);
 122        return 0;
 123}
 124
 125static void added_object(unsigned char *sha1, char *type, void *data, unsigned long size)
 126{
 127        struct delta_info **p = &delta_list;
 128        struct delta_info *info;
 129
 130        while ((info = *p) != NULL) {
 131                if (!memcmp(info->base_sha1, sha1, 20)) {
 132                        *p = info->next;
 133                        p = &delta_list;
 134                        resolve_delta(type, data, size, info->delta, info->size);
 135                        free(info);
 136                        continue;
 137                }
 138                p = &info->next;
 139        }
 140}
 141
 142static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
 143{
 144        void *buf = get_data(size);
 145        unsigned char sha1[20];
 146        char *type;
 147
 148        switch (kind) {
 149        case OBJ_COMMIT: type = "commit"; break;
 150        case OBJ_TREE:   type = "tree"; break;
 151        case OBJ_BLOB:   type = "blob"; break;
 152        case OBJ_TAG:    type = "tag"; break;
 153        default: die("bad type %d", kind);
 154        }
 155        if (write_sha1_file(buf, size, type, sha1) < 0)
 156                die("failed to write object");
 157        added_object(sha1, type, buf, size);
 158        free(buf);
 159        return 0;
 160}
 161
 162static int unpack_delta_entry(unsigned long delta_size)
 163{
 164        void *delta_data, *base;
 165        unsigned long base_size;
 166        char type[20];
 167        unsigned char base_sha1[20];
 168
 169        memcpy(base_sha1, fill(20), 20);
 170        use(20);
 171
 172        delta_data = get_data(delta_size);
 173
 174        if (!has_sha1_file(base_sha1)) {
 175                add_delta_to_list(base_sha1, delta_data, delta_size);
 176                return 0;
 177        }
 178        base = read_sha1_file(base_sha1, type, &base_size);
 179        if (!base)
 180                die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
 181        return resolve_delta(type, base, base_size, delta_data, delta_size);
 182}
 183
 184static void unpack_one(void)
 185{
 186        unsigned char *pack, c;
 187        unsigned long size;
 188        enum object_type type;
 189
 190        pack = fill(1);
 191        c = *pack;
 192        use(1);
 193        type = (c >> 4) & 7;
 194        size = (c & 15);
 195        while (c & 0x80) {
 196                pack = fill(1);
 197                c = *pack++;
 198                use(1);
 199                size = (size << 7) + (c & 0x7f);
 200        }
 201        switch (type) {
 202        case OBJ_COMMIT:
 203        case OBJ_TREE:
 204        case OBJ_BLOB:
 205        case OBJ_TAG:
 206                unpack_non_delta_entry(type, size);
 207                return;
 208        case OBJ_DELTA:
 209                unpack_delta_entry(size);
 210                return;
 211        default:
 212                die("bad object type %d", type);
 213        }
 214}
 215
 216/*
 217 * We unpack from the end, older files first. Now, usually
 218 * there are deltas etc, so we'll not actually write the
 219 * objects in that order, but we might as well try..
 220 */
 221static void unpack_all(void)
 222{
 223        int i;
 224        struct pack_header *hdr = fill(sizeof(struct pack_header));
 225        unsigned version = ntohl(hdr->hdr_version);
 226        unsigned nr_objects = ntohl(hdr->hdr_entries);
 227
 228        if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
 229                die("bad pack file");
 230        if (version != 1)
 231                die("unable to handle pack file version %d", version);
 232        fprintf(stderr, "Unpacking %d objects\n", nr_objects);
 233
 234        use(sizeof(struct pack_header));
 235        for (i = 0; i < nr_objects; i++)
 236                unpack_one();
 237        if (delta_list)
 238                die("unresolved deltas left after unpacking");
 239}
 240
 241int main(int argc, char **argv)
 242{
 243        int i;
 244        unsigned char sha1[20];
 245
 246        for (i = 1 ; i < argc; i++) {
 247                const char *arg = argv[i];
 248
 249                if (*arg == '-') {
 250                        if (!strcmp(arg, "-n")) {
 251                                dry_run = 1;
 252                                continue;
 253                        }
 254                        usage(unpack_usage);
 255                }
 256
 257                /* We don't take any non-flag arguments now.. Maybe some day */
 258                usage(unpack_usage);
 259        }
 260        SHA1_Init(&ctx);
 261        unpack_all();
 262        SHA1_Update(&ctx, buffer, offset);
 263        SHA1_Final(sha1, &ctx);
 264        if (memcmp(fill(20), sha1, 20))
 265                die("final sha1 did not match");
 266        use(20);
 267
 268        /* Write the last part of the buffer to stdout */
 269        while (len) {
 270                int ret = write(1, buffer + offset, len);
 271                if (!ret)
 272                        break;
 273                if (ret < 0) {
 274                        if (errno == EAGAIN || errno == EINTR)
 275                                continue;
 276                        break;
 277                }
 278                len -= ret;
 279                offset += ret;
 280        }
 281
 282        /* All done */
 283        return 0;
 284}