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