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