680f35eff0eca657af4aeb33caa496efcf6319af
   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
  12#define FSCK_FATAL -1
  13
  14#define FOREACH_MSG_ID(FUNC) \
  15        /* fatal errors */ \
  16        FUNC(NUL_IN_HEADER, FATAL) \
  17        FUNC(UNTERMINATED_HEADER, FATAL) \
  18        /* errors */ \
  19        FUNC(BAD_DATE, ERROR) \
  20        FUNC(BAD_DATE_OVERFLOW, ERROR) \
  21        FUNC(BAD_EMAIL, ERROR) \
  22        FUNC(BAD_NAME, ERROR) \
  23        FUNC(BAD_OBJECT_SHA1, ERROR) \
  24        FUNC(BAD_PARENT_SHA1, ERROR) \
  25        FUNC(BAD_TAG_OBJECT, ERROR) \
  26        FUNC(BAD_TIMEZONE, ERROR) \
  27        FUNC(BAD_TREE, ERROR) \
  28        FUNC(BAD_TREE_SHA1, ERROR) \
  29        FUNC(BAD_TYPE, ERROR) \
  30        FUNC(DUPLICATE_ENTRIES, ERROR) \
  31        FUNC(MISSING_AUTHOR, ERROR) \
  32        FUNC(MISSING_COMMITTER, ERROR) \
  33        FUNC(MISSING_EMAIL, ERROR) \
  34        FUNC(MISSING_GRAFT, ERROR) \
  35        FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
  36        FUNC(MISSING_OBJECT, ERROR) \
  37        FUNC(MISSING_PARENT, ERROR) \
  38        FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
  39        FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
  40        FUNC(MISSING_TAG, ERROR) \
  41        FUNC(MISSING_TAG_ENTRY, ERROR) \
  42        FUNC(MISSING_TAG_OBJECT, ERROR) \
  43        FUNC(MISSING_TREE, ERROR) \
  44        FUNC(MISSING_TYPE, ERROR) \
  45        FUNC(MISSING_TYPE_ENTRY, ERROR) \
  46        FUNC(MULTIPLE_AUTHORS, ERROR) \
  47        FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
  48        FUNC(TREE_NOT_SORTED, ERROR) \
  49        FUNC(UNKNOWN_TYPE, ERROR) \
  50        FUNC(ZERO_PADDED_DATE, ERROR) \
  51        /* warnings */ \
  52        FUNC(BAD_FILEMODE, WARN) \
  53        FUNC(BAD_TAG_NAME, WARN) \
  54        FUNC(EMPTY_NAME, WARN) \
  55        FUNC(FULL_PATHNAME, WARN) \
  56        FUNC(HAS_DOT, WARN) \
  57        FUNC(HAS_DOTDOT, WARN) \
  58        FUNC(HAS_DOTGIT, WARN) \
  59        FUNC(MISSING_TAGGER_ENTRY, WARN) \
  60        FUNC(NULL_SHA1, WARN) \
  61        FUNC(ZERO_PADDED_FILEMODE, WARN)
  62
  63#define MSG_ID(id, msg_type) FSCK_MSG_##id,
  64enum fsck_msg_id {
  65        FOREACH_MSG_ID(MSG_ID)
  66        FSCK_MSG_MAX
  67};
  68#undef MSG_ID
  69
  70#define STR(x) #x
  71#define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type },
  72static struct {
  73        const char *id_string;
  74        const char *downcased;
  75        int msg_type;
  76} msg_id_info[FSCK_MSG_MAX + 1] = {
  77        FOREACH_MSG_ID(MSG_ID)
  78        { NULL, NULL, -1 }
  79};
  80#undef MSG_ID
  81
  82static int parse_msg_id(const char *text)
  83{
  84        int i;
  85
  86        if (!msg_id_info[0].downcased) {
  87                /* convert id_string to lower case, without underscores. */
  88                for (i = 0; i < FSCK_MSG_MAX; i++) {
  89                        const char *p = msg_id_info[i].id_string;
  90                        int len = strlen(p);
  91                        char *q = xmalloc(len);
  92
  93                        msg_id_info[i].downcased = q;
  94                        while (*p)
  95                                if (*p == '_')
  96                                        p++;
  97                                else
  98                                        *(q)++ = tolower(*(p)++);
  99                        *q = '\0';
 100                }
 101        }
 102
 103        for (i = 0; i < FSCK_MSG_MAX; i++)
 104                if (!strcmp(text, msg_id_info[i].downcased))
 105                        return i;
 106
 107        return -1;
 108}
 109
 110static int fsck_msg_type(enum fsck_msg_id msg_id,
 111        struct fsck_options *options)
 112{
 113        int msg_type;
 114
 115        assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
 116
 117        if (options->msg_type)
 118                msg_type = options->msg_type[msg_id];
 119        else {
 120                msg_type = msg_id_info[msg_id].msg_type;
 121                if (options->strict && msg_type == FSCK_WARN)
 122                        msg_type = FSCK_ERROR;
 123        }
 124
 125        return msg_type;
 126}
 127
 128static int parse_msg_type(const char *str)
 129{
 130        if (!strcmp(str, "error"))
 131                return FSCK_ERROR;
 132        else if (!strcmp(str, "warn"))
 133                return FSCK_WARN;
 134        else if (!strcmp(str, "ignore"))
 135                return FSCK_IGNORE;
 136        else
 137                die("Unknown fsck message type: '%s'", str);
 138}
 139
 140int is_valid_msg_type(const char *msg_id, const char *msg_type)
 141{
 142        if (parse_msg_id(msg_id) < 0)
 143                return 0;
 144        parse_msg_type(msg_type);
 145        return 1;
 146}
 147
 148void fsck_set_msg_type(struct fsck_options *options,
 149                const char *msg_id, const char *msg_type)
 150{
 151        int id = parse_msg_id(msg_id), type;
 152
 153        if (id < 0)
 154                die("Unhandled message id: %s", msg_id);
 155        type = parse_msg_type(msg_type);
 156
 157        if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
 158                die("Cannot demote %s to %s", msg_id, msg_type);
 159
 160        if (!options->msg_type) {
 161                int i;
 162                int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
 163                for (i = 0; i < FSCK_MSG_MAX; i++)
 164                        msg_type[i] = fsck_msg_type(i, options);
 165                options->msg_type = msg_type;
 166        }
 167
 168        options->msg_type[id] = type;
 169}
 170
 171void fsck_set_msg_types(struct fsck_options *options, const char *values)
 172{
 173        char *buf = xstrdup(values), *to_free = buf;
 174        int done = 0;
 175
 176        while (!done) {
 177                int len = strcspn(buf, " ,|"), equal;
 178
 179                done = !buf[len];
 180                if (!len) {
 181                        buf++;
 182                        continue;
 183                }
 184                buf[len] = '\0';
 185
 186                for (equal = 0;
 187                     equal < len && buf[equal] != '=' && buf[equal] != ':';
 188                     equal++)
 189                        buf[equal] = tolower(buf[equal]);
 190                buf[equal] = '\0';
 191
 192                if (equal == len)
 193                        die("Missing '=': '%s'", buf);
 194
 195                fsck_set_msg_type(options, buf, buf + equal + 1);
 196                buf += len + 1;
 197        }
 198        free(to_free);
 199}
 200
 201static void append_msg_id(struct strbuf *sb, const char *msg_id)
 202{
 203        for (;;) {
 204                char c = *(msg_id)++;
 205
 206                if (!c)
 207                        break;
 208                if (c != '_')
 209                        strbuf_addch(sb, tolower(c));
 210                else {
 211                        assert(*msg_id);
 212                        strbuf_addch(sb, *(msg_id)++);
 213                }
 214        }
 215
 216        strbuf_addstr(sb, ": ");
 217}
 218
 219__attribute__((format (printf, 4, 5)))
 220static int report(struct fsck_options *options, struct object *object,
 221        enum fsck_msg_id id, const char *fmt, ...)
 222{
 223        va_list ap;
 224        struct strbuf sb = STRBUF_INIT;
 225        int msg_type = fsck_msg_type(id, options), result;
 226
 227        if (msg_type == FSCK_IGNORE)
 228                return 0;
 229
 230        if (msg_type == FSCK_FATAL)
 231                msg_type = FSCK_ERROR;
 232
 233        append_msg_id(&sb, msg_id_info[id].id_string);
 234
 235        va_start(ap, fmt);
 236        strbuf_vaddf(&sb, fmt, ap);
 237        result = options->error_func(object, msg_type, sb.buf);
 238        strbuf_release(&sb);
 239        va_end(ap);
 240
 241        return result;
 242}
 243
 244static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
 245{
 246        struct tree_desc desc;
 247        struct name_entry entry;
 248        int res = 0;
 249
 250        if (parse_tree(tree))
 251                return -1;
 252
 253        init_tree_desc(&desc, tree->buffer, tree->size);
 254        while (tree_entry(&desc, &entry)) {
 255                int result;
 256
 257                if (S_ISGITLINK(entry.mode))
 258                        continue;
 259                if (S_ISDIR(entry.mode))
 260                        result = options->walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data, options);
 261                else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
 262                        result = options->walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data, options);
 263                else {
 264                        result = error("in tree %s: entry %s has bad mode %.6o",
 265                                        sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
 266                }
 267                if (result < 0)
 268                        return result;
 269                if (!res)
 270                        res = result;
 271        }
 272        return res;
 273}
 274
 275static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
 276{
 277        struct commit_list *parents;
 278        int res;
 279        int result;
 280
 281        if (parse_commit(commit))
 282                return -1;
 283
 284        result = options->walk((struct object *)commit->tree, OBJ_TREE, data, options);
 285        if (result < 0)
 286                return result;
 287        res = result;
 288
 289        parents = commit->parents;
 290        while (parents) {
 291                result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
 292                if (result < 0)
 293                        return result;
 294                if (!res)
 295                        res = result;
 296                parents = parents->next;
 297        }
 298        return res;
 299}
 300
 301static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
 302{
 303        if (parse_tag(tag))
 304                return -1;
 305        return options->walk(tag->tagged, OBJ_ANY, data, options);
 306}
 307
 308int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
 309{
 310        if (!obj)
 311                return -1;
 312        switch (obj->type) {
 313        case OBJ_BLOB:
 314                return 0;
 315        case OBJ_TREE:
 316                return fsck_walk_tree((struct tree *)obj, data, options);
 317        case OBJ_COMMIT:
 318                return fsck_walk_commit((struct commit *)obj, data, options);
 319        case OBJ_TAG:
 320                return fsck_walk_tag((struct tag *)obj, data, options);
 321        default:
 322                error("Unknown object type for %s", sha1_to_hex(obj->sha1));
 323                return -1;
 324        }
 325}
 326
 327/*
 328 * The entries in a tree are ordered in the _path_ order,
 329 * which means that a directory entry is ordered by adding
 330 * a slash to the end of it.
 331 *
 332 * So a directory called "a" is ordered _after_ a file
 333 * called "a.c", because "a/" sorts after "a.c".
 334 */
 335#define TREE_UNORDERED (-1)
 336#define TREE_HAS_DUPS  (-2)
 337
 338static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
 339{
 340        int len1 = strlen(name1);
 341        int len2 = strlen(name2);
 342        int len = len1 < len2 ? len1 : len2;
 343        unsigned char c1, c2;
 344        int cmp;
 345
 346        cmp = memcmp(name1, name2, len);
 347        if (cmp < 0)
 348                return 0;
 349        if (cmp > 0)
 350                return TREE_UNORDERED;
 351
 352        /*
 353         * Ok, the first <len> characters are the same.
 354         * Now we need to order the next one, but turn
 355         * a '\0' into a '/' for a directory entry.
 356         */
 357        c1 = name1[len];
 358        c2 = name2[len];
 359        if (!c1 && !c2)
 360                /*
 361                 * git-write-tree used to write out a nonsense tree that has
 362                 * entries with the same name, one blob and one tree.  Make
 363                 * sure we do not have duplicate entries.
 364                 */
 365                return TREE_HAS_DUPS;
 366        if (!c1 && S_ISDIR(mode1))
 367                c1 = '/';
 368        if (!c2 && S_ISDIR(mode2))
 369                c2 = '/';
 370        return c1 < c2 ? 0 : TREE_UNORDERED;
 371}
 372
 373static int fsck_tree(struct tree *item, struct fsck_options *options)
 374{
 375        int retval;
 376        int has_null_sha1 = 0;
 377        int has_full_path = 0;
 378        int has_empty_name = 0;
 379        int has_dot = 0;
 380        int has_dotdot = 0;
 381        int has_dotgit = 0;
 382        int has_zero_pad = 0;
 383        int has_bad_modes = 0;
 384        int has_dup_entries = 0;
 385        int not_properly_sorted = 0;
 386        struct tree_desc desc;
 387        unsigned o_mode;
 388        const char *o_name;
 389
 390        init_tree_desc(&desc, item->buffer, item->size);
 391
 392        o_mode = 0;
 393        o_name = NULL;
 394
 395        while (desc.size) {
 396                unsigned mode;
 397                const char *name;
 398                const unsigned char *sha1;
 399
 400                sha1 = tree_entry_extract(&desc, &name, &mode);
 401
 402                has_null_sha1 |= is_null_sha1(sha1);
 403                has_full_path |= !!strchr(name, '/');
 404                has_empty_name |= !*name;
 405                has_dot |= !strcmp(name, ".");
 406                has_dotdot |= !strcmp(name, "..");
 407                has_dotgit |= (!strcmp(name, ".git") ||
 408                               is_hfs_dotgit(name) ||
 409                               is_ntfs_dotgit(name));
 410                has_zero_pad |= *(char *)desc.buffer == '0';
 411                update_tree_entry(&desc);
 412
 413                switch (mode) {
 414                /*
 415                 * Standard modes..
 416                 */
 417                case S_IFREG | 0755:
 418                case S_IFREG | 0644:
 419                case S_IFLNK:
 420                case S_IFDIR:
 421                case S_IFGITLINK:
 422                        break;
 423                /*
 424                 * This is nonstandard, but we had a few of these
 425                 * early on when we honored the full set of mode
 426                 * bits..
 427                 */
 428                case S_IFREG | 0664:
 429                        if (!options->strict)
 430                                break;
 431                default:
 432                        has_bad_modes = 1;
 433                }
 434
 435                if (o_name) {
 436                        switch (verify_ordered(o_mode, o_name, mode, name)) {
 437                        case TREE_UNORDERED:
 438                                not_properly_sorted = 1;
 439                                break;
 440                        case TREE_HAS_DUPS:
 441                                has_dup_entries = 1;
 442                                break;
 443                        default:
 444                                break;
 445                        }
 446                }
 447
 448                o_mode = mode;
 449                o_name = name;
 450        }
 451
 452        retval = 0;
 453        if (has_null_sha1)
 454                retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
 455        if (has_full_path)
 456                retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
 457        if (has_empty_name)
 458                retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
 459        if (has_dot)
 460                retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'");
 461        if (has_dotdot)
 462                retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'");
 463        if (has_dotgit)
 464                retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
 465        if (has_zero_pad)
 466                retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
 467        if (has_bad_modes)
 468                retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
 469        if (has_dup_entries)
 470                retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
 471        if (not_properly_sorted)
 472                retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
 473        return retval;
 474}
 475
 476static int require_end_of_header(const void *data, unsigned long size,
 477        struct object *obj, struct fsck_options *options)
 478{
 479        const char *buffer = (const char *)data;
 480        unsigned long i;
 481
 482        for (i = 0; i < size; i++) {
 483                switch (buffer[i]) {
 484                case '\0':
 485                        return report(options, obj,
 486                                FSCK_MSG_NUL_IN_HEADER,
 487                                "unterminated header: NUL at offset %ld", i);
 488                case '\n':
 489                        if (i + 1 < size && buffer[i + 1] == '\n')
 490                                return 0;
 491                }
 492        }
 493
 494        return report(options, obj,
 495                FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
 496}
 497
 498static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options)
 499{
 500        const char *p = *ident;
 501        char *end;
 502
 503        *ident = strchrnul(*ident, '\n');
 504        if (**ident == '\n')
 505                (*ident)++;
 506
 507        if (*p == '<')
 508                return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
 509        p += strcspn(p, "<>\n");
 510        if (*p == '>')
 511                return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
 512        if (*p != '<')
 513                return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
 514        if (p[-1] != ' ')
 515                return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
 516        p++;
 517        p += strcspn(p, "<>\n");
 518        if (*p != '>')
 519                return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
 520        p++;
 521        if (*p != ' ')
 522                return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
 523        p++;
 524        if (*p == '0' && p[1] != ' ')
 525                return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
 526        if (date_overflows(strtoul(p, &end, 10)))
 527                return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
 528        if ((end == p || *end != ' '))
 529                return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
 530        p = end + 1;
 531        if ((*p != '+' && *p != '-') ||
 532            !isdigit(p[1]) ||
 533            !isdigit(p[2]) ||
 534            !isdigit(p[3]) ||
 535            !isdigit(p[4]) ||
 536            (p[5] != '\n'))
 537                return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
 538        p += 6;
 539        return 0;
 540}
 541
 542static int fsck_commit_buffer(struct commit *commit, const char *buffer,
 543        unsigned long size, struct fsck_options *options)
 544{
 545        unsigned char tree_sha1[20], sha1[20];
 546        struct commit_graft *graft;
 547        unsigned parent_count, parent_line_count = 0, author_count;
 548        int err;
 549
 550        if (require_end_of_header(buffer, size, &commit->object, options))
 551                return -1;
 552
 553        if (!skip_prefix(buffer, "tree ", &buffer))
 554                return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
 555        if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n') {
 556                err = report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
 557                if (err)
 558                        return err;
 559        }
 560        buffer += 41;
 561        while (skip_prefix(buffer, "parent ", &buffer)) {
 562                if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
 563                        err = report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
 564                        if (err)
 565                                return err;
 566                }
 567                buffer += 41;
 568                parent_line_count++;
 569        }
 570        graft = lookup_commit_graft(commit->object.sha1);
 571        parent_count = commit_list_count(commit->parents);
 572        if (graft) {
 573                if (graft->nr_parent == -1 && !parent_count)
 574                        ; /* shallow commit */
 575                else if (graft->nr_parent != parent_count) {
 576                        err = report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing");
 577                        if (err)
 578                                return err;
 579                }
 580        } else {
 581                if (parent_count != parent_line_count) {
 582                        err = report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing");
 583                        if (err)
 584                                return err;
 585                }
 586        }
 587        author_count = 0;
 588        while (skip_prefix(buffer, "author ", &buffer)) {
 589                author_count++;
 590                err = fsck_ident(&buffer, &commit->object, options);
 591                if (err)
 592                        return err;
 593        }
 594        if (author_count < 1)
 595                err = report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
 596        else if (author_count > 1)
 597                err = report(options, &commit->object, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
 598        if (err)
 599                return err;
 600        if (!skip_prefix(buffer, "committer ", &buffer))
 601                return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
 602        err = fsck_ident(&buffer, &commit->object, options);
 603        if (err)
 604                return err;
 605        if (!commit->tree)
 606                return report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
 607
 608        return 0;
 609}
 610
 611static int fsck_commit(struct commit *commit, const char *data,
 612        unsigned long size, struct fsck_options *options)
 613{
 614        const char *buffer = data ?  data : get_commit_buffer(commit, &size);
 615        int ret = fsck_commit_buffer(commit, buffer, size, options);
 616        if (!data)
 617                unuse_commit_buffer(commit, buffer);
 618        return ret;
 619}
 620
 621static int fsck_tag_buffer(struct tag *tag, const char *data,
 622        unsigned long size, struct fsck_options *options)
 623{
 624        unsigned char sha1[20];
 625        int ret = 0;
 626        const char *buffer;
 627        char *to_free = NULL, *eol;
 628        struct strbuf sb = STRBUF_INIT;
 629
 630        if (data)
 631                buffer = data;
 632        else {
 633                enum object_type type;
 634
 635                buffer = to_free =
 636                        read_sha1_file(tag->object.sha1, &type, &size);
 637                if (!buffer)
 638                        return report(options, &tag->object,
 639                                FSCK_MSG_MISSING_TAG_OBJECT,
 640                                "cannot read tag object");
 641
 642                if (type != OBJ_TAG) {
 643                        ret = report(options, &tag->object,
 644                                FSCK_MSG_TAG_OBJECT_NOT_TAG,
 645                                "expected tag got %s",
 646                            typename(type));
 647                        goto done;
 648                }
 649        }
 650
 651        if (require_end_of_header(buffer, size, &tag->object, options))
 652                goto done;
 653
 654        if (!skip_prefix(buffer, "object ", &buffer)) {
 655                ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
 656                goto done;
 657        }
 658        if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
 659                ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
 660                if (ret)
 661                        goto done;
 662        }
 663        buffer += 41;
 664
 665        if (!skip_prefix(buffer, "type ", &buffer)) {
 666                ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
 667                goto done;
 668        }
 669        eol = strchr(buffer, '\n');
 670        if (!eol) {
 671                ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
 672                goto done;
 673        }
 674        if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
 675                ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
 676        if (ret)
 677                goto done;
 678        buffer = eol + 1;
 679
 680        if (!skip_prefix(buffer, "tag ", &buffer)) {
 681                ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
 682                goto done;
 683        }
 684        eol = strchr(buffer, '\n');
 685        if (!eol) {
 686                ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
 687                goto done;
 688        }
 689        strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
 690        if (check_refname_format(sb.buf, 0))
 691                report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME,
 692                           "invalid 'tag' name: %.*s",
 693                           (int)(eol - buffer), buffer);
 694        buffer = eol + 1;
 695
 696        if (!skip_prefix(buffer, "tagger ", &buffer))
 697                /* early tags do not contain 'tagger' lines; warn only */
 698                report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
 699        else
 700                ret = fsck_ident(&buffer, &tag->object, options);
 701
 702done:
 703        strbuf_release(&sb);
 704        free(to_free);
 705        return ret;
 706}
 707
 708static int fsck_tag(struct tag *tag, const char *data,
 709        unsigned long size, struct fsck_options *options)
 710{
 711        struct object *tagged = tag->tagged;
 712
 713        if (!tagged)
 714                return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object");
 715
 716        return fsck_tag_buffer(tag, data, size, options);
 717}
 718
 719int fsck_object(struct object *obj, void *data, unsigned long size,
 720        struct fsck_options *options)
 721{
 722        if (!obj)
 723                return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
 724
 725        if (obj->type == OBJ_BLOB)
 726                return 0;
 727        if (obj->type == OBJ_TREE)
 728                return fsck_tree((struct tree *) obj, options);
 729        if (obj->type == OBJ_COMMIT)
 730                return fsck_commit((struct commit *) obj, (const char *) data,
 731                        size, options);
 732        if (obj->type == OBJ_TAG)
 733                return fsck_tag((struct tag *) obj, (const char *) data,
 734                        size, options);
 735
 736        return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)",
 737                          obj->type);
 738}
 739
 740int fsck_error_function(struct object *obj, int msg_type, const char *message)
 741{
 742        if (msg_type == FSCK_WARN) {
 743                warning("object %s: %s", sha1_to_hex(obj->sha1), message);
 744                return 0;
 745        }
 746        error("object %s: %s", sha1_to_hex(obj->sha1), message);
 747        return 1;
 748}