12 #define FOREACH_MSG_ID(FUNC) \
14 FUNC(BAD_DATE, ERROR) \
15 FUNC(BAD_DATE_OVERFLOW, ERROR) \
16 FUNC(BAD_EMAIL, ERROR) \
17 FUNC(BAD_NAME, ERROR) \
18 FUNC(BAD_OBJECT_SHA1, ERROR) \
19 FUNC(BAD_PARENT_SHA1, ERROR) \
20 FUNC(BAD_TAG_OBJECT, ERROR) \
21 FUNC(BAD_TIMEZONE, ERROR) \
22 FUNC(BAD_TREE, ERROR) \
23 FUNC(BAD_TREE_SHA1, ERROR) \
24 FUNC(BAD_TYPE, ERROR) \
25 FUNC(DUPLICATE_ENTRIES, ERROR) \
26 FUNC(MISSING_AUTHOR, ERROR) \
27 FUNC(MISSING_COMMITTER, ERROR) \
28 FUNC(MISSING_EMAIL, ERROR) \
29 FUNC(MISSING_GRAFT, ERROR) \
30 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
31 FUNC(MISSING_OBJECT, ERROR) \
32 FUNC(MISSING_PARENT, ERROR) \
33 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
34 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
35 FUNC(MISSING_TAG, ERROR) \
36 FUNC(MISSING_TAG_ENTRY, ERROR) \
37 FUNC(MISSING_TAG_OBJECT, ERROR) \
38 FUNC(MISSING_TREE, ERROR) \
39 FUNC(MISSING_TYPE, ERROR) \
40 FUNC(MISSING_TYPE_ENTRY, ERROR) \
41 FUNC(MULTIPLE_AUTHORS, ERROR) \
42 FUNC(NUL_IN_HEADER, ERROR) \
43 FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
44 FUNC(TREE_NOT_SORTED, ERROR) \
45 FUNC(UNKNOWN_TYPE, ERROR) \
46 FUNC(UNTERMINATED_HEADER, ERROR) \
47 FUNC(ZERO_PADDED_DATE, ERROR) \
49 FUNC(BAD_FILEMODE, WARN) \
50 FUNC(BAD_TAG_NAME, WARN) \
51 FUNC(EMPTY_NAME, WARN) \
52 FUNC(FULL_PATHNAME, WARN) \
54 FUNC(HAS_DOTDOT, WARN) \
55 FUNC(HAS_DOTGIT, WARN) \
56 FUNC(MISSING_TAGGER_ENTRY, WARN) \
57 FUNC(NULL_SHA1, WARN) \
58 FUNC(ZERO_PADDED_FILEMODE, WARN)
60 #define MSG_ID(id, msg_type) FSCK_MSG_##id,
62 FOREACH_MSG_ID(MSG_ID
)
68 #define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type },
70 const char *id_string
;
71 const char *downcased
;
73 } msg_id_info
[FSCK_MSG_MAX
+ 1] = {
74 FOREACH_MSG_ID(MSG_ID
)
79 static int parse_msg_id(const char *text
)
83 if (!msg_id_info
[0].downcased
) {
84 /* convert id_string to lower case, without underscores. */
85 for (i
= 0; i
< FSCK_MSG_MAX
; i
++) {
86 const char *p
= msg_id_info
[i
].id_string
;
88 char *q
= xmalloc(len
);
90 msg_id_info
[i
].downcased
= q
;
95 *(q
)++ = tolower(*(p
)++);
100 for (i
= 0; i
< FSCK_MSG_MAX
; i
++)
101 if (!strcmp(text
, msg_id_info
[i
].downcased
))
107 static int fsck_msg_type(enum fsck_msg_id msg_id
,
108 struct fsck_options
*options
)
112 assert(msg_id
>= 0 && msg_id
< FSCK_MSG_MAX
);
114 if (options
->msg_type
)
115 msg_type
= options
->msg_type
[msg_id
];
117 msg_type
= msg_id_info
[msg_id
].msg_type
;
118 if (options
->strict
&& msg_type
== FSCK_WARN
)
119 msg_type
= FSCK_ERROR
;
125 static int parse_msg_type(const char *str
)
127 if (!strcmp(str
, "error"))
129 else if (!strcmp(str
, "warn"))
132 die("Unknown fsck message type: '%s'", str
);
135 int is_valid_msg_type(const char *msg_id
, const char *msg_type
)
137 if (parse_msg_id(msg_id
) < 0)
139 parse_msg_type(msg_type
);
143 void fsck_set_msg_type(struct fsck_options
*options
,
144 const char *msg_id
, const char *msg_type
)
146 int id
= parse_msg_id(msg_id
), type
;
149 die("Unhandled message id: %s", msg_id
);
150 type
= parse_msg_type(msg_type
);
152 if (!options
->msg_type
) {
154 int *msg_type
= xmalloc(sizeof(int) * FSCK_MSG_MAX
);
155 for (i
= 0; i
< FSCK_MSG_MAX
; i
++)
156 msg_type
[i
] = fsck_msg_type(i
, options
);
157 options
->msg_type
= msg_type
;
160 options
->msg_type
[id
] = type
;
163 void fsck_set_msg_types(struct fsck_options
*options
, const char *values
)
165 char *buf
= xstrdup(values
), *to_free
= buf
;
169 int len
= strcspn(buf
, " ,|"), equal
;
179 equal
< len
&& buf
[equal
] != '=' && buf
[equal
] != ':';
181 buf
[equal
] = tolower(buf
[equal
]);
185 die("Missing '=': '%s'", buf
);
187 fsck_set_msg_type(options
, buf
, buf
+ equal
+ 1);
193 static void append_msg_id(struct strbuf
*sb
, const char *msg_id
)
196 char c
= *(msg_id
)++;
201 strbuf_addch(sb
, tolower(c
));
204 strbuf_addch(sb
, *(msg_id
)++);
208 strbuf_addstr(sb
, ": ");
211 __attribute__((format (printf
, 4, 5)))
212 static int report(struct fsck_options
*options
, struct object
*object
,
213 enum fsck_msg_id id
, const char *fmt
, ...)
216 struct strbuf sb
= STRBUF_INIT
;
217 int msg_type
= fsck_msg_type(id
, options
), result
;
219 append_msg_id(&sb
, msg_id_info
[id
].id_string
);
222 strbuf_vaddf(&sb
, fmt
, ap
);
223 result
= options
->error_func(object
, msg_type
, sb
.buf
);
230 static int fsck_walk_tree(struct tree
*tree
, void *data
, struct fsck_options
*options
)
232 struct tree_desc desc
;
233 struct name_entry entry
;
236 if (parse_tree(tree
))
239 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
240 while (tree_entry(&desc
, &entry
)) {
243 if (S_ISGITLINK(entry
.mode
))
245 if (S_ISDIR(entry
.mode
))
246 result
= options
->walk(&lookup_tree(entry
.sha1
)->object
, OBJ_TREE
, data
, options
);
247 else if (S_ISREG(entry
.mode
) || S_ISLNK(entry
.mode
))
248 result
= options
->walk(&lookup_blob(entry
.sha1
)->object
, OBJ_BLOB
, data
, options
);
250 result
= error("in tree %s: entry %s has bad mode %.6o",
251 sha1_to_hex(tree
->object
.sha1
), entry
.path
, entry
.mode
);
261 static int fsck_walk_commit(struct commit
*commit
, void *data
, struct fsck_options
*options
)
263 struct commit_list
*parents
;
267 if (parse_commit(commit
))
270 result
= options
->walk((struct object
*)commit
->tree
, OBJ_TREE
, data
, options
);
275 parents
= commit
->parents
;
277 result
= options
->walk((struct object
*)parents
->item
, OBJ_COMMIT
, data
, options
);
282 parents
= parents
->next
;
287 static int fsck_walk_tag(struct tag
*tag
, void *data
, struct fsck_options
*options
)
291 return options
->walk(tag
->tagged
, OBJ_ANY
, data
, options
);
294 int fsck_walk(struct object
*obj
, void *data
, struct fsck_options
*options
)
302 return fsck_walk_tree((struct tree
*)obj
, data
, options
);
304 return fsck_walk_commit((struct commit
*)obj
, data
, options
);
306 return fsck_walk_tag((struct tag
*)obj
, data
, options
);
308 error("Unknown object type for %s", sha1_to_hex(obj
->sha1
));
314 * The entries in a tree are ordered in the _path_ order,
315 * which means that a directory entry is ordered by adding
316 * a slash to the end of it.
318 * So a directory called "a" is ordered _after_ a file
319 * called "a.c", because "a/" sorts after "a.c".
321 #define TREE_UNORDERED (-1)
322 #define TREE_HAS_DUPS (-2)
324 static int verify_ordered(unsigned mode1
, const char *name1
, unsigned mode2
, const char *name2
)
326 int len1
= strlen(name1
);
327 int len2
= strlen(name2
);
328 int len
= len1
< len2
? len1
: len2
;
329 unsigned char c1
, c2
;
332 cmp
= memcmp(name1
, name2
, len
);
336 return TREE_UNORDERED
;
339 * Ok, the first <len> characters are the same.
340 * Now we need to order the next one, but turn
341 * a '\0' into a '/' for a directory entry.
347 * git-write-tree used to write out a nonsense tree that has
348 * entries with the same name, one blob and one tree. Make
349 * sure we do not have duplicate entries.
351 return TREE_HAS_DUPS
;
352 if (!c1
&& S_ISDIR(mode1
))
354 if (!c2
&& S_ISDIR(mode2
))
356 return c1
< c2
? 0 : TREE_UNORDERED
;
359 static int fsck_tree(struct tree
*item
, struct fsck_options
*options
)
362 int has_null_sha1
= 0;
363 int has_full_path
= 0;
364 int has_empty_name
= 0;
368 int has_zero_pad
= 0;
369 int has_bad_modes
= 0;
370 int has_dup_entries
= 0;
371 int not_properly_sorted
= 0;
372 struct tree_desc desc
;
376 init_tree_desc(&desc
, item
->buffer
, item
->size
);
384 const unsigned char *sha1
;
386 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
388 has_null_sha1
|= is_null_sha1(sha1
);
389 has_full_path
|= !!strchr(name
, '/');
390 has_empty_name
|= !*name
;
391 has_dot
|= !strcmp(name
, ".");
392 has_dotdot
|= !strcmp(name
, "..");
393 has_dotgit
|= (!strcmp(name
, ".git") ||
394 is_hfs_dotgit(name
) ||
395 is_ntfs_dotgit(name
));
396 has_zero_pad
|= *(char *)desc
.buffer
== '0';
397 update_tree_entry(&desc
);
410 * This is nonstandard, but we had a few of these
411 * early on when we honored the full set of mode
415 if (!options
->strict
)
422 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
424 not_properly_sorted
= 1;
440 retval
+= report(options
, &item
->object
, FSCK_MSG_NULL_SHA1
, "contains entries pointing to null sha1");
442 retval
+= report(options
, &item
->object
, FSCK_MSG_FULL_PATHNAME
, "contains full pathnames");
444 retval
+= report(options
, &item
->object
, FSCK_MSG_EMPTY_NAME
, "contains empty pathname");
446 retval
+= report(options
, &item
->object
, FSCK_MSG_HAS_DOT
, "contains '.'");
448 retval
+= report(options
, &item
->object
, FSCK_MSG_HAS_DOTDOT
, "contains '..'");
450 retval
+= report(options
, &item
->object
, FSCK_MSG_HAS_DOTGIT
, "contains '.git'");
452 retval
+= report(options
, &item
->object
, FSCK_MSG_ZERO_PADDED_FILEMODE
, "contains zero-padded file modes");
454 retval
+= report(options
, &item
->object
, FSCK_MSG_BAD_FILEMODE
, "contains bad file modes");
456 retval
+= report(options
, &item
->object
, FSCK_MSG_DUPLICATE_ENTRIES
, "contains duplicate file entries");
457 if (not_properly_sorted
)
458 retval
+= report(options
, &item
->object
, FSCK_MSG_TREE_NOT_SORTED
, "not properly sorted");
462 static int require_end_of_header(const void *data
, unsigned long size
,
463 struct object
*obj
, struct fsck_options
*options
)
465 const char *buffer
= (const char *)data
;
468 for (i
= 0; i
< size
; i
++) {
471 return report(options
, obj
,
472 FSCK_MSG_NUL_IN_HEADER
,
473 "unterminated header: NUL at offset %ld", i
);
475 if (i
+ 1 < size
&& buffer
[i
+ 1] == '\n')
480 return report(options
, obj
,
481 FSCK_MSG_UNTERMINATED_HEADER
, "unterminated header");
484 static int fsck_ident(const char **ident
, struct object
*obj
, struct fsck_options
*options
)
486 const char *p
= *ident
;
489 *ident
= strchrnul(*ident
, '\n');
494 return report(options
, obj
, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL
, "invalid author/committer line - missing space before email");
495 p
+= strcspn(p
, "<>\n");
497 return report(options
, obj
, FSCK_MSG_BAD_NAME
, "invalid author/committer line - bad name");
499 return report(options
, obj
, FSCK_MSG_MISSING_EMAIL
, "invalid author/committer line - missing email");
501 return report(options
, obj
, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL
, "invalid author/committer line - missing space before email");
503 p
+= strcspn(p
, "<>\n");
505 return report(options
, obj
, FSCK_MSG_BAD_EMAIL
, "invalid author/committer line - bad email");
508 return report(options
, obj
, FSCK_MSG_MISSING_SPACE_BEFORE_DATE
, "invalid author/committer line - missing space before date");
510 if (*p
== '0' && p
[1] != ' ')
511 return report(options
, obj
, FSCK_MSG_ZERO_PADDED_DATE
, "invalid author/committer line - zero-padded date");
512 if (date_overflows(strtoul(p
, &end
, 10)))
513 return report(options
, obj
, FSCK_MSG_BAD_DATE_OVERFLOW
, "invalid author/committer line - date causes integer overflow");
514 if ((end
== p
|| *end
!= ' '))
515 return report(options
, obj
, FSCK_MSG_BAD_DATE
, "invalid author/committer line - bad date");
517 if ((*p
!= '+' && *p
!= '-') ||
523 return report(options
, obj
, FSCK_MSG_BAD_TIMEZONE
, "invalid author/committer line - bad time zone");
528 static int fsck_commit_buffer(struct commit
*commit
, const char *buffer
,
529 unsigned long size
, struct fsck_options
*options
)
531 unsigned char tree_sha1
[20], sha1
[20];
532 struct commit_graft
*graft
;
533 unsigned parent_count
, parent_line_count
= 0, author_count
;
536 if (require_end_of_header(buffer
, size
, &commit
->object
, options
))
539 if (!skip_prefix(buffer
, "tree ", &buffer
))
540 return report(options
, &commit
->object
, FSCK_MSG_MISSING_TREE
, "invalid format - expected 'tree' line");
541 if (get_sha1_hex(buffer
, tree_sha1
) || buffer
[40] != '\n') {
542 err
= report(options
, &commit
->object
, FSCK_MSG_BAD_TREE_SHA1
, "invalid 'tree' line format - bad sha1");
547 while (skip_prefix(buffer
, "parent ", &buffer
)) {
548 if (get_sha1_hex(buffer
, sha1
) || buffer
[40] != '\n') {
549 err
= report(options
, &commit
->object
, FSCK_MSG_BAD_PARENT_SHA1
, "invalid 'parent' line format - bad sha1");
556 graft
= lookup_commit_graft(commit
->object
.sha1
);
557 parent_count
= commit_list_count(commit
->parents
);
559 if (graft
->nr_parent
== -1 && !parent_count
)
560 ; /* shallow commit */
561 else if (graft
->nr_parent
!= parent_count
) {
562 err
= report(options
, &commit
->object
, FSCK_MSG_MISSING_GRAFT
, "graft objects missing");
567 if (parent_count
!= parent_line_count
) {
568 err
= report(options
, &commit
->object
, FSCK_MSG_MISSING_PARENT
, "parent objects missing");
574 while (skip_prefix(buffer
, "author ", &buffer
)) {
576 err
= fsck_ident(&buffer
, &commit
->object
, options
);
580 if (author_count
< 1)
581 err
= report(options
, &commit
->object
, FSCK_MSG_MISSING_AUTHOR
, "invalid format - expected 'author' line");
582 else if (author_count
> 1)
583 err
= report(options
, &commit
->object
, FSCK_MSG_MULTIPLE_AUTHORS
, "invalid format - multiple 'author' lines");
586 if (!skip_prefix(buffer
, "committer ", &buffer
))
587 return report(options
, &commit
->object
, FSCK_MSG_MISSING_COMMITTER
, "invalid format - expected 'committer' line");
588 err
= fsck_ident(&buffer
, &commit
->object
, options
);
592 return report(options
, &commit
->object
, FSCK_MSG_BAD_TREE
, "could not load commit's tree %s", sha1_to_hex(tree_sha1
));
597 static int fsck_commit(struct commit
*commit
, const char *data
,
598 unsigned long size
, struct fsck_options
*options
)
600 const char *buffer
= data
? data
: get_commit_buffer(commit
, &size
);
601 int ret
= fsck_commit_buffer(commit
, buffer
, size
, options
);
603 unuse_commit_buffer(commit
, buffer
);
607 static int fsck_tag_buffer(struct tag
*tag
, const char *data
,
608 unsigned long size
, struct fsck_options
*options
)
610 unsigned char sha1
[20];
613 char *to_free
= NULL
, *eol
;
614 struct strbuf sb
= STRBUF_INIT
;
619 enum object_type type
;
622 read_sha1_file(tag
->object
.sha1
, &type
, &size
);
624 return report(options
, &tag
->object
,
625 FSCK_MSG_MISSING_TAG_OBJECT
,
626 "cannot read tag object");
628 if (type
!= OBJ_TAG
) {
629 ret
= report(options
, &tag
->object
,
630 FSCK_MSG_TAG_OBJECT_NOT_TAG
,
631 "expected tag got %s",
637 if (require_end_of_header(buffer
, size
, &tag
->object
, options
))
640 if (!skip_prefix(buffer
, "object ", &buffer
)) {
641 ret
= report(options
, &tag
->object
, FSCK_MSG_MISSING_OBJECT
, "invalid format - expected 'object' line");
644 if (get_sha1_hex(buffer
, sha1
) || buffer
[40] != '\n') {
645 ret
= report(options
, &tag
->object
, FSCK_MSG_BAD_OBJECT_SHA1
, "invalid 'object' line format - bad sha1");
651 if (!skip_prefix(buffer
, "type ", &buffer
)) {
652 ret
= report(options
, &tag
->object
, FSCK_MSG_MISSING_TYPE_ENTRY
, "invalid format - expected 'type' line");
655 eol
= strchr(buffer
, '\n');
657 ret
= report(options
, &tag
->object
, FSCK_MSG_MISSING_TYPE
, "invalid format - unexpected end after 'type' line");
660 if (type_from_string_gently(buffer
, eol
- buffer
, 1) < 0)
661 ret
= report(options
, &tag
->object
, FSCK_MSG_BAD_TYPE
, "invalid 'type' value");
666 if (!skip_prefix(buffer
, "tag ", &buffer
)) {
667 ret
= report(options
, &tag
->object
, FSCK_MSG_MISSING_TAG_ENTRY
, "invalid format - expected 'tag' line");
670 eol
= strchr(buffer
, '\n');
672 ret
= report(options
, &tag
->object
, FSCK_MSG_MISSING_TAG
, "invalid format - unexpected end after 'type' line");
675 strbuf_addf(&sb
, "refs/tags/%.*s", (int)(eol
- buffer
), buffer
);
676 if (check_refname_format(sb
.buf
, 0))
677 report(options
, &tag
->object
, FSCK_MSG_BAD_TAG_NAME
,
678 "invalid 'tag' name: %.*s",
679 (int)(eol
- buffer
), buffer
);
682 if (!skip_prefix(buffer
, "tagger ", &buffer
))
683 /* early tags do not contain 'tagger' lines; warn only */
684 report(options
, &tag
->object
, FSCK_MSG_MISSING_TAGGER_ENTRY
, "invalid format - expected 'tagger' line");
686 ret
= fsck_ident(&buffer
, &tag
->object
, options
);
694 static int fsck_tag(struct tag
*tag
, const char *data
,
695 unsigned long size
, struct fsck_options
*options
)
697 struct object
*tagged
= tag
->tagged
;
700 return report(options
, &tag
->object
, FSCK_MSG_BAD_TAG_OBJECT
, "could not load tagged object");
702 return fsck_tag_buffer(tag
, data
, size
, options
);
705 int fsck_object(struct object
*obj
, void *data
, unsigned long size
,
706 struct fsck_options
*options
)
709 return report(options
, obj
, FSCK_MSG_BAD_OBJECT_SHA1
, "no valid object to fsck");
711 if (obj
->type
== OBJ_BLOB
)
713 if (obj
->type
== OBJ_TREE
)
714 return fsck_tree((struct tree
*) obj
, options
);
715 if (obj
->type
== OBJ_COMMIT
)
716 return fsck_commit((struct commit
*) obj
, (const char *) data
,
718 if (obj
->type
== OBJ_TAG
)
719 return fsck_tag((struct tag
*) obj
, (const char *) data
,
722 return report(options
, obj
, FSCK_MSG_UNKNOWN_TYPE
, "unknown type '%d' (internal fsck error)",
726 int fsck_error_function(struct object
*obj
, int msg_type
, const char *message
)
728 if (msg_type
== FSCK_WARN
) {
729 warning("object %s: %s", sha1_to_hex(obj
->sha1
), message
);
732 error("object %s: %s", sha1_to_hex(obj
->sha1
), message
);