fsck: exit with non-zero when problems are found
[git/debian.git] / builtin / fsck.c
blob63ab0bbb0c31e2ba3a6a08c5c78b84e290ebc128
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"
17 #define REACHABLE 0x0001
18 #define SEEN 0x0002
19 #define HAS_OBJ 0x0004
21 static int show_root;
22 static int show_tags;
23 static int show_unreachable;
24 static int include_reflogs = 1;
25 static int check_full = 1;
26 static int check_strict;
27 static int keep_cache_objects;
28 static struct object_id head_oid;
29 static const char *head_points_at;
30 static int errors_found;
31 static int write_lost_and_found;
32 static int verbose;
33 static int show_progress = -1;
34 static int show_dangling = 1;
35 #define ERROR_OBJECT 01
36 #define ERROR_REACHABLE 02
37 #define ERROR_PACK 04
38 #define ERROR_REFS 010
40 #ifdef NO_D_INO_IN_DIRENT
41 #define SORT_DIRENT 0
42 #define DIRENT_SORT_HINT(de) 0
43 #else
44 #define SORT_DIRENT 1
45 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
46 #endif
48 static void objreport(struct object *obj, const char *severity,
49 const char *err, va_list params)
51 fprintf(stderr, "%s in %s %s: ",
52 severity, typename(obj->type), sha1_to_hex(obj->sha1));
53 vfprintf(stderr, err, params);
54 fputs("\n", stderr);
57 __attribute__((format (printf, 2, 3)))
58 static int objerror(struct object *obj, const char *err, ...)
60 va_list params;
61 va_start(params, err);
62 errors_found |= ERROR_OBJECT;
63 objreport(obj, "error", err, params);
64 va_end(params);
65 return -1;
68 __attribute__((format (printf, 3, 4)))
69 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
71 va_list params;
72 va_start(params, err);
73 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
74 va_end(params);
75 return (type == FSCK_WARN) ? 0 : 1;
78 static struct object_array pending;
80 static int mark_object(struct object *obj, int type, void *data)
82 struct object *parent = data;
85 * The only case data is NULL or type is OBJ_ANY is when
86 * mark_object_reachable() calls us. All the callers of
87 * that function has non-NULL obj hence ...
89 if (!obj) {
90 /* ... these references to parent->fld are safe here */
91 printf("broken link from %7s %s\n",
92 typename(parent->type), sha1_to_hex(parent->sha1));
93 printf("broken link from %7s %s\n",
94 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
95 errors_found |= ERROR_REACHABLE;
96 return 1;
99 if (type != OBJ_ANY && obj->type != type)
100 /* ... and the reference to parent is safe here */
101 objerror(parent, "wrong object type in link");
103 if (obj->flags & REACHABLE)
104 return 0;
105 obj->flags |= REACHABLE;
106 if (!(obj->flags & HAS_OBJ)) {
107 if (parent && !has_sha1_file(obj->sha1)) {
108 printf("broken link from %7s %s\n",
109 typename(parent->type), sha1_to_hex(parent->sha1));
110 printf(" to %7s %s\n",
111 typename(obj->type), sha1_to_hex(obj->sha1));
112 errors_found |= ERROR_REACHABLE;
114 return 1;
117 add_object_array(obj, NULL, &pending);
118 return 0;
121 static void mark_object_reachable(struct object *obj)
123 mark_object(obj, OBJ_ANY, NULL);
126 static int traverse_one_object(struct object *obj)
128 int result;
129 struct tree *tree = NULL;
131 if (obj->type == OBJ_TREE) {
132 tree = (struct tree *)obj;
133 if (parse_tree(tree) < 0)
134 return 1; /* error already displayed */
136 result = fsck_walk(obj, mark_object, obj);
137 if (tree)
138 free_tree_buffer(tree);
139 return result;
142 static int traverse_reachable(void)
144 struct progress *progress = NULL;
145 unsigned int nr = 0;
146 int result = 0;
147 if (show_progress)
148 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
149 while (pending.nr) {
150 struct object_array_entry *entry;
151 struct object *obj;
153 entry = pending.objects + --pending.nr;
154 obj = entry->item;
155 result |= traverse_one_object(obj);
156 display_progress(progress, ++nr);
158 stop_progress(&progress);
159 return !!result;
162 static int mark_used(struct object *obj, int type, void *data)
164 if (!obj)
165 return 1;
166 obj->used = 1;
167 return 0;
171 * Check a single reachable object
173 static void check_reachable_object(struct object *obj)
176 * We obviously want the object to be parsed,
177 * except if it was in a pack-file and we didn't
178 * do a full fsck
180 if (!(obj->flags & HAS_OBJ)) {
181 if (has_sha1_pack(obj->sha1))
182 return; /* it is in pack - forget about it */
183 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
184 errors_found |= ERROR_REACHABLE;
185 return;
190 * Check a single unreachable object
192 static void check_unreachable_object(struct object *obj)
195 * Missing unreachable object? Ignore it. It's not like
196 * we miss it (since it can't be reached), nor do we want
197 * to complain about it being unreachable (since it does
198 * not exist).
200 if (!obj->parsed)
201 return;
204 * Unreachable object that exists? Show it if asked to,
205 * since this is something that is prunable.
207 if (show_unreachable) {
208 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
209 return;
213 * "!used" means that nothing at all points to it, including
214 * other unreachable objects. In other words, it's the "tip"
215 * of some set of unreachable objects, usually a commit that
216 * got dropped.
218 * Such starting points are more interesting than some random
219 * set of unreachable objects, so we show them even if the user
220 * hasn't asked for _all_ unreachable objects. If you have
221 * deleted a branch by mistake, this is a prime candidate to
222 * start looking at, for example.
224 if (!obj->used) {
225 if (show_dangling)
226 printf("dangling %s %s\n", typename(obj->type),
227 sha1_to_hex(obj->sha1));
228 if (write_lost_and_found) {
229 const char *filename = git_path("lost-found/%s/%s",
230 obj->type == OBJ_COMMIT ? "commit" : "other",
231 sha1_to_hex(obj->sha1));
232 FILE *f;
234 if (safe_create_leading_directories_const(filename)) {
235 error("Could not create lost-found");
236 return;
238 if (!(f = fopen(filename, "w")))
239 die_errno("Could not open '%s'", filename);
240 if (obj->type == OBJ_BLOB) {
241 if (stream_blob_to_fd(fileno(f), obj->sha1, NULL, 1))
242 die_errno("Could not write '%s'", filename);
243 } else
244 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
245 if (fclose(f))
246 die_errno("Could not finish '%s'",
247 filename);
249 return;
253 * Otherwise? It's there, it's unreachable, and some other unreachable
254 * object points to it. Ignore it - it's not interesting, and we showed
255 * all the interesting cases above.
259 static void check_object(struct object *obj)
261 if (verbose)
262 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
264 if (obj->flags & REACHABLE)
265 check_reachable_object(obj);
266 else
267 check_unreachable_object(obj);
270 static void check_connectivity(void)
272 int i, max;
274 /* Traverse the pending reachable objects */
275 traverse_reachable();
277 /* Look up all the requirements, warn about missing objects.. */
278 max = get_max_object_index();
279 if (verbose)
280 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
282 for (i = 0; i < max; i++) {
283 struct object *obj = get_indexed_object(i);
285 if (obj)
286 check_object(obj);
290 static int fsck_obj(struct object *obj)
292 if (obj->flags & SEEN)
293 return 0;
294 obj->flags |= SEEN;
296 if (verbose)
297 fprintf(stderr, "Checking %s %s\n",
298 typename(obj->type), sha1_to_hex(obj->sha1));
300 if (fsck_walk(obj, mark_used, NULL))
301 objerror(obj, "broken links");
302 if (fsck_object(obj, NULL, 0, check_strict, fsck_error_func))
303 return -1;
305 if (obj->type == OBJ_TREE) {
306 struct tree *item = (struct tree *) obj;
308 free_tree_buffer(item);
311 if (obj->type == OBJ_COMMIT) {
312 struct commit *commit = (struct commit *) obj;
314 free_commit_buffer(commit);
316 if (!commit->parents && show_root)
317 printf("root %s\n", sha1_to_hex(commit->object.sha1));
320 if (obj->type == OBJ_TAG) {
321 struct tag *tag = (struct tag *) obj;
323 if (show_tags && tag->tagged) {
324 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
325 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
329 return 0;
332 static int fsck_sha1(const unsigned char *sha1)
334 struct object *obj = parse_object(sha1);
335 if (!obj) {
336 errors_found |= ERROR_OBJECT;
337 return error("%s: object corrupt or missing",
338 sha1_to_hex(sha1));
340 obj->flags |= HAS_OBJ;
341 return fsck_obj(obj);
344 static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
345 unsigned long size, void *buffer, int *eaten)
347 struct object *obj;
348 obj = parse_object_buffer(sha1, type, size, buffer, eaten);
349 if (!obj) {
350 errors_found |= ERROR_OBJECT;
351 return error("%s: object corrupt or missing", sha1_to_hex(sha1));
353 obj->flags = HAS_OBJ;
354 return fsck_obj(obj);
358 * This is the sorting chunk size: make it reasonably
359 * big so that we can sort well..
361 #define MAX_SHA1_ENTRIES (1024)
363 struct sha1_entry {
364 unsigned long ino;
365 unsigned char sha1[20];
368 static struct {
369 unsigned long nr;
370 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
371 } sha1_list;
373 static int ino_compare(const void *_a, const void *_b)
375 const struct sha1_entry *a = _a, *b = _b;
376 unsigned long ino1 = a->ino, ino2 = b->ino;
377 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
380 static void fsck_sha1_list(void)
382 int i, nr = sha1_list.nr;
384 if (SORT_DIRENT)
385 qsort(sha1_list.entry, nr,
386 sizeof(struct sha1_entry *), ino_compare);
387 for (i = 0; i < nr; i++) {
388 struct sha1_entry *entry = sha1_list.entry[i];
389 unsigned char *sha1 = entry->sha1;
391 sha1_list.entry[i] = NULL;
392 if (fsck_sha1(sha1))
393 errors_found |= ERROR_OBJECT;
394 free(entry);
396 sha1_list.nr = 0;
399 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
401 struct sha1_entry *entry = xmalloc(sizeof(*entry));
402 int nr;
404 entry->ino = ino;
405 hashcpy(entry->sha1, sha1);
406 nr = sha1_list.nr;
407 if (nr == MAX_SHA1_ENTRIES) {
408 fsck_sha1_list();
409 nr = 0;
411 sha1_list.entry[nr] = entry;
412 sha1_list.nr = ++nr;
415 static inline int is_loose_object_file(struct dirent *de,
416 char *name, unsigned char *sha1)
418 if (strlen(de->d_name) != 38)
419 return 0;
420 memcpy(name + 2, de->d_name, 39);
421 return !get_sha1_hex(name, sha1);
424 static void fsck_dir(int i, char *path)
426 DIR *dir = opendir(path);
427 struct dirent *de;
428 char name[100];
430 if (!dir)
431 return;
433 if (verbose)
434 fprintf(stderr, "Checking directory %s\n", path);
436 sprintf(name, "%02x", i);
437 while ((de = readdir(dir)) != NULL) {
438 unsigned char sha1[20];
440 if (is_dot_or_dotdot(de->d_name))
441 continue;
442 if (is_loose_object_file(de, name, sha1)) {
443 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
444 continue;
446 if (starts_with(de->d_name, "tmp_obj_"))
447 continue;
448 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
450 closedir(dir);
453 static int default_refs;
455 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
456 const char *email, unsigned long timestamp, int tz,
457 const char *message, void *cb_data)
459 struct object *obj;
461 if (verbose)
462 fprintf(stderr, "Checking reflog %s->%s\n",
463 sha1_to_hex(osha1), sha1_to_hex(nsha1));
465 if (!is_null_sha1(osha1)) {
466 obj = lookup_object(osha1);
467 if (obj) {
468 obj->used = 1;
469 mark_object_reachable(obj);
472 obj = lookup_object(nsha1);
473 if (obj) {
474 obj->used = 1;
475 mark_object_reachable(obj);
477 return 0;
480 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
481 int flag, void *cb_data)
483 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
484 return 0;
487 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
488 int flag, void *cb_data)
490 struct object *obj;
492 obj = parse_object(oid->hash);
493 if (!obj) {
494 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
495 errors_found |= ERROR_REACHABLE;
496 /* We'll continue with the rest despite the error.. */
497 return 0;
499 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
500 error("%s: not a commit", refname);
501 errors_found |= ERROR_REFS;
503 default_refs++;
504 obj->used = 1;
505 mark_object_reachable(obj);
507 return 0;
510 static void get_default_heads(void)
512 if (head_points_at && !is_null_oid(&head_oid))
513 fsck_handle_ref("HEAD", &head_oid, 0, NULL);
514 for_each_rawref(fsck_handle_ref, NULL);
515 if (include_reflogs)
516 for_each_reflog(fsck_handle_reflog, NULL);
519 * Not having any default heads isn't really fatal, but
520 * it does mean that "--unreachable" no longer makes any
521 * sense (since in this case everything will obviously
522 * be unreachable by definition.
524 * Showing dangling objects is valid, though (as those
525 * dangling objects are likely lost heads).
527 * So we just print a warning about it, and clear the
528 * "show_unreachable" flag.
530 if (!default_refs) {
531 fprintf(stderr, "notice: No default references\n");
532 show_unreachable = 0;
536 static void fsck_object_dir(const char *path)
538 int i;
539 struct progress *progress = NULL;
541 if (verbose)
542 fprintf(stderr, "Checking object directory\n");
544 if (show_progress)
545 progress = start_progress(_("Checking object directories"), 256);
546 for (i = 0; i < 256; i++) {
547 static char dir[4096];
548 sprintf(dir, "%s/%02x", path, i);
549 fsck_dir(i, dir);
550 display_progress(progress, i+1);
552 stop_progress(&progress);
553 fsck_sha1_list();
556 static int fsck_head_link(void)
558 int flag;
559 int null_is_error = 0;
561 if (verbose)
562 fprintf(stderr, "Checking HEAD link\n");
564 head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
565 if (!head_points_at) {
566 errors_found |= ERROR_REFS;
567 return error("Invalid HEAD");
569 if (!strcmp(head_points_at, "HEAD"))
570 /* detached HEAD */
571 null_is_error = 1;
572 else if (!starts_with(head_points_at, "refs/heads/")) {
573 errors_found |= ERROR_REFS;
574 return error("HEAD points to something strange (%s)",
575 head_points_at);
577 if (is_null_oid(&head_oid)) {
578 if (null_is_error) {
579 errors_found |= ERROR_REFS;
580 return error("HEAD: detached HEAD points at nothing");
582 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
583 head_points_at + 11);
585 return 0;
588 static int fsck_cache_tree(struct cache_tree *it)
590 int i;
591 int err = 0;
593 if (verbose)
594 fprintf(stderr, "Checking cache tree\n");
596 if (0 <= it->entry_count) {
597 struct object *obj = parse_object(it->sha1);
598 if (!obj) {
599 error("%s: invalid sha1 pointer in cache-tree",
600 sha1_to_hex(it->sha1));
601 errors_found |= ERROR_REFS;
602 return 1;
604 obj->used = 1;
605 mark_object_reachable(obj);
606 if (obj->type != OBJ_TREE)
607 err |= objerror(obj, "non-tree in cache-tree");
609 for (i = 0; i < it->subtree_nr; i++)
610 err |= fsck_cache_tree(it->down[i]->cache_tree);
611 return err;
614 static char const * const fsck_usage[] = {
615 N_("git fsck [<options>] [<object>...]"),
616 NULL
619 static struct option fsck_opts[] = {
620 OPT__VERBOSE(&verbose, N_("be verbose")),
621 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
622 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
623 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
624 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
625 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
626 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
627 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
628 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
629 OPT_BOOL(0, "lost-found", &write_lost_and_found,
630 N_("write dangling objects in .git/lost-found")),
631 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
632 OPT_END(),
635 int cmd_fsck(int argc, const char **argv, const char *prefix)
637 int i, heads;
638 struct alternate_object_database *alt;
640 errors_found = 0;
641 check_replace_refs = 0;
643 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
645 if (show_progress == -1)
646 show_progress = isatty(2);
647 if (verbose)
648 show_progress = 0;
650 if (write_lost_and_found) {
651 check_full = 1;
652 include_reflogs = 0;
655 fsck_head_link();
656 fsck_object_dir(get_object_directory());
658 prepare_alt_odb();
659 for (alt = alt_odb_list; alt; alt = alt->next) {
660 char namebuf[PATH_MAX];
661 int namelen = alt->name - alt->base;
662 memcpy(namebuf, alt->base, namelen);
663 namebuf[namelen - 1] = 0;
664 fsck_object_dir(namebuf);
667 if (check_full) {
668 struct packed_git *p;
669 uint32_t total = 0, count = 0;
670 struct progress *progress = NULL;
672 prepare_packed_git();
674 if (show_progress) {
675 for (p = packed_git; p; p = p->next) {
676 if (open_pack_index(p))
677 continue;
678 total += p->num_objects;
681 progress = start_progress(_("Checking objects"), total);
683 for (p = packed_git; p; p = p->next) {
684 /* verify gives error messages itself */
685 if (verify_pack(p, fsck_obj_buffer,
686 progress, count))
687 errors_found |= ERROR_PACK;
688 count += p->num_objects;
690 stop_progress(&progress);
693 heads = 0;
694 for (i = 0; i < argc; i++) {
695 const char *arg = argv[i];
696 unsigned char sha1[20];
697 if (!get_sha1(arg, sha1)) {
698 struct object *obj = lookup_object(sha1);
700 /* Error is printed by lookup_object(). */
701 if (!obj)
702 continue;
704 obj->used = 1;
705 mark_object_reachable(obj);
706 heads++;
707 continue;
709 error("invalid parameter: expected sha1, got '%s'", arg);
713 * If we've not been given any explicit head information, do the
714 * default ones from .git/refs. We also consider the index file
715 * in this case (ie this implies --cache).
717 if (!heads) {
718 get_default_heads();
719 keep_cache_objects = 1;
722 if (keep_cache_objects) {
723 read_cache();
724 for (i = 0; i < active_nr; i++) {
725 unsigned int mode;
726 struct blob *blob;
727 struct object *obj;
729 mode = active_cache[i]->ce_mode;
730 if (S_ISGITLINK(mode))
731 continue;
732 blob = lookup_blob(active_cache[i]->sha1);
733 if (!blob)
734 continue;
735 obj = &blob->object;
736 obj->used = 1;
737 mark_object_reachable(obj);
739 if (active_cache_tree)
740 fsck_cache_tree(active_cache_tree);
743 check_connectivity();
744 return errors_found;