11 #include "sha1-array.h"
16 #define FOREACH_MSG_ID(FUNC) \
18 FUNC(NUL_IN_HEADER, FATAL) \
19 FUNC(UNTERMINATED_HEADER, FATAL) \
21 FUNC(BAD_DATE, ERROR) \
22 FUNC(BAD_DATE_OVERFLOW, ERROR) \
23 FUNC(BAD_EMAIL, ERROR) \
24 FUNC(BAD_NAME, ERROR) \
25 FUNC(BAD_OBJECT_SHA1, ERROR) \
26 FUNC(BAD_PARENT_SHA1, ERROR) \
27 FUNC(BAD_TAG_OBJECT, ERROR) \
28 FUNC(BAD_TIMEZONE, ERROR) \
29 FUNC(BAD_TREE, ERROR) \
30 FUNC(BAD_TREE_SHA1, ERROR) \
31 FUNC(BAD_TYPE, ERROR) \
32 FUNC(DUPLICATE_ENTRIES, ERROR) \
33 FUNC(MISSING_AUTHOR, ERROR) \
34 FUNC(MISSING_COMMITTER, ERROR) \
35 FUNC(MISSING_EMAIL, ERROR) \
36 FUNC(MISSING_GRAFT, ERROR) \
37 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
38 FUNC(MISSING_OBJECT, ERROR) \
39 FUNC(MISSING_PARENT, ERROR) \
40 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
41 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
42 FUNC(MISSING_TAG, ERROR) \
43 FUNC(MISSING_TAG_ENTRY, ERROR) \
44 FUNC(MISSING_TAG_OBJECT, ERROR) \
45 FUNC(MISSING_TREE, ERROR) \
46 FUNC(MISSING_TYPE, ERROR) \
47 FUNC(MISSING_TYPE_ENTRY, ERROR) \
48 FUNC(MULTIPLE_AUTHORS, ERROR) \
49 FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
50 FUNC(TREE_NOT_SORTED, ERROR) \
51 FUNC(UNKNOWN_TYPE, ERROR) \
52 FUNC(ZERO_PADDED_DATE, ERROR) \
54 FUNC(BAD_FILEMODE, WARN) \
55 FUNC(EMPTY_NAME, WARN) \
56 FUNC(FULL_PATHNAME, WARN) \
58 FUNC(HAS_DOTDOT, WARN) \
59 FUNC(HAS_DOTGIT, WARN) \
60 FUNC(NULL_SHA1, WARN) \
61 FUNC(ZERO_PADDED_FILEMODE, WARN) \
62 /* infos (reported as warnings, but ignored by default) */ \
63 FUNC(BAD_TAG_NAME, INFO) \
64 FUNC(MISSING_TAGGER_ENTRY, INFO)
66 #define MSG_ID(id, msg_type) FSCK_MSG_##id,
68 FOREACH_MSG_ID(MSG_ID)
74 #define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type },
76 const char *id_string;
77 const char *downcased;
79 } msg_id_info[FSCK_MSG_MAX + 1] = {
80 FOREACH_MSG_ID(MSG_ID)
85 static int parse_msg_id(const char *text)
89 if (!msg_id_info[0].downcased) {
90 /* convert id_string to lower case, without underscores. */
91 for (i = 0; i < FSCK_MSG_MAX; i++) {
92 const char *p = msg_id_info[i].id_string;
94 char *q = xmalloc(len);
96 msg_id_info[i].downcased = q;
101 *(q)++ = tolower(*(p)++);
106 for (i = 0; i < FSCK_MSG_MAX; i++)
107 if (!strcmp(text, msg_id_info[i].downcased))
113 static int fsck_msg_type(enum fsck_msg_id msg_id,
114 struct fsck_options *options)
118 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
120 if (options->msg_type)
121 msg_type = options->msg_type[msg_id];
123 msg_type = msg_id_info[msg_id].msg_type;
124 if (options->strict && msg_type == FSCK_WARN)
125 msg_type = FSCK_ERROR;
131 static void init_skiplist(struct fsck_options *options, const char *path)
133 static struct sha1_array skiplist = SHA1_ARRAY_INIT;
136 unsigned char sha1[20];
138 if (options->skiplist)
139 sorted = options->skiplist->sorted;
142 options->skiplist = &skiplist;
145 fd = open(path, O_RDONLY);
147 die("Could not open skip list: %s", path);
149 int result = read_in_full(fd, buffer, sizeof(buffer));
151 die_errno("Could not read '%s'", path);
154 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
155 die("Invalid SHA-1: %s", buffer);
156 sha1_array_append(&skiplist, sha1);
157 if (sorted && skiplist.nr > 1 &&
158 hashcmp(skiplist.sha1[skiplist.nr - 2],
168 static int parse_msg_type(const char *str)
170 if (!strcmp(str, "error"))
172 else if (!strcmp(str, "warn"))
174 else if (!strcmp(str, "ignore"))
177 die("Unknown fsck message type: '%s'", str);
180 int is_valid_msg_type(const char *msg_id, const char *msg_type)
182 if (parse_msg_id(msg_id) < 0)
184 parse_msg_type(msg_type);
188 void fsck_set_msg_type(struct fsck_options *options,
189 const char *msg_id, const char *msg_type)
191 int id = parse_msg_id(msg_id), type;
194 die("Unhandled message id: %s", msg_id);
195 type = parse_msg_type(msg_type);
197 if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
198 die("Cannot demote %s to %s", msg_id, msg_type);
200 if (!options->msg_type) {
202 int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
203 for (i = 0; i < FSCK_MSG_MAX; i++)
204 msg_type[i] = fsck_msg_type(i, options);
205 options->msg_type = msg_type;
208 options->msg_type[id] = type;
211 void fsck_set_msg_types(struct fsck_options *options, const char *values)
213 char *buf = xstrdup(values), *to_free = buf;
217 int len = strcspn(buf, " ,|"), equal;
227 equal < len && buf[equal] != '=' && buf[equal] != ':';
229 buf[equal] = tolower(buf[equal]);
232 if (!strcmp(buf, "skiplist")) {
234 die("skiplist requires a path");
235 init_skiplist(options, buf + equal + 1);
241 die("Missing '=': '%s'", buf);
243 fsck_set_msg_type(options, buf, buf + equal + 1);
249 static void append_msg_id(struct strbuf *sb, const char *msg_id)
252 char c = *(msg_id)++;
257 strbuf_addch(sb, tolower(c));
260 strbuf_addch(sb, *(msg_id)++);
264 strbuf_addstr(sb, ": ");
267 __attribute__((format (printf, 4, 5)))
268 static int report(struct fsck_options *options, struct object *object,
269 enum fsck_msg_id id, const char *fmt, ...)
272 struct strbuf sb = STRBUF_INIT;
273 int msg_type = fsck_msg_type(id, options), result;
275 if (msg_type == FSCK_IGNORE)
278 if (options->skiplist && object &&
279 sha1_array_lookup(options->skiplist, object->sha1) >= 0)
282 if (msg_type == FSCK_FATAL)
283 msg_type = FSCK_ERROR;
284 else if (msg_type == FSCK_INFO)
285 msg_type = FSCK_WARN;
287 append_msg_id(&sb, msg_id_info[id].id_string);
290 strbuf_vaddf(&sb, fmt, ap);
291 result = options->error_func(object, msg_type, sb.buf);
298 static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
300 struct tree_desc desc;
301 struct name_entry entry;
304 if (parse_tree(tree))
307 init_tree_desc(&desc, tree->buffer, tree->size);
308 while (tree_entry(&desc, &entry)) {
311 if (S_ISGITLINK(entry.mode))
313 if (S_ISDIR(entry.mode))
314 result = options->walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data, options);
315 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
316 result = options->walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data, options);
318 result = error("in tree %s: entry %s has bad mode %.6o",
319 sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
329 static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
331 struct commit_list *parents;
335 if (parse_commit(commit))
338 result = options->walk((struct object *)commit->tree, OBJ_TREE, data, options);
343 parents = commit->parents;
345 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
350 parents = parents->next;
355 static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
359 return options->walk(tag->tagged, OBJ_ANY, data, options);
362 int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
370 return fsck_walk_tree((struct tree *)obj, data, options);
372 return fsck_walk_commit((struct commit *)obj, data, options);
374 return fsck_walk_tag((struct tag *)obj, data, options);
376 error("Unknown object type for %s", sha1_to_hex(obj->sha1));
382 * The entries in a tree are ordered in the _path_ order,
383 * which means that a directory entry is ordered by adding
384 * a slash to the end of it.
386 * So a directory called "a" is ordered _after_ a file
387 * called "a.c", because "a/" sorts after "a.c".
389 #define TREE_UNORDERED (-1)
390 #define TREE_HAS_DUPS (-2)
392 static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
394 int len1 = strlen(name1);
395 int len2 = strlen(name2);
396 int len = len1 < len2 ? len1 : len2;
397 unsigned char c1, c2;
400 cmp = memcmp(name1, name2, len);
404 return TREE_UNORDERED;
407 * Ok, the first <len> characters are the same.
408 * Now we need to order the next one, but turn
409 * a '\0' into a '/' for a directory entry.
415 * git-write-tree used to write out a nonsense tree that has
416 * entries with the same name, one blob and one tree. Make
417 * sure we do not have duplicate entries.
419 return TREE_HAS_DUPS;
420 if (!c1 && S_ISDIR(mode1))
422 if (!c2 && S_ISDIR(mode2))
424 return c1 < c2 ? 0 : TREE_UNORDERED;
427 static int fsck_tree(struct tree *item, struct fsck_options *options)
430 int has_null_sha1 = 0;
431 int has_full_path = 0;
432 int has_empty_name = 0;
436 int has_zero_pad = 0;
437 int has_bad_modes = 0;
438 int has_dup_entries = 0;
439 int not_properly_sorted = 0;
440 struct tree_desc desc;
444 init_tree_desc(&desc, item->buffer, item->size);
452 const unsigned char *sha1;
454 sha1 = tree_entry_extract(&desc, &name, &mode);
456 has_null_sha1 |= is_null_sha1(sha1);
457 has_full_path |= !!strchr(name, '/');
458 has_empty_name |= !*name;
459 has_dot |= !strcmp(name, ".");
460 has_dotdot |= !strcmp(name, "..");
461 has_dotgit |= (!strcmp(name, ".git") ||
462 is_hfs_dotgit(name) ||
463 is_ntfs_dotgit(name));
464 has_zero_pad |= *(char *)desc.buffer == '0';
465 update_tree_entry(&desc);
478 * This is nonstandard, but we had a few of these
479 * early on when we honored the full set of mode
483 if (!options->strict)
490 switch (verify_ordered(o_mode, o_name, mode, name)) {
492 not_properly_sorted = 1;
508 retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
510 retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
512 retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
514 retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'");
516 retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'");
518 retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
520 retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
522 retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
524 retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
525 if (not_properly_sorted)
526 retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
530 static int verify_headers(const void *data, unsigned long size,
531 struct object *obj, struct fsck_options *options)
533 const char *buffer = (const char *)data;
536 for (i = 0; i < size; i++) {
539 return report(options, obj,
540 FSCK_MSG_NUL_IN_HEADER,
541 "unterminated header: NUL at offset %ld", i);
543 if (i + 1 < size && buffer[i + 1] == '\n')
549 * We did not find double-LF that separates the header
550 * and the body. Not having a body is not a crime but
551 * we do want to see the terminating LF for the last header
554 if (size && buffer[size - 1] == '\n')
557 return report(options, obj,
558 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
561 static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options)
563 const char *p = *ident;
566 *ident = strchrnul(*ident, '\n');
571 return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
572 p += strcspn(p, "<>\n");
574 return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
576 return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
578 return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
580 p += strcspn(p, "<>\n");
582 return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
585 return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
587 if (*p == '0' && p[1] != ' ')
588 return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
589 if (date_overflows(strtoul(p, &end, 10)))
590 return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
591 if ((end == p || *end != ' '))
592 return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
594 if ((*p != '+' && *p != '-') ||
600 return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
605 static int fsck_commit_buffer(struct commit *commit, const char *buffer,
606 unsigned long size, struct fsck_options *options)
608 unsigned char tree_sha1[20], sha1[20];
609 struct commit_graft *graft;
610 unsigned parent_count, parent_line_count = 0, author_count;
613 if (verify_headers(buffer, size, &commit->object, options))
616 if (!skip_prefix(buffer, "tree ", &buffer))
617 return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
618 if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n') {
619 err = report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
624 while (skip_prefix(buffer, "parent ", &buffer)) {
625 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
626 err = report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
633 graft = lookup_commit_graft(commit->object.sha1);
634 parent_count = commit_list_count(commit->parents);
636 if (graft->nr_parent == -1 && !parent_count)
637 ; /* shallow commit */
638 else if (graft->nr_parent != parent_count) {
639 err = report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing");
644 if (parent_count != parent_line_count) {
645 err = report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing");
651 while (skip_prefix(buffer, "author ", &buffer)) {
653 err = fsck_ident(&buffer, &commit->object, options);
657 if (author_count < 1)
658 err = report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
659 else if (author_count > 1)
660 err = report(options, &commit->object, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
663 if (!skip_prefix(buffer, "committer ", &buffer))
664 return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
665 err = fsck_ident(&buffer, &commit->object, options);
669 return report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
674 static int fsck_commit(struct commit *commit, const char *data,
675 unsigned long size, struct fsck_options *options)
677 const char *buffer = data ? data : get_commit_buffer(commit, &size);
678 int ret = fsck_commit_buffer(commit, buffer, size, options);
680 unuse_commit_buffer(commit, buffer);
684 static int fsck_tag_buffer(struct tag *tag, const char *data,
685 unsigned long size, struct fsck_options *options)
687 unsigned char sha1[20];
690 char *to_free = NULL, *eol;
691 struct strbuf sb = STRBUF_INIT;
696 enum object_type type;
699 read_sha1_file(tag->object.sha1, &type, &size);
701 return report(options, &tag->object,
702 FSCK_MSG_MISSING_TAG_OBJECT,
703 "cannot read tag object");
705 if (type != OBJ_TAG) {
706 ret = report(options, &tag->object,
707 FSCK_MSG_TAG_OBJECT_NOT_TAG,
708 "expected tag got %s",
714 ret = verify_headers(buffer, size, &tag->object, options);
718 if (!skip_prefix(buffer, "object ", &buffer)) {
719 ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
722 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
723 ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
729 if (!skip_prefix(buffer, "type ", &buffer)) {
730 ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
733 eol = strchr(buffer, '\n');
735 ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
738 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
739 ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
744 if (!skip_prefix(buffer, "tag ", &buffer)) {
745 ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
748 eol = strchr(buffer, '\n');
750 ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
753 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
754 if (check_refname_format(sb.buf, 0)) {
755 ret = report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME,
756 "invalid 'tag' name: %.*s",
757 (int)(eol - buffer), buffer);
763 if (!skip_prefix(buffer, "tagger ", &buffer)) {
764 /* early tags do not contain 'tagger' lines; warn only */
765 ret = report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
770 ret = fsck_ident(&buffer, &tag->object, options);
778 static int fsck_tag(struct tag *tag, const char *data,
779 unsigned long size, struct fsck_options *options)
781 struct object *tagged = tag->tagged;
784 return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object");
786 return fsck_tag_buffer(tag, data, size, options);
789 int fsck_object(struct object *obj, void *data, unsigned long size,
790 struct fsck_options *options)
793 return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
795 if (obj->type == OBJ_BLOB)
797 if (obj->type == OBJ_TREE)
798 return fsck_tree((struct tree *) obj, options);
799 if (obj->type == OBJ_COMMIT)
800 return fsck_commit((struct commit *) obj, (const char *) data,
802 if (obj->type == OBJ_TAG)
803 return fsck_tag((struct tag *) obj, (const char *) data,
806 return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)",
810 int fsck_error_function(struct object *obj, int msg_type, const char *message)
812 if (msg_type == FSCK_WARN) {
813 warning("object %s: %s", sha1_to_hex(obj->sha1), message);
816 error("object %s: %s", sha1_to_hex(obj->sha1), message);