Merge branch 'rs/fix-alt-odb-path-comparison' into maint
[git.git] / fsck.c
bloba7233c8d0b20c06f0aebffbec9c11dacb22b6769
1 #include "cache.h"
2 #include "object.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "tree-walk.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "fsck.h"
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;
14 int res = 0;
16 if (parse_tree(tree))
17 return -1;
19 init_tree_desc(&desc, tree->buffer, tree->size);
20 while (tree_entry(&desc, &entry)) {
21 int result;
23 if (S_ISGITLINK(entry.mode))
24 continue;
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);
29 else {
30 result = error("in tree %s: entry %s has bad mode %.6o",
31 sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
33 if (result < 0)
34 return result;
35 if (!res)
36 res = result;
38 return res;
41 static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)
43 struct commit_list *parents;
44 int res;
45 int result;
47 if (parse_commit(commit))
48 return -1;
50 result = walk((struct object *)commit->tree, OBJ_TREE, data);
51 if (result < 0)
52 return result;
53 res = result;
55 parents = commit->parents;
56 while (parents) {
57 result = walk((struct object *)parents->item, OBJ_COMMIT, data);
58 if (result < 0)
59 return result;
60 if (!res)
61 res = result;
62 parents = parents->next;
64 return res;
67 static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data)
69 if (parse_tag(tag))
70 return -1;
71 return walk(tag->tagged, OBJ_ANY, data);
74 int fsck_walk(struct object *obj, fsck_walk_func walk, void *data)
76 if (!obj)
77 return -1;
78 switch (obj->type) {
79 case OBJ_BLOB:
80 return 0;
81 case OBJ_TREE:
82 return fsck_walk_tree((struct tree *)obj, walk, data);
83 case OBJ_COMMIT:
84 return fsck_walk_commit((struct commit *)obj, walk, data);
85 case OBJ_TAG:
86 return fsck_walk_tag((struct tag *)obj, walk, data);
87 default:
88 error("Unknown object type for %s", sha1_to_hex(obj->sha1));
89 return -1;
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;
110 int cmp;
112 cmp = memcmp(name1, name2, len);
113 if (cmp < 0)
114 return 0;
115 if (cmp > 0)
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.
123 c1 = name1[len];
124 c2 = name2[len];
125 if (!c1 && !c2)
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))
133 c1 = '/';
134 if (!c2 && S_ISDIR(mode2))
135 c2 = '/';
136 return c1 < c2 ? 0 : TREE_UNORDERED;
139 static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
141 int retval;
142 int has_null_sha1 = 0;
143 int has_full_path = 0;
144 int has_empty_name = 0;
145 int has_dot = 0;
146 int has_dotdot = 0;
147 int has_dotgit = 0;
148 int has_zero_pad = 0;
149 int has_bad_modes = 0;
150 int has_dup_entries = 0;
151 int not_properly_sorted = 0;
152 struct tree_desc desc;
153 unsigned o_mode;
154 const char *o_name;
156 init_tree_desc(&desc, item->buffer, item->size);
158 o_mode = 0;
159 o_name = NULL;
161 while (desc.size) {
162 unsigned mode;
163 const char *name;
164 const unsigned char *sha1;
166 sha1 = tree_entry_extract(&desc, &name, &mode);
168 has_null_sha1 |= is_null_sha1(sha1);
169 has_full_path |= !!strchr(name, '/');
170 has_empty_name |= !*name;
171 has_dot |= !strcmp(name, ".");
172 has_dotdot |= !strcmp(name, "..");
173 has_dotgit |= !strcmp(name, ".git");
174 has_zero_pad |= *(char *)desc.buffer == '0';
175 update_tree_entry(&desc);
177 switch (mode) {
179 * Standard modes..
181 case S_IFREG | 0755:
182 case S_IFREG | 0644:
183 case S_IFLNK:
184 case S_IFDIR:
185 case S_IFGITLINK:
186 break;
188 * This is nonstandard, but we had a few of these
189 * early on when we honored the full set of mode
190 * bits..
192 case S_IFREG | 0664:
193 if (!strict)
194 break;
195 default:
196 has_bad_modes = 1;
199 if (o_name) {
200 switch (verify_ordered(o_mode, o_name, mode, name)) {
201 case TREE_UNORDERED:
202 not_properly_sorted = 1;
203 break;
204 case TREE_HAS_DUPS:
205 has_dup_entries = 1;
206 break;
207 default:
208 break;
212 o_mode = mode;
213 o_name = name;
216 retval = 0;
217 if (has_null_sha1)
218 retval += error_func(&item->object, FSCK_WARN, "contains entries pointing to null sha1");
219 if (has_full_path)
220 retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
221 if (has_empty_name)
222 retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");
223 if (has_dot)
224 retval += error_func(&item->object, FSCK_WARN, "contains '.'");
225 if (has_dotdot)
226 retval += error_func(&item->object, FSCK_WARN, "contains '..'");
227 if (has_dotgit)
228 retval += error_func(&item->object, FSCK_WARN, "contains '.git'");
229 if (has_zero_pad)
230 retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
231 if (has_bad_modes)
232 retval += error_func(&item->object, FSCK_WARN, "contains bad file modes");
233 if (has_dup_entries)
234 retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries");
235 if (not_properly_sorted)
236 retval += error_func(&item->object, FSCK_ERROR, "not properly sorted");
237 return retval;
240 static int fsck_ident(const char **ident, struct object *obj, fsck_error error_func)
242 char *end;
244 if (**ident == '<')
245 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
246 *ident += strcspn(*ident, "<>\n");
247 if (**ident == '>')
248 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad name");
249 if (**ident != '<')
250 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing email");
251 if ((*ident)[-1] != ' ')
252 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
253 (*ident)++;
254 *ident += strcspn(*ident, "<>\n");
255 if (**ident != '>')
256 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad email");
257 (*ident)++;
258 if (**ident != ' ')
259 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before date");
260 (*ident)++;
261 if (**ident == '0' && (*ident)[1] != ' ')
262 return error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date");
263 if (date_overflows(strtoul(*ident, &end, 10)))
264 return error_func(obj, FSCK_ERROR, "invalid author/committer line - date causes integer overflow");
265 if (end == *ident || *end != ' ')
266 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date");
267 *ident = end + 1;
268 if ((**ident != '+' && **ident != '-') ||
269 !isdigit((*ident)[1]) ||
270 !isdigit((*ident)[2]) ||
271 !isdigit((*ident)[3]) ||
272 !isdigit((*ident)[4]) ||
273 ((*ident)[5] != '\n'))
274 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad time zone");
275 (*ident) += 6;
276 return 0;
279 static int fsck_commit_buffer(struct commit *commit, const char *buffer,
280 fsck_error error_func)
282 const char *tmp;
283 unsigned char tree_sha1[20], sha1[20];
284 struct commit_graft *graft;
285 int parents = 0;
286 int err;
288 buffer = skip_prefix(buffer, "tree ");
289 if (!buffer)
290 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
291 if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n')
292 return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");
293 buffer += 41;
294 while ((tmp = skip_prefix(buffer, "parent "))) {
295 buffer = tmp;
296 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
297 return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
298 buffer += 41;
299 parents++;
301 graft = lookup_commit_graft(commit->object.sha1);
302 if (graft) {
303 struct commit_list *p = commit->parents;
304 parents = 0;
305 while (p) {
306 p = p->next;
307 parents++;
309 if (graft->nr_parent == -1 && !parents)
310 ; /* shallow commit */
311 else if (graft->nr_parent != parents)
312 return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
313 } else {
314 struct commit_list *p = commit->parents;
315 while (p && parents) {
316 p = p->next;
317 parents--;
319 if (p || parents)
320 return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
322 buffer = skip_prefix(buffer, "author ");
323 if (!buffer)
324 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");
325 err = fsck_ident(&buffer, &commit->object, error_func);
326 if (err)
327 return err;
328 buffer = skip_prefix(buffer, "committer ");
329 if (!buffer)
330 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'committer' line");
331 err = fsck_ident(&buffer, &commit->object, error_func);
332 if (err)
333 return err;
334 if (!commit->tree)
335 return error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
337 return 0;
340 static int fsck_commit(struct commit *commit, fsck_error error_func)
342 const char *buffer = get_commit_buffer(commit, NULL);
343 int ret = fsck_commit_buffer(commit, buffer, error_func);
344 unuse_commit_buffer(commit, buffer);
345 return ret;
348 static int fsck_tag(struct tag *tag, fsck_error error_func)
350 struct object *tagged = tag->tagged;
352 if (!tagged)
353 return error_func(&tag->object, FSCK_ERROR, "could not load tagged object");
354 return 0;
357 int fsck_object(struct object *obj, int strict, fsck_error error_func)
359 if (!obj)
360 return error_func(obj, FSCK_ERROR, "no valid object to fsck");
362 if (obj->type == OBJ_BLOB)
363 return 0;
364 if (obj->type == OBJ_TREE)
365 return fsck_tree((struct tree *) obj, strict, error_func);
366 if (obj->type == OBJ_COMMIT)
367 return fsck_commit((struct commit *) obj, error_func);
368 if (obj->type == OBJ_TAG)
369 return fsck_tag((struct tag *) obj, error_func);
371 return error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)",
372 obj->type);
375 int fsck_error_function(struct object *obj, int type, const char *fmt, ...)
377 va_list ap;
378 struct strbuf sb = STRBUF_INIT;
380 strbuf_addf(&sb, "object %s:", sha1_to_hex(obj->sha1));
382 va_start(ap, fmt);
383 strbuf_vaddf(&sb, fmt, ap);
384 va_end(ap);
386 error("%s", sb.buf);
387 strbuf_release(&sb);
388 return 1;