]>
Commit | Line | Data |
---|---|---|
355885d5 MK |
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" | |
a18fcc9f | 9 | #include "utf8.h" |
355885d5 MK |
10 | |
11 | static int fsck_walk_tree(struct tree *tree, fsck_walk_func walk, void *data) | |
12 | { | |
13 | struct tree_desc desc; | |
14 | struct name_entry entry; | |
15 | int res = 0; | |
16 | ||
17 | if (parse_tree(tree)) | |
18 | return -1; | |
19 | ||
20 | init_tree_desc(&desc, tree->buffer, tree->size); | |
21 | while (tree_entry(&desc, &entry)) { | |
22 | int result; | |
23 | ||
24 | if (S_ISGITLINK(entry.mode)) | |
25 | continue; | |
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); | |
30 | else { | |
82247e9b | 31 | result = error("in tree %s: entry %s has bad mode %.6o", |
355885d5 MK |
32 | sha1_to_hex(tree->object.sha1), entry.path, entry.mode); |
33 | } | |
34 | if (result < 0) | |
35 | return result; | |
36 | if (!res) | |
37 | res = result; | |
38 | } | |
39 | return res; | |
40 | } | |
41 | ||
42 | static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data) | |
43 | { | |
44 | struct commit_list *parents; | |
45 | int res; | |
46 | int result; | |
47 | ||
48 | if (parse_commit(commit)) | |
49 | return -1; | |
50 | ||
51 | result = walk((struct object *)commit->tree, OBJ_TREE, data); | |
52 | if (result < 0) | |
53 | return result; | |
54 | res = result; | |
55 | ||
56 | parents = commit->parents; | |
57 | while (parents) { | |
58 | result = walk((struct object *)parents->item, OBJ_COMMIT, data); | |
59 | if (result < 0) | |
60 | return result; | |
61 | if (!res) | |
62 | res = result; | |
63 | parents = parents->next; | |
64 | } | |
65 | return res; | |
66 | } | |
67 | ||
68 | static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data) | |
69 | { | |
70 | if (parse_tag(tag)) | |
71 | return -1; | |
72 | return walk(tag->tagged, OBJ_ANY, data); | |
73 | } | |
74 | ||
75 | int fsck_walk(struct object *obj, fsck_walk_func walk, void *data) | |
76 | { | |
77 | if (!obj) | |
78 | return -1; | |
79 | switch (obj->type) { | |
80 | case OBJ_BLOB: | |
81 | return 0; | |
82 | case OBJ_TREE: | |
83 | return fsck_walk_tree((struct tree *)obj, walk, data); | |
84 | case OBJ_COMMIT: | |
85 | return fsck_walk_commit((struct commit *)obj, walk, data); | |
86 | case OBJ_TAG: | |
87 | return fsck_walk_tag((struct tag *)obj, walk, data); | |
88 | default: | |
89 | error("Unknown object type for %s", sha1_to_hex(obj->sha1)); | |
90 | return -1; | |
91 | } | |
92 | } | |
ba002f3b MK |
93 | |
94 | /* | |
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. | |
98 | * | |
99 | * So a directory called "a" is ordered _after_ a file | |
100 | * called "a.c", because "a/" sorts after "a.c". | |
101 | */ | |
102 | #define TREE_UNORDERED (-1) | |
103 | #define TREE_HAS_DUPS (-2) | |
104 | ||
105 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) | |
106 | { | |
107 | int len1 = strlen(name1); | |
108 | int len2 = strlen(name2); | |
109 | int len = len1 < len2 ? len1 : len2; | |
110 | unsigned char c1, c2; | |
111 | int cmp; | |
112 | ||
113 | cmp = memcmp(name1, name2, len); | |
114 | if (cmp < 0) | |
115 | return 0; | |
116 | if (cmp > 0) | |
117 | return TREE_UNORDERED; | |
118 | ||
119 | /* | |
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. | |
123 | */ | |
124 | c1 = name1[len]; | |
125 | c2 = name2[len]; | |
126 | if (!c1 && !c2) | |
127 | /* | |
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. | |
131 | */ | |
132 | return TREE_HAS_DUPS; | |
133 | if (!c1 && S_ISDIR(mode1)) | |
134 | c1 = '/'; | |
135 | if (!c2 && S_ISDIR(mode2)) | |
136 | c2 = '/'; | |
137 | return c1 < c2 ? 0 : TREE_UNORDERED; | |
138 | } | |
139 | ||
140 | static int fsck_tree(struct tree *item, int strict, fsck_error error_func) | |
141 | { | |
142 | int retval; | |
c479d14a | 143 | int has_null_sha1 = 0; |
ba002f3b MK |
144 | int has_full_path = 0; |
145 | int has_empty_name = 0; | |
5d34a435 JK |
146 | int has_dot = 0; |
147 | int has_dotdot = 0; | |
5c17f512 | 148 | int has_dotgit = 0; |
ba002f3b MK |
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; | |
154 | unsigned o_mode; | |
155 | const char *o_name; | |
ba002f3b MK |
156 | |
157 | init_tree_desc(&desc, item->buffer, item->size); | |
158 | ||
159 | o_mode = 0; | |
160 | o_name = NULL; | |
ba002f3b MK |
161 | |
162 | while (desc.size) { | |
163 | unsigned mode; | |
164 | const char *name; | |
c479d14a | 165 | const unsigned char *sha1; |
ba002f3b | 166 | |
c479d14a | 167 | sha1 = tree_entry_extract(&desc, &name, &mode); |
ba002f3b | 168 | |
c479d14a JK |
169 | if (is_null_sha1(sha1)) |
170 | has_null_sha1 = 1; | |
ba002f3b MK |
171 | if (strchr(name, '/')) |
172 | has_full_path = 1; | |
173 | if (!*name) | |
174 | has_empty_name = 1; | |
5d34a435 JK |
175 | if (!strcmp(name, ".")) |
176 | has_dot = 1; | |
177 | if (!strcmp(name, "..")) | |
178 | has_dotdot = 1; | |
d08c13b9 JS |
179 | if (!strcasecmp(name, ".git") || is_hfs_dotgit(name) || |
180 | is_ntfs_dotgit(name)) | |
5c17f512 | 181 | has_dotgit = 1; |
ba002f3b MK |
182 | has_zero_pad |= *(char *)desc.buffer == '0'; |
183 | update_tree_entry(&desc); | |
184 | ||
185 | switch (mode) { | |
186 | /* | |
187 | * Standard modes.. | |
188 | */ | |
189 | case S_IFREG | 0755: | |
190 | case S_IFREG | 0644: | |
191 | case S_IFLNK: | |
192 | case S_IFDIR: | |
193 | case S_IFGITLINK: | |
194 | break; | |
195 | /* | |
196 | * This is nonstandard, but we had a few of these | |
197 | * early on when we honored the full set of mode | |
198 | * bits.. | |
199 | */ | |
200 | case S_IFREG | 0664: | |
201 | if (!strict) | |
202 | break; | |
203 | default: | |
204 | has_bad_modes = 1; | |
205 | } | |
206 | ||
207 | if (o_name) { | |
208 | switch (verify_ordered(o_mode, o_name, mode, name)) { | |
209 | case TREE_UNORDERED: | |
210 | not_properly_sorted = 1; | |
211 | break; | |
212 | case TREE_HAS_DUPS: | |
213 | has_dup_entries = 1; | |
214 | break; | |
215 | default: | |
216 | break; | |
217 | } | |
218 | } | |
219 | ||
220 | o_mode = mode; | |
221 | o_name = name; | |
ba002f3b MK |
222 | } |
223 | ||
224 | retval = 0; | |
c479d14a JK |
225 | if (has_null_sha1) |
226 | retval += error_func(&item->object, FSCK_WARN, "contains entries pointing to null sha1"); | |
ba002f3b MK |
227 | if (has_full_path) |
228 | retval += error_func(&item->object, FSCK_WARN, "contains full pathnames"); | |
229 | if (has_empty_name) | |
230 | retval += error_func(&item->object, FSCK_WARN, "contains empty pathname"); | |
5d34a435 JK |
231 | if (has_dot) |
232 | retval += error_func(&item->object, FSCK_WARN, "contains '.'"); | |
233 | if (has_dotdot) | |
234 | retval += error_func(&item->object, FSCK_WARN, "contains '..'"); | |
5c17f512 JK |
235 | if (has_dotgit) |
236 | retval += error_func(&item->object, FSCK_WARN, "contains '.git'"); | |
ba002f3b MK |
237 | if (has_zero_pad) |
238 | retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes"); | |
239 | if (has_bad_modes) | |
240 | retval += error_func(&item->object, FSCK_WARN, "contains bad file modes"); | |
241 | if (has_dup_entries) | |
242 | retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries"); | |
243 | if (not_properly_sorted) | |
244 | retval += error_func(&item->object, FSCK_ERROR, "not properly sorted"); | |
245 | return retval; | |
246 | } | |
247 | ||
daae1922 JN |
248 | static int fsck_ident(char **ident, struct object *obj, fsck_error error_func) |
249 | { | |
53f53cff | 250 | if (**ident == '<') |
daae1922 | 251 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email"); |
53f53cff DI |
252 | *ident += strcspn(*ident, "<>\n"); |
253 | if (**ident == '>') | |
254 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad name"); | |
daae1922 JN |
255 | if (**ident != '<') |
256 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing email"); | |
53f53cff DI |
257 | if ((*ident)[-1] != ' ') |
258 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email"); | |
daae1922 JN |
259 | (*ident)++; |
260 | *ident += strcspn(*ident, "<>\n"); | |
261 | if (**ident != '>') | |
262 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad email"); | |
263 | (*ident)++; | |
264 | if (**ident != ' ') | |
265 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before date"); | |
266 | (*ident)++; | |
267 | if (**ident == '0' && (*ident)[1] != ' ') | |
268 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date"); | |
269 | *ident += strspn(*ident, "0123456789"); | |
270 | if (**ident != ' ') | |
271 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date"); | |
272 | (*ident)++; | |
273 | if ((**ident != '+' && **ident != '-') || | |
274 | !isdigit((*ident)[1]) || | |
275 | !isdigit((*ident)[2]) || | |
276 | !isdigit((*ident)[3]) || | |
277 | !isdigit((*ident)[4]) || | |
278 | ((*ident)[5] != '\n')) | |
279 | return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad time zone"); | |
280 | (*ident) += 6; | |
281 | return 0; | |
282 | } | |
283 | ||
ba002f3b MK |
284 | static int fsck_commit(struct commit *commit, fsck_error error_func) |
285 | { | |
286 | char *buffer = commit->buffer; | |
287 | unsigned char tree_sha1[20], sha1[20]; | |
288 | struct commit_graft *graft; | |
289 | int parents = 0; | |
daae1922 | 290 | int err; |
ba002f3b | 291 | |
f2909743 | 292 | if (commit->date == ULONG_MAX) |
ba002f3b MK |
293 | return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line"); |
294 | ||
295 | if (memcmp(buffer, "tree ", 5)) | |
296 | return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line"); | |
297 | if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n') | |
298 | return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1"); | |
299 | buffer += 46; | |
300 | while (!memcmp(buffer, "parent ", 7)) { | |
301 | if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n') | |
302 | return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1"); | |
303 | buffer += 48; | |
304 | parents++; | |
305 | } | |
306 | graft = lookup_commit_graft(commit->object.sha1); | |
307 | if (graft) { | |
308 | struct commit_list *p = commit->parents; | |
309 | parents = 0; | |
310 | while (p) { | |
311 | p = p->next; | |
312 | parents++; | |
313 | } | |
314 | if (graft->nr_parent == -1 && !parents) | |
315 | ; /* shallow commit */ | |
316 | else if (graft->nr_parent != parents) | |
317 | return error_func(&commit->object, FSCK_ERROR, "graft objects missing"); | |
318 | } else { | |
319 | struct commit_list *p = commit->parents; | |
320 | while (p && parents) { | |
321 | p = p->next; | |
322 | parents--; | |
323 | } | |
324 | if (p || parents) | |
325 | return error_func(&commit->object, FSCK_ERROR, "parent objects missing"); | |
326 | } | |
327 | if (memcmp(buffer, "author ", 7)) | |
328 | return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line"); | |
daae1922 JN |
329 | buffer += 7; |
330 | err = fsck_ident(&buffer, &commit->object, error_func); | |
331 | if (err) | |
332 | return err; | |
333 | if (memcmp(buffer, "committer ", strlen("committer "))) | |
334 | return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'committer' line"); | |
335 | buffer += strlen("committer "); | |
336 | err = fsck_ident(&buffer, &commit->object, error_func); | |
337 | if (err) | |
338 | return err; | |
ba002f3b MK |
339 | if (!commit->tree) |
340 | return error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1)); | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
345 | static int fsck_tag(struct tag *tag, fsck_error error_func) | |
346 | { | |
347 | struct object *tagged = tag->tagged; | |
348 | ||
349 | if (!tagged) | |
350 | return error_func(&tag->object, FSCK_ERROR, "could not load tagged object"); | |
351 | return 0; | |
352 | } | |
353 | ||
354 | int fsck_object(struct object *obj, int strict, fsck_error error_func) | |
355 | { | |
356 | if (!obj) | |
357 | return error_func(obj, FSCK_ERROR, "no valid object to fsck"); | |
358 | ||
359 | if (obj->type == OBJ_BLOB) | |
360 | return 0; | |
361 | if (obj->type == OBJ_TREE) | |
362 | return fsck_tree((struct tree *) obj, strict, error_func); | |
363 | if (obj->type == OBJ_COMMIT) | |
364 | return fsck_commit((struct commit *) obj, error_func); | |
365 | if (obj->type == OBJ_TAG) | |
366 | return fsck_tag((struct tag *) obj, error_func); | |
367 | ||
368 | return error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)", | |
369 | obj->type); | |
370 | } | |
d6ffc8d7 MK |
371 | |
372 | int fsck_error_function(struct object *obj, int type, const char *fmt, ...) | |
373 | { | |
374 | va_list ap; | |
f285a2d7 | 375 | struct strbuf sb = STRBUF_INIT; |
d6ffc8d7 | 376 | |
5dd56489 | 377 | strbuf_addf(&sb, "object %s:", sha1_to_hex(obj->sha1)); |
d6ffc8d7 MK |
378 | |
379 | va_start(ap, fmt); | |
ebeb6090 | 380 | strbuf_vaddf(&sb, fmt, ap); |
d6ffc8d7 MK |
381 | va_end(ap); |
382 | ||
9db56f71 | 383 | error("%s", sb.buf); |
d6ffc8d7 MK |
384 | strbuf_release(&sb); |
385 | return 1; | |
386 | } |