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