]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
Git 2.45-rc0
[thirdparty/git.git] / fsck.c
CommitLineData
d812c3b6 1#include "git-compat-util.h"
d4a4f929 2#include "date.h"
a034e910 3#include "dir.h"
41771fa4 4#include "hex.h"
a034e910 5#include "object-store-ll.h"
c339932b 6#include "path.h"
109cd76d 7#include "repository.h"
355885d5 8#include "object.h"
27ab4784 9#include "attr.h"
355885d5
MK
10#include "blob.h"
11#include "tree.h"
12#include "tree-walk.h"
13#include "commit.h"
14#include "tag.h"
15#include "fsck.h"
cec097be 16#include "refs.h"
a2b26ffb 17#include "url.h"
a18fcc9f 18#include "utf8.h"
159e7b08 19#include "oidset.h"
27387444 20#include "packfile.h"
ed8b10f6
JK
21#include "submodule-config.h"
22#include "config.h"
3ac68a93 23#include "help.h"
159e7b08 24
0fbcaef6
JK
25static ssize_t max_tree_entry_len = 4096;
26
f417eed8 27#define STR(x) #x
a4a9cc19 28#define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
c99ba492 29static struct {
f417eed8
JS
30 const char *id_string;
31 const char *downcased;
a4a9cc19 32 const char *camelcased;
1b32b59f 33 enum fsck_msg_type msg_type;
c99ba492 34} msg_id_info[FSCK_MSG_MAX + 1] = {
901f2f67 35 FOREACH_FSCK_MSG_ID(MSG_ID)
a4a9cc19 36 { NULL, NULL, NULL, -1 }
c99ba492
JS
37};
38#undef MSG_ID
b5495024 39#undef STR
c99ba492 40
a46baac6 41static void prepare_msg_ids(void)
f417eed8
JS
42{
43 int i;
44
a46baac6
NTND
45 if (msg_id_info[0].downcased)
46 return;
47
48 /* convert id_string to lower case, without underscores. */
49 for (i = 0; i < FSCK_MSG_MAX; i++) {
50 const char *p = msg_id_info[i].id_string;
51 int len = strlen(p);
52 char *q = xmalloc(len);
53
54 msg_id_info[i].downcased = q;
55 while (*p)
56 if (*p == '_')
57 p++;
58 else
59 *(q)++ = tolower(*(p)++);
60 *q = '\0';
a4a9cc19
NTND
61
62 p = msg_id_info[i].id_string;
63 q = xmalloc(len);
64 msg_id_info[i].camelcased = q;
65 while (*p) {
66 if (*p == '_') {
67 p++;
68 if (*p)
69 *q++ = *p++;
70 } else {
71 *q++ = tolower(*p++);
72 }
f417eed8 73 }
a4a9cc19 74 *q = '\0';
f417eed8 75 }
a46baac6
NTND
76}
77
78static int parse_msg_id(const char *text)
79{
80 int i;
81
82 prepare_msg_ids();
f417eed8
JS
83
84 for (i = 0; i < FSCK_MSG_MAX; i++)
85 if (!strcmp(text, msg_id_info[i].downcased))
86 return i;
87
88 return -1;
89}
90
3ac68a93
NTND
91void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
92{
93 int i;
94
95 prepare_msg_ids();
96
3ac68a93 97 for (i = 0; i < FSCK_MSG_MAX; i++)
a4a9cc19 98 list_config_item(list, prefix, msg_id_info[i].camelcased);
3ac68a93
NTND
99}
100
1b32b59f 101static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id,
c99ba492
JS
102 struct fsck_options *options)
103{
0282f4dc
JS
104 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
105
e35d65a7 106 if (!options->msg_type) {
1b32b59f 107 enum fsck_msg_type msg_type = msg_id_info[msg_id].msg_type;
e35d65a7 108
0282f4dc
JS
109 if (options->strict && msg_type == FSCK_WARN)
110 msg_type = FSCK_ERROR;
e35d65a7 111 return msg_type;
0282f4dc 112 }
c99ba492 113
e35d65a7 114 return options->msg_type[msg_id];
c99ba492
JS
115}
116
1b32b59f 117static enum fsck_msg_type parse_msg_type(const char *str)
0282f4dc
JS
118{
119 if (!strcmp(str, "error"))
120 return FSCK_ERROR;
121 else if (!strcmp(str, "warn"))
122 return FSCK_WARN;
efaba7cc
JS
123 else if (!strcmp(str, "ignore"))
124 return FSCK_IGNORE;
0282f4dc
JS
125 else
126 die("Unknown fsck message type: '%s'", str);
127}
128
5d477a33
JS
129int is_valid_msg_type(const char *msg_id, const char *msg_type)
130{
131 if (parse_msg_id(msg_id) < 0)
132 return 0;
133 parse_msg_type(msg_type);
134 return 1;
135}
136
53692df2
ÆAB
137void fsck_set_msg_type_from_ids(struct fsck_options *options,
138 enum fsck_msg_id msg_id,
139 enum fsck_msg_type msg_type)
140{
141 if (!options->msg_type) {
142 int i;
143 enum fsck_msg_type *severity;
144 ALLOC_ARRAY(severity, FSCK_MSG_MAX);
145 for (i = 0; i < FSCK_MSG_MAX; i++)
146 severity[i] = fsck_msg_type(i, options);
147 options->msg_type = severity;
148 }
149
150 options->msg_type[msg_id] = msg_type;
151}
152
0282f4dc 153void fsck_set_msg_type(struct fsck_options *options,
f1abc2d0 154 const char *msg_id_str, const char *msg_type_str)
0282f4dc 155{
1b32b59f 156 int msg_id = parse_msg_id(msg_id_str);
0fbcaef6
JK
157 char *to_free = NULL;
158 enum fsck_msg_type msg_type;
0282f4dc 159
f1abc2d0
ÆAB
160 if (msg_id < 0)
161 die("Unhandled message id: %s", msg_id_str);
0282f4dc 162
0fbcaef6
JK
163 if (msg_id == FSCK_MSG_LARGE_PATHNAME) {
164 const char *colon = strchr(msg_type_str, ':');
165 if (colon) {
166 msg_type_str = to_free =
167 xmemdupz(msg_type_str, colon - msg_type_str);
168 colon++;
169 if (!git_parse_ssize_t(colon, &max_tree_entry_len))
170 die("unable to parse max tree entry len: %s", colon);
171 }
172 }
173 msg_type = parse_msg_type(msg_type_str);
174
f1abc2d0
ÆAB
175 if (msg_type != FSCK_ERROR && msg_id_info[msg_id].msg_type == FSCK_FATAL)
176 die("Cannot demote %s to %s", msg_id_str, msg_type_str);
f50c4407 177
53692df2 178 fsck_set_msg_type_from_ids(options, msg_id, msg_type);
0fbcaef6 179 free(to_free);
0282f4dc
JS
180}
181
182void fsck_set_msg_types(struct fsck_options *options, const char *values)
183{
184 char *buf = xstrdup(values), *to_free = buf;
185 int done = 0;
186
187 while (!done) {
188 int len = strcspn(buf, " ,|"), equal;
189
190 done = !buf[len];
191 if (!len) {
192 buf++;
193 continue;
194 }
195 buf[len] = '\0';
196
197 for (equal = 0;
198 equal < len && buf[equal] != '=' && buf[equal] != ':';
199 equal++)
200 buf[equal] = tolower(buf[equal]);
201 buf[equal] = '\0';
202
cd94c6f9
JS
203 if (!strcmp(buf, "skiplist")) {
204 if (equal == len)
205 die("skiplist requires a path");
24eb33eb 206 oidset_parse_file(&options->skiplist, buf + equal + 1);
cd94c6f9
JS
207 buf += len + 1;
208 continue;
209 }
210
0282f4dc
JS
211 if (equal == len)
212 die("Missing '=': '%s'", buf);
355885d5 213
0282f4dc
JS
214 fsck_set_msg_type(options, buf, buf + equal + 1);
215 buf += len + 1;
216 }
217 free(to_free);
218}
219
f5979376
JK
220static int object_on_skiplist(struct fsck_options *opts,
221 const struct object_id *oid)
fb162877 222{
f5979376 223 return opts && oid && oidset_contains(&opts->skiplist, oid);
fb162877
RJ
224}
225
38370253
JK
226__attribute__((format (printf, 5, 6)))
227static int report(struct fsck_options *options,
228 const struct object_id *oid, enum object_type object_type,
35af754b 229 enum fsck_msg_id msg_id, const char *fmt, ...)
c99ba492
JS
230{
231 va_list ap;
232 struct strbuf sb = STRBUF_INIT;
1b32b59f
ÆAB
233 enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
234 int result;
c99ba492 235
efaba7cc
JS
236 if (msg_type == FSCK_IGNORE)
237 return 0;
238
38370253 239 if (object_on_skiplist(options, oid))
cd94c6f9
JS
240 return 0;
241
f50c4407
JS
242 if (msg_type == FSCK_FATAL)
243 msg_type = FSCK_ERROR;
f27d05b1
JS
244 else if (msg_type == FSCK_INFO)
245 msg_type = FSCK_WARN;
f50c4407 246
034a7b7b 247 prepare_msg_ids();
35af754b 248 strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased);
71ab8fa8 249
c99ba492
JS
250 va_start(ap, fmt);
251 strbuf_vaddf(&sb, fmt, ap);
38370253 252 result = options->error_func(options, oid, object_type,
394d5d31 253 msg_type, msg_id, sb.buf);
c99ba492
JS
254 strbuf_release(&sb);
255 va_end(ap);
256
257 return result;
258}
259
a59cfb32
JK
260void fsck_enable_object_names(struct fsck_options *options)
261{
262 if (!options->object_names)
73390290 263 options->object_names = kh_init_oid_map();
a59cfb32
JK
264}
265
73390290
JK
266const char *fsck_get_object_name(struct fsck_options *options,
267 const struct object_id *oid)
7b35efd7 268{
73390290 269 khiter_t pos;
7b35efd7
JS
270 if (!options->object_names)
271 return NULL;
73390290
JK
272 pos = kh_get_oid_map(options->object_names, *oid);
273 if (pos >= kh_end(options->object_names))
274 return NULL;
275 return kh_value(options->object_names, pos);
7b35efd7
JS
276}
277
73390290
JK
278void fsck_put_object_name(struct fsck_options *options,
279 const struct object_id *oid,
a59cfb32 280 const char *fmt, ...)
7b35efd7
JS
281{
282 va_list ap;
283 struct strbuf buf = STRBUF_INIT;
73390290
JK
284 khiter_t pos;
285 int hashret;
7b35efd7
JS
286
287 if (!options->object_names)
288 return;
73390290
JK
289
290 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
291 if (!hashret)
7b35efd7
JS
292 return;
293 va_start(ap, fmt);
294 strbuf_vaddf(&buf, fmt, ap);
73390290 295 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
7b35efd7
JS
296 va_end(ap);
297}
298
a59cfb32 299const char *fsck_describe_object(struct fsck_options *options,
73390290 300 const struct object_id *oid)
90cf590f 301{
a59cfb32
JK
302 static struct strbuf bufs[] = {
303 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
304 };
305 static int b = 0;
306 struct strbuf *buf;
73390290 307 const char *name = fsck_get_object_name(options, oid);
a59cfb32
JK
308
309 buf = bufs + b;
310 b = (b + 1) % ARRAY_SIZE(bufs);
311 strbuf_reset(buf);
73390290 312 strbuf_addstr(buf, oid_to_hex(oid));
a59cfb32
JK
313 if (name)
314 strbuf_addf(buf, " (%s)", name);
90cf590f 315
a59cfb32 316 return buf->buf;
90cf590f
JS
317}
318
22410549 319static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
355885d5
MK
320{
321 struct tree_desc desc;
322 struct name_entry entry;
323 int res = 0;
7b35efd7 324 const char *name;
355885d5
MK
325
326 if (parse_tree(tree))
327 return -1;
328
73390290 329 name = fsck_get_object_name(options, &tree->object.oid);
efed687e
EB
330 if (init_tree_desc_gently(&desc, &tree->object.oid,
331 tree->buffer, tree->size, 0))
8354fa3d
DT
332 return -1;
333 while (tree_entry_gently(&desc, &entry)) {
7b35efd7 334 struct object *obj;
355885d5
MK
335 int result;
336
337 if (S_ISGITLINK(entry.mode))
338 continue;
7b35efd7
JS
339
340 if (S_ISDIR(entry.mode)) {
ea82b2a0 341 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
2720f6db 342 if (name && obj)
73390290 343 fsck_put_object_name(options, &entry.oid, "%s%s/",
a59cfb32 344 name, entry.path);
7b35efd7
JS
345 result = options->walk(obj, OBJ_TREE, data, options);
346 }
347 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
ea82b2a0 348 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
2720f6db 349 if (name && obj)
73390290 350 fsck_put_object_name(options, &entry.oid, "%s%s",
a59cfb32 351 name, entry.path);
7b35efd7
JS
352 result = options->walk(obj, OBJ_BLOB, data, options);
353 }
355885d5 354 else {
82247e9b 355 result = error("in tree %s: entry %s has bad mode %.6o",
73390290 356 fsck_describe_object(options, &tree->object.oid),
a59cfb32 357 entry.path, entry.mode);
355885d5
MK
358 }
359 if (result < 0)
360 return result;
361 if (!res)
362 res = result;
363 }
364 return res;
365}
366
22410549 367static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
355885d5 368{
7b35efd7 369 int counter = 0, generation = 0, name_prefix_len = 0;
355885d5
MK
370 struct commit_list *parents;
371 int res;
372 int result;
7b35efd7 373 const char *name;
355885d5 374
ecb5091f 375 if (repo_parse_commit(the_repository, commit))
355885d5
MK
376 return -1;
377
73390290 378 name = fsck_get_object_name(options, &commit->object.oid);
7b35efd7 379 if (name)
73390290 380 fsck_put_object_name(options, get_commit_tree_oid(commit),
a59cfb32 381 "%s:", name);
7b35efd7 382
ecb5091f 383 result = options->walk((struct object *) repo_get_commit_tree(the_repository, commit),
2e27bd77 384 OBJ_TREE, data, options);
355885d5
MK
385 if (result < 0)
386 return result;
387 res = result;
388
389 parents = commit->parents;
7b35efd7
JS
390 if (name && parents) {
391 int len = strlen(name), power;
392
393 if (len && name[len - 1] == '^') {
394 generation = 1;
395 name_prefix_len = len - 1;
396 }
397 else { /* parse ~<generation> suffix */
398 for (generation = 0, power = 1;
399 len && isdigit(name[len - 1]);
400 power *= 10)
401 generation += power * (name[--len] - '0');
402 if (power > 1 && len && name[len - 1] == '~')
403 name_prefix_len = len - 1;
e89f8936
JS
404 else {
405 /* Maybe a non-first parent, e.g. HEAD^2 */
406 generation = 0;
407 name_prefix_len = len;
408 }
7b35efd7
JS
409 }
410 }
411
355885d5 412 while (parents) {
7b35efd7 413 if (name) {
73390290 414 struct object_id *oid = &parents->item->object.oid;
7b35efd7 415
b84c7838 416 if (counter++)
73390290 417 fsck_put_object_name(options, oid, "%s^%d",
a59cfb32 418 name, counter);
7b35efd7 419 else if (generation > 0)
73390290 420 fsck_put_object_name(options, oid, "%.*s~%d",
a59cfb32
JK
421 name_prefix_len, name,
422 generation + 1);
7b35efd7 423 else
73390290 424 fsck_put_object_name(options, oid, "%s^", name);
7b35efd7 425 }
22410549 426 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
355885d5
MK
427 if (result < 0)
428 return result;
429 if (!res)
430 res = result;
431 parents = parents->next;
432 }
433 return res;
434}
435
22410549 436static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
355885d5 437{
73390290 438 const char *name = fsck_get_object_name(options, &tag->object.oid);
7b35efd7 439
355885d5
MK
440 if (parse_tag(tag))
441 return -1;
7b35efd7 442 if (name)
73390290 443 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
22410549 444 return options->walk(tag->tagged, OBJ_ANY, data, options);
355885d5
MK
445}
446
22410549 447int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
355885d5
MK
448{
449 if (!obj)
450 return -1;
a2b22854
JK
451
452 if (obj->type == OBJ_NONE)
109cd76d 453 parse_object(the_repository, &obj->oid);
a2b22854 454
355885d5
MK
455 switch (obj->type) {
456 case OBJ_BLOB:
457 return 0;
458 case OBJ_TREE:
22410549 459 return fsck_walk_tree((struct tree *)obj, data, options);
355885d5 460 case OBJ_COMMIT:
22410549 461 return fsck_walk_commit((struct commit *)obj, data, options);
355885d5 462 case OBJ_TAG:
22410549 463 return fsck_walk_tag((struct tag *)obj, data, options);
355885d5 464 default:
a59cfb32 465 error("Unknown object type for %s",
73390290 466 fsck_describe_object(options, &obj->oid));
355885d5
MK
467 return -1;
468 }
469}
ba002f3b 470
9068cfb2
RS
471struct name_stack {
472 const char **names;
473 size_t nr, alloc;
474};
475
476static void name_stack_push(struct name_stack *stack, const char *name)
477{
478 ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc);
479 stack->names[stack->nr++] = name;
480}
481
482static const char *name_stack_pop(struct name_stack *stack)
483{
484 return stack->nr ? stack->names[--stack->nr] : NULL;
485}
486
487static void name_stack_clear(struct name_stack *stack)
488{
489 FREE_AND_NULL(stack->names);
490 stack->nr = stack->alloc = 0;
491}
492
ba002f3b
MK
493/*
494 * The entries in a tree are ordered in the _path_ order,
495 * which means that a directory entry is ordered by adding
496 * a slash to the end of it.
497 *
498 * So a directory called "a" is ordered _after_ a file
499 * called "a.c", because "a/" sorts after "a.c".
500 */
501#define TREE_UNORDERED (-1)
502#define TREE_HAS_DUPS (-2)
503
9068cfb2
RS
504static int is_less_than_slash(unsigned char c)
505{
506 return '\0' < c && c < '/';
507}
508
509static int verify_ordered(unsigned mode1, const char *name1,
510 unsigned mode2, const char *name2,
511 struct name_stack *candidates)
ba002f3b
MK
512{
513 int len1 = strlen(name1);
514 int len2 = strlen(name2);
515 int len = len1 < len2 ? len1 : len2;
516 unsigned char c1, c2;
517 int cmp;
518
519 cmp = memcmp(name1, name2, len);
520 if (cmp < 0)
521 return 0;
522 if (cmp > 0)
523 return TREE_UNORDERED;
524
525 /*
526 * Ok, the first <len> characters are the same.
527 * Now we need to order the next one, but turn
528 * a '\0' into a '/' for a directory entry.
529 */
530 c1 = name1[len];
531 c2 = name2[len];
532 if (!c1 && !c2)
533 /*
534 * git-write-tree used to write out a nonsense tree that has
535 * entries with the same name, one blob and one tree. Make
536 * sure we do not have duplicate entries.
537 */
538 return TREE_HAS_DUPS;
539 if (!c1 && S_ISDIR(mode1))
540 c1 = '/';
541 if (!c2 && S_ISDIR(mode2))
542 c2 = '/';
9068cfb2
RS
543
544 /*
545 * There can be non-consecutive duplicates due to the implicitly
86715592 546 * added slash, e.g.:
9068cfb2
RS
547 *
548 * foo
549 * foo.bar
550 * foo.bar.baz
551 * foo.bar/
552 * foo/
553 *
554 * Record non-directory candidates (like "foo" and "foo.bar" in
555 * the example) on a stack and check directory candidates (like
556 * foo/" and "foo.bar/") against that stack.
557 */
558 if (!c1 && is_less_than_slash(c2)) {
559 name_stack_push(candidates, name1);
560 } else if (c2 == '/' && is_less_than_slash(c1)) {
561 for (;;) {
562 const char *p;
563 const char *f_name = name_stack_pop(candidates);
564
565 if (!f_name)
566 break;
567 if (!skip_prefix(name2, f_name, &p))
fe747043 568 continue;
9068cfb2
RS
569 if (!*p)
570 return TREE_HAS_DUPS;
571 if (is_less_than_slash(*p)) {
572 name_stack_push(candidates, f_name);
573 break;
574 }
575 }
576 }
577
ba002f3b
MK
578 return c1 < c2 ? 0 : TREE_UNORDERED;
579}
580
9e1947cb 581static int fsck_tree(const struct object_id *tree_oid,
23a173a7
JK
582 const char *buffer, unsigned long size,
583 struct fsck_options *options)
ba002f3b 584{
8354fa3d 585 int retval = 0;
c479d14a 586 int has_null_sha1 = 0;
ba002f3b
MK
587 int has_full_path = 0;
588 int has_empty_name = 0;
5d34a435
JK
589 int has_dot = 0;
590 int has_dotdot = 0;
5c17f512 591 int has_dotgit = 0;
ba002f3b
MK
592 int has_zero_pad = 0;
593 int has_bad_modes = 0;
594 int has_dup_entries = 0;
595 int not_properly_sorted = 0;
0fbcaef6 596 int has_large_name = 0;
ba002f3b
MK
597 struct tree_desc desc;
598 unsigned o_mode;
599 const char *o_name;
9068cfb2 600 struct name_stack df_dup_candidates = { NULL };
ba002f3b 601
efed687e
EB
602 if (init_tree_desc_gently(&desc, tree_oid, buffer, size,
603 TREE_DESC_RAW_MODES)) {
0282f679
JK
604 retval += report(options, tree_oid, OBJ_TREE,
605 FSCK_MSG_BAD_TREE,
606 "cannot be parsed as a tree");
8354fa3d
DT
607 return retval;
608 }
ba002f3b
MK
609
610 o_mode = 0;
611 o_name = NULL;
ba002f3b
MK
612
613 while (desc.size) {
5ec1e728 614 unsigned short mode;
288a74bc 615 const char *name, *backslash;
9e1947cb 616 const struct object_id *entry_oid;
ba002f3b 617
9e1947cb 618 entry_oid = tree_entry_extract(&desc, &name, &mode);
ba002f3b 619
9e1947cb 620 has_null_sha1 |= is_null_oid(entry_oid);
effd12ec
HS
621 has_full_path |= !!strchr(name, '/');
622 has_empty_name |= !*name;
623 has_dot |= !strcmp(name, ".");
624 has_dotdot |= !strcmp(name, "..");
ed9c3220 625 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
ba002f3b 626 has_zero_pad |= *(char *)desc.buffer == '0';
0fbcaef6 627 has_large_name |= tree_entry_len(&desc.entry) > max_tree_entry_len;
159e7b08 628
b7b1fca1
JK
629 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
630 if (!S_ISLNK(mode))
9e1947cb
JK
631 oidset_insert(&options->gitmodules_found,
632 entry_oid);
b7b1fca1 633 else
38370253 634 retval += report(options,
9e1947cb 635 tree_oid, OBJ_TREE,
b7b1fca1
JK
636 FSCK_MSG_GITMODULES_SYMLINK,
637 ".gitmodules is a symbolic link");
638 }
159e7b08 639
f8587c31 640 if (is_hfs_dotgitattributes(name) || is_ntfs_dotgitattributes(name)) {
27ab4784
PS
641 if (!S_ISLNK(mode))
642 oidset_insert(&options->gitattributes_found,
643 entry_oid);
644 else
f8587c31
PS
645 retval += report(options, tree_oid, OBJ_TREE,
646 FSCK_MSG_GITATTRIBUTES_SYMLINK,
647 ".gitattributes is a symlink");
648 }
649
bb6832d5
JK
650 if (S_ISLNK(mode)) {
651 if (is_hfs_dotgitignore(name) ||
652 is_ntfs_dotgitignore(name))
653 retval += report(options, tree_oid, OBJ_TREE,
654 FSCK_MSG_GITIGNORE_SYMLINK,
655 ".gitignore is a symlink");
bb6832d5
JK
656 if (is_hfs_dotmailmap(name) ||
657 is_ntfs_dotmailmap(name))
658 retval += report(options, tree_oid, OBJ_TREE,
659 FSCK_MSG_MAILMAP_SYMLINK,
660 ".mailmap is a symlink");
661 }
662
288a74bc
JS
663 if ((backslash = strchr(name, '\\'))) {
664 while (backslash) {
665 backslash++;
666 has_dotgit |= is_ntfs_dotgit(backslash);
bdfef049
JS
667 if (is_ntfs_dotgitmodules(backslash)) {
668 if (!S_ISLNK(mode))
9e1947cb
JK
669 oidset_insert(&options->gitmodules_found,
670 entry_oid);
bdfef049 671 else
9e1947cb 672 retval += report(options, tree_oid, OBJ_TREE,
bdfef049
JS
673 FSCK_MSG_GITMODULES_SYMLINK,
674 ".gitmodules is a symbolic link");
675 }
288a74bc
JS
676 backslash = strchr(backslash, '\\');
677 }
678 }
679
8354fa3d 680 if (update_tree_entry_gently(&desc)) {
0282f679
JK
681 retval += report(options, tree_oid, OBJ_TREE,
682 FSCK_MSG_BAD_TREE,
683 "cannot be parsed as a tree");
8354fa3d
DT
684 break;
685 }
ba002f3b
MK
686
687 switch (mode) {
688 /*
689 * Standard modes..
690 */
691 case S_IFREG | 0755:
692 case S_IFREG | 0644:
693 case S_IFLNK:
694 case S_IFDIR:
695 case S_IFGITLINK:
696 break;
697 /*
698 * This is nonstandard, but we had a few of these
699 * early on when we honored the full set of mode
700 * bits..
701 */
702 case S_IFREG | 0664:
22410549 703 if (!options->strict)
ba002f3b 704 break;
1cf01a34 705 /* fallthrough */
ba002f3b
MK
706 default:
707 has_bad_modes = 1;
708 }
709
710 if (o_name) {
9068cfb2
RS
711 switch (verify_ordered(o_mode, o_name, mode, name,
712 &df_dup_candidates)) {
ba002f3b
MK
713 case TREE_UNORDERED:
714 not_properly_sorted = 1;
715 break;
716 case TREE_HAS_DUPS:
717 has_dup_entries = 1;
718 break;
719 default:
720 break;
721 }
722 }
723
724 o_mode = mode;
725 o_name = name;
ba002f3b
MK
726 }
727
9068cfb2
RS
728 name_stack_clear(&df_dup_candidates);
729
c479d14a 730 if (has_null_sha1)
0282f679
JK
731 retval += report(options, tree_oid, OBJ_TREE,
732 FSCK_MSG_NULL_SHA1,
733 "contains entries pointing to null sha1");
ba002f3b 734 if (has_full_path)
0282f679
JK
735 retval += report(options, tree_oid, OBJ_TREE,
736 FSCK_MSG_FULL_PATHNAME,
737 "contains full pathnames");
ba002f3b 738 if (has_empty_name)
0282f679
JK
739 retval += report(options, tree_oid, OBJ_TREE,
740 FSCK_MSG_EMPTY_NAME,
741 "contains empty pathname");
5d34a435 742 if (has_dot)
0282f679
JK
743 retval += report(options, tree_oid, OBJ_TREE,
744 FSCK_MSG_HAS_DOT,
745 "contains '.'");
5d34a435 746 if (has_dotdot)
0282f679
JK
747 retval += report(options, tree_oid, OBJ_TREE,
748 FSCK_MSG_HAS_DOTDOT,
749 "contains '..'");
5c17f512 750 if (has_dotgit)
0282f679
JK
751 retval += report(options, tree_oid, OBJ_TREE,
752 FSCK_MSG_HAS_DOTGIT,
753 "contains '.git'");
ba002f3b 754 if (has_zero_pad)
0282f679
JK
755 retval += report(options, tree_oid, OBJ_TREE,
756 FSCK_MSG_ZERO_PADDED_FILEMODE,
757 "contains zero-padded file modes");
ba002f3b 758 if (has_bad_modes)
0282f679
JK
759 retval += report(options, tree_oid, OBJ_TREE,
760 FSCK_MSG_BAD_FILEMODE,
761 "contains bad file modes");
ba002f3b 762 if (has_dup_entries)
0282f679
JK
763 retval += report(options, tree_oid, OBJ_TREE,
764 FSCK_MSG_DUPLICATE_ENTRIES,
765 "contains duplicate file entries");
ba002f3b 766 if (not_properly_sorted)
0282f679
JK
767 retval += report(options, tree_oid, OBJ_TREE,
768 FSCK_MSG_TREE_NOT_SORTED,
769 "not properly sorted");
0fbcaef6
JK
770 if (has_large_name)
771 retval += report(options, tree_oid, OBJ_TREE,
772 FSCK_MSG_LARGE_PATHNAME,
773 "contains excessively large pathname");
ba002f3b
MK
774 return retval;
775}
776
8e430903
JK
777/*
778 * Confirm that the headers of a commit or tag object end in a reasonable way,
779 * either with the usual "\n\n" separator, or at least with a trailing newline
780 * on the final header line.
781 *
782 * This property is important for the memory safety of our callers. It allows
783 * them to scan the buffer linewise without constantly checking the remaining
784 * size as long as:
785 *
786 * - they check that there are bytes left in the buffer at the start of any
787 * line (i.e., that the last newline they saw was not the final one we
788 * found here)
789 *
790 * - any intra-line scanning they do will stop at a newline, which will worst
791 * case hit the newline we found here as the end-of-header. This makes it
792 * OK for them to use helpers like parse_oid_hex(), or even skip_prefix().
793 */
84d18c0b 794static int verify_headers(const void *data, unsigned long size,
cc579000
JK
795 const struct object_id *oid, enum object_type type,
796 struct fsck_options *options)
4d0d8975
JS
797{
798 const char *buffer = (const char *)data;
799 unsigned long i;
800
801 for (i = 0; i < size; i++) {
802 switch (buffer[i]) {
803 case '\0':
cc579000 804 return report(options, oid, type,
c99ba492
JS
805 FSCK_MSG_NUL_IN_HEADER,
806 "unterminated header: NUL at offset %ld", i);
4d0d8975
JS
807 case '\n':
808 if (i + 1 < size && buffer[i + 1] == '\n')
809 return 0;
810 }
811 }
812
84d18c0b
JH
813 /*
814 * We did not find double-LF that separates the header
815 * and the body. Not having a body is not a crime but
816 * we do want to see the terminating LF for the last header
817 * line.
818 */
819 if (size && buffer[size - 1] == '\n')
820 return 0;
821
cc579000 822 return report(options, oid, type,
c99ba492 823 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
4d0d8975
JS
824}
825
78543993
JK
826static int fsck_ident(const char **ident,
827 const struct object_id *oid, enum object_type type,
828 struct fsck_options *options)
daae1922 829{
e6826e33 830 const char *p = *ident;
d4b8de04
JK
831 char *end;
832
e6826e33
JS
833 *ident = strchrnul(*ident, '\n');
834 if (**ident == '\n')
835 (*ident)++;
836
837 if (*p == '<')
78543993 838 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
839 p += strcspn(p, "<>\n");
840 if (*p == '>')
78543993 841 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
e6826e33 842 if (*p != '<')
78543993 843 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
e6826e33 844 if (p[-1] != ' ')
78543993 845 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
846 p++;
847 p += strcspn(p, "<>\n");
848 if (*p != '>')
78543993 849 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
e6826e33
JS
850 p++;
851 if (*p != ' ')
78543993 852 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
e6826e33 853 p++;
8e430903
JK
854 /*
855 * Our timestamp parser is based on the C strto*() functions, which
856 * will happily eat whitespace, including the newline that is supposed
857 * to prevent us walking past the end of the buffer. So do our own
858 * scan, skipping linear whitespace but not newlines, and then
859 * confirming we found a digit. We _could_ be even more strict here,
860 * as we really expect only a single space, but since we have
861 * traditionally allowed extra whitespace, we'll continue to do so.
862 */
863 while (*p == ' ' || *p == '\t')
864 p++;
865 if (!isdigit(*p))
866 return report(options, oid, type, FSCK_MSG_BAD_DATE,
867 "invalid author/committer line - bad date");
e6826e33 868 if (*p == '0' && p[1] != ' ')
78543993 869 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
1aeb7e75 870 if (date_overflows(parse_timestamp(p, &end, 10)))
78543993 871 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
e6826e33 872 if ((end == p || *end != ' '))
78543993 873 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
e6826e33
JS
874 p = end + 1;
875 if ((*p != '+' && *p != '-') ||
876 !isdigit(p[1]) ||
877 !isdigit(p[2]) ||
878 !isdigit(p[3]) ||
879 !isdigit(p[4]) ||
880 (p[5] != '\n'))
78543993 881 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
e6826e33 882 p += 6;
daae1922
JN
883 return 0;
884}
885
c5b4269b
JK
886static int fsck_commit(const struct object_id *oid,
887 const char *buffer, unsigned long size,
888 struct fsck_options *options)
ba002f3b 889{
f648ee70 890 struct object_id tree_oid, parent_oid;
ec652315 891 unsigned author_count;
daae1922 892 int err;
6d2d780f 893 const char *buffer_begin = buffer;
8e430903 894 const char *buffer_end = buffer + size;
c54f5ca9 895 const char *p;
ba002f3b 896
8e430903
JK
897 /*
898 * We _must_ stop parsing immediately if this reports failure, as the
899 * memory safety of the rest of the function depends on it. See the
900 * comment above the definition of verify_headers() for more details.
901 */
c5b4269b 902 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
4d0d8975
JS
903 return -1;
904
8e430903 905 if (buffer >= buffer_end || !skip_prefix(buffer, "tree ", &buffer))
c5b4269b 906 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
c54f5ca9 907 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
c5b4269b 908 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
b3584761
JS
909 if (err)
910 return err;
911 }
c54f5ca9 912 buffer = p + 1;
8e430903 913 while (buffer < buffer_end && skip_prefix(buffer, "parent ", &buffer)) {
f648ee70 914 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
c5b4269b 915 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
b3584761
JS
916 if (err)
917 return err;
918 }
c54f5ca9 919 buffer = p + 1;
ba002f3b 920 }
c9ad147f 921 author_count = 0;
8e430903 922 while (buffer < buffer_end && skip_prefix(buffer, "author ", &buffer)) {
c9ad147f 923 author_count++;
c5b4269b 924 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
c9ad147f
JS
925 if (err)
926 return err;
ba002f3b 927 }
c9ad147f 928 if (author_count < 1)
c5b4269b 929 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
c9ad147f 930 else if (author_count > 1)
c5b4269b 931 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
daae1922
JN
932 if (err)
933 return err;
8e430903 934 if (buffer >= buffer_end || !skip_prefix(buffer, "committer ", &buffer))
c5b4269b
JK
935 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
936 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
daae1922
JN
937 if (err)
938 return err;
6d2d780f 939 if (memchr(buffer_begin, '\0', size)) {
c5b4269b 940 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
6d2d780f
JH
941 "NUL byte in the commit object body");
942 if (err)
943 return err;
944 }
ba002f3b
MK
945 return 0;
946}
947
103fb6d4 948static int fsck_tag(const struct object_id *oid, const char *buffer,
2175a0c6 949 unsigned long size, struct fsck_options *options)
cec097be 950{
f648ee70 951 struct object_id tagged_oid;
acf9de4c
ÆAB
952 int tagged_type;
953 return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid,
954 &tagged_type);
955}
956
957int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
958 unsigned long size, struct fsck_options *options,
959 struct object_id *tagged_oid,
960 int *tagged_type)
961{
cec097be 962 int ret = 0;
23a173a7 963 char *eol;
cec097be 964 struct strbuf sb = STRBUF_INIT;
8e430903 965 const char *buffer_end = buffer + size;
c54f5ca9 966 const char *p;
cec097be 967
8e430903
JK
968 /*
969 * We _must_ stop parsing immediately if this reports failure, as the
970 * memory safety of the rest of the function depends on it. See the
971 * comment above the definition of verify_headers() for more details.
972 */
103fb6d4 973 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
8a272f29 974 if (ret)
cec097be
JS
975 goto done;
976
8e430903 977 if (buffer >= buffer_end || !skip_prefix(buffer, "object ", &buffer)) {
103fb6d4 978 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
cec097be
JS
979 goto done;
980 }
acf9de4c 981 if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') {
103fb6d4 982 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
7d7d5b05
JS
983 if (ret)
984 goto done;
cec097be 985 }
c54f5ca9 986 buffer = p + 1;
cec097be 987
8e430903 988 if (buffer >= buffer_end || !skip_prefix(buffer, "type ", &buffer)) {
103fb6d4 989 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
cec097be
JS
990 goto done;
991 }
8e430903 992 eol = memchr(buffer, '\n', buffer_end - buffer);
cec097be 993 if (!eol) {
103fb6d4 994 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
cec097be
JS
995 goto done;
996 }
acf9de4c
ÆAB
997 *tagged_type = type_from_string_gently(buffer, eol - buffer, 1);
998 if (*tagged_type < 0)
103fb6d4 999 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
cec097be
JS
1000 if (ret)
1001 goto done;
1002 buffer = eol + 1;
1003
8e430903 1004 if (buffer >= buffer_end || !skip_prefix(buffer, "tag ", &buffer)) {
103fb6d4 1005 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
cec097be
JS
1006 goto done;
1007 }
8e430903 1008 eol = memchr(buffer, '\n', buffer_end - buffer);
cec097be 1009 if (!eol) {
103fb6d4 1010 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
cec097be
JS
1011 goto done;
1012 }
1013 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
f27d05b1 1014 if (check_refname_format(sb.buf, 0)) {
103fb6d4 1015 ret = report(options, oid, OBJ_TAG,
38370253
JK
1016 FSCK_MSG_BAD_TAG_NAME,
1017 "invalid 'tag' name: %.*s",
1018 (int)(eol - buffer), buffer);
f27d05b1
JS
1019 if (ret)
1020 goto done;
1021 }
cec097be
JS
1022 buffer = eol + 1;
1023
8e430903 1024 if (buffer >= buffer_end || !skip_prefix(buffer, "tagger ", &buffer)) {
cec097be 1025 /* early tags do not contain 'tagger' lines; warn only */
103fb6d4 1026 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
f27d05b1
JS
1027 if (ret)
1028 goto done;
1029 }
cec097be 1030 else
103fb6d4 1031 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
cec097be 1032
8e430903 1033 if (buffer < buffer_end && !starts_with(buffer, "\n")) {
acf9de4c
ÆAB
1034 /*
1035 * The verify_headers() check will allow
1036 * e.g. "[...]tagger <tagger>\nsome
1037 * garbage\n\nmessage" to pass, thinking "some
1038 * garbage" could be a custom header. E.g. "mktag"
1039 * doesn't want any unknown headers.
1040 */
1041 ret = report(options, oid, OBJ_TAG, FSCK_MSG_EXTRA_HEADER_ENTRY, "invalid format - extra header(s) after 'tagger'");
1042 if (ret)
1043 goto done;
1044 }
1045
cec097be
JS
1046done:
1047 strbuf_release(&sb);
cec097be
JS
1048 return ret;
1049}
1050
ed8b10f6 1051struct fsck_gitmodules_data {
6da40b22 1052 const struct object_id *oid;
ed8b10f6
JK
1053 struct fsck_options *options;
1054 int ret;
1055};
1056
a4e7e317
GC
1057static int fsck_gitmodules_fn(const char *var, const char *value,
1058 const struct config_context *ctx UNUSED,
1059 void *vdata)
ed8b10f6
JK
1060{
1061 struct fsck_gitmodules_data *data = vdata;
1062 const char *subsection, *key;
f5914f4b 1063 size_t subsection_len;
ed8b10f6
JK
1064 char *name;
1065
1066 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
1067 !subsection)
1068 return 0;
1069
1070 name = xmemdupz(subsection, subsection_len);
1071 if (check_submodule_name(name) < 0)
38370253 1072 data->ret |= report(data->options,
6da40b22 1073 data->oid, OBJ_BLOB,
ed8b10f6
JK
1074 FSCK_MSG_GITMODULES_NAME,
1075 "disallowed submodule name: %s",
1076 name);
a124133e 1077 if (!strcmp(key, "url") && value &&
07259e74 1078 check_submodule_url(value) < 0)
38370253 1079 data->ret |= report(data->options,
6da40b22 1080 data->oid, OBJ_BLOB,
a124133e
JK
1081 FSCK_MSG_GITMODULES_URL,
1082 "disallowed submodule url: %s",
1083 value);
1a7fd1fb
JK
1084 if (!strcmp(key, "path") && value &&
1085 looks_like_command_line_option(value))
38370253 1086 data->ret |= report(data->options,
6da40b22 1087 data->oid, OBJ_BLOB,
1a7fd1fb
JK
1088 FSCK_MSG_GITMODULES_PATH,
1089 "disallowed submodule path: %s",
1090 value);
bb92255e
JN
1091 if (!strcmp(key, "update") && value &&
1092 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
7034cd09 1093 data->ret |= report(data->options, data->oid, OBJ_BLOB,
bb92255e
JN
1094 FSCK_MSG_GITMODULES_UPDATE,
1095 "disallowed submodule update setting: %s",
1096 value);
ed8b10f6
JK
1097 free(name);
1098
1099 return 0;
1100}
1101
6da40b22 1102static int fsck_blob(const struct object_id *oid, const char *buf,
7ac4f3a0
JK
1103 unsigned long size, struct fsck_options *options)
1104{
bb3a9265 1105 int ret = 0;
ed8b10f6 1106
6da40b22 1107 if (object_on_skiplist(options, oid))
fb162877
RJ
1108 return 0;
1109
bb3a9265
PS
1110 if (oidset_contains(&options->gitmodules_found, oid)) {
1111 struct config_options config_opts = { 0 };
1112 struct fsck_gitmodules_data data;
1113
1114 oidset_insert(&options->gitmodules_done, oid);
1115
1116 if (!buf) {
1117 /*
1118 * A missing buffer here is a sign that the caller found the
1119 * blob too gigantic to load into memory. Let's just consider
1120 * that an error.
1121 */
1122 return report(options, oid, OBJ_BLOB,
1123 FSCK_MSG_GITMODULES_LARGE,
1124 ".gitmodules too large to parse");
1125 }
1126
1127 data.oid = oid;
1128 data.options = options;
1129 data.ret = 0;
1130 config_opts.error_action = CONFIG_ERROR_SILENT;
1131 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
809d8680
GC
1132 ".gitmodules", buf, size, &data,
1133 CONFIG_SCOPE_UNKNOWN, &config_opts))
bb3a9265
PS
1134 data.ret |= report(options, oid, OBJ_BLOB,
1135 FSCK_MSG_GITMODULES_PARSE,
1136 "could not parse gitmodules blob");
1137 ret |= data.ret;
ed8b10f6
JK
1138 }
1139
27ab4784
PS
1140 if (oidset_contains(&options->gitattributes_found, oid)) {
1141 const char *ptr;
1142
1143 oidset_insert(&options->gitattributes_done, oid);
1144
1145 if (!buf || size > ATTR_MAX_FILE_SIZE) {
1146 /*
1147 * A missing buffer here is a sign that the caller found the
1148 * blob too gigantic to load into memory. Let's just consider
1149 * that an error.
1150 */
1151 return report(options, oid, OBJ_BLOB,
1152 FSCK_MSG_GITATTRIBUTES_LARGE,
1153 ".gitattributes too large to parse");
1154 }
1155
1156 for (ptr = buf; *ptr; ) {
1157 const char *eol = strchrnul(ptr, '\n');
1158 if (eol - ptr >= ATTR_MAX_LINE_LENGTH) {
1159 ret |= report(options, oid, OBJ_BLOB,
1160 FSCK_MSG_GITATTRIBUTES_LINE_LENGTH,
1161 ".gitattributes has too long lines to parse");
1162 break;
1163 }
1164
1165 ptr = *eol ? eol + 1 : eol;
1166 }
1167 }
1168
bb3a9265 1169 return ret;
7ac4f3a0
JK
1170}
1171
90a398bb 1172int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 1173 struct fsck_options *options)
ba002f3b
MK
1174{
1175 if (!obj)
38370253 1176 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
ba002f3b 1177
35ff327e
JK
1178 return fsck_buffer(&obj->oid, obj->type, data, size, options);
1179}
1180
1181int fsck_buffer(const struct object_id *oid, enum object_type type,
1182 void *data, unsigned long size,
1183 struct fsck_options *options)
1184{
1185 if (type == OBJ_BLOB)
1186 return fsck_blob(oid, data, size, options);
1187 if (type == OBJ_TREE)
1188 return fsck_tree(oid, data, size, options);
1189 if (type == OBJ_COMMIT)
1190 return fsck_commit(oid, data, size, options);
1191 if (type == OBJ_TAG)
1192 return fsck_tag(oid, data, size, options);
1193
1194 return report(options, oid, type,
38370253
JK
1195 FSCK_MSG_UNKNOWN_TYPE,
1196 "unknown type '%d' (internal fsck error)",
35ff327e 1197 type);
ba002f3b 1198}
d6ffc8d7 1199
1cd772cc 1200int fsck_error_function(struct fsck_options *o,
5afc4b1d 1201 const struct object_id *oid,
0b4e9013 1202 enum object_type object_type UNUSED,
394d5d31 1203 enum fsck_msg_type msg_type,
0b4e9013 1204 enum fsck_msg_id msg_id UNUSED,
394d5d31 1205 const char *message)
d6ffc8d7 1206{
0282f4dc 1207 if (msg_type == FSCK_WARN) {
5afc4b1d 1208 warning("object %s: %s", fsck_describe_object(o, oid), message);
0282f4dc
JS
1209 return 0;
1210 }
5afc4b1d 1211 error("object %s: %s", fsck_describe_object(o, oid), message);
d6ffc8d7
MK
1212 return 1;
1213}
159e7b08 1214
a59a8c68
PS
1215static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
1216 enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type,
1217 struct fsck_options *options, const char *blob_type)
159e7b08
JK
1218{
1219 int ret = 0;
1220 struct oidset_iter iter;
1221 const struct object_id *oid;
1222
a59a8c68 1223 oidset_iter_init(blobs_found, &iter);
159e7b08 1224 while ((oid = oidset_iter_next(&iter))) {
159e7b08
JK
1225 enum object_type type;
1226 unsigned long size;
1227 char *buf;
1228
a59a8c68 1229 if (oidset_contains(blobs_done, oid))
159e7b08
JK
1230 continue;
1231
bc726bd0 1232 buf = repo_read_object_file(the_repository, oid, &type, &size);
159e7b08 1233 if (!buf) {
b8b00f16 1234 if (is_promisor_object(oid))
27387444 1235 continue;
38370253 1236 ret |= report(options,
a59a8c68
PS
1237 oid, OBJ_BLOB, msg_missing,
1238 "unable to read %s blob", blob_type);
159e7b08
JK
1239 continue;
1240 }
1241
1242 if (type == OBJ_BLOB)
b8b00f16 1243 ret |= fsck_blob(oid, buf, size, options);
159e7b08 1244 else
a59a8c68
PS
1245 ret |= report(options, oid, type, msg_type,
1246 "non-blob found at %s", blob_type);
159e7b08
JK
1247 free(buf);
1248 }
1249
a59a8c68
PS
1250 oidset_clear(blobs_found);
1251 oidset_clear(blobs_done);
1252
1253 return ret;
1254}
1255
1256int fsck_finish(struct fsck_options *options)
1257{
1258 int ret = 0;
1259
1260 ret |= fsck_blobs(&options->gitmodules_found, &options->gitmodules_done,
1261 FSCK_MSG_GITMODULES_MISSING, FSCK_MSG_GITMODULES_BLOB,
1262 options, ".gitmodules");
27ab4784
PS
1263 ret |= fsck_blobs(&options->gitattributes_found, &options->gitattributes_done,
1264 FSCK_MSG_GITATTRIBUTES_MISSING, FSCK_MSG_GITATTRIBUTES_BLOB,
1265 options, ".gitattributes");
159e7b08 1266
159e7b08
JK
1267 return ret;
1268}
1f3299fd 1269
a4e7e317
GC
1270int git_fsck_config(const char *var, const char *value,
1271 const struct config_context *ctx, void *cb)
1f3299fd 1272{
fb79f5bf 1273 struct fsck_options *options = cb;
d49cb162
JK
1274 const char *msg_id;
1275
1f3299fd
ÆAB
1276 if (strcmp(var, "fsck.skiplist") == 0) {
1277 const char *path;
1278 struct strbuf sb = STRBUF_INIT;
1279
1280 if (git_config_pathname(&path, var, value))
1281 return 1;
1282 strbuf_addf(&sb, "skiplist=%s", path);
1283 free((char *)path);
1284 fsck_set_msg_types(options, sb.buf);
1285 strbuf_release(&sb);
1286 return 0;
1287 }
1288
d49cb162
JK
1289 if (skip_prefix(var, "fsck.", &msg_id)) {
1290 if (!value)
1291 return config_error_nonbool(var);
1292 fsck_set_msg_type(options, msg_id, value);
1f3299fd
ÆAB
1293 return 0;
1294 }
1295
a4e7e317 1296 return git_default_config(var, value, ctx, cb);
1f3299fd 1297}
3745e269
ÆAB
1298
1299/*
1300 * Custom error callbacks that are used in more than one place.
1301 */
1302
1303int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o,
1304 const struct object_id *oid,
1305 enum object_type object_type,
1306 enum fsck_msg_type msg_type,
1307 enum fsck_msg_id msg_id,
1308 const char *message)
1309{
1310 if (msg_id == FSCK_MSG_GITMODULES_MISSING) {
1311 puts(oid_to_hex(oid));
1312 return 0;
1313 }
1314 return fsck_error_function(o, oid, object_type, msg_type, msg_id, message);
1315}