index-pack.con commit git-cvsserver: fix breakage when calling git merge-file (c6b4fa9)
   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>] [{ ---keep | --keep=<msg> }] { <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(void)
  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                memmove(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_children(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_children(&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_children(&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 or offset) 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        use(20);
 460
 461        /* If input_fd is a file, we should have reached its end now. */
 462        if (fstat(input_fd, &st))
 463                die("cannot fstat packfile: %s", strerror(errno));
 464        if (S_ISREG(st.st_mode) && st.st_size != consumed_bytes)
 465                die("pack has junk at the end");
 466
 467        if (!nr_deltas)
 468                return;
 469
 470        /* Sort deltas by base SHA1/offset for fast searching */
 471        qsort(deltas, nr_deltas, sizeof(struct delta_entry),
 472              compare_delta_entry);
 473
 474        /*
 475         * Second pass:
 476         * - for all non-delta objects, look if it is used as a base for
 477         *   deltas;
 478         * - if used as a base, uncompress the object and apply all deltas,
 479         *   recursively checking if the resulting object is used as a base
 480         *   for some more deltas.
 481         */
 482        if (verbose)
 483                fprintf(stderr, "Resolving %d deltas.\n", nr_deltas);
 484        for (i = 0; i < nr_objects; i++) {
 485                struct object_entry *obj = &objects[i];
 486                union delta_base base;
 487                int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
 488
 489                if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
 490                        continue;
 491                hashcpy(base.sha1, obj->sha1);
 492                ref = !find_delta_children(&base, &ref_first, &ref_last);
 493                memset(&base, 0, sizeof(base));
 494                base.offset = obj->offset;
 495                ofs = !find_delta_children(&base, &ofs_first, &ofs_last);
 496                if (!ref && !ofs)
 497                        continue;
 498                data = get_data_from_pack(obj);
 499                if (ref)
 500                        for (j = ref_first; j <= ref_last; j++) {
 501                                struct object_entry *child = objects + deltas[j].obj_no;
 502                                if (child->real_type == OBJ_REF_DELTA)
 503                                        resolve_delta(child, data,
 504                                                      obj->size, obj->type);
 505                        }
 506                if (ofs)
 507                        for (j = ofs_first; j <= ofs_last; j++) {
 508                                struct object_entry *child = objects + deltas[j].obj_no;
 509                                if (child->real_type == OBJ_OFS_DELTA)
 510                                        resolve_delta(child, data,
 511                                                      obj->size, obj->type);
 512                        }
 513                free(data);
 514                if (verbose)
 515                        percent = display_progress(nr_resolved_deltas,
 516                                                   nr_deltas, percent);
 517        }
 518        if (verbose && nr_resolved_deltas == nr_deltas)
 519                fputc('\n', stderr);
 520}
 521
 522static int write_compressed(int fd, void *in, unsigned int size)
 523{
 524        z_stream stream;
 525        unsigned long maxsize;
 526        void *out;
 527
 528        memset(&stream, 0, sizeof(stream));
 529        deflateInit(&stream, zlib_compression_level);
 530        maxsize = deflateBound(&stream, size);
 531        out = xmalloc(maxsize);
 532
 533        /* Compress it */
 534        stream.next_in = in;
 535        stream.avail_in = size;
 536        stream.next_out = out;
 537        stream.avail_out = maxsize;
 538        while (deflate(&stream, Z_FINISH) == Z_OK);
 539        deflateEnd(&stream);
 540
 541        size = stream.total_out;
 542        write_or_die(fd, out, size);
 543        free(out);
 544        return size;
 545}
 546
 547static void append_obj_to_pack(void *buf,
 548                               unsigned long size, enum object_type type)
 549{
 550        struct object_entry *obj = &objects[nr_objects++];
 551        unsigned char header[10];
 552        unsigned long s = size;
 553        int n = 0;
 554        unsigned char c = (type << 4) | (s & 15);
 555        s >>= 4;
 556        while (s) {
 557                header[n++] = c | 0x80;
 558                c = s & 0x7f;
 559                s >>= 7;
 560        }
 561        header[n++] = c;
 562        write_or_die(output_fd, header, n);
 563        obj[1].offset = obj[0].offset + n;
 564        obj[1].offset += write_compressed(output_fd, buf, size);
 565        sha1_object(buf, size, type, obj->sha1);
 566}
 567
 568static int delta_pos_compare(const void *_a, const void *_b)
 569{
 570        struct delta_entry *a = *(struct delta_entry **)_a;
 571        struct delta_entry *b = *(struct delta_entry **)_b;
 572        return a->obj_no - b->obj_no;
 573}
 574
 575static void fix_unresolved_deltas(int nr_unresolved)
 576{
 577        struct delta_entry **sorted_by_pos;
 578        int i, n = 0, percent = -1;
 579
 580        /*
 581         * Since many unresolved deltas may well be themselves base objects
 582         * for more unresolved deltas, we really want to include the
 583         * smallest number of base objects that would cover as much delta
 584         * as possible by picking the
 585         * trunc deltas first, allowing for other deltas to resolve without
 586         * additional base objects.  Since most base objects are to be found
 587         * before deltas depending on them, a good heuristic is to start
 588         * resolving deltas in the same order as their position in the pack.
 589         */
 590        sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
 591        for (i = 0; i < nr_deltas; i++) {
 592                if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
 593                        continue;
 594                sorted_by_pos[n++] = &deltas[i];
 595        }
 596        qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
 597
 598        for (i = 0; i < n; i++) {
 599                struct delta_entry *d = sorted_by_pos[i];
 600                void *data;
 601                unsigned long size;
 602                char type[10];
 603                enum object_type obj_type;
 604                int j, first, last;
 605
 606                if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
 607                        continue;
 608                data = read_sha1_file(d->base.sha1, type, &size);
 609                if (!data)
 610                        continue;
 611                if      (!strcmp(type, blob_type))   obj_type = OBJ_BLOB;
 612                else if (!strcmp(type, tree_type))   obj_type = OBJ_TREE;
 613                else if (!strcmp(type, commit_type)) obj_type = OBJ_COMMIT;
 614                else if (!strcmp(type, tag_type))    obj_type = OBJ_TAG;
 615                else die("base object %s is of type '%s'",
 616                         sha1_to_hex(d->base.sha1), type);
 617
 618                find_delta_children(&d->base, &first, &last);
 619                for (j = first; j <= last; j++) {
 620                        struct object_entry *child = objects + deltas[j].obj_no;
 621                        if (child->real_type == OBJ_REF_DELTA)
 622                                resolve_delta(child, data, size, obj_type);
 623                }
 624
 625                append_obj_to_pack(data, size, obj_type);
 626                free(data);
 627                if (verbose)
 628                        percent = display_progress(nr_resolved_deltas,
 629                                                   nr_deltas, percent);
 630        }
 631        free(sorted_by_pos);
 632        if (verbose)
 633                fputc('\n', stderr);
 634}
 635
 636static void readjust_pack_header_and_sha1(unsigned char *sha1)
 637{
 638        struct pack_header hdr;
 639        SHA_CTX ctx;
 640        int size;
 641
 642        /* Rewrite pack header with updated object number */
 643        if (lseek(output_fd, 0, SEEK_SET) != 0)
 644                die("cannot seek back: %s", strerror(errno));
 645        if (xread(output_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
 646                die("cannot read pack header back: %s", strerror(errno));
 647        hdr.hdr_entries = htonl(nr_objects);
 648        if (lseek(output_fd, 0, SEEK_SET) != 0)
 649                die("cannot seek back: %s", strerror(errno));
 650        write_or_die(output_fd, &hdr, sizeof(hdr));
 651        if (lseek(output_fd, 0, SEEK_SET) != 0)
 652                die("cannot seek back: %s", strerror(errno));
 653
 654        /* Recompute and store the new pack's SHA1 */
 655        SHA1_Init(&ctx);
 656        do {
 657                unsigned char *buf[4096];
 658                size = xread(output_fd, buf, sizeof(buf));
 659                if (size < 0)
 660                        die("cannot read pack data back: %s", strerror(errno));
 661                SHA1_Update(&ctx, buf, size);
 662        } while (size > 0);
 663        SHA1_Final(sha1, &ctx);
 664        write_or_die(output_fd, sha1, 20);
 665}
 666
 667static int sha1_compare(const void *_a, const void *_b)
 668{
 669        struct object_entry *a = *(struct object_entry **)_a;
 670        struct object_entry *b = *(struct object_entry **)_b;
 671        return hashcmp(a->sha1, b->sha1);
 672}
 673
 674/*
 675 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
 676 * the SHA1 hash of sorted object names.
 677 */
 678static const char *write_index_file(const char *index_name, unsigned char *sha1)
 679{
 680        struct sha1file *f;
 681        struct object_entry **sorted_by_sha, **list, **last;
 682        unsigned int array[256];
 683        int i, fd;
 684        SHA_CTX ctx;
 685
 686        if (nr_objects) {
 687                sorted_by_sha =
 688                        xcalloc(nr_objects, sizeof(struct object_entry *));
 689                list = sorted_by_sha;
 690                last = sorted_by_sha + nr_objects;
 691                for (i = 0; i < nr_objects; ++i)
 692                        sorted_by_sha[i] = &objects[i];
 693                qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
 694                      sha1_compare);
 695
 696        }
 697        else
 698                sorted_by_sha = list = last = NULL;
 699
 700        if (!index_name) {
 701                static char tmpfile[PATH_MAX];
 702                snprintf(tmpfile, sizeof(tmpfile),
 703                         "%s/index_XXXXXX", get_object_directory());
 704                fd = mkstemp(tmpfile);
 705                index_name = xstrdup(tmpfile);
 706        } else {
 707                unlink(index_name);
 708                fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
 709        }
 710        if (fd < 0)
 711                die("unable to create %s: %s", index_name, strerror(errno));
 712        f = sha1fd(fd, index_name);
 713
 714        /*
 715         * Write the first-level table (the list is sorted,
 716         * but we use a 256-entry lookup to be able to avoid
 717         * having to do eight extra binary search iterations).
 718         */
 719        for (i = 0; i < 256; i++) {
 720                struct object_entry **next = list;
 721                while (next < last) {
 722                        struct object_entry *obj = *next;
 723                        if (obj->sha1[0] != i)
 724                                break;
 725                        next++;
 726                }
 727                array[i] = htonl(next - sorted_by_sha);
 728                list = next;
 729        }
 730        sha1write(f, array, 256 * sizeof(int));
 731
 732        /* recompute the SHA1 hash of sorted object names.
 733         * currently pack-objects does not do this, but that
 734         * can be fixed.
 735         */
 736        SHA1_Init(&ctx);
 737        /*
 738         * Write the actual SHA1 entries..
 739         */
 740        list = sorted_by_sha;
 741        for (i = 0; i < nr_objects; i++) {
 742                struct object_entry *obj = *list++;
 743                unsigned int offset = htonl(obj->offset);
 744                sha1write(f, &offset, 4);
 745                sha1write(f, obj->sha1, 20);
 746                SHA1_Update(&ctx, obj->sha1, 20);
 747        }
 748        sha1write(f, sha1, 20);
 749        sha1close(f, NULL, 1);
 750        free(sorted_by_sha);
 751        SHA1_Final(sha1, &ctx);
 752        return index_name;
 753}
 754
 755static void final(const char *final_pack_name, const char *curr_pack_name,
 756                  const char *final_index_name, const char *curr_index_name,
 757                  const char *keep_name, const char *keep_msg,
 758                  unsigned char *sha1)
 759{
 760        char *report = "pack";
 761        char name[PATH_MAX];
 762        int err;
 763
 764        if (!from_stdin) {
 765                close(input_fd);
 766        } else {
 767                err = close(output_fd);
 768                if (err)
 769                        die("error while closing pack file: %s", strerror(errno));
 770                chmod(curr_pack_name, 0444);
 771        }
 772
 773        if (keep_msg) {
 774                int keep_fd, keep_msg_len = strlen(keep_msg);
 775                if (!keep_name) {
 776                        snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
 777                                 get_object_directory(), sha1_to_hex(sha1));
 778                        keep_name = name;
 779                }
 780                keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
 781                if (keep_fd < 0) {
 782                        if (errno != EEXIST)
 783                                die("cannot write keep file");
 784                } else {
 785                        if (keep_msg_len > 0) {
 786                                write_or_die(keep_fd, keep_msg, keep_msg_len);
 787                                write_or_die(keep_fd, "\n", 1);
 788                        }
 789                        close(keep_fd);
 790                        report = "keep";
 791                }
 792        }
 793
 794        if (final_pack_name != curr_pack_name) {
 795                if (!final_pack_name) {
 796                        snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
 797                                 get_object_directory(), sha1_to_hex(sha1));
 798                        final_pack_name = name;
 799                }
 800                if (move_temp_to_file(curr_pack_name, final_pack_name))
 801                        die("cannot store pack file");
 802        }
 803
 804        chmod(curr_index_name, 0444);
 805        if (final_index_name != curr_index_name) {
 806                if (!final_index_name) {
 807                        snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
 808                                 get_object_directory(), sha1_to_hex(sha1));
 809                        final_index_name = name;
 810                }
 811                if (move_temp_to_file(curr_index_name, final_index_name))
 812                        die("cannot store index file");
 813        }
 814
 815        if (!from_stdin) {
 816                printf("%s\n", sha1_to_hex(sha1));
 817        } else {
 818                char buf[48];
 819                int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
 820                                   report, sha1_to_hex(sha1));
 821                xwrite(1, buf, len);
 822
 823                /*
 824                 * Let's just mimic git-unpack-objects here and write
 825                 * the last part of the input buffer to stdout.
 826                 */
 827                while (input_len) {
 828                        err = xwrite(1, input_buffer + input_offset, input_len);
 829                        if (err <= 0)
 830                                break;
 831                        input_len -= err;
 832                        input_offset += err;
 833                }
 834        }
 835}
 836
 837int main(int argc, char **argv)
 838{
 839        int i, fix_thin_pack = 0;
 840        const char *curr_pack, *pack_name = NULL;
 841        const char *curr_index, *index_name = NULL;
 842        const char *keep_name = NULL, *keep_msg = NULL;
 843        char *index_name_buf = NULL, *keep_name_buf = NULL;
 844        unsigned char sha1[20];
 845
 846        for (i = 1; i < argc; i++) {
 847                const char *arg = argv[i];
 848
 849                if (*arg == '-') {
 850                        if (!strcmp(arg, "--stdin")) {
 851                                from_stdin = 1;
 852                        } else if (!strcmp(arg, "--fix-thin")) {
 853                                fix_thin_pack = 1;
 854                        } else if (!strcmp(arg, "--keep")) {
 855                                keep_msg = "";
 856                        } else if (!strncmp(arg, "--keep=", 7)) {
 857                                keep_msg = arg + 7;
 858                        } else if (!strncmp(arg, "--pack_header=", 14)) {
 859                                struct pack_header *hdr;
 860                                char *c;
 861
 862                                hdr = (struct pack_header *)input_buffer;
 863                                hdr->hdr_signature = htonl(PACK_SIGNATURE);
 864                                hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
 865                                if (*c != ',')
 866                                        die("bad %s", arg);
 867                                hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
 868                                if (*c)
 869                                        die("bad %s", arg);
 870                                input_len = sizeof(*hdr);
 871                        } else if (!strcmp(arg, "-v")) {
 872                                verbose = 1;
 873                        } else if (!strcmp(arg, "-o")) {
 874                                if (index_name || (i+1) >= argc)
 875                                        usage(index_pack_usage);
 876                                index_name = argv[++i];
 877                        } else
 878                                usage(index_pack_usage);
 879                        continue;
 880                }
 881
 882                if (pack_name)
 883                        usage(index_pack_usage);
 884                pack_name = arg;
 885        }
 886
 887        if (!pack_name && !from_stdin)
 888                usage(index_pack_usage);
 889        if (fix_thin_pack && !from_stdin)
 890                die("--fix-thin cannot be used without --stdin");
 891        if (!index_name && pack_name) {
 892                int len = strlen(pack_name);
 893                if (!has_extension(pack_name, ".pack"))
 894                        die("packfile name '%s' does not end with '.pack'",
 895                            pack_name);
 896                index_name_buf = xmalloc(len);
 897                memcpy(index_name_buf, pack_name, len - 5);
 898                strcpy(index_name_buf + len - 5, ".idx");
 899                index_name = index_name_buf;
 900        }
 901        if (keep_msg && !keep_name && pack_name) {
 902                int len = strlen(pack_name);
 903                if (!has_extension(pack_name, ".pack"))
 904                        die("packfile name '%s' does not end with '.pack'",
 905                            pack_name);
 906                keep_name_buf = xmalloc(len);
 907                memcpy(keep_name_buf, pack_name, len - 5);
 908                strcpy(keep_name_buf + len - 5, ".keep");
 909                keep_name = keep_name_buf;
 910        }
 911
 912        curr_pack = open_pack_file(pack_name);
 913        parse_pack_header();
 914        objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
 915        deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
 916        if (verbose)
 917                setup_progress_signal();
 918        parse_pack_objects(sha1);
 919        if (nr_deltas != nr_resolved_deltas) {
 920                if (fix_thin_pack) {
 921                        int nr_unresolved = nr_deltas - nr_resolved_deltas;
 922                        int nr_objects_initial = nr_objects;
 923                        if (nr_unresolved <= 0)
 924                                die("confusion beyond insanity");
 925                        objects = xrealloc(objects,
 926                                           (nr_objects + nr_unresolved + 1)
 927                                           * sizeof(*objects));
 928                        fix_unresolved_deltas(nr_unresolved);
 929                        if (verbose)
 930                                fprintf(stderr, "%d objects were added to complete this thin pack.\n",
 931                                        nr_objects - nr_objects_initial);
 932                        readjust_pack_header_and_sha1(sha1);
 933                }
 934                if (nr_deltas != nr_resolved_deltas)
 935                        die("pack has %d unresolved deltas",
 936                            nr_deltas - nr_resolved_deltas);
 937        } else {
 938                /* Flush remaining pack final 20-byte SHA1. */
 939                flush();
 940        }
 941        free(deltas);
 942        curr_index = write_index_file(index_name, sha1);
 943        final(pack_name, curr_pack,
 944                index_name, curr_index,
 945                keep_name, keep_msg,
 946                sha1);
 947        free(objects);
 948        free(index_name_buf);
 949        free(keep_name_buf);
 950
 951        return 0;
 952}