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