12 static int fsck_walk_tree(struct tree
*tree
, fsck_walk_func walk
, void *data
)
14 struct tree_desc desc
;
15 struct name_entry entry
;
21 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
22 while (tree_entry(&desc
, &entry
)) {
25 if (S_ISGITLINK(entry
.mode
))
27 if (S_ISDIR(entry
.mode
))
28 result
= walk(&lookup_tree(entry
.sha1
)->object
, OBJ_TREE
, data
);
29 else if (S_ISREG(entry
.mode
) || S_ISLNK(entry
.mode
))
30 result
= walk(&lookup_blob(entry
.sha1
)->object
, OBJ_BLOB
, data
);
32 result
= error("in tree %s: entry %s has bad mode %.6o",
33 sha1_to_hex(tree
->object
.sha1
), entry
.path
, entry
.mode
);
43 static int fsck_walk_commit(struct commit
*commit
, fsck_walk_func walk
, void *data
)
45 struct commit_list
*parents
;
49 if (parse_commit(commit
))
52 result
= walk((struct object
*)commit
->tree
, OBJ_TREE
, data
);
57 parents
= commit
->parents
;
59 result
= walk((struct object
*)parents
->item
, OBJ_COMMIT
, data
);
64 parents
= parents
->next
;
69 static int fsck_walk_tag(struct tag
*tag
, fsck_walk_func walk
, void *data
)
73 return walk(tag
->tagged
, OBJ_ANY
, data
);
76 int fsck_walk(struct object
*obj
, fsck_walk_func walk
, void *data
)
84 return fsck_walk_tree((struct tree
*)obj
, walk
, data
);
86 return fsck_walk_commit((struct commit
*)obj
, walk
, data
);
88 return fsck_walk_tag((struct tag
*)obj
, walk
, data
);
90 error("Unknown object type for %s", sha1_to_hex(obj
->sha1
));
96 * The entries in a tree are ordered in the _path_ order,
97 * which means that a directory entry is ordered by adding
98 * a slash to the end of it.
100 * So a directory called "a" is ordered _after_ a file
101 * called "a.c", because "a/" sorts after "a.c".
103 #define TREE_UNORDERED (-1)
104 #define TREE_HAS_DUPS (-2)
106 static int verify_ordered(unsigned mode1
, const char *name1
, unsigned mode2
, const char *name2
)
108 int len1
= strlen(name1
);
109 int len2
= strlen(name2
);
110 int len
= len1
< len2
? len1
: len2
;
111 unsigned char c1
, c2
;
114 cmp
= memcmp(name1
, name2
, len
);
118 return TREE_UNORDERED
;
121 * Ok, the first <len> characters are the same.
122 * Now we need to order the next one, but turn
123 * a '\0' into a '/' for a directory entry.
129 * git-write-tree used to write out a nonsense tree that has
130 * entries with the same name, one blob and one tree. Make
131 * sure we do not have duplicate entries.
133 return TREE_HAS_DUPS
;
134 if (!c1
&& S_ISDIR(mode1
))
136 if (!c2
&& S_ISDIR(mode2
))
138 return c1
< c2
? 0 : TREE_UNORDERED
;
141 static int fsck_tree(struct tree
*item
, int strict
, fsck_error error_func
)
144 int has_null_sha1
= 0;
145 int has_full_path
= 0;
146 int has_empty_name
= 0;
150 int has_zero_pad
= 0;
151 int has_bad_modes
= 0;
152 int has_dup_entries
= 0;
153 int not_properly_sorted
= 0;
154 struct tree_desc desc
;
158 init_tree_desc(&desc
, item
->buffer
, item
->size
);
166 const unsigned char *sha1
;
168 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
170 has_null_sha1
|= is_null_sha1(sha1
);
171 has_full_path
|= !!strchr(name
, '/');
172 has_empty_name
|= !*name
;
173 has_dot
|= !strcmp(name
, ".");
174 has_dotdot
|= !strcmp(name
, "..");
175 has_dotgit
|= (!strcmp(name
, ".git") ||
176 is_hfs_dotgit(name
) ||
177 is_ntfs_dotgit(name
));
178 has_zero_pad
|= *(char *)desc
.buffer
== '0';
179 update_tree_entry(&desc
);
192 * This is nonstandard, but we had a few of these
193 * early on when we honored the full set of mode
204 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
206 not_properly_sorted
= 1;
222 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains entries pointing to null sha1");
224 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains full pathnames");
226 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains empty pathname");
228 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains '.'");
230 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains '..'");
232 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains '.git'");
234 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains zero-padded file modes");
236 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains bad file modes");
238 retval
+= error_func(&item
->object
, FSCK_ERROR
, "contains duplicate file entries");
239 if (not_properly_sorted
)
240 retval
+= error_func(&item
->object
, FSCK_ERROR
, "not properly sorted");
244 static int verify_headers(const void *data
, unsigned long size
,
245 struct object
*obj
, fsck_error error_func
)
247 const char *buffer
= (const char *)data
;
250 for (i
= 0; i
< size
; i
++) {
253 return error_func(obj
, FSCK_ERROR
,
254 "unterminated header: NUL at offset %d", i
);
256 if (i
+ 1 < size
&& buffer
[i
+ 1] == '\n')
262 * We did not find double-LF that separates the header
263 * and the body. Not having a body is not a crime but
264 * we do want to see the terminating LF for the last header
267 if (size
&& buffer
[size
- 1] == '\n')
270 return error_func(obj
, FSCK_ERROR
, "unterminated header");
273 static int fsck_ident(const char **ident
, struct object
*obj
, fsck_error error_func
)
278 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - missing space before email");
279 *ident
+= strcspn(*ident
, "<>\n");
281 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - bad name");
283 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - missing email");
284 if ((*ident
)[-1] != ' ')
285 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - missing space before email");
287 *ident
+= strcspn(*ident
, "<>\n");
289 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - bad email");
292 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - missing space before date");
294 if (**ident
== '0' && (*ident
)[1] != ' ')
295 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - zero-padded date");
296 if (date_overflows(strtoul(*ident
, &end
, 10)))
297 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - date causes integer overflow");
298 if (end
== *ident
|| *end
!= ' ')
299 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - bad date");
301 if ((**ident
!= '+' && **ident
!= '-') ||
302 !isdigit((*ident
)[1]) ||
303 !isdigit((*ident
)[2]) ||
304 !isdigit((*ident
)[3]) ||
305 !isdigit((*ident
)[4]) ||
306 ((*ident
)[5] != '\n'))
307 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - bad time zone");
312 static int fsck_commit_buffer(struct commit
*commit
, const char *buffer
,
313 unsigned long size
, fsck_error error_func
)
315 unsigned char tree_sha1
[20], sha1
[20];
316 struct commit_graft
*graft
;
317 unsigned parent_count
, parent_line_count
= 0;
320 if (verify_headers(buffer
, size
, &commit
->object
, error_func
))
323 if (!skip_prefix(buffer
, "tree ", &buffer
))
324 return error_func(&commit
->object
, FSCK_ERROR
, "invalid format - expected 'tree' line");
325 if (get_sha1_hex(buffer
, tree_sha1
) || buffer
[40] != '\n')
326 return error_func(&commit
->object
, FSCK_ERROR
, "invalid 'tree' line format - bad sha1");
328 while (skip_prefix(buffer
, "parent ", &buffer
)) {
329 if (get_sha1_hex(buffer
, sha1
) || buffer
[40] != '\n')
330 return error_func(&commit
->object
, FSCK_ERROR
, "invalid 'parent' line format - bad sha1");
334 graft
= lookup_commit_graft(commit
->object
.sha1
);
335 parent_count
= commit_list_count(commit
->parents
);
337 if (graft
->nr_parent
== -1 && !parent_count
)
338 ; /* shallow commit */
339 else if (graft
->nr_parent
!= parent_count
)
340 return error_func(&commit
->object
, FSCK_ERROR
, "graft objects missing");
342 if (parent_count
!= parent_line_count
)
343 return error_func(&commit
->object
, FSCK_ERROR
, "parent objects missing");
345 if (!skip_prefix(buffer
, "author ", &buffer
))
346 return error_func(&commit
->object
, FSCK_ERROR
, "invalid format - expected 'author' line");
347 err
= fsck_ident(&buffer
, &commit
->object
, error_func
);
350 if (!skip_prefix(buffer
, "committer ", &buffer
))
351 return error_func(&commit
->object
, FSCK_ERROR
, "invalid format - expected 'committer' line");
352 err
= fsck_ident(&buffer
, &commit
->object
, error_func
);
356 return error_func(&commit
->object
, FSCK_ERROR
, "could not load commit's tree %s", sha1_to_hex(tree_sha1
));
361 static int fsck_commit(struct commit
*commit
, const char *data
,
362 unsigned long size
, fsck_error error_func
)
364 const char *buffer
= data
? data
: get_commit_buffer(commit
, &size
);
365 int ret
= fsck_commit_buffer(commit
, buffer
, size
, error_func
);
367 unuse_commit_buffer(commit
, buffer
);
371 static int fsck_tag_buffer(struct tag
*tag
, const char *data
,
372 unsigned long size
, fsck_error error_func
)
374 unsigned char sha1
[20];
377 char *to_free
= NULL
, *eol
;
378 struct strbuf sb
= STRBUF_INIT
;
383 enum object_type type
;
386 read_sha1_file(tag
->object
.sha1
, &type
, &size
);
388 return error_func(&tag
->object
, FSCK_ERROR
,
389 "cannot read tag object");
391 if (type
!= OBJ_TAG
) {
392 ret
= error_func(&tag
->object
, FSCK_ERROR
,
393 "expected tag got %s",
399 if (verify_headers(buffer
, size
, &tag
->object
, error_func
))
402 if (!skip_prefix(buffer
, "object ", &buffer
)) {
403 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - expected 'object' line");
406 if (get_sha1_hex(buffer
, sha1
) || buffer
[40] != '\n') {
407 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid 'object' line format - bad sha1");
412 if (!skip_prefix(buffer
, "type ", &buffer
)) {
413 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - expected 'type' line");
416 eol
= strchr(buffer
, '\n');
418 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - unexpected end after 'type' line");
421 if (type_from_string_gently(buffer
, eol
- buffer
, 1) < 0)
422 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid 'type' value");
427 if (!skip_prefix(buffer
, "tag ", &buffer
)) {
428 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - expected 'tag' line");
431 eol
= strchr(buffer
, '\n');
433 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - unexpected end after 'type' line");
436 strbuf_addf(&sb
, "refs/tags/%.*s", (int)(eol
- buffer
), buffer
);
437 if (check_refname_format(sb
.buf
, 0))
438 error_func(&tag
->object
, FSCK_WARN
, "invalid 'tag' name: %.*s",
439 (int)(eol
- buffer
), buffer
);
442 if (!skip_prefix(buffer
, "tagger ", &buffer
))
443 /* early tags do not contain 'tagger' lines; warn only */
444 error_func(&tag
->object
, FSCK_WARN
, "invalid format - expected 'tagger' line");
446 ret
= fsck_ident(&buffer
, &tag
->object
, error_func
);
454 static int fsck_tag(struct tag
*tag
, const char *data
,
455 unsigned long size
, fsck_error error_func
)
457 struct object
*tagged
= tag
->tagged
;
460 return error_func(&tag
->object
, FSCK_ERROR
, "could not load tagged object");
462 return fsck_tag_buffer(tag
, data
, size
, error_func
);
465 int fsck_object(struct object
*obj
, void *data
, unsigned long size
,
466 int strict
, fsck_error error_func
)
469 return error_func(obj
, FSCK_ERROR
, "no valid object to fsck");
471 if (obj
->type
== OBJ_BLOB
)
473 if (obj
->type
== OBJ_TREE
)
474 return fsck_tree((struct tree
*) obj
, strict
, error_func
);
475 if (obj
->type
== OBJ_COMMIT
)
476 return fsck_commit((struct commit
*) obj
, (const char *) data
,
478 if (obj
->type
== OBJ_TAG
)
479 return fsck_tag((struct tag
*) obj
, (const char *) data
,
482 return error_func(obj
, FSCK_ERROR
, "unknown type '%d' (internal fsck error)",
486 int fsck_error_function(struct object
*obj
, int type
, const char *fmt
, ...)
489 struct strbuf sb
= STRBUF_INIT
;
491 strbuf_addf(&sb
, "object %s:", sha1_to_hex(obj
->sha1
));
494 strbuf_vaddf(&sb
, fmt
, ap
);