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