]>
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" | |
cec097be | 9 | #include "refs.h" |
a18fcc9f | 10 | #include "utf8.h" |
355885d5 | 11 | |
c99ba492 JS |
12 | #define FOREACH_MSG_ID(FUNC) \ |
13 | /* errors */ \ | |
14 | FUNC(BAD_DATE, ERROR) \ | |
15 | FUNC(BAD_DATE_OVERFLOW, ERROR) \ | |
16 | FUNC(BAD_EMAIL, ERROR) \ | |
17 | FUNC(BAD_NAME, ERROR) \ | |
18 | FUNC(BAD_OBJECT_SHA1, ERROR) \ | |
19 | FUNC(BAD_PARENT_SHA1, ERROR) \ | |
20 | FUNC(BAD_TAG_OBJECT, ERROR) \ | |
21 | FUNC(BAD_TIMEZONE, ERROR) \ | |
22 | FUNC(BAD_TREE, ERROR) \ | |
23 | FUNC(BAD_TREE_SHA1, ERROR) \ | |
24 | FUNC(BAD_TYPE, ERROR) \ | |
25 | FUNC(DUPLICATE_ENTRIES, ERROR) \ | |
26 | FUNC(MISSING_AUTHOR, ERROR) \ | |
27 | FUNC(MISSING_COMMITTER, ERROR) \ | |
28 | FUNC(MISSING_EMAIL, ERROR) \ | |
29 | FUNC(MISSING_GRAFT, ERROR) \ | |
30 | FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \ | |
31 | FUNC(MISSING_OBJECT, ERROR) \ | |
32 | FUNC(MISSING_PARENT, ERROR) \ | |
33 | FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \ | |
34 | FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \ | |
35 | FUNC(MISSING_TAG, ERROR) \ | |
36 | FUNC(MISSING_TAG_ENTRY, ERROR) \ | |
37 | FUNC(MISSING_TAG_OBJECT, ERROR) \ | |
38 | FUNC(MISSING_TREE, ERROR) \ | |
39 | FUNC(MISSING_TYPE, ERROR) \ | |
40 | FUNC(MISSING_TYPE_ENTRY, ERROR) \ | |
41 | FUNC(NUL_IN_HEADER, ERROR) \ | |
42 | FUNC(TAG_OBJECT_NOT_TAG, ERROR) \ | |
43 | FUNC(TREE_NOT_SORTED, ERROR) \ | |
44 | FUNC(UNKNOWN_TYPE, ERROR) \ | |
45 | FUNC(UNTERMINATED_HEADER, ERROR) \ | |
46 | FUNC(ZERO_PADDED_DATE, ERROR) \ | |
47 | /* warnings */ \ | |
48 | FUNC(BAD_FILEMODE, WARN) \ | |
49 | FUNC(BAD_TAG_NAME, WARN) \ | |
50 | FUNC(EMPTY_NAME, WARN) \ | |
51 | FUNC(FULL_PATHNAME, WARN) \ | |
52 | FUNC(HAS_DOT, WARN) \ | |
53 | FUNC(HAS_DOTDOT, WARN) \ | |
54 | FUNC(HAS_DOTGIT, WARN) \ | |
55 | FUNC(MISSING_TAGGER_ENTRY, WARN) \ | |
56 | FUNC(NULL_SHA1, WARN) \ | |
57 | FUNC(ZERO_PADDED_FILEMODE, WARN) | |
58 | ||
59 | #define MSG_ID(id, msg_type) FSCK_MSG_##id, | |
60 | enum fsck_msg_id { | |
61 | FOREACH_MSG_ID(MSG_ID) | |
62 | FSCK_MSG_MAX | |
63 | }; | |
64 | #undef MSG_ID | |
65 | ||
f417eed8 JS |
66 | #define STR(x) #x |
67 | #define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type }, | |
c99ba492 | 68 | static struct { |
f417eed8 JS |
69 | const char *id_string; |
70 | const char *downcased; | |
c99ba492 JS |
71 | int msg_type; |
72 | } msg_id_info[FSCK_MSG_MAX + 1] = { | |
73 | FOREACH_MSG_ID(MSG_ID) | |
f417eed8 | 74 | { NULL, NULL, -1 } |
c99ba492 JS |
75 | }; |
76 | #undef MSG_ID | |
77 | ||
f417eed8 JS |
78 | static int parse_msg_id(const char *text) |
79 | { | |
80 | int i; | |
81 | ||
82 | if (!msg_id_info[0].downcased) { | |
83 | /* convert id_string to lower case, without underscores. */ | |
84 | for (i = 0; i < FSCK_MSG_MAX; i++) { | |
85 | const char *p = msg_id_info[i].id_string; | |
86 | int len = strlen(p); | |
87 | char *q = xmalloc(len); | |
88 | ||
89 | msg_id_info[i].downcased = q; | |
90 | while (*p) | |
91 | if (*p == '_') | |
92 | p++; | |
93 | else | |
94 | *(q)++ = tolower(*(p)++); | |
95 | *q = '\0'; | |
96 | } | |
97 | } | |
98 | ||
99 | for (i = 0; i < FSCK_MSG_MAX; i++) | |
100 | if (!strcmp(text, msg_id_info[i].downcased)) | |
101 | return i; | |
102 | ||
103 | return -1; | |
104 | } | |
105 | ||
c99ba492 JS |
106 | static int fsck_msg_type(enum fsck_msg_id msg_id, |
107 | struct fsck_options *options) | |
108 | { | |
109 | int msg_type; | |
110 | ||
0282f4dc JS |
111 | assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); |
112 | ||
113 | if (options->msg_type) | |
114 | msg_type = options->msg_type[msg_id]; | |
115 | else { | |
116 | msg_type = msg_id_info[msg_id].msg_type; | |
117 | if (options->strict && msg_type == FSCK_WARN) | |
118 | msg_type = FSCK_ERROR; | |
119 | } | |
c99ba492 JS |
120 | |
121 | return msg_type; | |
122 | } | |
123 | ||
0282f4dc JS |
124 | static int parse_msg_type(const char *str) |
125 | { | |
126 | if (!strcmp(str, "error")) | |
127 | return FSCK_ERROR; | |
128 | else if (!strcmp(str, "warn")) | |
129 | return FSCK_WARN; | |
130 | else | |
131 | die("Unknown fsck message type: '%s'", str); | |
132 | } | |
133 | ||
5d477a33 JS |
134 | int is_valid_msg_type(const char *msg_id, const char *msg_type) |
135 | { | |
136 | if (parse_msg_id(msg_id) < 0) | |
137 | return 0; | |
138 | parse_msg_type(msg_type); | |
139 | return 1; | |
140 | } | |
141 | ||
0282f4dc JS |
142 | void fsck_set_msg_type(struct fsck_options *options, |
143 | const char *msg_id, const char *msg_type) | |
144 | { | |
145 | int id = parse_msg_id(msg_id), type; | |
146 | ||
147 | if (id < 0) | |
148 | die("Unhandled message id: %s", msg_id); | |
149 | type = parse_msg_type(msg_type); | |
150 | ||
151 | if (!options->msg_type) { | |
152 | int i; | |
153 | int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX); | |
154 | for (i = 0; i < FSCK_MSG_MAX; i++) | |
155 | msg_type[i] = fsck_msg_type(i, options); | |
156 | options->msg_type = msg_type; | |
157 | } | |
158 | ||
159 | options->msg_type[id] = type; | |
160 | } | |
161 | ||
162 | void fsck_set_msg_types(struct fsck_options *options, const char *values) | |
163 | { | |
164 | char *buf = xstrdup(values), *to_free = buf; | |
165 | int done = 0; | |
166 | ||
167 | while (!done) { | |
168 | int len = strcspn(buf, " ,|"), equal; | |
169 | ||
170 | done = !buf[len]; | |
171 | if (!len) { | |
172 | buf++; | |
173 | continue; | |
174 | } | |
175 | buf[len] = '\0'; | |
176 | ||
177 | for (equal = 0; | |
178 | equal < len && buf[equal] != '=' && buf[equal] != ':'; | |
179 | equal++) | |
180 | buf[equal] = tolower(buf[equal]); | |
181 | buf[equal] = '\0'; | |
182 | ||
183 | if (equal == len) | |
184 | die("Missing '=': '%s'", buf); | |
185 | ||
186 | fsck_set_msg_type(options, buf, buf + equal + 1); | |
187 | buf += len + 1; | |
188 | } | |
189 | free(to_free); | |
190 | } | |
191 | ||
71ab8fa8 JS |
192 | static void append_msg_id(struct strbuf *sb, const char *msg_id) |
193 | { | |
194 | for (;;) { | |
195 | char c = *(msg_id)++; | |
196 | ||
197 | if (!c) | |
198 | break; | |
199 | if (c != '_') | |
200 | strbuf_addch(sb, tolower(c)); | |
201 | else { | |
202 | assert(*msg_id); | |
203 | strbuf_addch(sb, *(msg_id)++); | |
204 | } | |
205 | } | |
206 | ||
207 | strbuf_addstr(sb, ": "); | |
208 | } | |
209 | ||
c99ba492 JS |
210 | __attribute__((format (printf, 4, 5))) |
211 | static int report(struct fsck_options *options, struct object *object, | |
212 | enum fsck_msg_id id, const char *fmt, ...) | |
213 | { | |
214 | va_list ap; | |
215 | struct strbuf sb = STRBUF_INIT; | |
216 | int msg_type = fsck_msg_type(id, options), result; | |
217 | ||
71ab8fa8 JS |
218 | append_msg_id(&sb, msg_id_info[id].id_string); |
219 | ||
c99ba492 JS |
220 | va_start(ap, fmt); |
221 | strbuf_vaddf(&sb, fmt, ap); | |
222 | result = options->error_func(object, msg_type, sb.buf); | |
223 | strbuf_release(&sb); | |
224 | va_end(ap); | |
225 | ||
226 | return result; | |
227 | } | |
228 | ||
22410549 | 229 | static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options) |
355885d5 MK |
230 | { |
231 | struct tree_desc desc; | |
232 | struct name_entry entry; | |
233 | int res = 0; | |
234 | ||
235 | if (parse_tree(tree)) | |
236 | return -1; | |
237 | ||
238 | init_tree_desc(&desc, tree->buffer, tree->size); | |
239 | while (tree_entry(&desc, &entry)) { | |
240 | int result; | |
241 | ||
242 | if (S_ISGITLINK(entry.mode)) | |
243 | continue; | |
244 | if (S_ISDIR(entry.mode)) | |
22410549 | 245 | result = options->walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data, options); |
355885d5 | 246 | else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) |
22410549 | 247 | result = options->walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data, options); |
355885d5 | 248 | else { |
82247e9b | 249 | result = error("in tree %s: entry %s has bad mode %.6o", |
355885d5 MK |
250 | sha1_to_hex(tree->object.sha1), entry.path, entry.mode); |
251 | } | |
252 | if (result < 0) | |
253 | return result; | |
254 | if (!res) | |
255 | res = result; | |
256 | } | |
257 | return res; | |
258 | } | |
259 | ||
22410549 | 260 | static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options) |
355885d5 MK |
261 | { |
262 | struct commit_list *parents; | |
263 | int res; | |
264 | int result; | |
265 | ||
266 | if (parse_commit(commit)) | |
267 | return -1; | |
268 | ||
22410549 | 269 | result = options->walk((struct object *)commit->tree, OBJ_TREE, data, options); |
355885d5 MK |
270 | if (result < 0) |
271 | return result; | |
272 | res = result; | |
273 | ||
274 | parents = commit->parents; | |
275 | while (parents) { | |
22410549 | 276 | result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options); |
355885d5 MK |
277 | if (result < 0) |
278 | return result; | |
279 | if (!res) | |
280 | res = result; | |
281 | parents = parents->next; | |
282 | } | |
283 | return res; | |
284 | } | |
285 | ||
22410549 | 286 | static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options) |
355885d5 MK |
287 | { |
288 | if (parse_tag(tag)) | |
289 | return -1; | |
22410549 | 290 | return options->walk(tag->tagged, OBJ_ANY, data, options); |
355885d5 MK |
291 | } |
292 | ||
22410549 | 293 | int fsck_walk(struct object *obj, void *data, struct fsck_options *options) |
355885d5 MK |
294 | { |
295 | if (!obj) | |
296 | return -1; | |
297 | switch (obj->type) { | |
298 | case OBJ_BLOB: | |
299 | return 0; | |
300 | case OBJ_TREE: | |
22410549 | 301 | return fsck_walk_tree((struct tree *)obj, data, options); |
355885d5 | 302 | case OBJ_COMMIT: |
22410549 | 303 | return fsck_walk_commit((struct commit *)obj, data, options); |
355885d5 | 304 | case OBJ_TAG: |
22410549 | 305 | return fsck_walk_tag((struct tag *)obj, data, options); |
355885d5 MK |
306 | default: |
307 | error("Unknown object type for %s", sha1_to_hex(obj->sha1)); | |
308 | return -1; | |
309 | } | |
310 | } | |
ba002f3b MK |
311 | |
312 | /* | |
313 | * The entries in a tree are ordered in the _path_ order, | |
314 | * which means that a directory entry is ordered by adding | |
315 | * a slash to the end of it. | |
316 | * | |
317 | * So a directory called "a" is ordered _after_ a file | |
318 | * called "a.c", because "a/" sorts after "a.c". | |
319 | */ | |
320 | #define TREE_UNORDERED (-1) | |
321 | #define TREE_HAS_DUPS (-2) | |
322 | ||
323 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) | |
324 | { | |
325 | int len1 = strlen(name1); | |
326 | int len2 = strlen(name2); | |
327 | int len = len1 < len2 ? len1 : len2; | |
328 | unsigned char c1, c2; | |
329 | int cmp; | |
330 | ||
331 | cmp = memcmp(name1, name2, len); | |
332 | if (cmp < 0) | |
333 | return 0; | |
334 | if (cmp > 0) | |
335 | return TREE_UNORDERED; | |
336 | ||
337 | /* | |
338 | * Ok, the first <len> characters are the same. | |
339 | * Now we need to order the next one, but turn | |
340 | * a '\0' into a '/' for a directory entry. | |
341 | */ | |
342 | c1 = name1[len]; | |
343 | c2 = name2[len]; | |
344 | if (!c1 && !c2) | |
345 | /* | |
346 | * git-write-tree used to write out a nonsense tree that has | |
347 | * entries with the same name, one blob and one tree. Make | |
348 | * sure we do not have duplicate entries. | |
349 | */ | |
350 | return TREE_HAS_DUPS; | |
351 | if (!c1 && S_ISDIR(mode1)) | |
352 | c1 = '/'; | |
353 | if (!c2 && S_ISDIR(mode2)) | |
354 | c2 = '/'; | |
355 | return c1 < c2 ? 0 : TREE_UNORDERED; | |
356 | } | |
357 | ||
22410549 | 358 | static int fsck_tree(struct tree *item, struct fsck_options *options) |
ba002f3b MK |
359 | { |
360 | int retval; | |
c479d14a | 361 | int has_null_sha1 = 0; |
ba002f3b MK |
362 | int has_full_path = 0; |
363 | int has_empty_name = 0; | |
5d34a435 JK |
364 | int has_dot = 0; |
365 | int has_dotdot = 0; | |
5c17f512 | 366 | int has_dotgit = 0; |
ba002f3b MK |
367 | int has_zero_pad = 0; |
368 | int has_bad_modes = 0; | |
369 | int has_dup_entries = 0; | |
370 | int not_properly_sorted = 0; | |
371 | struct tree_desc desc; | |
372 | unsigned o_mode; | |
373 | const char *o_name; | |
ba002f3b MK |
374 | |
375 | init_tree_desc(&desc, item->buffer, item->size); | |
376 | ||
377 | o_mode = 0; | |
378 | o_name = NULL; | |
ba002f3b MK |
379 | |
380 | while (desc.size) { | |
381 | unsigned mode; | |
382 | const char *name; | |
c479d14a | 383 | const unsigned char *sha1; |
ba002f3b | 384 | |
c479d14a | 385 | sha1 = tree_entry_extract(&desc, &name, &mode); |
ba002f3b | 386 | |
effd12ec HS |
387 | has_null_sha1 |= is_null_sha1(sha1); |
388 | has_full_path |= !!strchr(name, '/'); | |
389 | has_empty_name |= !*name; | |
390 | has_dot |= !strcmp(name, "."); | |
391 | has_dotdot |= !strcmp(name, ".."); | |
5e519fb8 JH |
392 | has_dotgit |= (!strcmp(name, ".git") || |
393 | is_hfs_dotgit(name) || | |
394 | is_ntfs_dotgit(name)); | |
ba002f3b MK |
395 | has_zero_pad |= *(char *)desc.buffer == '0'; |
396 | update_tree_entry(&desc); | |
397 | ||
398 | switch (mode) { | |
399 | /* | |
400 | * Standard modes.. | |
401 | */ | |
402 | case S_IFREG | 0755: | |
403 | case S_IFREG | 0644: | |
404 | case S_IFLNK: | |
405 | case S_IFDIR: | |
406 | case S_IFGITLINK: | |
407 | break; | |
408 | /* | |
409 | * This is nonstandard, but we had a few of these | |
410 | * early on when we honored the full set of mode | |
411 | * bits.. | |
412 | */ | |
413 | case S_IFREG | 0664: | |
22410549 | 414 | if (!options->strict) |
ba002f3b MK |
415 | break; |
416 | default: | |
417 | has_bad_modes = 1; | |
418 | } | |
419 | ||
420 | if (o_name) { | |
421 | switch (verify_ordered(o_mode, o_name, mode, name)) { | |
422 | case TREE_UNORDERED: | |
423 | not_properly_sorted = 1; | |
424 | break; | |
425 | case TREE_HAS_DUPS: | |
426 | has_dup_entries = 1; | |
427 | break; | |
428 | default: | |
429 | break; | |
430 | } | |
431 | } | |
432 | ||
433 | o_mode = mode; | |
434 | o_name = name; | |
ba002f3b MK |
435 | } |
436 | ||
437 | retval = 0; | |
c479d14a | 438 | if (has_null_sha1) |
c99ba492 | 439 | retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1"); |
ba002f3b | 440 | if (has_full_path) |
c99ba492 | 441 | retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames"); |
ba002f3b | 442 | if (has_empty_name) |
c99ba492 | 443 | retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname"); |
5d34a435 | 444 | if (has_dot) |
c99ba492 | 445 | retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'"); |
5d34a435 | 446 | if (has_dotdot) |
c99ba492 | 447 | retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'"); |
5c17f512 | 448 | if (has_dotgit) |
c99ba492 | 449 | retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'"); |
ba002f3b | 450 | if (has_zero_pad) |
c99ba492 | 451 | retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes"); |
ba002f3b | 452 | if (has_bad_modes) |
c99ba492 | 453 | retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes"); |
ba002f3b | 454 | if (has_dup_entries) |
c99ba492 | 455 | retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries"); |
ba002f3b | 456 | if (not_properly_sorted) |
c99ba492 | 457 | retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted"); |
ba002f3b MK |
458 | return retval; |
459 | } | |
460 | ||
4d0d8975 | 461 | static int require_end_of_header(const void *data, unsigned long size, |
22410549 | 462 | struct object *obj, struct fsck_options *options) |
4d0d8975 JS |
463 | { |
464 | const char *buffer = (const char *)data; | |
465 | unsigned long i; | |
466 | ||
467 | for (i = 0; i < size; i++) { | |
468 | switch (buffer[i]) { | |
469 | case '\0': | |
c99ba492 JS |
470 | return report(options, obj, |
471 | FSCK_MSG_NUL_IN_HEADER, | |
472 | "unterminated header: NUL at offset %ld", i); | |
4d0d8975 JS |
473 | case '\n': |
474 | if (i + 1 < size && buffer[i + 1] == '\n') | |
475 | return 0; | |
476 | } | |
477 | } | |
478 | ||
c99ba492 JS |
479 | return report(options, obj, |
480 | FSCK_MSG_UNTERMINATED_HEADER, "unterminated header"); | |
4d0d8975 JS |
481 | } |
482 | ||
22410549 | 483 | static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options) |
daae1922 | 484 | { |
e6826e33 | 485 | const char *p = *ident; |
d4b8de04 JK |
486 | char *end; |
487 | ||
e6826e33 JS |
488 | *ident = strchrnul(*ident, '\n'); |
489 | if (**ident == '\n') | |
490 | (*ident)++; | |
491 | ||
492 | if (*p == '<') | |
c99ba492 | 493 | return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
e6826e33 JS |
494 | p += strcspn(p, "<>\n"); |
495 | if (*p == '>') | |
c99ba492 | 496 | return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name"); |
e6826e33 | 497 | if (*p != '<') |
c99ba492 | 498 | return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email"); |
e6826e33 | 499 | if (p[-1] != ' ') |
c99ba492 | 500 | return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
e6826e33 JS |
501 | p++; |
502 | p += strcspn(p, "<>\n"); | |
503 | if (*p != '>') | |
c99ba492 | 504 | return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email"); |
e6826e33 JS |
505 | p++; |
506 | if (*p != ' ') | |
c99ba492 | 507 | return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date"); |
e6826e33 JS |
508 | p++; |
509 | if (*p == '0' && p[1] != ' ') | |
c99ba492 | 510 | return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date"); |
e6826e33 | 511 | if (date_overflows(strtoul(p, &end, 10))) |
c99ba492 | 512 | return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow"); |
e6826e33 | 513 | if ((end == p || *end != ' ')) |
c99ba492 | 514 | return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date"); |
e6826e33 JS |
515 | p = end + 1; |
516 | if ((*p != '+' && *p != '-') || | |
517 | !isdigit(p[1]) || | |
518 | !isdigit(p[2]) || | |
519 | !isdigit(p[3]) || | |
520 | !isdigit(p[4]) || | |
521 | (p[5] != '\n')) | |
c99ba492 | 522 | return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone"); |
e6826e33 | 523 | p += 6; |
daae1922 JN |
524 | return 0; |
525 | } | |
526 | ||
bc6b8fc1 | 527 | static int fsck_commit_buffer(struct commit *commit, const char *buffer, |
22410549 | 528 | unsigned long size, struct fsck_options *options) |
ba002f3b | 529 | { |
ba002f3b MK |
530 | unsigned char tree_sha1[20], sha1[20]; |
531 | struct commit_graft *graft; | |
9d02150c | 532 | unsigned parent_count, parent_line_count = 0; |
daae1922 | 533 | int err; |
ba002f3b | 534 | |
22410549 | 535 | if (require_end_of_header(buffer, size, &commit->object, options)) |
4d0d8975 JS |
536 | return -1; |
537 | ||
cf4fff57 | 538 | if (!skip_prefix(buffer, "tree ", &buffer)) |
c99ba492 | 539 | return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line"); |
2d820a61 | 540 | if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n') |
c99ba492 | 541 | return report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1"); |
2d820a61 | 542 | buffer += 41; |
cf4fff57 | 543 | while (skip_prefix(buffer, "parent ", &buffer)) { |
2d820a61 | 544 | if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') |
c99ba492 | 545 | return report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1"); |
2d820a61 | 546 | buffer += 41; |
9d02150c | 547 | parent_line_count++; |
ba002f3b MK |
548 | } |
549 | graft = lookup_commit_graft(commit->object.sha1); | |
9d02150c | 550 | parent_count = commit_list_count(commit->parents); |
ba002f3b | 551 | if (graft) { |
9d02150c | 552 | if (graft->nr_parent == -1 && !parent_count) |
ba002f3b | 553 | ; /* shallow commit */ |
9d02150c | 554 | else if (graft->nr_parent != parent_count) |
c99ba492 | 555 | return report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing"); |
ba002f3b | 556 | } else { |
9d02150c | 557 | if (parent_count != parent_line_count) |
c99ba492 | 558 | return report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing"); |
ba002f3b | 559 | } |
cf4fff57 | 560 | if (!skip_prefix(buffer, "author ", &buffer)) |
c99ba492 | 561 | return report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line"); |
22410549 | 562 | err = fsck_ident(&buffer, &commit->object, options); |
daae1922 JN |
563 | if (err) |
564 | return err; | |
cf4fff57 | 565 | if (!skip_prefix(buffer, "committer ", &buffer)) |
c99ba492 | 566 | return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line"); |
22410549 | 567 | err = fsck_ident(&buffer, &commit->object, options); |
daae1922 JN |
568 | if (err) |
569 | return err; | |
ba002f3b | 570 | if (!commit->tree) |
c99ba492 | 571 | return report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1)); |
ba002f3b MK |
572 | |
573 | return 0; | |
574 | } | |
575 | ||
90a398bb | 576 | static int fsck_commit(struct commit *commit, const char *data, |
22410549 | 577 | unsigned long size, struct fsck_options *options) |
bc6b8fc1 | 578 | { |
90a398bb | 579 | const char *buffer = data ? data : get_commit_buffer(commit, &size); |
22410549 | 580 | int ret = fsck_commit_buffer(commit, buffer, size, options); |
90a398bb JS |
581 | if (!data) |
582 | unuse_commit_buffer(commit, buffer); | |
bc6b8fc1 JK |
583 | return ret; |
584 | } | |
585 | ||
cec097be | 586 | static int fsck_tag_buffer(struct tag *tag, const char *data, |
22410549 | 587 | unsigned long size, struct fsck_options *options) |
cec097be JS |
588 | { |
589 | unsigned char sha1[20]; | |
590 | int ret = 0; | |
591 | const char *buffer; | |
592 | char *to_free = NULL, *eol; | |
593 | struct strbuf sb = STRBUF_INIT; | |
594 | ||
595 | if (data) | |
596 | buffer = data; | |
597 | else { | |
598 | enum object_type type; | |
599 | ||
600 | buffer = to_free = | |
601 | read_sha1_file(tag->object.sha1, &type, &size); | |
602 | if (!buffer) | |
c99ba492 JS |
603 | return report(options, &tag->object, |
604 | FSCK_MSG_MISSING_TAG_OBJECT, | |
cec097be JS |
605 | "cannot read tag object"); |
606 | ||
607 | if (type != OBJ_TAG) { | |
c99ba492 JS |
608 | ret = report(options, &tag->object, |
609 | FSCK_MSG_TAG_OBJECT_NOT_TAG, | |
cec097be JS |
610 | "expected tag got %s", |
611 | typename(type)); | |
612 | goto done; | |
613 | } | |
614 | } | |
615 | ||
22410549 | 616 | if (require_end_of_header(buffer, size, &tag->object, options)) |
cec097be JS |
617 | goto done; |
618 | ||
619 | if (!skip_prefix(buffer, "object ", &buffer)) { | |
c99ba492 | 620 | ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line"); |
cec097be JS |
621 | goto done; |
622 | } | |
623 | if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') { | |
c99ba492 | 624 | ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1"); |
cec097be JS |
625 | goto done; |
626 | } | |
627 | buffer += 41; | |
628 | ||
629 | if (!skip_prefix(buffer, "type ", &buffer)) { | |
c99ba492 | 630 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line"); |
cec097be JS |
631 | goto done; |
632 | } | |
633 | eol = strchr(buffer, '\n'); | |
634 | if (!eol) { | |
c99ba492 | 635 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line"); |
cec097be JS |
636 | goto done; |
637 | } | |
638 | if (type_from_string_gently(buffer, eol - buffer, 1) < 0) | |
c99ba492 | 639 | ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value"); |
cec097be JS |
640 | if (ret) |
641 | goto done; | |
642 | buffer = eol + 1; | |
643 | ||
644 | if (!skip_prefix(buffer, "tag ", &buffer)) { | |
c99ba492 | 645 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line"); |
cec097be JS |
646 | goto done; |
647 | } | |
648 | eol = strchr(buffer, '\n'); | |
649 | if (!eol) { | |
c99ba492 | 650 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line"); |
cec097be JS |
651 | goto done; |
652 | } | |
653 | strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer); | |
654 | if (check_refname_format(sb.buf, 0)) | |
c99ba492 JS |
655 | report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME, |
656 | "invalid 'tag' name: %.*s", | |
7add4419 | 657 | (int)(eol - buffer), buffer); |
cec097be JS |
658 | buffer = eol + 1; |
659 | ||
660 | if (!skip_prefix(buffer, "tagger ", &buffer)) | |
661 | /* early tags do not contain 'tagger' lines; warn only */ | |
c99ba492 | 662 | report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line"); |
cec097be | 663 | else |
22410549 | 664 | ret = fsck_ident(&buffer, &tag->object, options); |
cec097be JS |
665 | |
666 | done: | |
667 | strbuf_release(&sb); | |
668 | free(to_free); | |
669 | return ret; | |
670 | } | |
671 | ||
90a398bb | 672 | static int fsck_tag(struct tag *tag, const char *data, |
22410549 | 673 | unsigned long size, struct fsck_options *options) |
ba002f3b MK |
674 | { |
675 | struct object *tagged = tag->tagged; | |
676 | ||
677 | if (!tagged) | |
c99ba492 | 678 | return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object"); |
cec097be | 679 | |
22410549 | 680 | return fsck_tag_buffer(tag, data, size, options); |
ba002f3b MK |
681 | } |
682 | ||
90a398bb | 683 | int fsck_object(struct object *obj, void *data, unsigned long size, |
22410549 | 684 | struct fsck_options *options) |
ba002f3b MK |
685 | { |
686 | if (!obj) | |
c99ba492 | 687 | return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck"); |
ba002f3b MK |
688 | |
689 | if (obj->type == OBJ_BLOB) | |
690 | return 0; | |
691 | if (obj->type == OBJ_TREE) | |
22410549 | 692 | return fsck_tree((struct tree *) obj, options); |
ba002f3b | 693 | if (obj->type == OBJ_COMMIT) |
90a398bb | 694 | return fsck_commit((struct commit *) obj, (const char *) data, |
22410549 | 695 | size, options); |
ba002f3b | 696 | if (obj->type == OBJ_TAG) |
90a398bb | 697 | return fsck_tag((struct tag *) obj, (const char *) data, |
22410549 | 698 | size, options); |
ba002f3b | 699 | |
c99ba492 | 700 | return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)", |
ba002f3b MK |
701 | obj->type); |
702 | } | |
d6ffc8d7 | 703 | |
c99ba492 | 704 | int fsck_error_function(struct object *obj, int msg_type, const char *message) |
d6ffc8d7 | 705 | { |
0282f4dc JS |
706 | if (msg_type == FSCK_WARN) { |
707 | warning("object %s: %s", sha1_to_hex(obj->sha1), message); | |
708 | return 0; | |
709 | } | |
c99ba492 | 710 | error("object %s: %s", sha1_to_hex(obj->sha1), message); |
d6ffc8d7 MK |
711 | return 1; |
712 | } |