builtin-unpack-objects.con commit Merge git://git.kernel.org/pub/scm/gitk/gitk (d4f6bc8)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "object.h"
   4#include "delta.h"
   5#include "pack.h"
   6#include "blob.h"
   7#include "commit.h"
   8#include "tag.h"
   9#include "tree.h"
  10#include "tree-walk.h"
  11#include "progress.h"
  12#include "decorate.h"
  13#include "fsck.h"
  14
  15static int dry_run, quiet, recover, has_errors, strict;
  16static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict] < pack-file";
  17
  18/* We always read in 4kB chunks. */
  19static unsigned char buffer[4096];
  20static unsigned int offset, len;
  21static off_t consumed_bytes;
  22static git_SHA_CTX ctx;
  23
  24/*
  25 * When running under --strict mode, objects whose reachability are
  26 * suspect are kept in core without getting written in the object
  27 * store.
  28 */
  29struct obj_buffer {
  30        char *buffer;
  31        unsigned long size;
  32};
  33
  34static struct decoration obj_decorate;
  35
  36static struct obj_buffer *lookup_object_buffer(struct object *base)
  37{
  38        return lookup_decoration(&obj_decorate, base);
  39}
  40
  41static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
  42{
  43        struct obj_buffer *obj;
  44        obj = xcalloc(1, sizeof(struct obj_buffer));
  45        obj->buffer = buffer;
  46        obj->size = size;
  47        if (add_decoration(&obj_decorate, object, obj))
  48                die("object %s tried to add buffer twice!", sha1_to_hex(object->sha1));
  49}
  50
  51/*
  52 * Make sure at least "min" bytes are available in the buffer, and
  53 * return the pointer to the buffer.
  54 */
  55static void *fill(int min)
  56{
  57        if (min <= len)
  58                return buffer + offset;
  59        if (min > sizeof(buffer))
  60                die("cannot fill %d bytes", min);
  61        if (offset) {
  62                git_SHA1_Update(&ctx, buffer, offset);
  63                memmove(buffer, buffer + offset, len);
  64                offset = 0;
  65        }
  66        do {
  67                ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
  68                if (ret <= 0) {
  69                        if (!ret)
  70                                die("early EOF");
  71                        die("read error on input: %s", strerror(errno));
  72                }
  73                len += ret;
  74        } while (len < min);
  75        return buffer;
  76}
  77
  78static void use(int bytes)
  79{
  80        if (bytes > len)
  81                die("used more bytes than were available");
  82        len -= bytes;
  83        offset += bytes;
  84
  85        /* make sure off_t is sufficiently large not to wrap */
  86        if (consumed_bytes > consumed_bytes + bytes)
  87                die("pack too large for current definition of off_t");
  88        consumed_bytes += bytes;
  89}
  90
  91static void *get_data(unsigned long size)
  92{
  93        z_stream stream;
  94        void *buf = xmalloc(size);
  95
  96        memset(&stream, 0, sizeof(stream));
  97
  98        stream.next_out = buf;
  99        stream.avail_out = size;
 100        stream.next_in = fill(1);
 101        stream.avail_in = len;
 102        git_inflate_init(&stream);
 103
 104        for (;;) {
 105                int ret = git_inflate(&stream, 0);
 106                use(len - stream.avail_in);
 107                if (stream.total_out == size && ret == Z_STREAM_END)
 108                        break;
 109                if (ret != Z_OK) {
 110                        error("inflate returned %d\n", ret);
 111                        free(buf);
 112                        buf = NULL;
 113                        if (!recover)
 114                                exit(1);
 115                        has_errors = 1;
 116                        break;
 117                }
 118                stream.next_in = fill(1);
 119                stream.avail_in = len;
 120        }
 121        git_inflate_end(&stream);
 122        return buf;
 123}
 124
 125struct delta_info {
 126        unsigned char base_sha1[20];
 127        unsigned nr;
 128        off_t base_offset;
 129        unsigned long size;
 130        void *delta;
 131        struct delta_info *next;
 132};
 133
 134static struct delta_info *delta_list;
 135
 136static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
 137                              off_t base_offset,
 138                              void *delta, unsigned long size)
 139{
 140        struct delta_info *info = xmalloc(sizeof(*info));
 141
 142        hashcpy(info->base_sha1, base_sha1);
 143        info->base_offset = base_offset;
 144        info->size = size;
 145        info->delta = delta;
 146        info->nr = nr;
 147        info->next = delta_list;
 148        delta_list = info;
 149}
 150
 151struct obj_info {
 152        off_t offset;
 153        unsigned char sha1[20];
 154        struct object *obj;
 155};
 156
 157#define FLAG_OPEN (1u<<20)
 158#define FLAG_WRITTEN (1u<<21)
 159
 160static struct obj_info *obj_list;
 161unsigned nr_objects;
 162
 163/*
 164 * Called only from check_object() after it verified this object
 165 * is Ok.
 166 */
 167static void write_cached_object(struct object *obj)
 168{
 169        unsigned char sha1[20];
 170        struct obj_buffer *obj_buf = lookup_object_buffer(obj);
 171        if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), sha1) < 0)
 172                die("failed to write object %s", sha1_to_hex(obj->sha1));
 173        obj->flags |= FLAG_WRITTEN;
 174}
 175
 176/*
 177 * At the very end of the processing, write_rest() scans the objects
 178 * that have reachability requirements and calls this function.
 179 * Verify its reachability and validity recursively and write it out.
 180 */
 181static int check_object(struct object *obj, int type, void *data)
 182{
 183        if (!obj)
 184                return 0;
 185
 186        if (obj->flags & FLAG_WRITTEN)
 187                return 1;
 188
 189        if (type != OBJ_ANY && obj->type != type)
 190                die("object type mismatch");
 191
 192        if (!(obj->flags & FLAG_OPEN)) {
 193                unsigned long size;
 194                int type = sha1_object_info(obj->sha1, &size);
 195                if (type != obj->type || type <= 0)
 196                        die("object of unexpected type");
 197                obj->flags |= FLAG_WRITTEN;
 198                return 1;
 199        }
 200
 201        if (fsck_object(obj, 1, fsck_error_function))
 202                die("Error in object");
 203        if (!fsck_walk(obj, check_object, 0))
 204                die("Error on reachable objects of %s", sha1_to_hex(obj->sha1));
 205        write_cached_object(obj);
 206        return 1;
 207}
 208
 209static void write_rest(void)
 210{
 211        unsigned i;
 212        for (i = 0; i < nr_objects; i++)
 213                check_object(obj_list[i].obj, OBJ_ANY, 0);
 214}
 215
 216static void added_object(unsigned nr, enum object_type type,
 217                         void *data, unsigned long size);
 218
 219/*
 220 * Write out nr-th object from the list, now we know the contents
 221 * of it.  Under --strict, this buffers structured objects in-core,
 222 * to be checked at the end.
 223 */
 224static void write_object(unsigned nr, enum object_type type,
 225                         void *buf, unsigned long size)
 226{
 227        if (!strict) {
 228                if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
 229                        die("failed to write object");
 230                added_object(nr, type, buf, size);
 231                free(buf);
 232                obj_list[nr].obj = NULL;
 233        } else if (type == OBJ_BLOB) {
 234                struct blob *blob;
 235                if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
 236                        die("failed to write object");
 237                added_object(nr, type, buf, size);
 238                free(buf);
 239
 240                blob = lookup_blob(obj_list[nr].sha1);
 241                if (blob)
 242                        blob->object.flags |= FLAG_WRITTEN;
 243                else
 244                        die("invalid blob object");
 245                obj_list[nr].obj = NULL;
 246        } else {
 247                struct object *obj;
 248                int eaten;
 249                hash_sha1_file(buf, size, typename(type), obj_list[nr].sha1);
 250                added_object(nr, type, buf, size);
 251                obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten);
 252                if (!obj)
 253                        die("invalid %s", typename(type));
 254                add_object_buffer(obj, buf, size);
 255                obj->flags |= FLAG_OPEN;
 256                obj_list[nr].obj = obj;
 257        }
 258}
 259
 260static void resolve_delta(unsigned nr, enum object_type type,
 261                          void *base, unsigned long base_size,
 262                          void *delta, unsigned long delta_size)
 263{
 264        void *result;
 265        unsigned long result_size;
 266
 267        result = patch_delta(base, base_size,
 268                             delta, delta_size,
 269                             &result_size);
 270        if (!result)
 271                die("failed to apply delta");
 272        free(delta);
 273        write_object(nr, type, result, result_size);
 274}
 275
 276/*
 277 * We now know the contents of an object (which is nr-th in the pack);
 278 * resolve all the deltified objects that are based on it.
 279 */
 280static void added_object(unsigned nr, enum object_type type,
 281                         void *data, unsigned long size)
 282{
 283        struct delta_info **p = &delta_list;
 284        struct delta_info *info;
 285
 286        while ((info = *p) != NULL) {
 287                if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
 288                    info->base_offset == obj_list[nr].offset) {
 289                        *p = info->next;
 290                        p = &delta_list;
 291                        resolve_delta(info->nr, type, data, size,
 292                                      info->delta, info->size);
 293                        free(info);
 294                        continue;
 295                }
 296                p = &info->next;
 297        }
 298}
 299
 300static void unpack_non_delta_entry(enum object_type type, unsigned long size,
 301                                   unsigned nr)
 302{
 303        void *buf = get_data(size);
 304
 305        if (!dry_run && buf)
 306                write_object(nr, type, buf, size);
 307        else
 308                free(buf);
 309}
 310
 311static int resolve_against_held(unsigned nr, const unsigned char *base,
 312                                void *delta_data, unsigned long delta_size)
 313{
 314        struct object *obj;
 315        struct obj_buffer *obj_buffer;
 316        obj = lookup_object(base);
 317        if (!obj)
 318                return 0;
 319        obj_buffer = lookup_object_buffer(obj);
 320        if (!obj_buffer)
 321                return 0;
 322        resolve_delta(nr, obj->type, obj_buffer->buffer,
 323                      obj_buffer->size, delta_data, delta_size);
 324        return 1;
 325}
 326
 327static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
 328                               unsigned nr)
 329{
 330        void *delta_data, *base;
 331        unsigned long base_size;
 332        unsigned char base_sha1[20];
 333
 334        if (type == OBJ_REF_DELTA) {
 335                hashcpy(base_sha1, fill(20));
 336                use(20);
 337                delta_data = get_data(delta_size);
 338                if (dry_run || !delta_data) {
 339                        free(delta_data);
 340                        return;
 341                }
 342                if (has_sha1_file(base_sha1))
 343                        ; /* Ok we have this one */
 344                else if (resolve_against_held(nr, base_sha1,
 345                                              delta_data, delta_size))
 346                        return; /* we are done */
 347                else {
 348                        /* cannot resolve yet --- queue it */
 349                        hashcpy(obj_list[nr].sha1, null_sha1);
 350                        add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
 351                        return;
 352                }
 353        } else {
 354                unsigned base_found = 0;
 355                unsigned char *pack, c;
 356                off_t base_offset;
 357                unsigned lo, mid, hi;
 358
 359                pack = fill(1);
 360                c = *pack;
 361                use(1);
 362                base_offset = c & 127;
 363                while (c & 128) {
 364                        base_offset += 1;
 365                        if (!base_offset || MSB(base_offset, 7))
 366                                die("offset value overflow for delta base object");
 367                        pack = fill(1);
 368                        c = *pack;
 369                        use(1);
 370                        base_offset = (base_offset << 7) + (c & 127);
 371                }
 372                base_offset = obj_list[nr].offset - base_offset;
 373                if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
 374                        die("offset value out of bound for delta base object");
 375
 376                delta_data = get_data(delta_size);
 377                if (dry_run || !delta_data) {
 378                        free(delta_data);
 379                        return;
 380                }
 381                lo = 0;
 382                hi = nr;
 383                while (lo < hi) {
 384                        mid = (lo + hi)/2;
 385                        if (base_offset < obj_list[mid].offset) {
 386                                hi = mid;
 387                        } else if (base_offset > obj_list[mid].offset) {
 388                                lo = mid + 1;
 389                        } else {
 390                                hashcpy(base_sha1, obj_list[mid].sha1);
 391                                base_found = !is_null_sha1(base_sha1);
 392                                break;
 393                        }
 394                }
 395                if (!base_found) {
 396                        /*
 397                         * The delta base object is itself a delta that
 398                         * has not been resolved yet.
 399                         */
 400                        hashcpy(obj_list[nr].sha1, null_sha1);
 401                        add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
 402                        return;
 403                }
 404        }
 405
 406        if (resolve_against_held(nr, base_sha1, delta_data, delta_size))
 407                return;
 408
 409        base = read_sha1_file(base_sha1, &type, &base_size);
 410        if (!base) {
 411                error("failed to read delta-pack base object %s",
 412                      sha1_to_hex(base_sha1));
 413                if (!recover)
 414                        exit(1);
 415                has_errors = 1;
 416                return;
 417        }
 418        resolve_delta(nr, type, base, base_size, delta_data, delta_size);
 419        free(base);
 420}
 421
 422static void unpack_one(unsigned nr)
 423{
 424        unsigned shift;
 425        unsigned char *pack, c;
 426        unsigned long size;
 427        enum object_type type;
 428
 429        obj_list[nr].offset = consumed_bytes;
 430
 431        pack = fill(1);
 432        c = *pack;
 433        use(1);
 434        type = (c >> 4) & 7;
 435        size = (c & 15);
 436        shift = 4;
 437        while (c & 0x80) {
 438                pack = fill(1);
 439                c = *pack;
 440                use(1);
 441                size += (c & 0x7f) << shift;
 442                shift += 7;
 443        }
 444
 445        switch (type) {
 446        case OBJ_COMMIT:
 447        case OBJ_TREE:
 448        case OBJ_BLOB:
 449        case OBJ_TAG:
 450                unpack_non_delta_entry(type, size, nr);
 451                return;
 452        case OBJ_REF_DELTA:
 453        case OBJ_OFS_DELTA:
 454                unpack_delta_entry(type, size, nr);
 455                return;
 456        default:
 457                error("bad object type %d", type);
 458                has_errors = 1;
 459                if (recover)
 460                        return;
 461                exit(1);
 462        }
 463}
 464
 465static void unpack_all(void)
 466{
 467        int i;
 468        struct progress *progress = NULL;
 469        struct pack_header *hdr = fill(sizeof(struct pack_header));
 470
 471        nr_objects = ntohl(hdr->hdr_entries);
 472
 473        if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
 474                die("bad pack file");
 475        if (!pack_version_ok(hdr->hdr_version))
 476                die("unknown pack file version %"PRIu32,
 477                        ntohl(hdr->hdr_version));
 478        use(sizeof(struct pack_header));
 479
 480        if (!quiet)
 481                progress = start_progress("Unpacking objects", nr_objects);
 482        obj_list = xcalloc(nr_objects, sizeof(*obj_list));
 483        for (i = 0; i < nr_objects; i++) {
 484                unpack_one(i);
 485                display_progress(progress, i + 1);
 486        }
 487        stop_progress(&progress);
 488
 489        if (delta_list)
 490                die("unresolved deltas left after unpacking");
 491}
 492
 493int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
 494{
 495        int i;
 496        unsigned char sha1[20];
 497
 498        git_config(git_default_config, NULL);
 499
 500        quiet = !isatty(2);
 501
 502        for (i = 1 ; i < argc; i++) {
 503                const char *arg = argv[i];
 504
 505                if (*arg == '-') {
 506                        if (!strcmp(arg, "-n")) {
 507                                dry_run = 1;
 508                                continue;
 509                        }
 510                        if (!strcmp(arg, "-q")) {
 511                                quiet = 1;
 512                                continue;
 513                        }
 514                        if (!strcmp(arg, "-r")) {
 515                                recover = 1;
 516                                continue;
 517                        }
 518                        if (!strcmp(arg, "--strict")) {
 519                                strict = 1;
 520                                continue;
 521                        }
 522                        if (!prefixcmp(arg, "--pack_header=")) {
 523                                struct pack_header *hdr;
 524                                char *c;
 525
 526                                hdr = (struct pack_header *)buffer;
 527                                hdr->hdr_signature = htonl(PACK_SIGNATURE);
 528                                hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
 529                                if (*c != ',')
 530                                        die("bad %s", arg);
 531                                hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
 532                                if (*c)
 533                                        die("bad %s", arg);
 534                                len = sizeof(*hdr);
 535                                continue;
 536                        }
 537                        usage(unpack_usage);
 538                }
 539
 540                /* We don't take any non-flag arguments now.. Maybe some day */
 541                usage(unpack_usage);
 542        }
 543        git_SHA1_Init(&ctx);
 544        unpack_all();
 545        git_SHA1_Update(&ctx, buffer, offset);
 546        git_SHA1_Final(sha1, &ctx);
 547        if (strict)
 548                write_rest();
 549        if (hashcmp(fill(20), sha1))
 550                die("final sha1 did not match");
 551        use(20);
 552
 553        /* Write the last part of the buffer to stdout */
 554        while (len) {
 555                int ret = xwrite(1, buffer + offset, len);
 556                if (ret <= 0)
 557                        break;
 558                len -= ret;
 559                offset += ret;
 560        }
 561
 562        /* All done */
 563        return has_errors;
 564}