]>
Commit | Line | Data |
---|---|---|
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" | |
9 | #include "refs.h" | |
10 | #include "utf8.h" | |
11 | #include "sha1-array.h" | |
12 | #include "decorate.h" | |
13 | ||
14 | #define FSCK_FATAL -1 | |
15 | #define FSCK_INFO -2 | |
16 | ||
17 | #define FOREACH_MSG_ID(FUNC) \ | |
18 | /* fatal errors */ \ | |
19 | FUNC(NUL_IN_HEADER, FATAL) \ | |
20 | FUNC(UNTERMINATED_HEADER, FATAL) \ | |
21 | /* errors */ \ | |
22 | FUNC(BAD_DATE, ERROR) \ | |
23 | FUNC(BAD_DATE_OVERFLOW, ERROR) \ | |
24 | FUNC(BAD_EMAIL, ERROR) \ | |
25 | FUNC(BAD_NAME, ERROR) \ | |
26 | FUNC(BAD_OBJECT_SHA1, ERROR) \ | |
27 | FUNC(BAD_PARENT_SHA1, ERROR) \ | |
28 | FUNC(BAD_TAG_OBJECT, ERROR) \ | |
29 | FUNC(BAD_TIMEZONE, ERROR) \ | |
30 | FUNC(BAD_TREE, ERROR) \ | |
31 | FUNC(BAD_TREE_SHA1, ERROR) \ | |
32 | FUNC(BAD_TYPE, ERROR) \ | |
33 | FUNC(DUPLICATE_ENTRIES, ERROR) \ | |
34 | FUNC(MISSING_AUTHOR, ERROR) \ | |
35 | FUNC(MISSING_COMMITTER, ERROR) \ | |
36 | FUNC(MISSING_EMAIL, ERROR) \ | |
37 | FUNC(MISSING_GRAFT, ERROR) \ | |
38 | FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \ | |
39 | FUNC(MISSING_OBJECT, ERROR) \ | |
40 | FUNC(MISSING_PARENT, ERROR) \ | |
41 | FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \ | |
42 | FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \ | |
43 | FUNC(MISSING_TAG, ERROR) \ | |
44 | FUNC(MISSING_TAG_ENTRY, ERROR) \ | |
45 | FUNC(MISSING_TAG_OBJECT, ERROR) \ | |
46 | FUNC(MISSING_TREE, ERROR) \ | |
47 | FUNC(MISSING_TYPE, ERROR) \ | |
48 | FUNC(MISSING_TYPE_ENTRY, ERROR) \ | |
49 | FUNC(MULTIPLE_AUTHORS, ERROR) \ | |
50 | FUNC(TAG_OBJECT_NOT_TAG, ERROR) \ | |
51 | FUNC(TREE_NOT_SORTED, ERROR) \ | |
52 | FUNC(UNKNOWN_TYPE, ERROR) \ | |
53 | FUNC(ZERO_PADDED_DATE, ERROR) \ | |
54 | /* warnings */ \ | |
55 | FUNC(BAD_FILEMODE, WARN) \ | |
56 | FUNC(EMPTY_NAME, WARN) \ | |
57 | FUNC(FULL_PATHNAME, WARN) \ | |
58 | FUNC(HAS_DOT, WARN) \ | |
59 | FUNC(HAS_DOTDOT, WARN) \ | |
60 | FUNC(HAS_DOTGIT, WARN) \ | |
61 | FUNC(NULL_SHA1, WARN) \ | |
62 | FUNC(ZERO_PADDED_FILEMODE, WARN) \ | |
63 | FUNC(NUL_IN_COMMIT, WARN) \ | |
64 | /* infos (reported as warnings, but ignored by default) */ \ | |
65 | FUNC(BAD_TAG_NAME, INFO) \ | |
66 | FUNC(MISSING_TAGGER_ENTRY, INFO) | |
67 | ||
68 | #define MSG_ID(id, msg_type) FSCK_MSG_##id, | |
69 | enum fsck_msg_id { | |
70 | FOREACH_MSG_ID(MSG_ID) | |
71 | FSCK_MSG_MAX | |
72 | }; | |
73 | #undef MSG_ID | |
74 | ||
75 | #define STR(x) #x | |
76 | #define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type }, | |
77 | static struct { | |
78 | const char *id_string; | |
79 | const char *downcased; | |
80 | int msg_type; | |
81 | } msg_id_info[FSCK_MSG_MAX + 1] = { | |
82 | FOREACH_MSG_ID(MSG_ID) | |
83 | { NULL, NULL, -1 } | |
84 | }; | |
85 | #undef MSG_ID | |
86 | ||
87 | static void prepare_msg_ids(void) | |
88 | { | |
89 | int i; | |
90 | ||
91 | if (msg_id_info[0].downcased) | |
92 | return; | |
93 | ||
94 | /* convert id_string to lower case, without underscores. */ | |
95 | for (i = 0; i < FSCK_MSG_MAX; i++) { | |
96 | const char *p = msg_id_info[i].id_string; | |
97 | int len = strlen(p); | |
98 | char *q = xmalloc(len); | |
99 | ||
100 | msg_id_info[i].downcased = q; | |
101 | while (*p) | |
102 | if (*p == '_') | |
103 | p++; | |
104 | else | |
105 | *(q)++ = tolower(*(p)++); | |
106 | *q = '\0'; | |
107 | } | |
108 | } | |
109 | ||
110 | static int parse_msg_id(const char *text) | |
111 | { | |
112 | int i; | |
113 | ||
114 | prepare_msg_ids(); | |
115 | ||
116 | for (i = 0; i < FSCK_MSG_MAX; i++) | |
117 | if (!strcmp(text, msg_id_info[i].downcased)) | |
118 | return i; | |
119 | ||
120 | return -1; | |
121 | } | |
122 | ||
123 | static int fsck_msg_type(enum fsck_msg_id msg_id, | |
124 | struct fsck_options *options) | |
125 | { | |
126 | int msg_type; | |
127 | ||
128 | assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); | |
129 | ||
130 | if (options->msg_type) | |
131 | msg_type = options->msg_type[msg_id]; | |
132 | else { | |
133 | msg_type = msg_id_info[msg_id].msg_type; | |
134 | if (options->strict && msg_type == FSCK_WARN) | |
135 | msg_type = FSCK_ERROR; | |
136 | } | |
137 | ||
138 | return msg_type; | |
139 | } | |
140 | ||
141 | static void init_skiplist(struct fsck_options *options, const char *path) | |
142 | { | |
143 | static struct oid_array skiplist = OID_ARRAY_INIT; | |
144 | int sorted, fd; | |
145 | char buffer[GIT_MAX_HEXSZ + 1]; | |
146 | struct object_id oid; | |
147 | ||
148 | if (options->skiplist) | |
149 | sorted = options->skiplist->sorted; | |
150 | else { | |
151 | sorted = 1; | |
152 | options->skiplist = &skiplist; | |
153 | } | |
154 | ||
155 | fd = open(path, O_RDONLY); | |
156 | if (fd < 0) | |
157 | die("Could not open skip list: %s", path); | |
158 | for (;;) { | |
159 | const char *p; | |
160 | int result = read_in_full(fd, buffer, sizeof(buffer)); | |
161 | if (result < 0) | |
162 | die_errno("Could not read '%s'", path); | |
163 | if (!result) | |
164 | break; | |
165 | if (parse_oid_hex(buffer, &oid, &p) || *p != '\n') | |
166 | die("Invalid SHA-1: %s", buffer); | |
167 | oid_array_append(&skiplist, &oid); | |
168 | if (sorted && skiplist.nr > 1 && | |
169 | oidcmp(&skiplist.oid[skiplist.nr - 2], | |
170 | &oid) > 0) | |
171 | sorted = 0; | |
172 | } | |
173 | close(fd); | |
174 | ||
175 | if (sorted) | |
176 | skiplist.sorted = 1; | |
177 | } | |
178 | ||
179 | static int parse_msg_type(const char *str) | |
180 | { | |
181 | if (!strcmp(str, "error")) | |
182 | return FSCK_ERROR; | |
183 | else if (!strcmp(str, "warn")) | |
184 | return FSCK_WARN; | |
185 | else if (!strcmp(str, "ignore")) | |
186 | return FSCK_IGNORE; | |
187 | else | |
188 | die("Unknown fsck message type: '%s'", str); | |
189 | } | |
190 | ||
191 | int is_valid_msg_type(const char *msg_id, const char *msg_type) | |
192 | { | |
193 | if (parse_msg_id(msg_id) < 0) | |
194 | return 0; | |
195 | parse_msg_type(msg_type); | |
196 | return 1; | |
197 | } | |
198 | ||
199 | void fsck_set_msg_type(struct fsck_options *options, | |
200 | const char *msg_id, const char *msg_type) | |
201 | { | |
202 | int id = parse_msg_id(msg_id), type; | |
203 | ||
204 | if (id < 0) | |
205 | die("Unhandled message id: %s", msg_id); | |
206 | type = parse_msg_type(msg_type); | |
207 | ||
208 | if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL) | |
209 | die("Cannot demote %s to %s", msg_id, msg_type); | |
210 | ||
211 | if (!options->msg_type) { | |
212 | int i; | |
213 | int *msg_type; | |
214 | ALLOC_ARRAY(msg_type, FSCK_MSG_MAX); | |
215 | for (i = 0; i < FSCK_MSG_MAX; i++) | |
216 | msg_type[i] = fsck_msg_type(i, options); | |
217 | options->msg_type = msg_type; | |
218 | } | |
219 | ||
220 | options->msg_type[id] = type; | |
221 | } | |
222 | ||
223 | void fsck_set_msg_types(struct fsck_options *options, const char *values) | |
224 | { | |
225 | char *buf = xstrdup(values), *to_free = buf; | |
226 | int done = 0; | |
227 | ||
228 | while (!done) { | |
229 | int len = strcspn(buf, " ,|"), equal; | |
230 | ||
231 | done = !buf[len]; | |
232 | if (!len) { | |
233 | buf++; | |
234 | continue; | |
235 | } | |
236 | buf[len] = '\0'; | |
237 | ||
238 | for (equal = 0; | |
239 | equal < len && buf[equal] != '=' && buf[equal] != ':'; | |
240 | equal++) | |
241 | buf[equal] = tolower(buf[equal]); | |
242 | buf[equal] = '\0'; | |
243 | ||
244 | if (!strcmp(buf, "skiplist")) { | |
245 | if (equal == len) | |
246 | die("skiplist requires a path"); | |
247 | init_skiplist(options, buf + equal + 1); | |
248 | buf += len + 1; | |
249 | continue; | |
250 | } | |
251 | ||
252 | if (equal == len) | |
253 | die("Missing '=': '%s'", buf); | |
254 | ||
255 | fsck_set_msg_type(options, buf, buf + equal + 1); | |
256 | buf += len + 1; | |
257 | } | |
258 | free(to_free); | |
259 | } | |
260 | ||
261 | static void append_msg_id(struct strbuf *sb, const char *msg_id) | |
262 | { | |
263 | for (;;) { | |
264 | char c = *(msg_id)++; | |
265 | ||
266 | if (!c) | |
267 | break; | |
268 | if (c != '_') | |
269 | strbuf_addch(sb, tolower(c)); | |
270 | else { | |
271 | assert(*msg_id); | |
272 | strbuf_addch(sb, *(msg_id)++); | |
273 | } | |
274 | } | |
275 | ||
276 | strbuf_addstr(sb, ": "); | |
277 | } | |
278 | ||
279 | __attribute__((format (printf, 4, 5))) | |
280 | static int report(struct fsck_options *options, struct object *object, | |
281 | enum fsck_msg_id id, const char *fmt, ...) | |
282 | { | |
283 | va_list ap; | |
284 | struct strbuf sb = STRBUF_INIT; | |
285 | int msg_type = fsck_msg_type(id, options), result; | |
286 | ||
287 | if (msg_type == FSCK_IGNORE) | |
288 | return 0; | |
289 | ||
290 | if (options->skiplist && object && | |
291 | oid_array_lookup(options->skiplist, &object->oid) >= 0) | |
292 | return 0; | |
293 | ||
294 | if (msg_type == FSCK_FATAL) | |
295 | msg_type = FSCK_ERROR; | |
296 | else if (msg_type == FSCK_INFO) | |
297 | msg_type = FSCK_WARN; | |
298 | ||
299 | append_msg_id(&sb, msg_id_info[id].id_string); | |
300 | ||
301 | va_start(ap, fmt); | |
302 | strbuf_vaddf(&sb, fmt, ap); | |
303 | result = options->error_func(options, object, msg_type, sb.buf); | |
304 | strbuf_release(&sb); | |
305 | va_end(ap); | |
306 | ||
307 | return result; | |
308 | } | |
309 | ||
310 | static char *get_object_name(struct fsck_options *options, struct object *obj) | |
311 | { | |
312 | if (!options->object_names) | |
313 | return NULL; | |
314 | return lookup_decoration(options->object_names, obj); | |
315 | } | |
316 | ||
317 | static void put_object_name(struct fsck_options *options, struct object *obj, | |
318 | const char *fmt, ...) | |
319 | { | |
320 | va_list ap; | |
321 | struct strbuf buf = STRBUF_INIT; | |
322 | char *existing; | |
323 | ||
324 | if (!options->object_names) | |
325 | return; | |
326 | existing = lookup_decoration(options->object_names, obj); | |
327 | if (existing) | |
328 | return; | |
329 | va_start(ap, fmt); | |
330 | strbuf_vaddf(&buf, fmt, ap); | |
331 | add_decoration(options->object_names, obj, strbuf_detach(&buf, NULL)); | |
332 | va_end(ap); | |
333 | } | |
334 | ||
335 | static const char *describe_object(struct fsck_options *o, struct object *obj) | |
336 | { | |
337 | static struct strbuf buf = STRBUF_INIT; | |
338 | char *name; | |
339 | ||
340 | strbuf_reset(&buf); | |
341 | strbuf_addstr(&buf, oid_to_hex(&obj->oid)); | |
342 | if (o->object_names && (name = lookup_decoration(o->object_names, obj))) | |
343 | strbuf_addf(&buf, " (%s)", name); | |
344 | ||
345 | return buf.buf; | |
346 | } | |
347 | ||
348 | static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options) | |
349 | { | |
350 | struct tree_desc desc; | |
351 | struct name_entry entry; | |
352 | int res = 0; | |
353 | const char *name; | |
354 | ||
355 | if (parse_tree(tree)) | |
356 | return -1; | |
357 | ||
358 | name = get_object_name(options, &tree->object); | |
359 | if (init_tree_desc_gently(&desc, tree->buffer, tree->size)) | |
360 | return -1; | |
361 | while (tree_entry_gently(&desc, &entry)) { | |
362 | struct object *obj; | |
363 | int result; | |
364 | ||
365 | if (S_ISGITLINK(entry.mode)) | |
366 | continue; | |
367 | ||
368 | if (S_ISDIR(entry.mode)) { | |
369 | obj = (struct object *)lookup_tree(entry.oid); | |
370 | if (name && obj) | |
371 | put_object_name(options, obj, "%s%s/", name, | |
372 | entry.path); | |
373 | result = options->walk(obj, OBJ_TREE, data, options); | |
374 | } | |
375 | else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) { | |
376 | obj = (struct object *)lookup_blob(entry.oid); | |
377 | if (name && obj) | |
378 | put_object_name(options, obj, "%s%s", name, | |
379 | entry.path); | |
380 | result = options->walk(obj, OBJ_BLOB, data, options); | |
381 | } | |
382 | else { | |
383 | result = error("in tree %s: entry %s has bad mode %.6o", | |
384 | describe_object(options, &tree->object), entry.path, entry.mode); | |
385 | } | |
386 | if (result < 0) | |
387 | return result; | |
388 | if (!res) | |
389 | res = result; | |
390 | } | |
391 | return res; | |
392 | } | |
393 | ||
394 | static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options) | |
395 | { | |
396 | int counter = 0, generation = 0, name_prefix_len = 0; | |
397 | struct commit_list *parents; | |
398 | int res; | |
399 | int result; | |
400 | const char *name; | |
401 | ||
402 | if (parse_commit(commit)) | |
403 | return -1; | |
404 | ||
405 | name = get_object_name(options, &commit->object); | |
406 | if (name) | |
407 | put_object_name(options, &get_commit_tree(commit)->object, | |
408 | "%s:", name); | |
409 | ||
410 | result = options->walk((struct object *)get_commit_tree(commit), | |
411 | OBJ_TREE, data, options); | |
412 | if (result < 0) | |
413 | return result; | |
414 | res = result; | |
415 | ||
416 | parents = commit->parents; | |
417 | if (name && parents) { | |
418 | int len = strlen(name), power; | |
419 | ||
420 | if (len && name[len - 1] == '^') { | |
421 | generation = 1; | |
422 | name_prefix_len = len - 1; | |
423 | } | |
424 | else { /* parse ~<generation> suffix */ | |
425 | for (generation = 0, power = 1; | |
426 | len && isdigit(name[len - 1]); | |
427 | power *= 10) | |
428 | generation += power * (name[--len] - '0'); | |
429 | if (power > 1 && len && name[len - 1] == '~') | |
430 | name_prefix_len = len - 1; | |
431 | } | |
432 | } | |
433 | ||
434 | while (parents) { | |
435 | if (name) { | |
436 | struct object *obj = &parents->item->object; | |
437 | ||
438 | if (++counter > 1) | |
439 | put_object_name(options, obj, "%s^%d", | |
440 | name, counter); | |
441 | else if (generation > 0) | |
442 | put_object_name(options, obj, "%.*s~%d", | |
443 | name_prefix_len, name, generation + 1); | |
444 | else | |
445 | put_object_name(options, obj, "%s^", name); | |
446 | } | |
447 | result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options); | |
448 | if (result < 0) | |
449 | return result; | |
450 | if (!res) | |
451 | res = result; | |
452 | parents = parents->next; | |
453 | } | |
454 | return res; | |
455 | } | |
456 | ||
457 | static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options) | |
458 | { | |
459 | char *name = get_object_name(options, &tag->object); | |
460 | ||
461 | if (parse_tag(tag)) | |
462 | return -1; | |
463 | if (name) | |
464 | put_object_name(options, tag->tagged, "%s", name); | |
465 | return options->walk(tag->tagged, OBJ_ANY, data, options); | |
466 | } | |
467 | ||
468 | int fsck_walk(struct object *obj, void *data, struct fsck_options *options) | |
469 | { | |
470 | if (!obj) | |
471 | return -1; | |
472 | ||
473 | if (obj->type == OBJ_NONE) | |
474 | parse_object(&obj->oid); | |
475 | ||
476 | switch (obj->type) { | |
477 | case OBJ_BLOB: | |
478 | return 0; | |
479 | case OBJ_TREE: | |
480 | return fsck_walk_tree((struct tree *)obj, data, options); | |
481 | case OBJ_COMMIT: | |
482 | return fsck_walk_commit((struct commit *)obj, data, options); | |
483 | case OBJ_TAG: | |
484 | return fsck_walk_tag((struct tag *)obj, data, options); | |
485 | default: | |
486 | error("Unknown object type for %s", describe_object(options, obj)); | |
487 | return -1; | |
488 | } | |
489 | } | |
490 | ||
491 | /* | |
492 | * The entries in a tree are ordered in the _path_ order, | |
493 | * which means that a directory entry is ordered by adding | |
494 | * a slash to the end of it. | |
495 | * | |
496 | * So a directory called "a" is ordered _after_ a file | |
497 | * called "a.c", because "a/" sorts after "a.c". | |
498 | */ | |
499 | #define TREE_UNORDERED (-1) | |
500 | #define TREE_HAS_DUPS (-2) | |
501 | ||
502 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) | |
503 | { | |
504 | int len1 = strlen(name1); | |
505 | int len2 = strlen(name2); | |
506 | int len = len1 < len2 ? len1 : len2; | |
507 | unsigned char c1, c2; | |
508 | int cmp; | |
509 | ||
510 | cmp = memcmp(name1, name2, len); | |
511 | if (cmp < 0) | |
512 | return 0; | |
513 | if (cmp > 0) | |
514 | return TREE_UNORDERED; | |
515 | ||
516 | /* | |
517 | * Ok, the first <len> characters are the same. | |
518 | * Now we need to order the next one, but turn | |
519 | * a '\0' into a '/' for a directory entry. | |
520 | */ | |
521 | c1 = name1[len]; | |
522 | c2 = name2[len]; | |
523 | if (!c1 && !c2) | |
524 | /* | |
525 | * git-write-tree used to write out a nonsense tree that has | |
526 | * entries with the same name, one blob and one tree. Make | |
527 | * sure we do not have duplicate entries. | |
528 | */ | |
529 | return TREE_HAS_DUPS; | |
530 | if (!c1 && S_ISDIR(mode1)) | |
531 | c1 = '/'; | |
532 | if (!c2 && S_ISDIR(mode2)) | |
533 | c2 = '/'; | |
534 | return c1 < c2 ? 0 : TREE_UNORDERED; | |
535 | } | |
536 | ||
537 | static int fsck_tree(struct tree *item, struct fsck_options *options) | |
538 | { | |
539 | int retval = 0; | |
540 | int has_null_sha1 = 0; | |
541 | int has_full_path = 0; | |
542 | int has_empty_name = 0; | |
543 | int has_dot = 0; | |
544 | int has_dotdot = 0; | |
545 | int has_dotgit = 0; | |
546 | int has_zero_pad = 0; | |
547 | int has_bad_modes = 0; | |
548 | int has_dup_entries = 0; | |
549 | int not_properly_sorted = 0; | |
550 | struct tree_desc desc; | |
551 | unsigned o_mode; | |
552 | const char *o_name; | |
553 | ||
554 | if (init_tree_desc_gently(&desc, item->buffer, item->size)) { | |
555 | retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree"); | |
556 | return retval; | |
557 | } | |
558 | ||
559 | o_mode = 0; | |
560 | o_name = NULL; | |
561 | ||
562 | while (desc.size) { | |
563 | unsigned mode; | |
564 | const char *name; | |
565 | const struct object_id *oid; | |
566 | ||
567 | oid = tree_entry_extract(&desc, &name, &mode); | |
568 | ||
569 | has_null_sha1 |= is_null_oid(oid); | |
570 | has_full_path |= !!strchr(name, '/'); | |
571 | has_empty_name |= !*name; | |
572 | has_dot |= !strcmp(name, "."); | |
573 | has_dotdot |= !strcmp(name, ".."); | |
574 | has_dotgit |= (!strcmp(name, ".git") || | |
575 | is_hfs_dotgit(name) || | |
576 | is_ntfs_dotgit(name)); | |
577 | has_zero_pad |= *(char *)desc.buffer == '0'; | |
578 | if (update_tree_entry_gently(&desc)) { | |
579 | retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree"); | |
580 | break; | |
581 | } | |
582 | ||
583 | switch (mode) { | |
584 | /* | |
585 | * Standard modes.. | |
586 | */ | |
587 | case S_IFREG | 0755: | |
588 | case S_IFREG | 0644: | |
589 | case S_IFLNK: | |
590 | case S_IFDIR: | |
591 | case S_IFGITLINK: | |
592 | break; | |
593 | /* | |
594 | * This is nonstandard, but we had a few of these | |
595 | * early on when we honored the full set of mode | |
596 | * bits.. | |
597 | */ | |
598 | case S_IFREG | 0664: | |
599 | if (!options->strict) | |
600 | break; | |
601 | /* fallthrough */ | |
602 | default: | |
603 | has_bad_modes = 1; | |
604 | } | |
605 | ||
606 | if (o_name) { | |
607 | switch (verify_ordered(o_mode, o_name, mode, name)) { | |
608 | case TREE_UNORDERED: | |
609 | not_properly_sorted = 1; | |
610 | break; | |
611 | case TREE_HAS_DUPS: | |
612 | has_dup_entries = 1; | |
613 | break; | |
614 | default: | |
615 | break; | |
616 | } | |
617 | } | |
618 | ||
619 | o_mode = mode; | |
620 | o_name = name; | |
621 | } | |
622 | ||
623 | if (has_null_sha1) | |
624 | retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1"); | |
625 | if (has_full_path) | |
626 | retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames"); | |
627 | if (has_empty_name) | |
628 | retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname"); | |
629 | if (has_dot) | |
630 | retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'"); | |
631 | if (has_dotdot) | |
632 | retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'"); | |
633 | if (has_dotgit) | |
634 | retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'"); | |
635 | if (has_zero_pad) | |
636 | retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes"); | |
637 | if (has_bad_modes) | |
638 | retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes"); | |
639 | if (has_dup_entries) | |
640 | retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries"); | |
641 | if (not_properly_sorted) | |
642 | retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted"); | |
643 | return retval; | |
644 | } | |
645 | ||
646 | static int verify_headers(const void *data, unsigned long size, | |
647 | struct object *obj, struct fsck_options *options) | |
648 | { | |
649 | const char *buffer = (const char *)data; | |
650 | unsigned long i; | |
651 | ||
652 | for (i = 0; i < size; i++) { | |
653 | switch (buffer[i]) { | |
654 | case '\0': | |
655 | return report(options, obj, | |
656 | FSCK_MSG_NUL_IN_HEADER, | |
657 | "unterminated header: NUL at offset %ld", i); | |
658 | case '\n': | |
659 | if (i + 1 < size && buffer[i + 1] == '\n') | |
660 | return 0; | |
661 | } | |
662 | } | |
663 | ||
664 | /* | |
665 | * We did not find double-LF that separates the header | |
666 | * and the body. Not having a body is not a crime but | |
667 | * we do want to see the terminating LF for the last header | |
668 | * line. | |
669 | */ | |
670 | if (size && buffer[size - 1] == '\n') | |
671 | return 0; | |
672 | ||
673 | return report(options, obj, | |
674 | FSCK_MSG_UNTERMINATED_HEADER, "unterminated header"); | |
675 | } | |
676 | ||
677 | static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options) | |
678 | { | |
679 | const char *p = *ident; | |
680 | char *end; | |
681 | ||
682 | *ident = strchrnul(*ident, '\n'); | |
683 | if (**ident == '\n') | |
684 | (*ident)++; | |
685 | ||
686 | if (*p == '<') | |
687 | return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); | |
688 | p += strcspn(p, "<>\n"); | |
689 | if (*p == '>') | |
690 | return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name"); | |
691 | if (*p != '<') | |
692 | return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email"); | |
693 | if (p[-1] != ' ') | |
694 | return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); | |
695 | p++; | |
696 | p += strcspn(p, "<>\n"); | |
697 | if (*p != '>') | |
698 | return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email"); | |
699 | p++; | |
700 | if (*p != ' ') | |
701 | return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date"); | |
702 | p++; | |
703 | if (*p == '0' && p[1] != ' ') | |
704 | return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date"); | |
705 | if (date_overflows(parse_timestamp(p, &end, 10))) | |
706 | return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow"); | |
707 | if ((end == p || *end != ' ')) | |
708 | return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date"); | |
709 | p = end + 1; | |
710 | if ((*p != '+' && *p != '-') || | |
711 | !isdigit(p[1]) || | |
712 | !isdigit(p[2]) || | |
713 | !isdigit(p[3]) || | |
714 | !isdigit(p[4]) || | |
715 | (p[5] != '\n')) | |
716 | return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone"); | |
717 | p += 6; | |
718 | return 0; | |
719 | } | |
720 | ||
721 | static int fsck_commit_buffer(struct commit *commit, const char *buffer, | |
722 | unsigned long size, struct fsck_options *options) | |
723 | { | |
724 | unsigned char tree_sha1[20], sha1[20]; | |
725 | struct commit_graft *graft; | |
726 | unsigned parent_count, parent_line_count = 0, author_count; | |
727 | int err; | |
728 | const char *buffer_begin = buffer; | |
729 | ||
730 | if (verify_headers(buffer, size, &commit->object, options)) | |
731 | return -1; | |
732 | ||
733 | if (!skip_prefix(buffer, "tree ", &buffer)) | |
734 | return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line"); | |
735 | if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n') { | |
736 | err = report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1"); | |
737 | if (err) | |
738 | return err; | |
739 | } | |
740 | buffer += 41; | |
741 | while (skip_prefix(buffer, "parent ", &buffer)) { | |
742 | if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') { | |
743 | err = report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1"); | |
744 | if (err) | |
745 | return err; | |
746 | } | |
747 | buffer += 41; | |
748 | parent_line_count++; | |
749 | } | |
750 | graft = lookup_commit_graft(&commit->object.oid); | |
751 | parent_count = commit_list_count(commit->parents); | |
752 | if (graft) { | |
753 | if (graft->nr_parent == -1 && !parent_count) | |
754 | ; /* shallow commit */ | |
755 | else if (graft->nr_parent != parent_count) { | |
756 | err = report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing"); | |
757 | if (err) | |
758 | return err; | |
759 | } | |
760 | } else { | |
761 | if (parent_count != parent_line_count) { | |
762 | err = report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing"); | |
763 | if (err) | |
764 | return err; | |
765 | } | |
766 | } | |
767 | author_count = 0; | |
768 | while (skip_prefix(buffer, "author ", &buffer)) { | |
769 | author_count++; | |
770 | err = fsck_ident(&buffer, &commit->object, options); | |
771 | if (err) | |
772 | return err; | |
773 | } | |
774 | if (author_count < 1) | |
775 | err = report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line"); | |
776 | else if (author_count > 1) | |
777 | err = report(options, &commit->object, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines"); | |
778 | if (err) | |
779 | return err; | |
780 | if (!skip_prefix(buffer, "committer ", &buffer)) | |
781 | return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line"); | |
782 | err = fsck_ident(&buffer, &commit->object, options); | |
783 | if (err) | |
784 | return err; | |
785 | if (!get_commit_tree(commit)) { | |
786 | err = report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1)); | |
787 | if (err) | |
788 | return err; | |
789 | } | |
790 | if (memchr(buffer_begin, '\0', size)) { | |
791 | err = report(options, &commit->object, FSCK_MSG_NUL_IN_COMMIT, | |
792 | "NUL byte in the commit object body"); | |
793 | if (err) | |
794 | return err; | |
795 | } | |
796 | return 0; | |
797 | } | |
798 | ||
799 | static int fsck_commit(struct commit *commit, const char *data, | |
800 | unsigned long size, struct fsck_options *options) | |
801 | { | |
802 | const char *buffer = data ? data : get_commit_buffer(commit, &size); | |
803 | int ret = fsck_commit_buffer(commit, buffer, size, options); | |
804 | if (!data) | |
805 | unuse_commit_buffer(commit, buffer); | |
806 | return ret; | |
807 | } | |
808 | ||
809 | static int fsck_tag_buffer(struct tag *tag, const char *data, | |
810 | unsigned long size, struct fsck_options *options) | |
811 | { | |
812 | unsigned char sha1[20]; | |
813 | int ret = 0; | |
814 | const char *buffer; | |
815 | char *to_free = NULL, *eol; | |
816 | struct strbuf sb = STRBUF_INIT; | |
817 | ||
818 | if (data) | |
819 | buffer = data; | |
820 | else { | |
821 | enum object_type type; | |
822 | ||
823 | buffer = to_free = | |
824 | read_object_file(&tag->object.oid, &type, &size); | |
825 | if (!buffer) | |
826 | return report(options, &tag->object, | |
827 | FSCK_MSG_MISSING_TAG_OBJECT, | |
828 | "cannot read tag object"); | |
829 | ||
830 | if (type != OBJ_TAG) { | |
831 | ret = report(options, &tag->object, | |
832 | FSCK_MSG_TAG_OBJECT_NOT_TAG, | |
833 | "expected tag got %s", | |
834 | type_name(type)); | |
835 | goto done; | |
836 | } | |
837 | } | |
838 | ||
839 | ret = verify_headers(buffer, size, &tag->object, options); | |
840 | if (ret) | |
841 | goto done; | |
842 | ||
843 | if (!skip_prefix(buffer, "object ", &buffer)) { | |
844 | ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line"); | |
845 | goto done; | |
846 | } | |
847 | if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') { | |
848 | ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1"); | |
849 | if (ret) | |
850 | goto done; | |
851 | } | |
852 | buffer += 41; | |
853 | ||
854 | if (!skip_prefix(buffer, "type ", &buffer)) { | |
855 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line"); | |
856 | goto done; | |
857 | } | |
858 | eol = strchr(buffer, '\n'); | |
859 | if (!eol) { | |
860 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line"); | |
861 | goto done; | |
862 | } | |
863 | if (type_from_string_gently(buffer, eol - buffer, 1) < 0) | |
864 | ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value"); | |
865 | if (ret) | |
866 | goto done; | |
867 | buffer = eol + 1; | |
868 | ||
869 | if (!skip_prefix(buffer, "tag ", &buffer)) { | |
870 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line"); | |
871 | goto done; | |
872 | } | |
873 | eol = strchr(buffer, '\n'); | |
874 | if (!eol) { | |
875 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line"); | |
876 | goto done; | |
877 | } | |
878 | strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer); | |
879 | if (check_refname_format(sb.buf, 0)) { | |
880 | ret = report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME, | |
881 | "invalid 'tag' name: %.*s", | |
882 | (int)(eol - buffer), buffer); | |
883 | if (ret) | |
884 | goto done; | |
885 | } | |
886 | buffer = eol + 1; | |
887 | ||
888 | if (!skip_prefix(buffer, "tagger ", &buffer)) { | |
889 | /* early tags do not contain 'tagger' lines; warn only */ | |
890 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line"); | |
891 | if (ret) | |
892 | goto done; | |
893 | } | |
894 | else | |
895 | ret = fsck_ident(&buffer, &tag->object, options); | |
896 | ||
897 | done: | |
898 | strbuf_release(&sb); | |
899 | free(to_free); | |
900 | return ret; | |
901 | } | |
902 | ||
903 | static int fsck_tag(struct tag *tag, const char *data, | |
904 | unsigned long size, struct fsck_options *options) | |
905 | { | |
906 | struct object *tagged = tag->tagged; | |
907 | ||
908 | if (!tagged) | |
909 | return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object"); | |
910 | ||
911 | return fsck_tag_buffer(tag, data, size, options); | |
912 | } | |
913 | ||
914 | int fsck_object(struct object *obj, void *data, unsigned long size, | |
915 | struct fsck_options *options) | |
916 | { | |
917 | if (!obj) | |
918 | return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck"); | |
919 | ||
920 | if (obj->type == OBJ_BLOB) | |
921 | return 0; | |
922 | if (obj->type == OBJ_TREE) | |
923 | return fsck_tree((struct tree *) obj, options); | |
924 | if (obj->type == OBJ_COMMIT) | |
925 | return fsck_commit((struct commit *) obj, (const char *) data, | |
926 | size, options); | |
927 | if (obj->type == OBJ_TAG) | |
928 | return fsck_tag((struct tag *) obj, (const char *) data, | |
929 | size, options); | |
930 | ||
931 | return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)", | |
932 | obj->type); | |
933 | } | |
934 | ||
935 | int fsck_error_function(struct fsck_options *o, | |
936 | struct object *obj, int msg_type, const char *message) | |
937 | { | |
938 | if (msg_type == FSCK_WARN) { | |
939 | warning("object %s: %s", describe_object(o, obj), message); | |
940 | return 0; | |
941 | } | |
942 | error("object %s: %s", describe_object(o, obj), message); | |
943 | return 1; | |
944 | } |