fsck-objects.con commit Remove "tree->entries" tree-entry list from tree parser (097dc3d)
   1#include <sys/types.h>
   2#include <dirent.h>
   3
   4#include "cache.h"
   5#include "commit.h"
   6#include "tree.h"
   7#include "blob.h"
   8#include "tag.h"
   9#include "refs.h"
  10#include "pack.h"
  11
  12#define REACHABLE 0x0001
  13#define SEEN      0x0002
  14
  15static int show_root = 0;
  16static int show_tags = 0;
  17static int show_unreachable = 0;
  18static int check_full = 0;
  19static int check_strict = 0;
  20static int keep_cache_objects = 0;
  21static unsigned char head_sha1[20];
  22
  23#ifdef NO_D_INO_IN_DIRENT
  24#define SORT_DIRENT 0
  25#define DIRENT_SORT_HINT(de) 0
  26#else
  27#define SORT_DIRENT 1
  28#define DIRENT_SORT_HINT(de) ((de)->d_ino)
  29#endif
  30
  31static void objreport(struct object *obj, const char *severity,
  32                      const char *err, va_list params)
  33{
  34        fprintf(stderr, "%s in %s %s: ",
  35                severity, obj->type, sha1_to_hex(obj->sha1));
  36        vfprintf(stderr, err, params);
  37        fputs("\n", stderr);
  38}
  39
  40static int objerror(struct object *obj, const char *err, ...)
  41{
  42        va_list params;
  43        va_start(params, err);
  44        objreport(obj, "error", err, params);
  45        va_end(params);
  46        return -1;
  47}
  48
  49static int objwarning(struct object *obj, const char *err, ...)
  50{
  51        va_list params;
  52        va_start(params, err);
  53        objreport(obj, "warning", err, params);
  54        va_end(params);
  55        return -1;
  56}
  57
  58
  59static void check_connectivity(void)
  60{
  61        int i;
  62
  63        /* Look up all the requirements, warn about missing objects.. */
  64        for (i = 0; i < obj_allocs; i++) {
  65                struct object *obj = objs[i];
  66
  67                if (!obj)
  68                        continue;
  69
  70                if (!obj->parsed) {
  71                        if (has_sha1_file(obj->sha1))
  72                                ; /* it is in pack */
  73                        else
  74                                printf("missing %s %s\n",
  75                                       obj->type, sha1_to_hex(obj->sha1));
  76                        continue;
  77                }
  78
  79                if (obj->refs) {
  80                        const struct object_refs *refs = obj->refs;
  81                        unsigned j;
  82                        for (j = 0; j < refs->count; j++) {
  83                                struct object *ref = refs->ref[j];
  84                                if (ref->parsed ||
  85                                    (has_sha1_file(ref->sha1)))
  86                                        continue;
  87                                printf("broken link from %7s %s\n",
  88                                       obj->type, sha1_to_hex(obj->sha1));
  89                                printf("              to %7s %s\n",
  90                                       ref->type, sha1_to_hex(ref->sha1));
  91                        }
  92                }
  93
  94                if (show_unreachable && !(obj->flags & REACHABLE)) {
  95                        printf("unreachable %s %s\n",
  96                               obj->type, sha1_to_hex(obj->sha1));
  97                        continue;
  98                }
  99
 100                if (!obj->used) {
 101                        printf("dangling %s %s\n", obj->type, 
 102                               sha1_to_hex(obj->sha1));
 103                }
 104        }
 105}
 106
 107/*
 108 * The entries in a tree are ordered in the _path_ order,
 109 * which means that a directory entry is ordered by adding
 110 * a slash to the end of it.
 111 *
 112 * So a directory called "a" is ordered _after_ a file
 113 * called "a.c", because "a/" sorts after "a.c".
 114 */
 115#define TREE_UNORDERED (-1)
 116#define TREE_HAS_DUPS  (-2)
 117
 118static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
 119{
 120        int len1 = strlen(a->name);
 121        int len2 = strlen(b->name);
 122        int len = len1 < len2 ? len1 : len2;
 123        unsigned char c1, c2;
 124        int cmp;
 125
 126        cmp = memcmp(a->name, b->name, len);
 127        if (cmp < 0)
 128                return 0;
 129        if (cmp > 0)
 130                return TREE_UNORDERED;
 131
 132        /*
 133         * Ok, the first <len> characters are the same.
 134         * Now we need to order the next one, but turn
 135         * a '\0' into a '/' for a directory entry.
 136         */
 137        c1 = a->name[len];
 138        c2 = b->name[len];
 139        if (!c1 && !c2)
 140                /*
 141                 * git-write-tree used to write out a nonsense tree that has
 142                 * entries with the same name, one blob and one tree.  Make
 143                 * sure we do not have duplicate entries.
 144                 */
 145                return TREE_HAS_DUPS;
 146        if (!c1 && a->directory)
 147                c1 = '/';
 148        if (!c2 && b->directory)
 149                c2 = '/';
 150        return c1 < c2 ? 0 : TREE_UNORDERED;
 151}
 152
 153static int fsck_tree(struct tree *item)
 154{
 155        int retval;
 156        int has_full_path = 0;
 157        int has_zero_pad = 0;
 158        int has_bad_modes = 0;
 159        int has_dup_entries = 0;
 160        int not_properly_sorted = 0;
 161        struct tree_entry_list *entry, *last;
 162
 163        last = NULL;
 164        for (entry = create_tree_entry_list(item); entry; entry = entry->next) {
 165                if (strchr(entry->name, '/'))
 166                        has_full_path = 1;
 167                has_zero_pad |= entry->zeropad;
 168
 169                switch (entry->mode) {
 170                /*
 171                 * Standard modes.. 
 172                 */
 173                case S_IFREG | 0755:
 174                case S_IFREG | 0644:
 175                case S_IFLNK:
 176                case S_IFDIR:
 177                        break;
 178                /*
 179                 * This is nonstandard, but we had a few of these
 180                 * early on when we honored the full set of mode
 181                 * bits..
 182                 */
 183                case S_IFREG | 0664:
 184                        if (!check_strict)
 185                                break;
 186                default:
 187                        has_bad_modes = 1;
 188                }
 189
 190                if (last) {
 191                        switch (verify_ordered(last, entry)) {
 192                        case TREE_UNORDERED:
 193                                not_properly_sorted = 1;
 194                                break;
 195                        case TREE_HAS_DUPS:
 196                                has_dup_entries = 1;
 197                                break;
 198                        default:
 199                                break;
 200                        }
 201                        free(last);
 202                }
 203
 204                last = entry;
 205        }
 206        if (last)
 207                free(last);
 208        free(item->buffer);
 209        item->buffer = NULL;
 210
 211        retval = 0;
 212        if (has_full_path) {
 213                objwarning(&item->object, "contains full pathnames");
 214        }
 215        if (has_zero_pad) {
 216                objwarning(&item->object, "contains zero-padded file modes");
 217        }
 218        if (has_bad_modes) {
 219                objwarning(&item->object, "contains bad file modes");
 220        }
 221        if (has_dup_entries) {
 222                retval = objerror(&item->object, "contains duplicate file entries");
 223        }
 224        if (not_properly_sorted) {
 225                retval = objerror(&item->object, "not properly sorted");
 226        }
 227        return retval;
 228}
 229
 230static int fsck_commit(struct commit *commit)
 231{
 232        char *buffer = commit->buffer;
 233        unsigned char tree_sha1[20], sha1[20];
 234
 235        if (memcmp(buffer, "tree ", 5))
 236                return objerror(&commit->object, "invalid format - expected 'tree' line");
 237        if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
 238                return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
 239        buffer += 46;
 240        while (!memcmp(buffer, "parent ", 7)) {
 241                if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
 242                        return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
 243                buffer += 48;
 244        }
 245        if (memcmp(buffer, "author ", 7))
 246                return objerror(&commit->object, "invalid format - expected 'author' line");
 247        free(commit->buffer);
 248        commit->buffer = NULL;
 249        if (!commit->tree)
 250                return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
 251        if (!commit->parents && show_root)
 252                printf("root %s\n", sha1_to_hex(commit->object.sha1));
 253        if (!commit->date)
 254                printf("bad commit date in %s\n", 
 255                       sha1_to_hex(commit->object.sha1));
 256        return 0;
 257}
 258
 259static int fsck_tag(struct tag *tag)
 260{
 261        struct object *tagged = tag->tagged;
 262
 263        if (!tagged) {
 264                return objerror(&tag->object, "could not load tagged object");
 265        }
 266        if (!show_tags)
 267                return 0;
 268
 269        printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
 270        printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
 271        return 0;
 272}
 273
 274static int fsck_sha1(unsigned char *sha1)
 275{
 276        struct object *obj = parse_object(sha1);
 277        if (!obj)
 278                return error("%s: object not found", sha1_to_hex(sha1));
 279        if (obj->flags & SEEN)
 280                return 0;
 281        obj->flags |= SEEN;
 282        if (obj->type == blob_type)
 283                return 0;
 284        if (obj->type == tree_type)
 285                return fsck_tree((struct tree *) obj);
 286        if (obj->type == commit_type)
 287                return fsck_commit((struct commit *) obj);
 288        if (obj->type == tag_type)
 289                return fsck_tag((struct tag *) obj);
 290        /* By now, parse_object() would've returned NULL instead. */
 291        return objerror(obj, "unknown type '%s' (internal fsck error)", obj->type);
 292}
 293
 294/*
 295 * This is the sorting chunk size: make it reasonably
 296 * big so that we can sort well..
 297 */
 298#define MAX_SHA1_ENTRIES (1024)
 299
 300struct sha1_entry {
 301        unsigned long ino;
 302        unsigned char sha1[20];
 303};
 304
 305static struct {
 306        unsigned long nr;
 307        struct sha1_entry *entry[MAX_SHA1_ENTRIES];
 308} sha1_list;
 309
 310static int ino_compare(const void *_a, const void *_b)
 311{
 312        const struct sha1_entry *a = _a, *b = _b;
 313        unsigned long ino1 = a->ino, ino2 = b->ino;
 314        return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
 315}
 316
 317static void fsck_sha1_list(void)
 318{
 319        int i, nr = sha1_list.nr;
 320
 321        if (SORT_DIRENT)
 322                qsort(sha1_list.entry, nr,
 323                      sizeof(struct sha1_entry *), ino_compare);
 324        for (i = 0; i < nr; i++) {
 325                struct sha1_entry *entry = sha1_list.entry[i];
 326                unsigned char *sha1 = entry->sha1;
 327
 328                sha1_list.entry[i] = NULL;
 329                fsck_sha1(sha1);
 330                free(entry);
 331        }
 332        sha1_list.nr = 0;
 333}
 334
 335static void add_sha1_list(unsigned char *sha1, unsigned long ino)
 336{
 337        struct sha1_entry *entry = xmalloc(sizeof(*entry));
 338        int nr;
 339
 340        entry->ino = ino;
 341        memcpy(entry->sha1, sha1, 20);
 342        nr = sha1_list.nr;
 343        if (nr == MAX_SHA1_ENTRIES) {
 344                fsck_sha1_list();
 345                nr = 0;
 346        }
 347        sha1_list.entry[nr] = entry;
 348        sha1_list.nr = ++nr;
 349}
 350
 351static int fsck_dir(int i, char *path)
 352{
 353        DIR *dir = opendir(path);
 354        struct dirent *de;
 355
 356        if (!dir)
 357                return 0;
 358
 359        while ((de = readdir(dir)) != NULL) {
 360                char name[100];
 361                unsigned char sha1[20];
 362                int len = strlen(de->d_name);
 363
 364                switch (len) {
 365                case 2:
 366                        if (de->d_name[1] != '.')
 367                                break;
 368                case 1:
 369                        if (de->d_name[0] != '.')
 370                                break;
 371                        continue;
 372                case 38:
 373                        sprintf(name, "%02x", i);
 374                        memcpy(name+2, de->d_name, len+1);
 375                        if (get_sha1_hex(name, sha1) < 0)
 376                                break;
 377                        add_sha1_list(sha1, DIRENT_SORT_HINT(de));
 378                        continue;
 379                }
 380                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
 381        }
 382        closedir(dir);
 383        return 0;
 384}
 385
 386static int default_refs = 0;
 387
 388static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
 389{
 390        struct object *obj;
 391
 392        obj = lookup_object(sha1);
 393        if (!obj) {
 394                if (has_sha1_file(sha1)) {
 395                        default_refs++;
 396                        return 0; /* it is in a pack */
 397                }
 398                error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
 399                /* We'll continue with the rest despite the error.. */
 400                return 0;
 401        }
 402        default_refs++;
 403        obj->used = 1;
 404        mark_reachable(obj, REACHABLE);
 405        return 0;
 406}
 407
 408static void get_default_heads(void)
 409{
 410        for_each_ref(fsck_handle_ref);
 411        if (!default_refs)
 412                die("No default references");
 413}
 414
 415static void fsck_object_dir(const char *path)
 416{
 417        int i;
 418        for (i = 0; i < 256; i++) {
 419                static char dir[4096];
 420                sprintf(dir, "%s/%02x", path, i);
 421                fsck_dir(i, dir);
 422        }
 423        fsck_sha1_list();
 424}
 425
 426static int fsck_head_link(void)
 427{
 428        unsigned char sha1[20];
 429        const char *git_HEAD = strdup(git_path("HEAD"));
 430        const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 1);
 431        int pfxlen = strlen(git_HEAD) - 4; /* strip .../.git/ part */
 432
 433        if (!git_refs_heads_master)
 434                return error("HEAD is not a symbolic ref");
 435        if (strncmp(git_refs_heads_master + pfxlen, "refs/heads/", 11))
 436                return error("HEAD points to something strange (%s)",
 437                             git_refs_heads_master + pfxlen);
 438        if (!memcmp(null_sha1, sha1, 20))
 439                return error("HEAD: not a valid git pointer");
 440        return 0;
 441}
 442
 443int main(int argc, char **argv)
 444{
 445        int i, heads;
 446
 447        track_object_refs = 1;
 448        setup_git_directory();
 449
 450        for (i = 1; i < argc; i++) {
 451                const char *arg = argv[i];
 452
 453                if (!strcmp(arg, "--unreachable")) {
 454                        show_unreachable = 1;
 455                        continue;
 456                }
 457                if (!strcmp(arg, "--tags")) {
 458                        show_tags = 1;
 459                        continue;
 460                }
 461                if (!strcmp(arg, "--root")) {
 462                        show_root = 1;
 463                        continue;
 464                }
 465                if (!strcmp(arg, "--cache")) {
 466                        keep_cache_objects = 1;
 467                        continue;
 468                }
 469                if (!strcmp(arg, "--full")) {
 470                        check_full = 1;
 471                        continue;
 472                }
 473                if (!strcmp(arg, "--strict")) {
 474                        check_strict = 1;
 475                        continue;
 476                }
 477                if (*arg == '-')
 478                        usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
 479        }
 480
 481        fsck_head_link();
 482        fsck_object_dir(get_object_directory());
 483        if (check_full) {
 484                struct alternate_object_database *alt;
 485                struct packed_git *p;
 486                prepare_alt_odb();
 487                for (alt = alt_odb_list; alt; alt = alt->next) {
 488                        char namebuf[PATH_MAX];
 489                        int namelen = alt->name - alt->base;
 490                        memcpy(namebuf, alt->base, namelen);
 491                        namebuf[namelen - 1] = 0;
 492                        fsck_object_dir(namebuf);
 493                }
 494                prepare_packed_git();
 495                for (p = packed_git; p; p = p->next)
 496                        /* verify gives error messages itself */
 497                        verify_pack(p, 0);
 498
 499                for (p = packed_git; p; p = p->next) {
 500                        int num = num_packed_objects(p);
 501                        for (i = 0; i < num; i++) {
 502                                unsigned char sha1[20];
 503                                nth_packed_object_sha1(p, i, sha1);
 504                                fsck_sha1(sha1);
 505                        }
 506                }
 507        }
 508
 509        heads = 0;
 510        for (i = 1; i < argc; i++) {
 511                const char *arg = argv[i]; 
 512
 513                if (*arg == '-')
 514                        continue;
 515
 516                if (!get_sha1(arg, head_sha1)) {
 517                        struct object *obj = lookup_object(head_sha1);
 518
 519                        /* Error is printed by lookup_object(). */
 520                        if (!obj)
 521                                continue;
 522
 523                        obj->used = 1;
 524                        mark_reachable(obj, REACHABLE);
 525                        heads++;
 526                        continue;
 527                }
 528                error("invalid parameter: expected sha1, got '%s'", arg);
 529        }
 530
 531        /*
 532         * If we've not been given any explicit head information, do the
 533         * default ones from .git/refs. We also consider the index file
 534         * in this case (ie this implies --cache).
 535         */
 536        if (!heads) {
 537                get_default_heads();
 538                keep_cache_objects = 1;
 539        }
 540
 541        if (keep_cache_objects) {
 542                int i;
 543                read_cache();
 544                for (i = 0; i < active_nr; i++) {
 545                        struct blob *blob = lookup_blob(active_cache[i]->sha1);
 546                        struct object *obj;
 547                        if (!blob)
 548                                continue;
 549                        obj = &blob->object;
 550                        obj->used = 1;
 551                        mark_reachable(obj, REACHABLE);
 552                }
 553        }
 554
 555        check_connectivity();
 556        return 0;
 557}