10 static int fsck_walk_tree(struct tree *tree, fsck_walk_func walk, void *data)
12 struct tree_desc desc;
13 struct name_entry entry;
19 init_tree_desc(&desc, tree->buffer, tree->size);
20 while (tree_entry(&desc, &entry)) {
23 if (S_ISGITLINK(entry.mode))
25 if (S_ISDIR(entry.mode))
26 result = walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data);
27 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
28 result = walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data);
30 result = error("in tree %s: entry %s has bad mode %.6o",
31 sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
41 static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)
43 struct commit_list *parents;
47 if (parse_commit(commit))
50 result = walk((struct object *)commit->tree, OBJ_TREE, data);
55 parents = commit->parents;
57 result = walk((struct object *)parents->item, OBJ_COMMIT, data);
62 parents = parents->next;
67 static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data)
71 return walk(tag->tagged, OBJ_ANY, data);
74 int fsck_walk(struct object *obj, fsck_walk_func walk, void *data)
82 return fsck_walk_tree((struct tree *)obj, walk, data);
84 return fsck_walk_commit((struct commit *)obj, walk, data);
86 return fsck_walk_tag((struct tag *)obj, walk, data);
88 error("Unknown object type for %s", sha1_to_hex(obj->sha1));
94 * The entries in a tree are ordered in the _path_ order,
95 * which means that a directory entry is ordered by adding
96 * a slash to the end of it.
98 * So a directory called "a" is ordered _after_ a file
99 * called "a.c", because "a/" sorts after "a.c".
101 #define TREE_UNORDERED (-1)
102 #define TREE_HAS_DUPS (-2)
104 static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
106 int len1 = strlen(name1);
107 int len2 = strlen(name2);
108 int len = len1 < len2 ? len1 : len2;
109 unsigned char c1, c2;
112 cmp = memcmp(name1, name2, len);
116 return TREE_UNORDERED;
119 * Ok, the first <len> characters are the same.
120 * Now we need to order the next one, but turn
121 * a '\0' into a '/' for a directory entry.
127 * git-write-tree used to write out a nonsense tree that has
128 * entries with the same name, one blob and one tree. Make
129 * sure we do not have duplicate entries.
131 return TREE_HAS_DUPS;
132 if (!c1 && S_ISDIR(mode1))
134 if (!c2 && S_ISDIR(mode2))
136 return c1 < c2 ? 0 : TREE_UNORDERED;
139 static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
142 int has_null_sha1 = 0;
143 int has_full_path = 0;
144 int has_empty_name = 0;
145 int has_zero_pad = 0;
146 int has_bad_modes = 0;
147 int has_dup_entries = 0;
148 int not_properly_sorted = 0;
149 struct tree_desc desc;
153 init_tree_desc(&desc, item->buffer, item->size);
161 const unsigned char *sha1;
163 sha1 = tree_entry_extract(&desc, &name, &mode);
165 if (is_null_sha1(sha1))
167 if (strchr(name, '/'))
171 has_zero_pad |= *(char *)desc.buffer == '0';
172 update_tree_entry(&desc);
185 * This is nonstandard, but we had a few of these
186 * early on when we honored the full set of mode
197 switch (verify_ordered(o_mode, o_name, mode, name)) {
199 not_properly_sorted = 1;
215 retval += error_func(&item->object, FSCK_WARN, "contains entries pointing to null sha1");
217 retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
219 retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");
221 retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
223 retval += error_func(&item->object, FSCK_WARN, "contains bad file modes");
225 retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries");
226 if (not_properly_sorted)
227 retval += error_func(&item->object, FSCK_ERROR, "not properly sorted");
231 static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)
234 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
235 *ident += strcspn(*ident, "<>\n");
237 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad name");
239 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing email");
240 if ((*ident)[-1] != ' ')
241 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
243 *ident += strcspn(*ident, "<>\n");
245 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad email");
248 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before date");
250 if (**ident == '0' && (*ident)[1] != ' ')
251 return error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date");
252 *ident += strspn(*ident, "0123456789");
254 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date");
256 if ((**ident != '+' && **ident != '-') ||
257 !isdigit((*ident)[1]) ||
258 !isdigit((*ident)[2]) ||
259 !isdigit((*ident)[3]) ||
260 !isdigit((*ident)[4]) ||
261 ((*ident)[5] != '\n'))
262 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad time zone");
267 static int fsck_commit(struct commit *commit, fsck_error error_func)
269 char *buffer = commit->buffer;
270 unsigned char tree_sha1[20], sha1[20];
271 struct commit_graft *graft;
275 if (commit->date == ULONG_MAX)
276 return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line");
278 if (memcmp(buffer, "tree ", 5))
279 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
280 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
281 return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");
283 while (!memcmp(buffer, "parent ", 7)) {
284 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
285 return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
289 graft = lookup_commit_graft(commit->object.sha1);
291 struct commit_list *p = commit->parents;
297 if (graft->nr_parent == -1 && !parents)
298 ; /* shallow commit */
299 else if (graft->nr_parent != parents)
300 return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
302 struct commit_list *p = commit->parents;
303 while (p && parents) {
308 return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
310 if (memcmp(buffer, "author ", 7))
311 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");
313 err = fsck_ident(&buffer, &commit->object, error_func);
316 if (memcmp(buffer, "committer ", strlen("committer ")))
317 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'committer' line");
318 buffer += strlen("committer ");
319 err = fsck_ident(&buffer, &commit->object, error_func);
323 return error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
328 static int fsck_tag(struct tag *tag, fsck_error error_func)
330 struct object *tagged = tag->tagged;
333 return error_func(&tag->object, FSCK_ERROR, "could not load tagged object");
337 int fsck_object(struct object *obj, int strict, fsck_error error_func)
340 return error_func(obj, FSCK_ERROR, "no valid object to fsck");
342 if (obj->type == OBJ_BLOB)
344 if (obj->type == OBJ_TREE)
345 return fsck_tree((struct tree *) obj, strict, error_func);
346 if (obj->type == OBJ_COMMIT)
347 return fsck_commit((struct commit *) obj, error_func);
348 if (obj->type == OBJ_TAG)
349 return fsck_tag((struct tag *) obj, error_func);
351 return error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)",
355 int fsck_error_function(struct object *obj, int type, const char *fmt, ...)
358 struct strbuf sb = STRBUF_INIT;
360 strbuf_addf(&sb, "object %s:", sha1_to_hex(obj->sha1));
363 strbuf_vaddf(&sb, fmt, ap);