builtin-fsck.con commit pack-objects: quickfix for permission modes. (aef5aed)
   1#include "cache.h"
   2#include "commit.h"
   3#include "tree.h"
   4#include "blob.h"
   5#include "tag.h"
   6#include "refs.h"
   7#include "pack.h"
   8#include "cache-tree.h"
   9#include "tree-walk.h"
  10
  11#define REACHABLE 0x0001
  12#define SEEN      0x0002
  13
  14static int show_root;
  15static int show_tags;
  16static int show_unreachable;
  17static int include_reflogs = 1;
  18static int check_full;
  19static int check_strict;
  20static int keep_cache_objects;
  21static unsigned char head_sha1[20];
  22static int errors_found;
  23#define ERROR_OBJECT 01
  24#define ERROR_REACHABLE 02
  25
  26#ifdef NO_D_INO_IN_DIRENT
  27#define SORT_DIRENT 0
  28#define DIRENT_SORT_HINT(de) 0
  29#else
  30#define SORT_DIRENT 1
  31#define DIRENT_SORT_HINT(de) ((de)->d_ino)
  32#endif
  33
  34static void objreport(struct object *obj, const char *severity,
  35                      const char *err, va_list params)
  36{
  37        fprintf(stderr, "%s in %s %s: ",
  38                severity, typename(obj->type), sha1_to_hex(obj->sha1));
  39        vfprintf(stderr, err, params);
  40        fputs("\n", stderr);
  41}
  42
  43static int objerror(struct object *obj, const char *err, ...)
  44{
  45        va_list params;
  46        va_start(params, err);
  47        errors_found |= ERROR_OBJECT;
  48        objreport(obj, "error", err, params);
  49        va_end(params);
  50        return -1;
  51}
  52
  53static int objwarning(struct object *obj, const char *err, ...)
  54{
  55        va_list params;
  56        va_start(params, err);
  57        objreport(obj, "warning", err, params);
  58        va_end(params);
  59        return -1;
  60}
  61
  62/*
  63 * Check a single reachable object
  64 */
  65static void check_reachable_object(struct object *obj)
  66{
  67        const struct object_refs *refs;
  68
  69        /*
  70         * We obviously want the object to be parsed,
  71         * except if it was in a pack-file and we didn't
  72         * do a full fsck
  73         */
  74        if (!obj->parsed) {
  75                if (has_sha1_pack(obj->sha1, NULL))
  76                        return; /* it is in pack - forget about it */
  77                printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
  78                errors_found |= ERROR_REACHABLE;
  79                return;
  80        }
  81
  82        /*
  83         * Check that everything that we try to reference is also good.
  84         */
  85        refs = lookup_object_refs(obj);
  86        if (refs) {
  87                unsigned j;
  88                for (j = 0; j < refs->count; j++) {
  89                        struct object *ref = refs->ref[j];
  90                        if (ref->parsed ||
  91                            (has_sha1_file(ref->sha1)))
  92                                continue;
  93                        printf("broken link from %7s %s\n",
  94                               typename(obj->type), sha1_to_hex(obj->sha1));
  95                        printf("              to %7s %s\n",
  96                               typename(ref->type), sha1_to_hex(ref->sha1));
  97                        errors_found |= ERROR_REACHABLE;
  98                }
  99        }
 100}
 101
 102/*
 103 * Check a single unreachable object
 104 */
 105static void check_unreachable_object(struct object *obj)
 106{
 107        /*
 108         * Missing unreachable object? Ignore it. It's not like
 109         * we miss it (since it can't be reached), nor do we want
 110         * to complain about it being unreachable (since it does
 111         * not exist).
 112         */
 113        if (!obj->parsed)
 114                return;
 115
 116        /*
 117         * Unreachable object that exists? Show it if asked to,
 118         * since this is something that is prunable.
 119         */
 120        if (show_unreachable) {
 121                printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
 122                return;
 123        }
 124
 125        /*
 126         * "!used" means that nothing at all points to it, including
 127         * other unreachable objects. In other words, it's the "tip"
 128         * of some set of unreachable objects, usually a commit that
 129         * got dropped.
 130         *
 131         * Such starting points are more interesting than some random
 132         * set of unreachable objects, so we show them even if the user
 133         * hasn't asked for _all_ unreachable objects. If you have
 134         * deleted a branch by mistake, this is a prime candidate to
 135         * start looking at, for example.
 136         */
 137        if (!obj->used) {
 138                printf("dangling %s %s\n", typename(obj->type),
 139                       sha1_to_hex(obj->sha1));
 140                return;
 141        }
 142
 143        /*
 144         * Otherwise? It's there, it's unreachable, and some other unreachable
 145         * object points to it. Ignore it - it's not interesting, and we showed
 146         * all the interesting cases above.
 147         */
 148}
 149
 150static void check_object(struct object *obj)
 151{
 152        if (obj->flags & REACHABLE)
 153                check_reachable_object(obj);
 154        else
 155                check_unreachable_object(obj);
 156}
 157
 158static void check_connectivity(void)
 159{
 160        int i, max;
 161
 162        /* Look up all the requirements, warn about missing objects.. */
 163        max = get_max_object_index();
 164        for (i = 0; i < max; i++) {
 165                struct object *obj = get_indexed_object(i);
 166
 167                if (obj)
 168                        check_object(obj);
 169        }
 170}
 171
 172/*
 173 * The entries in a tree are ordered in the _path_ order,
 174 * which means that a directory entry is ordered by adding
 175 * a slash to the end of it.
 176 *
 177 * So a directory called "a" is ordered _after_ a file
 178 * called "a.c", because "a/" sorts after "a.c".
 179 */
 180#define TREE_UNORDERED (-1)
 181#define TREE_HAS_DUPS  (-2)
 182
 183static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
 184{
 185        int len1 = strlen(name1);
 186        int len2 = strlen(name2);
 187        int len = len1 < len2 ? len1 : len2;
 188        unsigned char c1, c2;
 189        int cmp;
 190
 191        cmp = memcmp(name1, name2, len);
 192        if (cmp < 0)
 193                return 0;
 194        if (cmp > 0)
 195                return TREE_UNORDERED;
 196
 197        /*
 198         * Ok, the first <len> characters are the same.
 199         * Now we need to order the next one, but turn
 200         * a '\0' into a '/' for a directory entry.
 201         */
 202        c1 = name1[len];
 203        c2 = name2[len];
 204        if (!c1 && !c2)
 205                /*
 206                 * git-write-tree used to write out a nonsense tree that has
 207                 * entries with the same name, one blob and one tree.  Make
 208                 * sure we do not have duplicate entries.
 209                 */
 210                return TREE_HAS_DUPS;
 211        if (!c1 && S_ISDIR(mode1))
 212                c1 = '/';
 213        if (!c2 && S_ISDIR(mode2))
 214                c2 = '/';
 215        return c1 < c2 ? 0 : TREE_UNORDERED;
 216}
 217
 218static int fsck_tree(struct tree *item)
 219{
 220        int retval;
 221        int has_full_path = 0;
 222        int has_zero_pad = 0;
 223        int has_bad_modes = 0;
 224        int has_dup_entries = 0;
 225        int not_properly_sorted = 0;
 226        struct tree_desc desc;
 227        unsigned o_mode;
 228        const char *o_name;
 229        const unsigned char *o_sha1;
 230
 231        init_tree_desc(&desc, item->buffer, item->size);
 232
 233        o_mode = 0;
 234        o_name = NULL;
 235        o_sha1 = NULL;
 236        while (desc.size) {
 237                unsigned mode;
 238                const char *name;
 239                const unsigned char *sha1;
 240
 241                sha1 = tree_entry_extract(&desc, &name, &mode);
 242
 243                if (strchr(name, '/'))
 244                        has_full_path = 1;
 245                has_zero_pad |= *(char *)desc.buffer == '0';
 246                update_tree_entry(&desc);
 247
 248                switch (mode) {
 249                /*
 250                 * Standard modes..
 251                 */
 252                case S_IFREG | 0755:
 253                case S_IFREG | 0644:
 254                case S_IFLNK:
 255                case S_IFDIR:
 256                case S_IFDIRLNK:
 257                        break;
 258                /*
 259                 * This is nonstandard, but we had a few of these
 260                 * early on when we honored the full set of mode
 261                 * bits..
 262                 */
 263                case S_IFREG | 0664:
 264                        if (!check_strict)
 265                                break;
 266                default:
 267                        has_bad_modes = 1;
 268                }
 269
 270                if (o_name) {
 271                        switch (verify_ordered(o_mode, o_name, mode, name)) {
 272                        case TREE_UNORDERED:
 273                                not_properly_sorted = 1;
 274                                break;
 275                        case TREE_HAS_DUPS:
 276                                has_dup_entries = 1;
 277                                break;
 278                        default:
 279                                break;
 280                        }
 281                }
 282
 283                o_mode = mode;
 284                o_name = name;
 285                o_sha1 = sha1;
 286        }
 287        free(item->buffer);
 288        item->buffer = NULL;
 289
 290        retval = 0;
 291        if (has_full_path) {
 292                objwarning(&item->object, "contains full pathnames");
 293        }
 294        if (has_zero_pad) {
 295                objwarning(&item->object, "contains zero-padded file modes");
 296        }
 297        if (has_bad_modes) {
 298                objwarning(&item->object, "contains bad file modes");
 299        }
 300        if (has_dup_entries) {
 301                retval = objerror(&item->object, "contains duplicate file entries");
 302        }
 303        if (not_properly_sorted) {
 304                retval = objerror(&item->object, "not properly sorted");
 305        }
 306        return retval;
 307}
 308
 309static int fsck_commit(struct commit *commit)
 310{
 311        char *buffer = commit->buffer;
 312        unsigned char tree_sha1[20], sha1[20];
 313
 314        if (memcmp(buffer, "tree ", 5))
 315                return objerror(&commit->object, "invalid format - expected 'tree' line");
 316        if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
 317                return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
 318        buffer += 46;
 319        while (!memcmp(buffer, "parent ", 7)) {
 320                if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
 321                        return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
 322                buffer += 48;
 323        }
 324        if (memcmp(buffer, "author ", 7))
 325                return objerror(&commit->object, "invalid format - expected 'author' line");
 326        free(commit->buffer);
 327        commit->buffer = NULL;
 328        if (!commit->tree)
 329                return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
 330        if (!commit->parents && show_root)
 331                printf("root %s\n", sha1_to_hex(commit->object.sha1));
 332        if (!commit->date)
 333                printf("bad commit date in %s\n", 
 334                       sha1_to_hex(commit->object.sha1));
 335        return 0;
 336}
 337
 338static int fsck_tag(struct tag *tag)
 339{
 340        struct object *tagged = tag->tagged;
 341
 342        if (!tagged) {
 343                return objerror(&tag->object, "could not load tagged object");
 344        }
 345        if (!show_tags)
 346                return 0;
 347
 348        printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
 349        printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
 350        return 0;
 351}
 352
 353static int fsck_sha1(const unsigned char *sha1)
 354{
 355        struct object *obj = parse_object(sha1);
 356        if (!obj) {
 357                errors_found |= ERROR_OBJECT;
 358                return error("%s: object corrupt or missing",
 359                             sha1_to_hex(sha1));
 360        }
 361        if (obj->flags & SEEN)
 362                return 0;
 363        obj->flags |= SEEN;
 364        if (obj->type == OBJ_BLOB)
 365                return 0;
 366        if (obj->type == OBJ_TREE)
 367                return fsck_tree((struct tree *) obj);
 368        if (obj->type == OBJ_COMMIT)
 369                return fsck_commit((struct commit *) obj);
 370        if (obj->type == OBJ_TAG)
 371                return fsck_tag((struct tag *) obj);
 372
 373        /* By now, parse_object() would've returned NULL instead. */
 374        return objerror(obj, "unknown type '%d' (internal fsck error)",
 375                        obj->type);
 376}
 377
 378/*
 379 * This is the sorting chunk size: make it reasonably
 380 * big so that we can sort well..
 381 */
 382#define MAX_SHA1_ENTRIES (1024)
 383
 384struct sha1_entry {
 385        unsigned long ino;
 386        unsigned char sha1[20];
 387};
 388
 389static struct {
 390        unsigned long nr;
 391        struct sha1_entry *entry[MAX_SHA1_ENTRIES];
 392} sha1_list;
 393
 394static int ino_compare(const void *_a, const void *_b)
 395{
 396        const struct sha1_entry *a = _a, *b = _b;
 397        unsigned long ino1 = a->ino, ino2 = b->ino;
 398        return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
 399}
 400
 401static void fsck_sha1_list(void)
 402{
 403        int i, nr = sha1_list.nr;
 404
 405        if (SORT_DIRENT)
 406                qsort(sha1_list.entry, nr,
 407                      sizeof(struct sha1_entry *), ino_compare);
 408        for (i = 0; i < nr; i++) {
 409                struct sha1_entry *entry = sha1_list.entry[i];
 410                unsigned char *sha1 = entry->sha1;
 411
 412                sha1_list.entry[i] = NULL;
 413                fsck_sha1(sha1);
 414                free(entry);
 415        }
 416        sha1_list.nr = 0;
 417}
 418
 419static void add_sha1_list(unsigned char *sha1, unsigned long ino)
 420{
 421        struct sha1_entry *entry = xmalloc(sizeof(*entry));
 422        int nr;
 423
 424        entry->ino = ino;
 425        hashcpy(entry->sha1, sha1);
 426        nr = sha1_list.nr;
 427        if (nr == MAX_SHA1_ENTRIES) {
 428                fsck_sha1_list();
 429                nr = 0;
 430        }
 431        sha1_list.entry[nr] = entry;
 432        sha1_list.nr = ++nr;
 433}
 434
 435static void fsck_dir(int i, char *path)
 436{
 437        DIR *dir = opendir(path);
 438        struct dirent *de;
 439
 440        if (!dir)
 441                return;
 442
 443        while ((de = readdir(dir)) != NULL) {
 444                char name[100];
 445                unsigned char sha1[20];
 446                int len = strlen(de->d_name);
 447
 448                switch (len) {
 449                case 2:
 450                        if (de->d_name[1] != '.')
 451                                break;
 452                case 1:
 453                        if (de->d_name[0] != '.')
 454                                break;
 455                        continue;
 456                case 38:
 457                        sprintf(name, "%02x", i);
 458                        memcpy(name+2, de->d_name, len+1);
 459                        if (get_sha1_hex(name, sha1) < 0)
 460                                break;
 461                        add_sha1_list(sha1, DIRENT_SORT_HINT(de));
 462                        continue;
 463                }
 464                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
 465        }
 466        closedir(dir);
 467}
 468
 469static int default_refs;
 470
 471static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 472                const char *email, unsigned long timestamp, int tz,
 473                const char *message, void *cb_data)
 474{
 475        struct object *obj;
 476
 477        if (!is_null_sha1(osha1)) {
 478                obj = lookup_object(osha1);
 479                if (obj) {
 480                        obj->used = 1;
 481                        mark_reachable(obj, REACHABLE);
 482                }
 483        }
 484        obj = lookup_object(nsha1);
 485        if (obj) {
 486                obj->used = 1;
 487                mark_reachable(obj, REACHABLE);
 488        }
 489        return 0;
 490}
 491
 492static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
 493{
 494        for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
 495        return 0;
 496}
 497
 498static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 499{
 500        struct object *obj;
 501
 502        obj = lookup_object(sha1);
 503        if (!obj) {
 504                if (has_sha1_file(sha1)) {
 505                        default_refs++;
 506                        return 0; /* it is in a pack */
 507                }
 508                error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
 509                /* We'll continue with the rest despite the error.. */
 510                return 0;
 511        }
 512        default_refs++;
 513        obj->used = 1;
 514        mark_reachable(obj, REACHABLE);
 515
 516        return 0;
 517}
 518
 519static void get_default_heads(void)
 520{
 521        for_each_ref(fsck_handle_ref, NULL);
 522        if (include_reflogs)
 523                for_each_reflog(fsck_handle_reflog, NULL);
 524
 525        /*
 526         * Not having any default heads isn't really fatal, but
 527         * it does mean that "--unreachable" no longer makes any
 528         * sense (since in this case everything will obviously
 529         * be unreachable by definition.
 530         *
 531         * Showing dangling objects is valid, though (as those
 532         * dangling objects are likely lost heads).
 533         *
 534         * So we just print a warning about it, and clear the
 535         * "show_unreachable" flag.
 536         */
 537        if (!default_refs) {
 538                fprintf(stderr, "notice: No default references\n");
 539                show_unreachable = 0;
 540        }
 541}
 542
 543static void fsck_object_dir(const char *path)
 544{
 545        int i;
 546        for (i = 0; i < 256; i++) {
 547                static char dir[4096];
 548                sprintf(dir, "%s/%02x", path, i);
 549                fsck_dir(i, dir);
 550        }
 551        fsck_sha1_list();
 552}
 553
 554static int fsck_head_link(void)
 555{
 556        unsigned char sha1[20];
 557        int flag;
 558        int null_is_error = 0;
 559        const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
 560
 561        if (!head_points_at)
 562                return error("Invalid HEAD");
 563        if (!strcmp(head_points_at, "HEAD"))
 564                /* detached HEAD */
 565                null_is_error = 1;
 566        else if (prefixcmp(head_points_at, "refs/heads/"))
 567                return error("HEAD points to something strange (%s)",
 568                             head_points_at);
 569        if (is_null_sha1(sha1)) {
 570                if (null_is_error)
 571                        return error("HEAD: detached HEAD points at nothing");
 572                fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
 573                        head_points_at + 11);
 574        }
 575        return 0;
 576}
 577
 578static int fsck_cache_tree(struct cache_tree *it)
 579{
 580        int i;
 581        int err = 0;
 582
 583        if (0 <= it->entry_count) {
 584                struct object *obj = parse_object(it->sha1);
 585                if (!obj) {
 586                        error("%s: invalid sha1 pointer in cache-tree",
 587                              sha1_to_hex(it->sha1));
 588                        return 1;
 589                }
 590                mark_reachable(obj, REACHABLE);
 591                obj->used = 1;
 592                if (obj->type != OBJ_TREE)
 593                        err |= objerror(obj, "non-tree in cache-tree");
 594        }
 595        for (i = 0; i < it->subtree_nr; i++)
 596                err |= fsck_cache_tree(it->down[i]->cache_tree);
 597        return err;
 598}
 599
 600static const char fsck_usage[] =
 601"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
 602"[--strict] <head-sha1>*]";
 603
 604int cmd_fsck(int argc, char **argv, const char *prefix)
 605{
 606        int i, heads;
 607
 608        track_object_refs = 1;
 609        errors_found = 0;
 610
 611        for (i = 1; i < argc; i++) {
 612                const char *arg = argv[i];
 613
 614                if (!strcmp(arg, "--unreachable")) {
 615                        show_unreachable = 1;
 616                        continue;
 617                }
 618                if (!strcmp(arg, "--tags")) {
 619                        show_tags = 1;
 620                        continue;
 621                }
 622                if (!strcmp(arg, "--root")) {
 623                        show_root = 1;
 624                        continue;
 625                }
 626                if (!strcmp(arg, "--cache")) {
 627                        keep_cache_objects = 1;
 628                        continue;
 629                }
 630                if (!strcmp(arg, "--no-reflogs")) {
 631                        include_reflogs = 0;
 632                        continue;
 633                }
 634                if (!strcmp(arg, "--full")) {
 635                        check_full = 1;
 636                        continue;
 637                }
 638                if (!strcmp(arg, "--strict")) {
 639                        check_strict = 1;
 640                        continue;
 641                }
 642                if (*arg == '-')
 643                        usage(fsck_usage);
 644        }
 645
 646        fsck_head_link();
 647        fsck_object_dir(get_object_directory());
 648        if (check_full) {
 649                struct alternate_object_database *alt;
 650                struct packed_git *p;
 651                prepare_alt_odb();
 652                for (alt = alt_odb_list; alt; alt = alt->next) {
 653                        char namebuf[PATH_MAX];
 654                        int namelen = alt->name - alt->base;
 655                        memcpy(namebuf, alt->base, namelen);
 656                        namebuf[namelen - 1] = 0;
 657                        fsck_object_dir(namebuf);
 658                }
 659                prepare_packed_git();
 660                for (p = packed_git; p; p = p->next)
 661                        /* verify gives error messages itself */
 662                        verify_pack(p, 0);
 663
 664                for (p = packed_git; p; p = p->next) {
 665                        uint32_t i, num = p->num_objects;
 666                        for (i = 0; i < num; i++)
 667                                fsck_sha1(nth_packed_object_sha1(p, i));
 668                }
 669        }
 670
 671        heads = 0;
 672        for (i = 1; i < argc; i++) {
 673                const char *arg = argv[i]; 
 674
 675                if (*arg == '-')
 676                        continue;
 677
 678                if (!get_sha1(arg, head_sha1)) {
 679                        struct object *obj = lookup_object(head_sha1);
 680
 681                        /* Error is printed by lookup_object(). */
 682                        if (!obj)
 683                                continue;
 684
 685                        obj->used = 1;
 686                        mark_reachable(obj, REACHABLE);
 687                        heads++;
 688                        continue;
 689                }
 690                error("invalid parameter: expected sha1, got '%s'", arg);
 691        }
 692
 693        /*
 694         * If we've not been given any explicit head information, do the
 695         * default ones from .git/refs. We also consider the index file
 696         * in this case (ie this implies --cache).
 697         */
 698        if (!heads) {
 699                get_default_heads();
 700                keep_cache_objects = 1;
 701        }
 702
 703        if (keep_cache_objects) {
 704                int i;
 705                read_cache();
 706                for (i = 0; i < active_nr; i++) {
 707                        unsigned int mode;
 708                        struct blob *blob;
 709                        struct object *obj;
 710
 711                        mode = ntohl(active_cache[i]->ce_mode);
 712                        if (S_ISDIRLNK(mode))
 713                                continue;
 714                        blob = lookup_blob(active_cache[i]->sha1);
 715                        if (!blob)
 716                                continue;
 717                        obj = &blob->object;
 718                        obj->used = 1;
 719                        mark_reachable(obj, REACHABLE);
 720                }
 721                if (active_cache_tree)
 722                        fsck_cache_tree(active_cache_tree);
 723        }
 724
 725        check_connectivity();
 726        return errors_found;
 727}