fsck.con commit fsck: support comments & empty lines in skipList (371a655)
   1#include "cache.h"
   2#include "object-store.h"
   3#include "repository.h"
   4#include "object.h"
   5#include "blob.h"
   6#include "tree.h"
   7#include "tree-walk.h"
   8#include "commit.h"
   9#include "tag.h"
  10#include "fsck.h"
  11#include "refs.h"
  12#include "utf8.h"
  13#include "decorate.h"
  14#include "oidset.h"
  15#include "packfile.h"
  16#include "submodule-config.h"
  17#include "config.h"
  18#include "help.h"
  19
  20static struct oidset gitmodules_found = OIDSET_INIT;
  21static struct oidset gitmodules_done = OIDSET_INIT;
  22
  23#define FSCK_FATAL -1
  24#define FSCK_INFO -2
  25
  26#define FOREACH_MSG_ID(FUNC) \
  27        /* fatal errors */ \
  28        FUNC(NUL_IN_HEADER, FATAL) \
  29        FUNC(UNTERMINATED_HEADER, FATAL) \
  30        /* errors */ \
  31        FUNC(BAD_DATE, ERROR) \
  32        FUNC(BAD_DATE_OVERFLOW, ERROR) \
  33        FUNC(BAD_EMAIL, ERROR) \
  34        FUNC(BAD_NAME, ERROR) \
  35        FUNC(BAD_OBJECT_SHA1, ERROR) \
  36        FUNC(BAD_PARENT_SHA1, ERROR) \
  37        FUNC(BAD_TAG_OBJECT, ERROR) \
  38        FUNC(BAD_TIMEZONE, ERROR) \
  39        FUNC(BAD_TREE, ERROR) \
  40        FUNC(BAD_TREE_SHA1, ERROR) \
  41        FUNC(BAD_TYPE, ERROR) \
  42        FUNC(DUPLICATE_ENTRIES, ERROR) \
  43        FUNC(MISSING_AUTHOR, ERROR) \
  44        FUNC(MISSING_COMMITTER, ERROR) \
  45        FUNC(MISSING_EMAIL, ERROR) \
  46        FUNC(MISSING_GRAFT, ERROR) \
  47        FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
  48        FUNC(MISSING_OBJECT, ERROR) \
  49        FUNC(MISSING_PARENT, ERROR) \
  50        FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
  51        FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
  52        FUNC(MISSING_TAG, ERROR) \
  53        FUNC(MISSING_TAG_ENTRY, ERROR) \
  54        FUNC(MISSING_TAG_OBJECT, ERROR) \
  55        FUNC(MISSING_TREE, ERROR) \
  56        FUNC(MISSING_TREE_OBJECT, ERROR) \
  57        FUNC(MISSING_TYPE, ERROR) \
  58        FUNC(MISSING_TYPE_ENTRY, ERROR) \
  59        FUNC(MULTIPLE_AUTHORS, ERROR) \
  60        FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
  61        FUNC(TREE_NOT_SORTED, ERROR) \
  62        FUNC(UNKNOWN_TYPE, ERROR) \
  63        FUNC(ZERO_PADDED_DATE, ERROR) \
  64        FUNC(GITMODULES_MISSING, ERROR) \
  65        FUNC(GITMODULES_BLOB, ERROR) \
  66        FUNC(GITMODULES_LARGE, ERROR) \
  67        FUNC(GITMODULES_NAME, ERROR) \
  68        FUNC(GITMODULES_SYMLINK, ERROR) \
  69        /* warnings */ \
  70        FUNC(BAD_FILEMODE, WARN) \
  71        FUNC(EMPTY_NAME, WARN) \
  72        FUNC(FULL_PATHNAME, WARN) \
  73        FUNC(HAS_DOT, WARN) \
  74        FUNC(HAS_DOTDOT, WARN) \
  75        FUNC(HAS_DOTGIT, WARN) \
  76        FUNC(NULL_SHA1, WARN) \
  77        FUNC(ZERO_PADDED_FILEMODE, WARN) \
  78        FUNC(NUL_IN_COMMIT, WARN) \
  79        /* infos (reported as warnings, but ignored by default) */ \
  80        FUNC(GITMODULES_PARSE, INFO) \
  81        FUNC(BAD_TAG_NAME, INFO) \
  82        FUNC(MISSING_TAGGER_ENTRY, INFO)
  83
  84#define MSG_ID(id, msg_type) FSCK_MSG_##id,
  85enum fsck_msg_id {
  86        FOREACH_MSG_ID(MSG_ID)
  87        FSCK_MSG_MAX
  88};
  89#undef MSG_ID
  90
  91#define STR(x) #x
  92#define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
  93static struct {
  94        const char *id_string;
  95        const char *downcased;
  96        const char *camelcased;
  97        int msg_type;
  98} msg_id_info[FSCK_MSG_MAX + 1] = {
  99        FOREACH_MSG_ID(MSG_ID)
 100        { NULL, NULL, NULL, -1 }
 101};
 102#undef MSG_ID
 103
 104static void prepare_msg_ids(void)
 105{
 106        int i;
 107
 108        if (msg_id_info[0].downcased)
 109                return;
 110
 111        /* convert id_string to lower case, without underscores. */
 112        for (i = 0; i < FSCK_MSG_MAX; i++) {
 113                const char *p = msg_id_info[i].id_string;
 114                int len = strlen(p);
 115                char *q = xmalloc(len);
 116
 117                msg_id_info[i].downcased = q;
 118                while (*p)
 119                        if (*p == '_')
 120                                p++;
 121                        else
 122                                *(q)++ = tolower(*(p)++);
 123                *q = '\0';
 124
 125                p = msg_id_info[i].id_string;
 126                q = xmalloc(len);
 127                msg_id_info[i].camelcased = q;
 128                while (*p) {
 129                        if (*p == '_') {
 130                                p++;
 131                                if (*p)
 132                                        *q++ = *p++;
 133                        } else {
 134                                *q++ = tolower(*p++);
 135                        }
 136                }
 137                *q = '\0';
 138        }
 139}
 140
 141static int parse_msg_id(const char *text)
 142{
 143        int i;
 144
 145        prepare_msg_ids();
 146
 147        for (i = 0; i < FSCK_MSG_MAX; i++)
 148                if (!strcmp(text, msg_id_info[i].downcased))
 149                        return i;
 150
 151        return -1;
 152}
 153
 154void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
 155{
 156        int i;
 157
 158        prepare_msg_ids();
 159
 160        for (i = 0; i < FSCK_MSG_MAX; i++)
 161                list_config_item(list, prefix, msg_id_info[i].camelcased);
 162}
 163
 164static int fsck_msg_type(enum fsck_msg_id msg_id,
 165        struct fsck_options *options)
 166{
 167        int msg_type;
 168
 169        assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
 170
 171        if (options->msg_type)
 172                msg_type = options->msg_type[msg_id];
 173        else {
 174                msg_type = msg_id_info[msg_id].msg_type;
 175                if (options->strict && msg_type == FSCK_WARN)
 176                        msg_type = FSCK_ERROR;
 177        }
 178
 179        return msg_type;
 180}
 181
 182static void init_skiplist(struct fsck_options *options, const char *path)
 183{
 184        FILE *fp;
 185        struct strbuf sb = STRBUF_INIT;
 186        struct object_id oid;
 187
 188        fp = fopen(path, "r");
 189        if (!fp)
 190                die("Could not open skip list: %s", path);
 191        while (!strbuf_getline(&sb, fp)) {
 192                const char *p;
 193                const char *hash;
 194
 195                /*
 196                 * Allow trailing comments, leading whitespace
 197                 * (including before commits), and empty or whitespace
 198                 * only lines.
 199                 */
 200                hash = strchr(sb.buf, '#');
 201                if (hash)
 202                        strbuf_setlen(&sb, hash - sb.buf);
 203                strbuf_trim(&sb);
 204                if (!sb.len)
 205                        continue;
 206
 207                if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0')
 208                        die("Invalid SHA-1: %s", sb.buf);
 209                oidset_insert(&options->skiplist, &oid);
 210        }
 211        if (ferror(fp))
 212                die_errno("Could not read '%s'", path);
 213        fclose(fp);
 214        strbuf_release(&sb);
 215}
 216
 217static int parse_msg_type(const char *str)
 218{
 219        if (!strcmp(str, "error"))
 220                return FSCK_ERROR;
 221        else if (!strcmp(str, "warn"))
 222                return FSCK_WARN;
 223        else if (!strcmp(str, "ignore"))
 224                return FSCK_IGNORE;
 225        else
 226                die("Unknown fsck message type: '%s'", str);
 227}
 228
 229int is_valid_msg_type(const char *msg_id, const char *msg_type)
 230{
 231        if (parse_msg_id(msg_id) < 0)
 232                return 0;
 233        parse_msg_type(msg_type);
 234        return 1;
 235}
 236
 237void fsck_set_msg_type(struct fsck_options *options,
 238                const char *msg_id, const char *msg_type)
 239{
 240        int id = parse_msg_id(msg_id), type;
 241
 242        if (id < 0)
 243                die("Unhandled message id: %s", msg_id);
 244        type = parse_msg_type(msg_type);
 245
 246        if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
 247                die("Cannot demote %s to %s", msg_id, msg_type);
 248
 249        if (!options->msg_type) {
 250                int i;
 251                int *msg_type;
 252                ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
 253                for (i = 0; i < FSCK_MSG_MAX; i++)
 254                        msg_type[i] = fsck_msg_type(i, options);
 255                options->msg_type = msg_type;
 256        }
 257
 258        options->msg_type[id] = type;
 259}
 260
 261void fsck_set_msg_types(struct fsck_options *options, const char *values)
 262{
 263        char *buf = xstrdup(values), *to_free = buf;
 264        int done = 0;
 265
 266        while (!done) {
 267                int len = strcspn(buf, " ,|"), equal;
 268
 269                done = !buf[len];
 270                if (!len) {
 271                        buf++;
 272                        continue;
 273                }
 274                buf[len] = '\0';
 275
 276                for (equal = 0;
 277                     equal < len && buf[equal] != '=' && buf[equal] != ':';
 278                     equal++)
 279                        buf[equal] = tolower(buf[equal]);
 280                buf[equal] = '\0';
 281
 282                if (!strcmp(buf, "skiplist")) {
 283                        if (equal == len)
 284                                die("skiplist requires a path");
 285                        init_skiplist(options, buf + equal + 1);
 286                        buf += len + 1;
 287                        continue;
 288                }
 289
 290                if (equal == len)
 291                        die("Missing '=': '%s'", buf);
 292
 293                fsck_set_msg_type(options, buf, buf + equal + 1);
 294                buf += len + 1;
 295        }
 296        free(to_free);
 297}
 298
 299static void append_msg_id(struct strbuf *sb, const char *msg_id)
 300{
 301        for (;;) {
 302                char c = *(msg_id)++;
 303
 304                if (!c)
 305                        break;
 306                if (c != '_')
 307                        strbuf_addch(sb, tolower(c));
 308                else {
 309                        assert(*msg_id);
 310                        strbuf_addch(sb, *(msg_id)++);
 311                }
 312        }
 313
 314        strbuf_addstr(sb, ": ");
 315}
 316
 317static int object_on_skiplist(struct fsck_options *opts, struct object *obj)
 318{
 319        return opts && obj && oidset_contains(&opts->skiplist, &obj->oid);
 320}
 321
 322__attribute__((format (printf, 4, 5)))
 323static int report(struct fsck_options *options, struct object *object,
 324        enum fsck_msg_id id, const char *fmt, ...)
 325{
 326        va_list ap;
 327        struct strbuf sb = STRBUF_INIT;
 328        int msg_type = fsck_msg_type(id, options), result;
 329
 330        if (msg_type == FSCK_IGNORE)
 331                return 0;
 332
 333        if (object_on_skiplist(options, object))
 334                return 0;
 335
 336        if (msg_type == FSCK_FATAL)
 337                msg_type = FSCK_ERROR;
 338        else if (msg_type == FSCK_INFO)
 339                msg_type = FSCK_WARN;
 340
 341        append_msg_id(&sb, msg_id_info[id].id_string);
 342
 343        va_start(ap, fmt);
 344        strbuf_vaddf(&sb, fmt, ap);
 345        result = options->error_func(options, object, msg_type, sb.buf);
 346        strbuf_release(&sb);
 347        va_end(ap);
 348
 349        return result;
 350}
 351
 352static char *get_object_name(struct fsck_options *options, struct object *obj)
 353{
 354        if (!options->object_names)
 355                return NULL;
 356        return lookup_decoration(options->object_names, obj);
 357}
 358
 359static void put_object_name(struct fsck_options *options, struct object *obj,
 360        const char *fmt, ...)
 361{
 362        va_list ap;
 363        struct strbuf buf = STRBUF_INIT;
 364        char *existing;
 365
 366        if (!options->object_names)
 367                return;
 368        existing = lookup_decoration(options->object_names, obj);
 369        if (existing)
 370                return;
 371        va_start(ap, fmt);
 372        strbuf_vaddf(&buf, fmt, ap);
 373        add_decoration(options->object_names, obj, strbuf_detach(&buf, NULL));
 374        va_end(ap);
 375}
 376
 377static const char *describe_object(struct fsck_options *o, struct object *obj)
 378{
 379        static struct strbuf buf = STRBUF_INIT;
 380        char *name;
 381
 382        strbuf_reset(&buf);
 383        strbuf_addstr(&buf, oid_to_hex(&obj->oid));
 384        if (o->object_names && (name = lookup_decoration(o->object_names, obj)))
 385                strbuf_addf(&buf, " (%s)", name);
 386
 387        return buf.buf;
 388}
 389
 390static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
 391{
 392        struct tree_desc desc;
 393        struct name_entry entry;
 394        int res = 0;
 395        const char *name;
 396
 397        if (parse_tree(tree))
 398                return -1;
 399
 400        name = get_object_name(options, &tree->object);
 401        if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
 402                return -1;
 403        while (tree_entry_gently(&desc, &entry)) {
 404                struct object *obj;
 405                int result;
 406
 407                if (S_ISGITLINK(entry.mode))
 408                        continue;
 409
 410                if (S_ISDIR(entry.mode)) {
 411                        obj = (struct object *)lookup_tree(the_repository, entry.oid);
 412                        if (name && obj)
 413                                put_object_name(options, obj, "%s%s/", name,
 414                                        entry.path);
 415                        result = options->walk(obj, OBJ_TREE, data, options);
 416                }
 417                else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
 418                        obj = (struct object *)lookup_blob(the_repository, entry.oid);
 419                        if (name && obj)
 420                                put_object_name(options, obj, "%s%s", name,
 421                                        entry.path);
 422                        result = options->walk(obj, OBJ_BLOB, data, options);
 423                }
 424                else {
 425                        result = error("in tree %s: entry %s has bad mode %.6o",
 426                                        describe_object(options, &tree->object), entry.path, entry.mode);
 427                }
 428                if (result < 0)
 429                        return result;
 430                if (!res)
 431                        res = result;
 432        }
 433        return res;
 434}
 435
 436static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
 437{
 438        int counter = 0, generation = 0, name_prefix_len = 0;
 439        struct commit_list *parents;
 440        int res;
 441        int result;
 442        const char *name;
 443
 444        if (parse_commit(commit))
 445                return -1;
 446
 447        name = get_object_name(options, &commit->object);
 448        if (name)
 449                put_object_name(options, &get_commit_tree(commit)->object,
 450                                "%s:", name);
 451
 452        result = options->walk((struct object *)get_commit_tree(commit),
 453                               OBJ_TREE, data, options);
 454        if (result < 0)
 455                return result;
 456        res = result;
 457
 458        parents = commit->parents;
 459        if (name && parents) {
 460                int len = strlen(name), power;
 461
 462                if (len && name[len - 1] == '^') {
 463                        generation = 1;
 464                        name_prefix_len = len - 1;
 465                }
 466                else { /* parse ~<generation> suffix */
 467                        for (generation = 0, power = 1;
 468                             len && isdigit(name[len - 1]);
 469                             power *= 10)
 470                                generation += power * (name[--len] - '0');
 471                        if (power > 1 && len && name[len - 1] == '~')
 472                                name_prefix_len = len - 1;
 473                }
 474        }
 475
 476        while (parents) {
 477                if (name) {
 478                        struct object *obj = &parents->item->object;
 479
 480                        if (++counter > 1)
 481                                put_object_name(options, obj, "%s^%d",
 482                                        name, counter);
 483                        else if (generation > 0)
 484                                put_object_name(options, obj, "%.*s~%d",
 485                                        name_prefix_len, name, generation + 1);
 486                        else
 487                                put_object_name(options, obj, "%s^", name);
 488                }
 489                result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
 490                if (result < 0)
 491                        return result;
 492                if (!res)
 493                        res = result;
 494                parents = parents->next;
 495        }
 496        return res;
 497}
 498
 499static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
 500{
 501        char *name = get_object_name(options, &tag->object);
 502
 503        if (parse_tag(tag))
 504                return -1;
 505        if (name)
 506                put_object_name(options, tag->tagged, "%s", name);
 507        return options->walk(tag->tagged, OBJ_ANY, data, options);
 508}
 509
 510int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
 511{
 512        if (!obj)
 513                return -1;
 514
 515        if (obj->type == OBJ_NONE)
 516                parse_object(the_repository, &obj->oid);
 517
 518        switch (obj->type) {
 519        case OBJ_BLOB:
 520                return 0;
 521        case OBJ_TREE:
 522                return fsck_walk_tree((struct tree *)obj, data, options);
 523        case OBJ_COMMIT:
 524                return fsck_walk_commit((struct commit *)obj, data, options);
 525        case OBJ_TAG:
 526                return fsck_walk_tag((struct tag *)obj, data, options);
 527        default:
 528                error("Unknown object type for %s", describe_object(options, obj));
 529                return -1;
 530        }
 531}
 532
 533/*
 534 * The entries in a tree are ordered in the _path_ order,
 535 * which means that a directory entry is ordered by adding
 536 * a slash to the end of it.
 537 *
 538 * So a directory called "a" is ordered _after_ a file
 539 * called "a.c", because "a/" sorts after "a.c".
 540 */
 541#define TREE_UNORDERED (-1)
 542#define TREE_HAS_DUPS  (-2)
 543
 544static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
 545{
 546        int len1 = strlen(name1);
 547        int len2 = strlen(name2);
 548        int len = len1 < len2 ? len1 : len2;
 549        unsigned char c1, c2;
 550        int cmp;
 551
 552        cmp = memcmp(name1, name2, len);
 553        if (cmp < 0)
 554                return 0;
 555        if (cmp > 0)
 556                return TREE_UNORDERED;
 557
 558        /*
 559         * Ok, the first <len> characters are the same.
 560         * Now we need to order the next one, but turn
 561         * a '\0' into a '/' for a directory entry.
 562         */
 563        c1 = name1[len];
 564        c2 = name2[len];
 565        if (!c1 && !c2)
 566                /*
 567                 * git-write-tree used to write out a nonsense tree that has
 568                 * entries with the same name, one blob and one tree.  Make
 569                 * sure we do not have duplicate entries.
 570                 */
 571                return TREE_HAS_DUPS;
 572        if (!c1 && S_ISDIR(mode1))
 573                c1 = '/';
 574        if (!c2 && S_ISDIR(mode2))
 575                c2 = '/';
 576        return c1 < c2 ? 0 : TREE_UNORDERED;
 577}
 578
 579static int fsck_tree(struct tree *item, struct fsck_options *options)
 580{
 581        int retval = 0;
 582        int has_null_sha1 = 0;
 583        int has_full_path = 0;
 584        int has_empty_name = 0;
 585        int has_dot = 0;
 586        int has_dotdot = 0;
 587        int has_dotgit = 0;
 588        int has_zero_pad = 0;
 589        int has_bad_modes = 0;
 590        int has_dup_entries = 0;
 591        int not_properly_sorted = 0;
 592        struct tree_desc desc;
 593        unsigned o_mode;
 594        const char *o_name;
 595
 596        if (init_tree_desc_gently(&desc, item->buffer, item->size)) {
 597                retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
 598                return retval;
 599        }
 600
 601        o_mode = 0;
 602        o_name = NULL;
 603
 604        while (desc.size) {
 605                unsigned mode;
 606                const char *name;
 607                const struct object_id *oid;
 608
 609                oid = tree_entry_extract(&desc, &name, &mode);
 610
 611                has_null_sha1 |= is_null_oid(oid);
 612                has_full_path |= !!strchr(name, '/');
 613                has_empty_name |= !*name;
 614                has_dot |= !strcmp(name, ".");
 615                has_dotdot |= !strcmp(name, "..");
 616                has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
 617                has_zero_pad |= *(char *)desc.buffer == '0';
 618
 619                if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
 620                        if (!S_ISLNK(mode))
 621                                oidset_insert(&gitmodules_found, oid);
 622                        else
 623                                retval += report(options, &item->object,
 624                                                 FSCK_MSG_GITMODULES_SYMLINK,
 625                                                 ".gitmodules is a symbolic link");
 626                }
 627
 628                if (update_tree_entry_gently(&desc)) {
 629                        retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
 630                        break;
 631                }
 632
 633                switch (mode) {
 634                /*
 635                 * Standard modes..
 636                 */
 637                case S_IFREG | 0755:
 638                case S_IFREG | 0644:
 639                case S_IFLNK:
 640                case S_IFDIR:
 641                case S_IFGITLINK:
 642                        break;
 643                /*
 644                 * This is nonstandard, but we had a few of these
 645                 * early on when we honored the full set of mode
 646                 * bits..
 647                 */
 648                case S_IFREG | 0664:
 649                        if (!options->strict)
 650                                break;
 651                        /* fallthrough */
 652                default:
 653                        has_bad_modes = 1;
 654                }
 655
 656                if (o_name) {
 657                        switch (verify_ordered(o_mode, o_name, mode, name)) {
 658                        case TREE_UNORDERED:
 659                                not_properly_sorted = 1;
 660                                break;
 661                        case TREE_HAS_DUPS:
 662                                has_dup_entries = 1;
 663                                break;
 664                        default:
 665                                break;
 666                        }
 667                }
 668
 669                o_mode = mode;
 670                o_name = name;
 671        }
 672
 673        if (has_null_sha1)
 674                retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
 675        if (has_full_path)
 676                retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
 677        if (has_empty_name)
 678                retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
 679        if (has_dot)
 680                retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'");
 681        if (has_dotdot)
 682                retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'");
 683        if (has_dotgit)
 684                retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
 685        if (has_zero_pad)
 686                retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
 687        if (has_bad_modes)
 688                retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
 689        if (has_dup_entries)
 690                retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
 691        if (not_properly_sorted)
 692                retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
 693        return retval;
 694}
 695
 696static int verify_headers(const void *data, unsigned long size,
 697                          struct object *obj, struct fsck_options *options)
 698{
 699        const char *buffer = (const char *)data;
 700        unsigned long i;
 701
 702        for (i = 0; i < size; i++) {
 703                switch (buffer[i]) {
 704                case '\0':
 705                        return report(options, obj,
 706                                FSCK_MSG_NUL_IN_HEADER,
 707                                "unterminated header: NUL at offset %ld", i);
 708                case '\n':
 709                        if (i + 1 < size && buffer[i + 1] == '\n')
 710                                return 0;
 711                }
 712        }
 713
 714        /*
 715         * We did not find double-LF that separates the header
 716         * and the body.  Not having a body is not a crime but
 717         * we do want to see the terminating LF for the last header
 718         * line.
 719         */
 720        if (size && buffer[size - 1] == '\n')
 721                return 0;
 722
 723        return report(options, obj,
 724                FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
 725}
 726
 727static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options)
 728{
 729        const char *p = *ident;
 730        char *end;
 731
 732        *ident = strchrnul(*ident, '\n');
 733        if (**ident == '\n')
 734                (*ident)++;
 735
 736        if (*p == '<')
 737                return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
 738        p += strcspn(p, "<>\n");
 739        if (*p == '>')
 740                return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
 741        if (*p != '<')
 742                return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
 743        if (p[-1] != ' ')
 744                return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
 745        p++;
 746        p += strcspn(p, "<>\n");
 747        if (*p != '>')
 748                return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
 749        p++;
 750        if (*p != ' ')
 751                return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
 752        p++;
 753        if (*p == '0' && p[1] != ' ')
 754                return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
 755        if (date_overflows(parse_timestamp(p, &end, 10)))
 756                return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
 757        if ((end == p || *end != ' '))
 758                return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
 759        p = end + 1;
 760        if ((*p != '+' && *p != '-') ||
 761            !isdigit(p[1]) ||
 762            !isdigit(p[2]) ||
 763            !isdigit(p[3]) ||
 764            !isdigit(p[4]) ||
 765            (p[5] != '\n'))
 766                return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
 767        p += 6;
 768        return 0;
 769}
 770
 771static int fsck_commit_buffer(struct commit *commit, const char *buffer,
 772        unsigned long size, struct fsck_options *options)
 773{
 774        struct object_id tree_oid, oid;
 775        struct commit_graft *graft;
 776        unsigned parent_count, parent_line_count = 0, author_count;
 777        int err;
 778        const char *buffer_begin = buffer;
 779        const char *p;
 780
 781        if (verify_headers(buffer, size, &commit->object, options))
 782                return -1;
 783
 784        if (!skip_prefix(buffer, "tree ", &buffer))
 785                return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
 786        if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
 787                err = report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
 788                if (err)
 789                        return err;
 790        }
 791        buffer = p + 1;
 792        while (skip_prefix(buffer, "parent ", &buffer)) {
 793                if (parse_oid_hex(buffer, &oid, &p) || *p != '\n') {
 794                        err = report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
 795                        if (err)
 796                                return err;
 797                }
 798                buffer = p + 1;
 799                parent_line_count++;
 800        }
 801        graft = lookup_commit_graft(the_repository, &commit->object.oid);
 802        parent_count = commit_list_count(commit->parents);
 803        if (graft) {
 804                if (graft->nr_parent == -1 && !parent_count)
 805                        ; /* shallow commit */
 806                else if (graft->nr_parent != parent_count) {
 807                        err = report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing");
 808                        if (err)
 809                                return err;
 810                }
 811        } else {
 812                if (parent_count != parent_line_count) {
 813                        err = report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing");
 814                        if (err)
 815                                return err;
 816                }
 817        }
 818        author_count = 0;
 819        while (skip_prefix(buffer, "author ", &buffer)) {
 820                author_count++;
 821                err = fsck_ident(&buffer, &commit->object, options);
 822                if (err)
 823                        return err;
 824        }
 825        if (author_count < 1)
 826                err = report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
 827        else if (author_count > 1)
 828                err = report(options, &commit->object, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
 829        if (err)
 830                return err;
 831        if (!skip_prefix(buffer, "committer ", &buffer))
 832                return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
 833        err = fsck_ident(&buffer, &commit->object, options);
 834        if (err)
 835                return err;
 836        if (!get_commit_tree(commit)) {
 837                err = report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", oid_to_hex(&tree_oid));
 838                if (err)
 839                        return err;
 840        }
 841        if (memchr(buffer_begin, '\0', size)) {
 842                err = report(options, &commit->object, FSCK_MSG_NUL_IN_COMMIT,
 843                             "NUL byte in the commit object body");
 844                if (err)
 845                        return err;
 846        }
 847        return 0;
 848}
 849
 850static int fsck_commit(struct commit *commit, const char *data,
 851        unsigned long size, struct fsck_options *options)
 852{
 853        const char *buffer = data ?  data : get_commit_buffer(commit, &size);
 854        int ret = fsck_commit_buffer(commit, buffer, size, options);
 855        if (!data)
 856                unuse_commit_buffer(commit, buffer);
 857        return ret;
 858}
 859
 860static int fsck_tag_buffer(struct tag *tag, const char *data,
 861        unsigned long size, struct fsck_options *options)
 862{
 863        struct object_id oid;
 864        int ret = 0;
 865        const char *buffer;
 866        char *to_free = NULL, *eol;
 867        struct strbuf sb = STRBUF_INIT;
 868        const char *p;
 869
 870        if (data)
 871                buffer = data;
 872        else {
 873                enum object_type type;
 874
 875                buffer = to_free =
 876                        read_object_file(&tag->object.oid, &type, &size);
 877                if (!buffer)
 878                        return report(options, &tag->object,
 879                                FSCK_MSG_MISSING_TAG_OBJECT,
 880                                "cannot read tag object");
 881
 882                if (type != OBJ_TAG) {
 883                        ret = report(options, &tag->object,
 884                                FSCK_MSG_TAG_OBJECT_NOT_TAG,
 885                                "expected tag got %s",
 886                            type_name(type));
 887                        goto done;
 888                }
 889        }
 890
 891        ret = verify_headers(buffer, size, &tag->object, options);
 892        if (ret)
 893                goto done;
 894
 895        if (!skip_prefix(buffer, "object ", &buffer)) {
 896                ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
 897                goto done;
 898        }
 899        if (parse_oid_hex(buffer, &oid, &p) || *p != '\n') {
 900                ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
 901                if (ret)
 902                        goto done;
 903        }
 904        buffer = p + 1;
 905
 906        if (!skip_prefix(buffer, "type ", &buffer)) {
 907                ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
 908                goto done;
 909        }
 910        eol = strchr(buffer, '\n');
 911        if (!eol) {
 912                ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
 913                goto done;
 914        }
 915        if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
 916                ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
 917        if (ret)
 918                goto done;
 919        buffer = eol + 1;
 920
 921        if (!skip_prefix(buffer, "tag ", &buffer)) {
 922                ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
 923                goto done;
 924        }
 925        eol = strchr(buffer, '\n');
 926        if (!eol) {
 927                ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
 928                goto done;
 929        }
 930        strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
 931        if (check_refname_format(sb.buf, 0)) {
 932                ret = report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME,
 933                           "invalid 'tag' name: %.*s",
 934                           (int)(eol - buffer), buffer);
 935                if (ret)
 936                        goto done;
 937        }
 938        buffer = eol + 1;
 939
 940        if (!skip_prefix(buffer, "tagger ", &buffer)) {
 941                /* early tags do not contain 'tagger' lines; warn only */
 942                ret = report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
 943                if (ret)
 944                        goto done;
 945        }
 946        else
 947                ret = fsck_ident(&buffer, &tag->object, options);
 948
 949done:
 950        strbuf_release(&sb);
 951        free(to_free);
 952        return ret;
 953}
 954
 955static int fsck_tag(struct tag *tag, const char *data,
 956        unsigned long size, struct fsck_options *options)
 957{
 958        struct object *tagged = tag->tagged;
 959
 960        if (!tagged)
 961                return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object");
 962
 963        return fsck_tag_buffer(tag, data, size, options);
 964}
 965
 966struct fsck_gitmodules_data {
 967        struct object *obj;
 968        struct fsck_options *options;
 969        int ret;
 970};
 971
 972static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
 973{
 974        struct fsck_gitmodules_data *data = vdata;
 975        const char *subsection, *key;
 976        int subsection_len;
 977        char *name;
 978
 979        if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
 980            !subsection)
 981                return 0;
 982
 983        name = xmemdupz(subsection, subsection_len);
 984        if (check_submodule_name(name) < 0)
 985                data->ret |= report(data->options, data->obj,
 986                                    FSCK_MSG_GITMODULES_NAME,
 987                                    "disallowed submodule name: %s",
 988                                    name);
 989        free(name);
 990
 991        return 0;
 992}
 993
 994static int fsck_blob(struct blob *blob, const char *buf,
 995                     unsigned long size, struct fsck_options *options)
 996{
 997        struct fsck_gitmodules_data data;
 998        struct config_options config_opts = { 0 };
 999
1000        if (!oidset_contains(&gitmodules_found, &blob->object.oid))
1001                return 0;
1002        oidset_insert(&gitmodules_done, &blob->object.oid);
1003
1004        if (object_on_skiplist(options, &blob->object))
1005                return 0;
1006
1007        if (!buf) {
1008                /*
1009                 * A missing buffer here is a sign that the caller found the
1010                 * blob too gigantic to load into memory. Let's just consider
1011                 * that an error.
1012                 */
1013                return report(options, &blob->object,
1014                              FSCK_MSG_GITMODULES_LARGE,
1015                              ".gitmodules too large to parse");
1016        }
1017
1018        data.obj = &blob->object;
1019        data.options = options;
1020        data.ret = 0;
1021        config_opts.error_action = CONFIG_ERROR_SILENT;
1022        if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
1023                                ".gitmodules", buf, size, &data, &config_opts))
1024                data.ret |= report(options, &blob->object,
1025                                   FSCK_MSG_GITMODULES_PARSE,
1026                                   "could not parse gitmodules blob");
1027
1028        return data.ret;
1029}
1030
1031int fsck_object(struct object *obj, void *data, unsigned long size,
1032        struct fsck_options *options)
1033{
1034        if (!obj)
1035                return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
1036
1037        if (obj->type == OBJ_BLOB)
1038                return fsck_blob((struct blob *)obj, data, size, options);
1039        if (obj->type == OBJ_TREE)
1040                return fsck_tree((struct tree *) obj, options);
1041        if (obj->type == OBJ_COMMIT)
1042                return fsck_commit((struct commit *) obj, (const char *) data,
1043                        size, options);
1044        if (obj->type == OBJ_TAG)
1045                return fsck_tag((struct tag *) obj, (const char *) data,
1046                        size, options);
1047
1048        return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)",
1049                          obj->type);
1050}
1051
1052int fsck_error_function(struct fsck_options *o,
1053        struct object *obj, int msg_type, const char *message)
1054{
1055        if (msg_type == FSCK_WARN) {
1056                warning("object %s: %s", describe_object(o, obj), message);
1057                return 0;
1058        }
1059        error("object %s: %s", describe_object(o, obj), message);
1060        return 1;
1061}
1062
1063int fsck_finish(struct fsck_options *options)
1064{
1065        int ret = 0;
1066        struct oidset_iter iter;
1067        const struct object_id *oid;
1068
1069        oidset_iter_init(&gitmodules_found, &iter);
1070        while ((oid = oidset_iter_next(&iter))) {
1071                struct blob *blob;
1072                enum object_type type;
1073                unsigned long size;
1074                char *buf;
1075
1076                if (oidset_contains(&gitmodules_done, oid))
1077                        continue;
1078
1079                blob = lookup_blob(the_repository, oid);
1080                if (!blob) {
1081                        struct object *obj = lookup_unknown_object(oid->hash);
1082                        ret |= report(options, obj,
1083                                      FSCK_MSG_GITMODULES_BLOB,
1084                                      "non-blob found at .gitmodules");
1085                        continue;
1086                }
1087
1088                buf = read_object_file(oid, &type, &size);
1089                if (!buf) {
1090                        if (is_promisor_object(&blob->object.oid))
1091                                continue;
1092                        ret |= report(options, &blob->object,
1093                                      FSCK_MSG_GITMODULES_MISSING,
1094                                      "unable to read .gitmodules blob");
1095                        continue;
1096                }
1097
1098                if (type == OBJ_BLOB)
1099                        ret |= fsck_blob(blob, buf, size, options);
1100                else
1101                        ret |= report(options, &blob->object,
1102                                      FSCK_MSG_GITMODULES_BLOB,
1103                                      "non-blob found at .gitmodules");
1104                free(buf);
1105        }
1106
1107
1108        oidset_clear(&gitmodules_found);
1109        oidset_clear(&gitmodules_done);
1110        return ret;
1111}