2046b37e4bf8977c8b8e700dca4c8ce3c5ead327
   1#include "cache.h"
   2#include "delta.h"
   3#include "pack.h"
   4#include "csum-file.h"
   5#include "blob.h"
   6#include "commit.h"
   7#include "tag.h"
   8#include "tree.h"
   9#include <sys/time.h>
  10#include <signal.h>
  11
  12static const char index_pack_usage[] =
  13"git-index-pack [-v] [-o <index-file>] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
  14
  15struct object_entry
  16{
  17        unsigned long offset;
  18        unsigned long size;
  19        unsigned int hdr_size;
  20        enum object_type type;
  21        enum object_type real_type;
  22        unsigned char sha1[20];
  23};
  24
  25union delta_base {
  26        unsigned char sha1[20];
  27        unsigned long offset;
  28};
  29
  30/*
  31 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
  32 * to memcmp() only the first 20 bytes.
  33 */
  34#define UNION_BASE_SZ   20
  35
  36struct delta_entry
  37{
  38        union delta_base base;
  39        int obj_no;
  40};
  41
  42static struct object_entry *objects;
  43static struct delta_entry *deltas;
  44static int nr_objects;
  45static int nr_deltas;
  46static int nr_resolved_deltas;
  47
  48static int from_stdin;
  49static int verbose;
  50
  51static volatile sig_atomic_t progress_update;
  52
  53static void progress_interval(int signum)
  54{
  55        progress_update = 1;
  56}
  57
  58static void setup_progress_signal(void)
  59{
  60        struct sigaction sa;
  61        struct itimerval v;
  62
  63        memset(&sa, 0, sizeof(sa));
  64        sa.sa_handler = progress_interval;
  65        sigemptyset(&sa.sa_mask);
  66        sa.sa_flags = SA_RESTART;
  67        sigaction(SIGALRM, &sa, NULL);
  68
  69        v.it_interval.tv_sec = 1;
  70        v.it_interval.tv_usec = 0;
  71        v.it_value = v.it_interval;
  72        setitimer(ITIMER_REAL, &v, NULL);
  73
  74}
  75
  76static unsigned display_progress(unsigned n, unsigned total, unsigned last_pc)
  77{
  78        unsigned percent = n * 100 / total;
  79        if (percent != last_pc || progress_update) {
  80                fprintf(stderr, "%4u%% (%u/%u) done\r", percent, n, total);
  81                progress_update = 0;
  82        }
  83        return percent;
  84}
  85
  86/* We always read in 4kB chunks. */
  87static unsigned char input_buffer[4096];
  88static unsigned long input_offset, input_len, consumed_bytes;
  89static SHA_CTX input_ctx;
  90static int input_fd, output_fd, mmap_fd;
  91
  92/* Discard current buffer used content. */
  93static void flush()
  94{
  95        if (input_offset) {
  96                if (output_fd >= 0)
  97                        write_or_die(output_fd, input_buffer, input_offset);
  98                SHA1_Update(&input_ctx, input_buffer, input_offset);
  99                memcpy(input_buffer, input_buffer + input_offset, input_len);
 100                input_offset = 0;
 101        }
 102}
 103
 104/*
 105 * Make sure at least "min" bytes are available in the buffer, and
 106 * return the pointer to the buffer.
 107 */
 108static void * fill(int min)
 109{
 110        if (min <= input_len)
 111                return input_buffer + input_offset;
 112        if (min > sizeof(input_buffer))
 113                die("cannot fill %d bytes", min);
 114        flush();
 115        do {
 116                int ret = xread(input_fd, input_buffer + input_len,
 117                                sizeof(input_buffer) - input_len);
 118                if (ret <= 0) {
 119                        if (!ret)
 120                                die("early EOF");
 121                        die("read error on input: %s", strerror(errno));
 122                }
 123                input_len += ret;
 124        } while (input_len < min);
 125        return input_buffer;
 126}
 127
 128static void use(int bytes)
 129{
 130        if (bytes > input_len)
 131                die("used more bytes than were available");
 132        input_len -= bytes;
 133        input_offset += bytes;
 134        consumed_bytes += bytes;
 135}
 136
 137static const char * open_pack_file(const char *pack_name)
 138{
 139        if (from_stdin) {
 140                input_fd = 0;
 141                if (!pack_name) {
 142                        static char tmpfile[PATH_MAX];
 143                        snprintf(tmpfile, sizeof(tmpfile),
 144                                 "%s/pack_XXXXXX", get_object_directory());
 145                        output_fd = mkstemp(tmpfile);
 146                        pack_name = xstrdup(tmpfile);
 147                } else
 148                        output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
 149                if (output_fd < 0)
 150                        die("unable to create %s: %s\n", pack_name, strerror(errno));
 151                mmap_fd = output_fd;
 152        } else {
 153                input_fd = open(pack_name, O_RDONLY);
 154                if (input_fd < 0)
 155                        die("cannot open packfile '%s': %s",
 156                            pack_name, strerror(errno));
 157                output_fd = -1;
 158                mmap_fd = input_fd;
 159        }
 160        SHA1_Init(&input_ctx);
 161        return pack_name;
 162}
 163
 164static void parse_pack_header(void)
 165{
 166        struct pack_header *hdr = fill(sizeof(struct pack_header));
 167
 168        /* Header consistency check */
 169        if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
 170                die("pack signature mismatch");
 171        if (!pack_version_ok(hdr->hdr_version))
 172                die("pack version %d unsupported", ntohl(hdr->hdr_version));
 173
 174        nr_objects = ntohl(hdr->hdr_entries);
 175        use(sizeof(struct pack_header));
 176}
 177
 178static void bad_object(unsigned long offset, const char *format,
 179                       ...) NORETURN __attribute__((format (printf, 2, 3)));
 180
 181static void bad_object(unsigned long offset, const char *format, ...)
 182{
 183        va_list params;
 184        char buf[1024];
 185
 186        va_start(params, format);
 187        vsnprintf(buf, sizeof(buf), format, params);
 188        va_end(params);
 189        die("pack has bad object at offset %lu: %s", offset, buf);
 190}
 191
 192static void *unpack_entry_data(unsigned long offset, unsigned long size)
 193{
 194        z_stream stream;
 195        void *buf = xmalloc(size);
 196
 197        memset(&stream, 0, sizeof(stream));
 198        stream.next_out = buf;
 199        stream.avail_out = size;
 200        stream.next_in = fill(1);
 201        stream.avail_in = input_len;
 202        inflateInit(&stream);
 203
 204        for (;;) {
 205                int ret = inflate(&stream, 0);
 206                use(input_len - stream.avail_in);
 207                if (stream.total_out == size && ret == Z_STREAM_END)
 208                        break;
 209                if (ret != Z_OK)
 210                        bad_object(offset, "inflate returned %d", ret);
 211                stream.next_in = fill(1);
 212                stream.avail_in = input_len;
 213        }
 214        inflateEnd(&stream);
 215        return buf;
 216}
 217
 218static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
 219{
 220        unsigned char *p, c;
 221        unsigned long size, base_offset;
 222        unsigned shift;
 223
 224        obj->offset = consumed_bytes;
 225
 226        p = fill(1);
 227        c = *p;
 228        use(1);
 229        obj->type = (c >> 4) & 7;
 230        size = (c & 15);
 231        shift = 4;
 232        while (c & 0x80) {
 233                p = fill(1);
 234                c = *p;
 235                use(1);
 236                size += (c & 0x7fUL) << shift;
 237                shift += 7;
 238        }
 239        obj->size = size;
 240
 241        switch (obj->type) {
 242        case OBJ_REF_DELTA:
 243                hashcpy(delta_base->sha1, fill(20));
 244                use(20);
 245                break;
 246        case OBJ_OFS_DELTA:
 247                memset(delta_base, 0, sizeof(*delta_base));
 248                p = fill(1);
 249                c = *p;
 250                use(1);
 251                base_offset = c & 127;
 252                while (c & 128) {
 253                        base_offset += 1;
 254                        if (!base_offset || base_offset & ~(~0UL >> 7))
 255                                bad_object(obj->offset, "offset value overflow for delta base object");
 256                        p = fill(1);
 257                        c = *p;
 258                        use(1);
 259                        base_offset = (base_offset << 7) + (c & 127);
 260                }
 261                delta_base->offset = obj->offset - base_offset;
 262                if (delta_base->offset >= obj->offset)
 263                        bad_object(obj->offset, "delta base offset is out of bound");
 264                break;
 265        case OBJ_COMMIT:
 266        case OBJ_TREE:
 267        case OBJ_BLOB:
 268        case OBJ_TAG:
 269                break;
 270        default:
 271                bad_object(obj->offset, "bad object type %d", obj->type);
 272        }
 273        obj->hdr_size = consumed_bytes - obj->offset;
 274
 275        return unpack_entry_data(obj->offset, obj->size);
 276}
 277
 278static void * get_data_from_pack(struct object_entry *obj)
 279{
 280        unsigned long from = obj[0].offset + obj[0].hdr_size;
 281        unsigned long len = obj[1].offset - from;
 282        unsigned pg_offset = from % getpagesize();
 283        unsigned char *map, *data;
 284        z_stream stream;
 285        int st;
 286
 287        map = mmap(NULL, len + pg_offset, PROT_READ, MAP_PRIVATE,
 288                   mmap_fd, from - pg_offset);
 289        if (map == MAP_FAILED)
 290                die("cannot mmap pack file: %s", strerror(errno));
 291        data = xmalloc(obj->size);
 292        memset(&stream, 0, sizeof(stream));
 293        stream.next_out = data;
 294        stream.avail_out = obj->size;
 295        stream.next_in = map + pg_offset;
 296        stream.avail_in = len;
 297        inflateInit(&stream);
 298        while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
 299        inflateEnd(&stream);
 300        if (st != Z_STREAM_END || stream.total_out != obj->size)
 301                die("serious inflate inconsistency");
 302        munmap(map, len + pg_offset);
 303        return data;
 304}
 305
 306static int find_delta(const union delta_base *base)
 307{
 308        int first = 0, last = nr_deltas;
 309
 310        while (first < last) {
 311                int next = (first + last) / 2;
 312                struct delta_entry *delta = &deltas[next];
 313                int cmp;
 314
 315                cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
 316                if (!cmp)
 317                        return next;
 318                if (cmp < 0) {
 319                        last = next;
 320                        continue;
 321                }
 322                first = next+1;
 323        }
 324        return -first-1;
 325}
 326
 327static int find_delta_childs(const union delta_base *base,
 328                             int *first_index, int *last_index)
 329{
 330        int first = find_delta(base);
 331        int last = first;
 332        int end = nr_deltas - 1;
 333
 334        if (first < 0)
 335                return -1;
 336        while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
 337                --first;
 338        while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
 339                ++last;
 340        *first_index = first;
 341        *last_index = last;
 342        return 0;
 343}
 344
 345static void sha1_object(const void *data, unsigned long size,
 346                        enum object_type type, unsigned char *sha1)
 347{
 348        SHA_CTX ctx;
 349        char header[50];
 350        int header_size;
 351        const char *type_str;
 352
 353        switch (type) {
 354        case OBJ_COMMIT: type_str = commit_type; break;
 355        case OBJ_TREE:   type_str = tree_type; break;
 356        case OBJ_BLOB:   type_str = blob_type; break;
 357        case OBJ_TAG:    type_str = tag_type; break;
 358        default:
 359                die("bad type %d", type);
 360        }
 361
 362        header_size = sprintf(header, "%s %lu", type_str, size) + 1;
 363
 364        SHA1_Init(&ctx);
 365        SHA1_Update(&ctx, header, header_size);
 366        SHA1_Update(&ctx, data, size);
 367        SHA1_Final(sha1, &ctx);
 368}
 369
 370static void resolve_delta(struct object_entry *delta_obj, void *base_data,
 371                          unsigned long base_size, enum object_type type)
 372{
 373        void *delta_data;
 374        unsigned long delta_size;
 375        void *result;
 376        unsigned long result_size;
 377        union delta_base delta_base;
 378        int j, first, last;
 379
 380        delta_obj->real_type = type;
 381        delta_data = get_data_from_pack(delta_obj);
 382        delta_size = delta_obj->size;
 383        result = patch_delta(base_data, base_size, delta_data, delta_size,
 384                             &result_size);
 385        free(delta_data);
 386        if (!result)
 387                bad_object(delta_obj->offset, "failed to apply delta");
 388        sha1_object(result, result_size, type, delta_obj->sha1);
 389        nr_resolved_deltas++;
 390
 391        hashcpy(delta_base.sha1, delta_obj->sha1);
 392        if (!find_delta_childs(&delta_base, &first, &last)) {
 393                for (j = first; j <= last; j++) {
 394                        struct object_entry *child = objects + deltas[j].obj_no;
 395                        if (child->real_type == OBJ_REF_DELTA)
 396                                resolve_delta(child, result, result_size, type);
 397                }
 398        }
 399
 400        memset(&delta_base, 0, sizeof(delta_base));
 401        delta_base.offset = delta_obj->offset;
 402        if (!find_delta_childs(&delta_base, &first, &last)) {
 403                for (j = first; j <= last; j++) {
 404                        struct object_entry *child = objects + deltas[j].obj_no;
 405                        if (child->real_type == OBJ_OFS_DELTA)
 406                                resolve_delta(child, result, result_size, type);
 407                }
 408        }
 409
 410        free(result);
 411}
 412
 413static int compare_delta_entry(const void *a, const void *b)
 414{
 415        const struct delta_entry *delta_a = a;
 416        const struct delta_entry *delta_b = b;
 417        return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
 418}
 419
 420/* Parse all objects and return the pack content SHA1 hash */
 421static void parse_pack_objects(unsigned char *sha1)
 422{
 423        int i, percent = -1;
 424        struct delta_entry *delta = deltas;
 425        void *data;
 426        struct stat st;
 427
 428        /*
 429         * First pass:
 430         * - find locations of all objects;
 431         * - calculate SHA1 of all non-delta objects;
 432         * - remember base SHA1 for all deltas.
 433         */
 434        if (verbose)
 435                fprintf(stderr, "Indexing %d objects.\n", nr_objects);
 436        for (i = 0; i < nr_objects; i++) {
 437                struct object_entry *obj = &objects[i];
 438                data = unpack_raw_entry(obj, &delta->base);
 439                obj->real_type = obj->type;
 440                if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
 441                        nr_deltas++;
 442                        delta->obj_no = i;
 443                        delta++;
 444                } else
 445                        sha1_object(data, obj->size, obj->type, obj->sha1);
 446                free(data);
 447                if (verbose)
 448                        percent = display_progress(i+1, nr_objects, percent);
 449        }
 450        objects[i].offset = consumed_bytes;
 451        if (verbose)
 452                fputc('\n', stderr);
 453
 454        /* Check pack integrity */
 455        flush();
 456        SHA1_Final(sha1, &input_ctx);
 457        if (hashcmp(fill(20), sha1))
 458                die("pack is corrupted (SHA1 mismatch)");
 459
 460        /* If input_fd is a file, we should have reached its end now. */
 461        if (fstat(input_fd, &st))
 462                die("cannot fstat packfile: %s", strerror(errno));
 463        if (S_ISREG(st.st_mode) && st.st_size != consumed_bytes + 20)
 464                die("pack has junk at the end");
 465
 466        if (!nr_deltas)
 467                return;
 468
 469        /* Sort deltas by base SHA1/offset for fast searching */
 470        qsort(deltas, nr_deltas, sizeof(struct delta_entry),
 471              compare_delta_entry);
 472
 473        /*
 474         * Second pass:
 475         * - for all non-delta objects, look if it is used as a base for
 476         *   deltas;
 477         * - if used as a base, uncompress the object and apply all deltas,
 478         *   recursively checking if the resulting object is used as a base
 479         *   for some more deltas.
 480         */
 481        if (verbose)
 482                fprintf(stderr, "Resolving %d deltas.\n", nr_deltas);
 483        for (i = 0; i < nr_objects; i++) {
 484                struct object_entry *obj = &objects[i];
 485                union delta_base base;
 486                int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
 487
 488                if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
 489                        continue;
 490                hashcpy(base.sha1, obj->sha1);
 491                ref = !find_delta_childs(&base, &ref_first, &ref_last);
 492                memset(&base, 0, sizeof(base));
 493                base.offset = obj->offset;
 494                ofs = !find_delta_childs(&base, &ofs_first, &ofs_last);
 495                if (!ref && !ofs)
 496                        continue;
 497                data = get_data_from_pack(obj);
 498                if (ref)
 499                        for (j = ref_first; j <= ref_last; j++) {
 500                                struct object_entry *child = objects + deltas[j].obj_no;
 501                                if (child->real_type == OBJ_REF_DELTA)
 502                                        resolve_delta(child, data,
 503                                                      obj->size, obj->type);
 504                        }
 505                if (ofs)
 506                        for (j = ofs_first; j <= ofs_last; j++) {
 507                                struct object_entry *child = objects + deltas[j].obj_no;
 508                                if (child->real_type == OBJ_OFS_DELTA)
 509                                        resolve_delta(child, data,
 510                                                      obj->size, obj->type);
 511                        }
 512                free(data);
 513                if (verbose)
 514                        percent = display_progress(nr_resolved_deltas,
 515                                                   nr_deltas, percent);
 516        }
 517        if (verbose && nr_resolved_deltas == nr_deltas)
 518                fputc('\n', stderr);
 519}
 520
 521static int write_compressed(int fd, void *in, unsigned int size)
 522{
 523        z_stream stream;
 524        unsigned long maxsize;
 525        void *out;
 526
 527        memset(&stream, 0, sizeof(stream));
 528        deflateInit(&stream, zlib_compression_level);
 529        maxsize = deflateBound(&stream, size);
 530        out = xmalloc(maxsize);
 531
 532        /* Compress it */
 533        stream.next_in = in;
 534        stream.avail_in = size;
 535        stream.next_out = out;
 536        stream.avail_out = maxsize;
 537        while (deflate(&stream, Z_FINISH) == Z_OK);
 538        deflateEnd(&stream);
 539
 540        size = stream.total_out;
 541        write_or_die(fd, out, size);
 542        free(out);
 543        return size;
 544}
 545
 546static void append_obj_to_pack(void *buf,
 547                               unsigned long size, enum object_type type)
 548{
 549        struct object_entry *obj = &objects[nr_objects++];
 550        unsigned char header[10];
 551        unsigned long s = size;
 552        int n = 0;
 553        unsigned char c = (type << 4) | (s & 15);
 554        s >>= 4;
 555        while (s) {
 556                header[n++] = c | 0x80;
 557                c = s & 0x7f;
 558                s >>= 7;
 559        }
 560        header[n++] = c;
 561        write_or_die(output_fd, header, n);
 562        obj[1].offset = obj[0].offset + n;
 563        obj[1].offset += write_compressed(output_fd, buf, size);
 564        sha1_object(buf, size, type, obj->sha1);
 565}
 566
 567static int delta_pos_compare(const void *_a, const void *_b)
 568{
 569        struct delta_entry *a = *(struct delta_entry **)_a;
 570        struct delta_entry *b = *(struct delta_entry **)_b;
 571        return a->obj_no - b->obj_no;
 572}
 573
 574static void fix_unresolved_deltas(int nr_unresolved)
 575{
 576        struct delta_entry **sorted_by_pos;
 577        int i, n = 0, percent = -1;
 578
 579        /*
 580         * Since many unresolved deltas may well be themselves base objects
 581         * for more unresolved deltas, we really want to include the
 582         * smallest number of base objects that would cover as much delta
 583         * as possible by picking the
 584         * trunc deltas first, allowing for other deltas to resolve without
 585         * additional base objects.  Since most base objects are to be found
 586         * before deltas depending on them, a good heuristic is to start
 587         * resolving deltas in the same order as their position in the pack.
 588         */
 589        sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
 590        for (i = 0; i < nr_deltas; i++) {
 591                if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
 592                        continue;
 593                sorted_by_pos[n++] = &deltas[i];
 594        }
 595        qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
 596
 597        for (i = 0; i < n; i++) {
 598                struct delta_entry *d = sorted_by_pos[i];
 599                void *data;
 600                unsigned long size;
 601                char type[10];
 602                enum object_type obj_type;
 603                int j, first, last;
 604
 605                if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
 606                        continue;
 607                data = read_sha1_file(d->base.sha1, type, &size);
 608                if (!data)
 609                        continue;
 610                if      (!strcmp(type, blob_type))   obj_type = OBJ_BLOB;
 611                else if (!strcmp(type, tree_type))   obj_type = OBJ_TREE;
 612                else if (!strcmp(type, commit_type)) obj_type = OBJ_COMMIT;
 613                else if (!strcmp(type, tag_type))    obj_type = OBJ_TAG;
 614                else die("base object %s is of type '%s'",
 615                         sha1_to_hex(d->base.sha1), type);
 616
 617                find_delta_childs(&d->base, &first, &last);
 618                for (j = first; j <= last; j++) {
 619                        struct object_entry *child = objects + deltas[j].obj_no;
 620                        if (child->real_type == OBJ_REF_DELTA)
 621                                resolve_delta(child, data, size, obj_type);
 622                }
 623
 624                append_obj_to_pack(data, size, obj_type);
 625                free(data);
 626                if (verbose)
 627                        percent = display_progress(nr_resolved_deltas,
 628                                                   nr_deltas, percent);
 629        }
 630        free(sorted_by_pos);
 631        if (verbose)
 632                fputc('\n', stderr);
 633}
 634
 635static void readjust_pack_header_and_sha1(unsigned char *sha1)
 636{
 637        struct pack_header hdr;
 638        SHA_CTX ctx;
 639        int size;
 640
 641        /* Rewrite pack header with updated object number */
 642        if (lseek(output_fd, 0, SEEK_SET) != 0)
 643                die("cannot seek back: %s", strerror(errno));
 644        if (xread(output_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
 645                die("cannot read pack header back: %s", strerror(errno));
 646        hdr.hdr_entries = htonl(nr_objects);
 647        if (lseek(output_fd, 0, SEEK_SET) != 0)
 648                die("cannot seek back: %s", strerror(errno));
 649        write_or_die(output_fd, &hdr, sizeof(hdr));
 650        if (lseek(output_fd, 0, SEEK_SET) != 0)
 651                die("cannot seek back: %s", strerror(errno));
 652
 653        /* Recompute and store the new pack's SHA1 */
 654        SHA1_Init(&ctx);
 655        do {
 656                unsigned char *buf[4096];
 657                size = xread(output_fd, buf, sizeof(buf));
 658                if (size < 0)
 659                        die("cannot read pack data back: %s", strerror(errno));
 660                SHA1_Update(&ctx, buf, size);
 661        } while (size > 0);
 662        SHA1_Final(sha1, &ctx);
 663        write_or_die(output_fd, sha1, 20);
 664}
 665
 666static int sha1_compare(const void *_a, const void *_b)
 667{
 668        struct object_entry *a = *(struct object_entry **)_a;
 669        struct object_entry *b = *(struct object_entry **)_b;
 670        return hashcmp(a->sha1, b->sha1);
 671}
 672
 673/*
 674 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
 675 * the SHA1 hash of sorted object names.
 676 */
 677static const char * write_index_file(const char *index_name, unsigned char *sha1)
 678{
 679        struct sha1file *f;
 680        struct object_entry **sorted_by_sha, **list, **last;
 681        unsigned int array[256];
 682        int i, fd;
 683        SHA_CTX ctx;
 684
 685        if (nr_objects) {
 686                sorted_by_sha =
 687                        xcalloc(nr_objects, sizeof(struct object_entry *));
 688                list = sorted_by_sha;
 689                last = sorted_by_sha + nr_objects;
 690                for (i = 0; i < nr_objects; ++i)
 691                        sorted_by_sha[i] = &objects[i];
 692                qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
 693                      sha1_compare);
 694
 695        }
 696        else
 697                sorted_by_sha = list = last = NULL;
 698
 699        if (!index_name) {
 700                static char tmpfile[PATH_MAX];
 701                snprintf(tmpfile, sizeof(tmpfile),
 702                         "%s/index_XXXXXX", get_object_directory());
 703                fd = mkstemp(tmpfile);
 704                index_name = xstrdup(tmpfile);
 705        } else {
 706                unlink(index_name);
 707                fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
 708        }
 709        if (fd < 0)
 710                die("unable to create %s: %s", index_name, strerror(errno));
 711        f = sha1fd(fd, index_name);
 712
 713        /*
 714         * Write the first-level table (the list is sorted,
 715         * but we use a 256-entry lookup to be able to avoid
 716         * having to do eight extra binary search iterations).
 717         */
 718        for (i = 0; i < 256; i++) {
 719                struct object_entry **next = list;
 720                while (next < last) {
 721                        struct object_entry *obj = *next;
 722                        if (obj->sha1[0] != i)
 723                                break;
 724                        next++;
 725                }
 726                array[i] = htonl(next - sorted_by_sha);
 727                list = next;
 728        }
 729        sha1write(f, array, 256 * sizeof(int));
 730
 731        /* recompute the SHA1 hash of sorted object names.
 732         * currently pack-objects does not do this, but that
 733         * can be fixed.
 734         */
 735        SHA1_Init(&ctx);
 736        /*
 737         * Write the actual SHA1 entries..
 738         */
 739        list = sorted_by_sha;
 740        for (i = 0; i < nr_objects; i++) {
 741                struct object_entry *obj = *list++;
 742                unsigned int offset = htonl(obj->offset);
 743                sha1write(f, &offset, 4);
 744                sha1write(f, obj->sha1, 20);
 745                SHA1_Update(&ctx, obj->sha1, 20);
 746        }
 747        sha1write(f, sha1, 20);
 748        sha1close(f, NULL, 1);
 749        free(sorted_by_sha);
 750        SHA1_Final(sha1, &ctx);
 751        return index_name;
 752}
 753
 754static void final(const char *final_pack_name, const char *curr_pack_name,
 755                  const char *final_index_name, const char *curr_index_name,
 756                  unsigned char *sha1)
 757{
 758        char name[PATH_MAX];
 759        int err;
 760
 761        if (!from_stdin) {
 762                close(input_fd);
 763        } else {
 764                err = close(output_fd);
 765                if (err)
 766                        die("error while closing pack file: %s", strerror(errno));
 767                chmod(curr_pack_name, 0444);
 768        }
 769
 770        if (final_pack_name != curr_pack_name) {
 771                if (!final_pack_name) {
 772                        snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
 773                                 get_object_directory(), sha1_to_hex(sha1));
 774                        final_pack_name = name;
 775                }
 776                if (move_temp_to_file(curr_pack_name, final_pack_name))
 777                        die("cannot store pack file");
 778        }
 779
 780        chmod(curr_index_name, 0444);
 781        if (final_index_name != curr_index_name) {
 782                if (!final_index_name) {
 783                        snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
 784                                 get_object_directory(), sha1_to_hex(sha1));
 785                        final_index_name = name;
 786                }
 787                if (move_temp_to_file(curr_index_name, final_index_name))
 788                        die("cannot store index file");
 789        }
 790}
 791
 792int main(int argc, char **argv)
 793{
 794        int i, fix_thin_pack = 0;
 795        const char *curr_pack, *pack_name = NULL;
 796        const char *curr_index, *index_name = NULL;
 797        char *index_name_buf = NULL;
 798        unsigned char sha1[20];
 799
 800        for (i = 1; i < argc; i++) {
 801                const char *arg = argv[i];
 802
 803                if (*arg == '-') {
 804                        if (!strcmp(arg, "--stdin")) {
 805                                from_stdin = 1;
 806                        } else if (!strcmp(arg, "--fix-thin")) {
 807                                fix_thin_pack = 1;
 808                        } else if (!strcmp(arg, "-v")) {
 809                                verbose = 1;
 810                        } else if (!strcmp(arg, "-o")) {
 811                                if (index_name || (i+1) >= argc)
 812                                        usage(index_pack_usage);
 813                                index_name = argv[++i];
 814                        } else
 815                                usage(index_pack_usage);
 816                        continue;
 817                }
 818
 819                if (pack_name)
 820                        usage(index_pack_usage);
 821                pack_name = arg;
 822        }
 823
 824        if (!pack_name && !from_stdin)
 825                usage(index_pack_usage);
 826        if (fix_thin_pack && !from_stdin)
 827                die("--fix-thin cannot be used without --stdin");
 828        if (!index_name && pack_name) {
 829                int len = strlen(pack_name);
 830                if (!has_extension(pack_name, ".pack"))
 831                        die("packfile name '%s' does not end with '.pack'",
 832                            pack_name);
 833                index_name_buf = xmalloc(len);
 834                memcpy(index_name_buf, pack_name, len - 5);
 835                strcpy(index_name_buf + len - 5, ".idx");
 836                index_name = index_name_buf;
 837        }
 838
 839        curr_pack = open_pack_file(pack_name);
 840        parse_pack_header();
 841        objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
 842        deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
 843        if (verbose)
 844                setup_progress_signal();
 845        parse_pack_objects(sha1);
 846        if (nr_deltas != nr_resolved_deltas) {
 847                if (fix_thin_pack) {
 848                        int nr_unresolved = nr_deltas - nr_resolved_deltas;
 849                        int nr_objects_initial = nr_objects;
 850                        if (nr_unresolved <= 0)
 851                                die("confusion beyond insanity");
 852                        objects = xrealloc(objects,
 853                                           (nr_objects + nr_unresolved + 1)
 854                                           * sizeof(*objects));
 855                        fix_unresolved_deltas(nr_unresolved);
 856                        if (verbose)
 857                                fprintf(stderr, "%d objects were added to complete this thin pack.\n",
 858                                        nr_objects - nr_objects_initial);
 859                        readjust_pack_header_and_sha1(sha1);
 860                }
 861                if (nr_deltas != nr_resolved_deltas)
 862                        die("pack has %d unresolved deltas",
 863                            nr_deltas - nr_resolved_deltas);
 864        } else {
 865                /* Flush remaining pack final 20-byte SHA1. */
 866                use(20);
 867                flush();
 868        }
 869        free(deltas);
 870        curr_index = write_index_file(index_name, sha1);
 871        final(pack_name, curr_pack, index_name, curr_index, sha1);
 872        free(objects);
 873        free(index_name_buf);
 874
 875        printf("%s\n", sha1_to_hex(sha1));
 876
 877        return 0;
 878}