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