1#include "builtin.h"
2#include "cache.h"
3#include "commit.h"
4#include "tree.h"
5#include "blob.h"
6#include "tag.h"
7#include "refs.h"
8#include "pack.h"
9#include "cache-tree.h"
10#include "tree-walk.h"
11#include "fsck.h"
12#include "parse-options.h"
13#include "dir.h"
14#include "progress.h"
15#include "streaming.h"
16
17#define REACHABLE 0x0001
18#define SEEN 0x0002
19#define HAS_OBJ 0x0004
20
21static int show_root;
22static int show_tags;
23static int show_unreachable;
24static int include_reflogs = 1;
25static int check_full = 1;
26static int check_strict;
27static int keep_cache_objects;
28static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
29static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
30static struct object_id head_oid;
31static const char *head_points_at;
32static int errors_found;
33static int write_lost_and_found;
34static int verbose;
35static int show_progress = -1;
36static int show_dangling = 1;
37#define ERROR_OBJECT 01
38#define ERROR_REACHABLE 02
39#define ERROR_PACK 04
40
41#ifdef NO_D_INO_IN_DIRENT
42#define SORT_DIRENT 0
43#define DIRENT_SORT_HINT(de) 0
44#else
45#define SORT_DIRENT 1
46#define DIRENT_SORT_HINT(de) ((de)->d_ino)
47#endif
48
49static void objreport(struct object *obj, const char *msg_type,
50 const char *err)
51{
52 fprintf(stderr, "%s in %s %s: %s\n",
53 msg_type, typename(obj->type), sha1_to_hex(obj->sha1), err);
54}
55
56static int objerror(struct object *obj, const char *err)
57{
58 errors_found |= ERROR_OBJECT;
59 objreport(obj, "error", err);
60 return -1;
61}
62
63static int fsck_error_func(struct object *obj, int type, const char *message)
64{
65 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
66 return (type == FSCK_WARN) ? 0 : 1;
67}
68
69static struct object_array pending;
70
71static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
72{
73 struct object *parent = data;
74
75 /*
76 * The only case data is NULL or type is OBJ_ANY is when
77 * mark_object_reachable() calls us. All the callers of
78 * that function has non-NULL obj hence ...
79 */
80 if (!obj) {
81 /* ... these references to parent->fld are safe here */
82 printf("broken link from %7s %s\n",
83 typename(parent->type), sha1_to_hex(parent->sha1));
84 printf("broken link from %7s %s\n",
85 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
86 errors_found |= ERROR_REACHABLE;
87 return 1;
88 }
89
90 if (type != OBJ_ANY && obj->type != type)
91 /* ... and the reference to parent is safe here */
92 objerror(parent, "wrong object type in link");
93
94 if (obj->flags & REACHABLE)
95 return 0;
96 obj->flags |= REACHABLE;
97 if (!(obj->flags & HAS_OBJ)) {
98 if (parent && !has_sha1_file(obj->sha1)) {
99 printf("broken link from %7s %s\n",
100 typename(parent->type), sha1_to_hex(parent->sha1));
101 printf(" to %7s %s\n",
102 typename(obj->type), sha1_to_hex(obj->sha1));
103 errors_found |= ERROR_REACHABLE;
104 }
105 return 1;
106 }
107
108 add_object_array(obj, NULL, &pending);
109 return 0;
110}
111
112static void mark_object_reachable(struct object *obj)
113{
114 mark_object(obj, OBJ_ANY, NULL, NULL);
115}
116
117static int traverse_one_object(struct object *obj)
118{
119 int result;
120 struct tree *tree = NULL;
121
122 if (obj->type == OBJ_TREE) {
123 tree = (struct tree *)obj;
124 if (parse_tree(tree) < 0)
125 return 1; /* error already displayed */
126 }
127 result = fsck_walk(obj, obj, &fsck_walk_options);
128 if (tree)
129 free_tree_buffer(tree);
130 return result;
131}
132
133static int traverse_reachable(void)
134{
135 struct progress *progress = NULL;
136 unsigned int nr = 0;
137 int result = 0;
138 if (show_progress)
139 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
140 while (pending.nr) {
141 struct object_array_entry *entry;
142 struct object *obj;
143
144 entry = pending.objects + --pending.nr;
145 obj = entry->item;
146 result |= traverse_one_object(obj);
147 display_progress(progress, ++nr);
148 }
149 stop_progress(&progress);
150 return !!result;
151}
152
153static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
154{
155 if (!obj)
156 return 1;
157 obj->used = 1;
158 return 0;
159}
160
161/*
162 * Check a single reachable object
163 */
164static void check_reachable_object(struct object *obj)
165{
166 /*
167 * We obviously want the object to be parsed,
168 * except if it was in a pack-file and we didn't
169 * do a full fsck
170 */
171 if (!(obj->flags & HAS_OBJ)) {
172 if (has_sha1_pack(obj->sha1))
173 return; /* it is in pack - forget about it */
174 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
175 errors_found |= ERROR_REACHABLE;
176 return;
177 }
178}
179
180/*
181 * Check a single unreachable object
182 */
183static void check_unreachable_object(struct object *obj)
184{
185 /*
186 * Missing unreachable object? Ignore it. It's not like
187 * we miss it (since it can't be reached), nor do we want
188 * to complain about it being unreachable (since it does
189 * not exist).
190 */
191 if (!obj->parsed)
192 return;
193
194 /*
195 * Unreachable object that exists? Show it if asked to,
196 * since this is something that is prunable.
197 */
198 if (show_unreachable) {
199 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
200 return;
201 }
202
203 /*
204 * "!used" means that nothing at all points to it, including
205 * other unreachable objects. In other words, it's the "tip"
206 * of some set of unreachable objects, usually a commit that
207 * got dropped.
208 *
209 * Such starting points are more interesting than some random
210 * set of unreachable objects, so we show them even if the user
211 * hasn't asked for _all_ unreachable objects. If you have
212 * deleted a branch by mistake, this is a prime candidate to
213 * start looking at, for example.
214 */
215 if (!obj->used) {
216 if (show_dangling)
217 printf("dangling %s %s\n", typename(obj->type),
218 sha1_to_hex(obj->sha1));
219 if (write_lost_and_found) {
220 const char *filename = git_path("lost-found/%s/%s",
221 obj->type == OBJ_COMMIT ? "commit" : "other",
222 sha1_to_hex(obj->sha1));
223 FILE *f;
224
225 if (safe_create_leading_directories_const(filename)) {
226 error("Could not create lost-found");
227 return;
228 }
229 if (!(f = fopen(filename, "w")))
230 die_errno("Could not open '%s'", filename);
231 if (obj->type == OBJ_BLOB) {
232 if (stream_blob_to_fd(fileno(f), obj->sha1, NULL, 1))
233 die_errno("Could not write '%s'", filename);
234 } else
235 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
236 if (fclose(f))
237 die_errno("Could not finish '%s'",
238 filename);
239 }
240 return;
241 }
242
243 /*
244 * Otherwise? It's there, it's unreachable, and some other unreachable
245 * object points to it. Ignore it - it's not interesting, and we showed
246 * all the interesting cases above.
247 */
248}
249
250static void check_object(struct object *obj)
251{
252 if (verbose)
253 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
254
255 if (obj->flags & REACHABLE)
256 check_reachable_object(obj);
257 else
258 check_unreachable_object(obj);
259}
260
261static void check_connectivity(void)
262{
263 int i, max;
264
265 /* Traverse the pending reachable objects */
266 traverse_reachable();
267
268 /* Look up all the requirements, warn about missing objects.. */
269 max = get_max_object_index();
270 if (verbose)
271 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
272
273 for (i = 0; i < max; i++) {
274 struct object *obj = get_indexed_object(i);
275
276 if (obj)
277 check_object(obj);
278 }
279}
280
281static int fsck_obj(struct object *obj)
282{
283 if (obj->flags & SEEN)
284 return 0;
285 obj->flags |= SEEN;
286
287 if (verbose)
288 fprintf(stderr, "Checking %s %s\n",
289 typename(obj->type), sha1_to_hex(obj->sha1));
290
291 if (fsck_walk(obj, NULL, &fsck_obj_options))
292 objerror(obj, "broken links");
293 if (fsck_object(obj, NULL, 0, &fsck_obj_options))
294 return -1;
295
296 if (obj->type == OBJ_TREE) {
297 struct tree *item = (struct tree *) obj;
298
299 free_tree_buffer(item);
300 }
301
302 if (obj->type == OBJ_COMMIT) {
303 struct commit *commit = (struct commit *) obj;
304
305 free_commit_buffer(commit);
306
307 if (!commit->parents && show_root)
308 printf("root %s\n", sha1_to_hex(commit->object.sha1));
309 }
310
311 if (obj->type == OBJ_TAG) {
312 struct tag *tag = (struct tag *) obj;
313
314 if (show_tags && tag->tagged) {
315 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
316 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
317 }
318 }
319
320 return 0;
321}
322
323static int fsck_sha1(const unsigned char *sha1)
324{
325 struct object *obj = parse_object(sha1);
326 if (!obj) {
327 errors_found |= ERROR_OBJECT;
328 return error("%s: object corrupt or missing",
329 sha1_to_hex(sha1));
330 }
331 obj->flags |= HAS_OBJ;
332 return fsck_obj(obj);
333}
334
335static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
336 unsigned long size, void *buffer, int *eaten)
337{
338 struct object *obj;
339 obj = parse_object_buffer(sha1, type, size, buffer, eaten);
340 if (!obj) {
341 errors_found |= ERROR_OBJECT;
342 return error("%s: object corrupt or missing", sha1_to_hex(sha1));
343 }
344 obj->flags = HAS_OBJ;
345 return fsck_obj(obj);
346}
347
348/*
349 * This is the sorting chunk size: make it reasonably
350 * big so that we can sort well..
351 */
352#define MAX_SHA1_ENTRIES (1024)
353
354struct sha1_entry {
355 unsigned long ino;
356 unsigned char sha1[20];
357};
358
359static struct {
360 unsigned long nr;
361 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
362} sha1_list;
363
364static int ino_compare(const void *_a, const void *_b)
365{
366 const struct sha1_entry *a = _a, *b = _b;
367 unsigned long ino1 = a->ino, ino2 = b->ino;
368 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
369}
370
371static void fsck_sha1_list(void)
372{
373 int i, nr = sha1_list.nr;
374
375 if (SORT_DIRENT)
376 qsort(sha1_list.entry, nr,
377 sizeof(struct sha1_entry *), ino_compare);
378 for (i = 0; i < nr; i++) {
379 struct sha1_entry *entry = sha1_list.entry[i];
380 unsigned char *sha1 = entry->sha1;
381
382 sha1_list.entry[i] = NULL;
383 if (fsck_sha1(sha1))
384 errors_found |= ERROR_OBJECT;
385 free(entry);
386 }
387 sha1_list.nr = 0;
388}
389
390static void add_sha1_list(unsigned char *sha1, unsigned long ino)
391{
392 struct sha1_entry *entry = xmalloc(sizeof(*entry));
393 int nr;
394
395 entry->ino = ino;
396 hashcpy(entry->sha1, sha1);
397 nr = sha1_list.nr;
398 if (nr == MAX_SHA1_ENTRIES) {
399 fsck_sha1_list();
400 nr = 0;
401 }
402 sha1_list.entry[nr] = entry;
403 sha1_list.nr = ++nr;
404}
405
406static inline int is_loose_object_file(struct dirent *de,
407 char *name, unsigned char *sha1)
408{
409 if (strlen(de->d_name) != 38)
410 return 0;
411 memcpy(name + 2, de->d_name, 39);
412 return !get_sha1_hex(name, sha1);
413}
414
415static void fsck_dir(int i, char *path)
416{
417 DIR *dir = opendir(path);
418 struct dirent *de;
419 char name[100];
420
421 if (!dir)
422 return;
423
424 if (verbose)
425 fprintf(stderr, "Checking directory %s\n", path);
426
427 sprintf(name, "%02x", i);
428 while ((de = readdir(dir)) != NULL) {
429 unsigned char sha1[20];
430
431 if (is_dot_or_dotdot(de->d_name))
432 continue;
433 if (is_loose_object_file(de, name, sha1)) {
434 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
435 continue;
436 }
437 if (starts_with(de->d_name, "tmp_obj_"))
438 continue;
439 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
440 }
441 closedir(dir);
442}
443
444static int default_refs;
445
446static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
447 const char *email, unsigned long timestamp, int tz,
448 const char *message, void *cb_data)
449{
450 struct object *obj;
451
452 if (verbose)
453 fprintf(stderr, "Checking reflog %s->%s\n",
454 sha1_to_hex(osha1), sha1_to_hex(nsha1));
455
456 if (!is_null_sha1(osha1)) {
457 obj = lookup_object(osha1);
458 if (obj) {
459 obj->used = 1;
460 mark_object_reachable(obj);
461 }
462 }
463 obj = lookup_object(nsha1);
464 if (obj) {
465 obj->used = 1;
466 mark_object_reachable(obj);
467 }
468 return 0;
469}
470
471static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
472 int flag, void *cb_data)
473{
474 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
475 return 0;
476}
477
478static int fsck_handle_ref(const char *refname, const struct object_id *oid,
479 int flag, void *cb_data)
480{
481 struct object *obj;
482
483 obj = parse_object(oid->hash);
484 if (!obj) {
485 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
486 errors_found |= ERROR_REACHABLE;
487 /* We'll continue with the rest despite the error.. */
488 return 0;
489 }
490 if (obj->type != OBJ_COMMIT && is_branch(refname))
491 error("%s: not a commit", refname);
492 default_refs++;
493 obj->used = 1;
494 mark_object_reachable(obj);
495
496 return 0;
497}
498
499static void get_default_heads(void)
500{
501 if (head_points_at && !is_null_oid(&head_oid))
502 fsck_handle_ref("HEAD", &head_oid, 0, NULL);
503 for_each_rawref(fsck_handle_ref, NULL);
504 if (include_reflogs)
505 for_each_reflog(fsck_handle_reflog, NULL);
506
507 /*
508 * Not having any default heads isn't really fatal, but
509 * it does mean that "--unreachable" no longer makes any
510 * sense (since in this case everything will obviously
511 * be unreachable by definition.
512 *
513 * Showing dangling objects is valid, though (as those
514 * dangling objects are likely lost heads).
515 *
516 * So we just print a warning about it, and clear the
517 * "show_unreachable" flag.
518 */
519 if (!default_refs) {
520 fprintf(stderr, "notice: No default references\n");
521 show_unreachable = 0;
522 }
523}
524
525static void fsck_object_dir(const char *path)
526{
527 int i;
528 struct progress *progress = NULL;
529
530 if (verbose)
531 fprintf(stderr, "Checking object directory\n");
532
533 if (show_progress)
534 progress = start_progress(_("Checking object directories"), 256);
535 for (i = 0; i < 256; i++) {
536 static char dir[4096];
537 sprintf(dir, "%s/%02x", path, i);
538 fsck_dir(i, dir);
539 display_progress(progress, i+1);
540 }
541 stop_progress(&progress);
542 fsck_sha1_list();
543}
544
545static int fsck_head_link(void)
546{
547 int flag;
548 int null_is_error = 0;
549
550 if (verbose)
551 fprintf(stderr, "Checking HEAD link\n");
552
553 head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
554 if (!head_points_at)
555 return error("Invalid HEAD");
556 if (!strcmp(head_points_at, "HEAD"))
557 /* detached HEAD */
558 null_is_error = 1;
559 else if (!starts_with(head_points_at, "refs/heads/"))
560 return error("HEAD points to something strange (%s)",
561 head_points_at);
562 if (is_null_oid(&head_oid)) {
563 if (null_is_error)
564 return error("HEAD: detached HEAD points at nothing");
565 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
566 head_points_at + 11);
567 }
568 return 0;
569}
570
571static int fsck_cache_tree(struct cache_tree *it)
572{
573 int i;
574 int err = 0;
575
576 if (verbose)
577 fprintf(stderr, "Checking cache tree\n");
578
579 if (0 <= it->entry_count) {
580 struct object *obj = parse_object(it->sha1);
581 if (!obj) {
582 error("%s: invalid sha1 pointer in cache-tree",
583 sha1_to_hex(it->sha1));
584 return 1;
585 }
586 obj->used = 1;
587 mark_object_reachable(obj);
588 if (obj->type != OBJ_TREE)
589 err |= objerror(obj, "non-tree in cache-tree");
590 }
591 for (i = 0; i < it->subtree_nr; i++)
592 err |= fsck_cache_tree(it->down[i]->cache_tree);
593 return err;
594}
595
596static char const * const fsck_usage[] = {
597 N_("git fsck [<options>] [<object>...]"),
598 NULL
599};
600
601static struct option fsck_opts[] = {
602 OPT__VERBOSE(&verbose, N_("be verbose")),
603 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
604 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
605 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
606 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
607 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
608 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
609 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
610 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
611 OPT_BOOL(0, "lost-found", &write_lost_and_found,
612 N_("write dangling objects in .git/lost-found")),
613 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
614 OPT_END(),
615};
616
617int cmd_fsck(int argc, const char **argv, const char *prefix)
618{
619 int i, heads;
620 struct alternate_object_database *alt;
621
622 errors_found = 0;
623 check_replace_refs = 0;
624
625 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
626
627 fsck_walk_options.walk = mark_object;
628 fsck_obj_options.walk = mark_used;
629 fsck_obj_options.error_func = fsck_error_func;
630 if (check_strict)
631 fsck_obj_options.strict = 1;
632
633 if (show_progress == -1)
634 show_progress = isatty(2);
635 if (verbose)
636 show_progress = 0;
637
638 if (write_lost_and_found) {
639 check_full = 1;
640 include_reflogs = 0;
641 }
642
643 fsck_head_link();
644 fsck_object_dir(get_object_directory());
645
646 prepare_alt_odb();
647 for (alt = alt_odb_list; alt; alt = alt->next) {
648 char namebuf[PATH_MAX];
649 int namelen = alt->name - alt->base;
650 memcpy(namebuf, alt->base, namelen);
651 namebuf[namelen - 1] = 0;
652 fsck_object_dir(namebuf);
653 }
654
655 if (check_full) {
656 struct packed_git *p;
657 uint32_t total = 0, count = 0;
658 struct progress *progress = NULL;
659
660 prepare_packed_git();
661
662 if (show_progress) {
663 for (p = packed_git; p; p = p->next) {
664 if (open_pack_index(p))
665 continue;
666 total += p->num_objects;
667 }
668
669 progress = start_progress(_("Checking objects"), total);
670 }
671 for (p = packed_git; p; p = p->next) {
672 /* verify gives error messages itself */
673 if (verify_pack(p, fsck_obj_buffer,
674 progress, count))
675 errors_found |= ERROR_PACK;
676 count += p->num_objects;
677 }
678 stop_progress(&progress);
679 }
680
681 heads = 0;
682 for (i = 0; i < argc; i++) {
683 const char *arg = argv[i];
684 unsigned char sha1[20];
685 if (!get_sha1(arg, sha1)) {
686 struct object *obj = lookup_object(sha1);
687
688 /* Error is printed by lookup_object(). */
689 if (!obj)
690 continue;
691
692 obj->used = 1;
693 mark_object_reachable(obj);
694 heads++;
695 continue;
696 }
697 error("invalid parameter: expected sha1, got '%s'", arg);
698 }
699
700 /*
701 * If we've not been given any explicit head information, do the
702 * default ones from .git/refs. We also consider the index file
703 * in this case (ie this implies --cache).
704 */
705 if (!heads) {
706 get_default_heads();
707 keep_cache_objects = 1;
708 }
709
710 if (keep_cache_objects) {
711 read_cache();
712 for (i = 0; i < active_nr; i++) {
713 unsigned int mode;
714 struct blob *blob;
715 struct object *obj;
716
717 mode = active_cache[i]->ce_mode;
718 if (S_ISGITLINK(mode))
719 continue;
720 blob = lookup_blob(active_cache[i]->sha1);
721 if (!blob)
722 continue;
723 obj = &blob->object;
724 obj->used = 1;
725 mark_object_reachable(obj);
726 }
727 if (active_cache_tree)
728 fsck_cache_tree(active_cache_tree);
729 }
730
731 check_connectivity();
732 return errors_found;
733}