11 static int fsck_walk_tree(struct tree
*tree
, fsck_walk_func walk
, void *data
)
13 struct tree_desc desc
;
14 struct name_entry entry
;
20 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
21 while (tree_entry(&desc
, &entry
)) {
24 if (S_ISGITLINK(entry
.mode
))
26 if (S_ISDIR(entry
.mode
))
27 result
= walk(&lookup_tree(entry
.sha1
)->object
, OBJ_TREE
, data
);
28 else if (S_ISREG(entry
.mode
) || S_ISLNK(entry
.mode
))
29 result
= walk(&lookup_blob(entry
.sha1
)->object
, OBJ_BLOB
, data
);
31 result
= error("in tree %s: entry %s has bad mode %.6o",
32 sha1_to_hex(tree
->object
.sha1
), entry
.path
, entry
.mode
);
42 static int fsck_walk_commit(struct commit
*commit
, fsck_walk_func walk
, void *data
)
44 struct commit_list
*parents
;
48 if (parse_commit(commit
))
51 result
= walk((struct object
*)commit
->tree
, OBJ_TREE
, data
);
56 parents
= commit
->parents
;
58 result
= walk((struct object
*)parents
->item
, OBJ_COMMIT
, data
);
63 parents
= parents
->next
;
68 static int fsck_walk_tag(struct tag
*tag
, fsck_walk_func walk
, void *data
)
72 return walk(tag
->tagged
, OBJ_ANY
, data
);
75 int fsck_walk(struct object
*obj
, fsck_walk_func walk
, void *data
)
83 return fsck_walk_tree((struct tree
*)obj
, walk
, data
);
85 return fsck_walk_commit((struct commit
*)obj
, walk
, data
);
87 return fsck_walk_tag((struct tag
*)obj
, walk
, data
);
89 error("Unknown object type for %s", sha1_to_hex(obj
->sha1
));
95 * The entries in a tree are ordered in the _path_ order,
96 * which means that a directory entry is ordered by adding
97 * a slash to the end of it.
99 * So a directory called "a" is ordered _after_ a file
100 * called "a.c", because "a/" sorts after "a.c".
102 #define TREE_UNORDERED (-1)
103 #define TREE_HAS_DUPS (-2)
105 static int verify_ordered(unsigned mode1
, const char *name1
, unsigned mode2
, const char *name2
)
107 int len1
= strlen(name1
);
108 int len2
= strlen(name2
);
109 int len
= len1
< len2
? len1
: len2
;
110 unsigned char c1
, c2
;
113 cmp
= memcmp(name1
, name2
, len
);
117 return TREE_UNORDERED
;
120 * Ok, the first <len> characters are the same.
121 * Now we need to order the next one, but turn
122 * a '\0' into a '/' for a directory entry.
128 * git-write-tree used to write out a nonsense tree that has
129 * entries with the same name, one blob and one tree. Make
130 * sure we do not have duplicate entries.
132 return TREE_HAS_DUPS
;
133 if (!c1
&& S_ISDIR(mode1
))
135 if (!c2
&& S_ISDIR(mode2
))
137 return c1
< c2
? 0 : TREE_UNORDERED
;
140 static int fsck_tree(struct tree
*item
, int strict
, fsck_error error_func
)
143 int has_null_sha1
= 0;
144 int has_full_path
= 0;
145 int has_empty_name
= 0;
149 int has_zero_pad
= 0;
150 int has_bad_modes
= 0;
151 int has_dup_entries
= 0;
152 int not_properly_sorted
= 0;
153 struct tree_desc desc
;
157 init_tree_desc(&desc
, item
->buffer
, item
->size
);
165 const unsigned char *sha1
;
167 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
169 has_null_sha1
|= is_null_sha1(sha1
);
170 has_full_path
|= !!strchr(name
, '/');
171 has_empty_name
|= !*name
;
172 has_dot
|= !strcmp(name
, ".");
173 has_dotdot
|= !strcmp(name
, "..");
174 has_dotgit
|= !strcmp(name
, ".git");
175 has_zero_pad
|= *(char *)desc
.buffer
== '0';
176 update_tree_entry(&desc
);
189 * This is nonstandard, but we had a few of these
190 * early on when we honored the full set of mode
201 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
203 not_properly_sorted
= 1;
219 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains entries pointing to null sha1");
221 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains full pathnames");
223 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains empty pathname");
225 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains '.'");
227 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains '..'");
229 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains '.git'");
231 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains zero-padded file modes");
233 retval
+= error_func(&item
->object
, FSCK_WARN
, "contains bad file modes");
235 retval
+= error_func(&item
->object
, FSCK_ERROR
, "contains duplicate file entries");
236 if (not_properly_sorted
)
237 retval
+= error_func(&item
->object
, FSCK_ERROR
, "not properly sorted");
241 static int verify_headers(const void *data
, unsigned long size
,
242 struct object
*obj
, fsck_error error_func
)
244 const char *buffer
= (const char *)data
;
247 for (i
= 0; i
< size
; i
++) {
250 return error_func(obj
, FSCK_ERROR
,
251 "unterminated header: NUL at offset %d", i
);
253 if (i
+ 1 < size
&& buffer
[i
+ 1] == '\n')
259 * We did not find double-LF that separates the header
260 * and the body. Not having a body is not a crime but
261 * we do want to see the terminating LF for the last header
264 if (size
&& buffer
[size
- 1] == '\n')
267 return error_func(obj
, FSCK_ERROR
, "unterminated header");
270 static int fsck_ident(const char **ident
, struct object
*obj
, fsck_error error_func
)
275 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - missing space before email");
276 *ident
+= strcspn(*ident
, "<>\n");
278 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - bad name");
280 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - missing email");
281 if ((*ident
)[-1] != ' ')
282 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - missing space before email");
284 *ident
+= strcspn(*ident
, "<>\n");
286 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - bad email");
289 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - missing space before date");
291 if (**ident
== '0' && (*ident
)[1] != ' ')
292 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - zero-padded date");
293 if (date_overflows(strtoul(*ident
, &end
, 10)))
294 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - date causes integer overflow");
295 if (end
== *ident
|| *end
!= ' ')
296 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - bad date");
298 if ((**ident
!= '+' && **ident
!= '-') ||
299 !isdigit((*ident
)[1]) ||
300 !isdigit((*ident
)[2]) ||
301 !isdigit((*ident
)[3]) ||
302 !isdigit((*ident
)[4]) ||
303 ((*ident
)[5] != '\n'))
304 return error_func(obj
, FSCK_ERROR
, "invalid author/committer line - bad time zone");
309 static int fsck_commit_buffer(struct commit
*commit
, const char *buffer
,
310 unsigned long size
, fsck_error error_func
)
312 unsigned char tree_sha1
[20], sha1
[20];
313 struct commit_graft
*graft
;
314 unsigned parent_count
, parent_line_count
= 0;
317 if (verify_headers(buffer
, size
, &commit
->object
, error_func
))
320 if (!skip_prefix(buffer
, "tree ", &buffer
))
321 return error_func(&commit
->object
, FSCK_ERROR
, "invalid format - expected 'tree' line");
322 if (get_sha1_hex(buffer
, tree_sha1
) || buffer
[40] != '\n')
323 return error_func(&commit
->object
, FSCK_ERROR
, "invalid 'tree' line format - bad sha1");
325 while (skip_prefix(buffer
, "parent ", &buffer
)) {
326 if (get_sha1_hex(buffer
, sha1
) || buffer
[40] != '\n')
327 return error_func(&commit
->object
, FSCK_ERROR
, "invalid 'parent' line format - bad sha1");
331 graft
= lookup_commit_graft(commit
->object
.sha1
);
332 parent_count
= commit_list_count(commit
->parents
);
334 if (graft
->nr_parent
== -1 && !parent_count
)
335 ; /* shallow commit */
336 else if (graft
->nr_parent
!= parent_count
)
337 return error_func(&commit
->object
, FSCK_ERROR
, "graft objects missing");
339 if (parent_count
!= parent_line_count
)
340 return error_func(&commit
->object
, FSCK_ERROR
, "parent objects missing");
342 if (!skip_prefix(buffer
, "author ", &buffer
))
343 return error_func(&commit
->object
, FSCK_ERROR
, "invalid format - expected 'author' line");
344 err
= fsck_ident(&buffer
, &commit
->object
, error_func
);
347 if (!skip_prefix(buffer
, "committer ", &buffer
))
348 return error_func(&commit
->object
, FSCK_ERROR
, "invalid format - expected 'committer' line");
349 err
= fsck_ident(&buffer
, &commit
->object
, error_func
);
353 return error_func(&commit
->object
, FSCK_ERROR
, "could not load commit's tree %s", sha1_to_hex(tree_sha1
));
358 static int fsck_commit(struct commit
*commit
, const char *data
,
359 unsigned long size
, fsck_error error_func
)
361 const char *buffer
= data
? data
: get_commit_buffer(commit
, &size
);
362 int ret
= fsck_commit_buffer(commit
, buffer
, size
, error_func
);
364 unuse_commit_buffer(commit
, buffer
);
368 static int fsck_tag_buffer(struct tag
*tag
, const char *data
,
369 unsigned long size
, fsck_error error_func
)
371 unsigned char sha1
[20];
374 char *to_free
= NULL
, *eol
;
375 struct strbuf sb
= STRBUF_INIT
;
380 enum object_type type
;
383 read_sha1_file(tag
->object
.sha1
, &type
, &size
);
385 return error_func(&tag
->object
, FSCK_ERROR
,
386 "cannot read tag object");
388 if (type
!= OBJ_TAG
) {
389 ret
= error_func(&tag
->object
, FSCK_ERROR
,
390 "expected tag got %s",
396 if (verify_headers(buffer
, size
, &tag
->object
, error_func
))
399 if (!skip_prefix(buffer
, "object ", &buffer
)) {
400 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - expected 'object' line");
403 if (get_sha1_hex(buffer
, sha1
) || buffer
[40] != '\n') {
404 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid 'object' line format - bad sha1");
409 if (!skip_prefix(buffer
, "type ", &buffer
)) {
410 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - expected 'type' line");
413 eol
= strchr(buffer
, '\n');
415 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - unexpected end after 'type' line");
418 if (type_from_string_gently(buffer
, eol
- buffer
, 1) < 0)
419 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid 'type' value");
424 if (!skip_prefix(buffer
, "tag ", &buffer
)) {
425 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - expected 'tag' line");
428 eol
= strchr(buffer
, '\n');
430 ret
= error_func(&tag
->object
, FSCK_ERROR
, "invalid format - unexpected end after 'type' line");
433 strbuf_addf(&sb
, "refs/tags/%.*s", (int)(eol
- buffer
), buffer
);
434 if (check_refname_format(sb
.buf
, 0))
435 error_func(&tag
->object
, FSCK_WARN
, "invalid 'tag' name: %.*s",
436 (int)(eol
- buffer
), buffer
);
439 if (!skip_prefix(buffer
, "tagger ", &buffer
))
440 /* early tags do not contain 'tagger' lines; warn only */
441 error_func(&tag
->object
, FSCK_WARN
, "invalid format - expected 'tagger' line");
443 ret
= fsck_ident(&buffer
, &tag
->object
, error_func
);
451 static int fsck_tag(struct tag
*tag
, const char *data
,
452 unsigned long size
, fsck_error error_func
)
454 struct object
*tagged
= tag
->tagged
;
457 return error_func(&tag
->object
, FSCK_ERROR
, "could not load tagged object");
459 return fsck_tag_buffer(tag
, data
, size
, error_func
);
462 int fsck_object(struct object
*obj
, void *data
, unsigned long size
,
463 int strict
, fsck_error error_func
)
466 return error_func(obj
, FSCK_ERROR
, "no valid object to fsck");
468 if (obj
->type
== OBJ_BLOB
)
470 if (obj
->type
== OBJ_TREE
)
471 return fsck_tree((struct tree
*) obj
, strict
, error_func
);
472 if (obj
->type
== OBJ_COMMIT
)
473 return fsck_commit((struct commit
*) obj
, (const char *) data
,
475 if (obj
->type
== OBJ_TAG
)
476 return fsck_tag((struct tag
*) obj
, (const char *) data
,
479 return error_func(obj
, FSCK_ERROR
, "unknown type '%d' (internal fsck error)",
483 int fsck_error_function(struct object
*obj
, int type
, const char *fmt
, ...)
486 struct strbuf sb
= STRBUF_INIT
;
488 strbuf_addf(&sb
, "object %s:", sha1_to_hex(obj
->sha1
));
491 strbuf_vaddf(&sb
, fmt
, ap
);