git_extract_argv0_path(): Move check for valid argv0 from caller to callee
[git/mjg.git] / builtin-fsck.c
blobaecc8280a0aedb37437ccce446f3382ccba08829
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"
15 #define REACHABLE 0x0001
16 #define SEEN 0x0002
18 static int show_root;
19 static int show_tags;
20 static int show_unreachable;
21 static int include_reflogs = 1;
22 static int check_full;
23 static int check_strict;
24 static int keep_cache_objects;
25 static unsigned char head_sha1[20];
26 static int errors_found;
27 static int write_lost_and_found;
28 static int verbose;
29 #define ERROR_OBJECT 01
30 #define ERROR_REACHABLE 02
32 #ifdef NO_D_INO_IN_DIRENT
33 #define SORT_DIRENT 0
34 #define DIRENT_SORT_HINT(de) 0
35 #else
36 #define SORT_DIRENT 1
37 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
38 #endif
40 static void objreport(struct object *obj, const char *severity,
41 const char *err, va_list params)
43 fprintf(stderr, "%s in %s %s: ",
44 severity, typename(obj->type), sha1_to_hex(obj->sha1));
45 vfprintf(stderr, err, params);
46 fputs("\n", stderr);
49 static int objerror(struct object *obj, const char *err, ...)
51 va_list params;
52 va_start(params, err);
53 errors_found |= ERROR_OBJECT;
54 objreport(obj, "error", err, params);
55 va_end(params);
56 return -1;
59 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
61 va_list params;
62 va_start(params, err);
63 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
64 va_end(params);
65 return (type == FSCK_WARN) ? 0 : 1;
68 static struct object_array pending;
70 static int mark_object(struct object *obj, int type, void *data)
72 struct object *parent = data;
74 if (!obj) {
75 printf("broken link from %7s %s\n",
76 typename(parent->type), sha1_to_hex(parent->sha1));
77 printf("broken link from %7s %s\n",
78 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
79 errors_found |= ERROR_REACHABLE;
80 return 1;
83 if (type != OBJ_ANY && obj->type != type)
84 objerror(parent, "wrong object type in link");
86 if (obj->flags & REACHABLE)
87 return 0;
88 obj->flags |= REACHABLE;
89 if (!obj->parsed) {
90 if (parent && !has_sha1_file(obj->sha1)) {
91 printf("broken link from %7s %s\n",
92 typename(parent->type), sha1_to_hex(parent->sha1));
93 printf(" to %7s %s\n",
94 typename(obj->type), sha1_to_hex(obj->sha1));
95 errors_found |= ERROR_REACHABLE;
97 return 1;
100 add_object_array(obj, (void *) parent, &pending);
101 return 0;
104 static void mark_object_reachable(struct object *obj)
106 mark_object(obj, OBJ_ANY, 0);
109 static int traverse_one_object(struct object *obj, struct object *parent)
111 int result;
112 struct tree *tree = NULL;
114 if (obj->type == OBJ_TREE) {
115 obj->parsed = 0;
116 tree = (struct tree *)obj;
117 if (parse_tree(tree) < 0)
118 return 1; /* error already displayed */
120 result = fsck_walk(obj, mark_object, obj);
121 if (tree) {
122 free(tree->buffer);
123 tree->buffer = NULL;
125 return result;
128 static int traverse_reachable(void)
130 int result = 0;
131 while (pending.nr) {
132 struct object_array_entry *entry;
133 struct object *obj, *parent;
135 entry = pending.objects + --pending.nr;
136 obj = entry->item;
137 parent = (struct object *) entry->name;
138 result |= traverse_one_object(obj, parent);
140 return !!result;
143 static int mark_used(struct object *obj, int type, void *data)
145 if (!obj)
146 return 1;
147 obj->used = 1;
148 return 0;
152 * Check a single reachable object
154 static void check_reachable_object(struct object *obj)
157 * We obviously want the object to be parsed,
158 * except if it was in a pack-file and we didn't
159 * do a full fsck
161 if (!obj->parsed) {
162 if (has_sha1_pack(obj->sha1, NULL))
163 return; /* it is in pack - forget about it */
164 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
165 errors_found |= ERROR_REACHABLE;
166 return;
171 * Check a single unreachable object
173 static void check_unreachable_object(struct object *obj)
176 * Missing unreachable object? Ignore it. It's not like
177 * we miss it (since it can't be reached), nor do we want
178 * to complain about it being unreachable (since it does
179 * not exist).
181 if (!obj->parsed)
182 return;
185 * Unreachable object that exists? Show it if asked to,
186 * since this is something that is prunable.
188 if (show_unreachable) {
189 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
190 return;
194 * "!used" means that nothing at all points to it, including
195 * other unreachable objects. In other words, it's the "tip"
196 * of some set of unreachable objects, usually a commit that
197 * got dropped.
199 * Such starting points are more interesting than some random
200 * set of unreachable objects, so we show them even if the user
201 * hasn't asked for _all_ unreachable objects. If you have
202 * deleted a branch by mistake, this is a prime candidate to
203 * start looking at, for example.
205 if (!obj->used) {
206 printf("dangling %s %s\n", typename(obj->type),
207 sha1_to_hex(obj->sha1));
208 if (write_lost_and_found) {
209 char *filename = git_path("lost-found/%s/%s",
210 obj->type == OBJ_COMMIT ? "commit" : "other",
211 sha1_to_hex(obj->sha1));
212 FILE *f;
214 if (safe_create_leading_directories(filename)) {
215 error("Could not create lost-found");
216 return;
218 if (!(f = fopen(filename, "w")))
219 die("Could not open %s", filename);
220 if (obj->type == OBJ_BLOB) {
221 enum object_type type;
222 unsigned long size;
223 char *buf = read_sha1_file(obj->sha1,
224 &type, &size);
225 if (buf) {
226 if (fwrite(buf, size, 1, f) != 1)
227 die("Could not write %s: %s",
228 filename, strerror(errno));
229 free(buf);
231 } else
232 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
233 if (fclose(f))
234 die("Could not finish %s: %s",
235 filename, strerror(errno));
237 return;
241 * Otherwise? It's there, it's unreachable, and some other unreachable
242 * object points to it. Ignore it - it's not interesting, and we showed
243 * all the interesting cases above.
247 static void check_object(struct object *obj)
249 if (verbose)
250 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
252 if (obj->flags & REACHABLE)
253 check_reachable_object(obj);
254 else
255 check_unreachable_object(obj);
258 static void check_connectivity(void)
260 int i, max;
262 /* Traverse the pending reachable objects */
263 traverse_reachable();
265 /* Look up all the requirements, warn about missing objects.. */
266 max = get_max_object_index();
267 if (verbose)
268 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
270 for (i = 0; i < max; i++) {
271 struct object *obj = get_indexed_object(i);
273 if (obj)
274 check_object(obj);
278 static int fsck_sha1(const unsigned char *sha1)
280 struct object *obj = parse_object(sha1);
281 if (!obj) {
282 errors_found |= ERROR_OBJECT;
283 return error("%s: object corrupt or missing",
284 sha1_to_hex(sha1));
286 if (obj->flags & SEEN)
287 return 0;
288 obj->flags |= SEEN;
290 if (verbose)
291 fprintf(stderr, "Checking %s %s\n",
292 typename(obj->type), sha1_to_hex(obj->sha1));
294 if (fsck_walk(obj, mark_used, 0))
295 objerror(obj, "broken links");
296 if (fsck_object(obj, check_strict, fsck_error_func))
297 return -1;
299 if (obj->type == OBJ_TREE) {
300 struct tree *item = (struct tree *) obj;
302 free(item->buffer);
303 item->buffer = NULL;
306 if (obj->type == OBJ_COMMIT) {
307 struct commit *commit = (struct commit *) obj;
309 free(commit->buffer);
310 commit->buffer = NULL;
312 if (!commit->parents && show_root)
313 printf("root %s\n", sha1_to_hex(commit->object.sha1));
316 if (obj->type == OBJ_TAG) {
317 struct tag *tag = (struct tag *) obj;
319 if (show_tags && tag->tagged) {
320 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
321 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
325 return 0;
329 * This is the sorting chunk size: make it reasonably
330 * big so that we can sort well..
332 #define MAX_SHA1_ENTRIES (1024)
334 struct sha1_entry {
335 unsigned long ino;
336 unsigned char sha1[20];
339 static struct {
340 unsigned long nr;
341 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
342 } sha1_list;
344 static int ino_compare(const void *_a, const void *_b)
346 const struct sha1_entry *a = _a, *b = _b;
347 unsigned long ino1 = a->ino, ino2 = b->ino;
348 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
351 static void fsck_sha1_list(void)
353 int i, nr = sha1_list.nr;
355 if (SORT_DIRENT)
356 qsort(sha1_list.entry, nr,
357 sizeof(struct sha1_entry *), ino_compare);
358 for (i = 0; i < nr; i++) {
359 struct sha1_entry *entry = sha1_list.entry[i];
360 unsigned char *sha1 = entry->sha1;
362 sha1_list.entry[i] = NULL;
363 fsck_sha1(sha1);
364 free(entry);
366 sha1_list.nr = 0;
369 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
371 struct sha1_entry *entry = xmalloc(sizeof(*entry));
372 int nr;
374 entry->ino = ino;
375 hashcpy(entry->sha1, sha1);
376 nr = sha1_list.nr;
377 if (nr == MAX_SHA1_ENTRIES) {
378 fsck_sha1_list();
379 nr = 0;
381 sha1_list.entry[nr] = entry;
382 sha1_list.nr = ++nr;
385 static void fsck_dir(int i, char *path)
387 DIR *dir = opendir(path);
388 struct dirent *de;
390 if (!dir)
391 return;
393 if (verbose)
394 fprintf(stderr, "Checking directory %s\n", path);
396 while ((de = readdir(dir)) != NULL) {
397 char name[100];
398 unsigned char sha1[20];
400 if (is_dot_or_dotdot(de->d_name))
401 continue;
402 if (strlen(de->d_name) == 38) {
403 sprintf(name, "%02x", i);
404 memcpy(name+2, de->d_name, 39);
405 if (get_sha1_hex(name, sha1) < 0)
406 break;
407 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
408 continue;
410 if (!prefixcmp(de->d_name, "tmp_obj_"))
411 continue;
412 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
414 closedir(dir);
417 static int default_refs;
419 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
420 const char *email, unsigned long timestamp, int tz,
421 const char *message, void *cb_data)
423 struct object *obj;
425 if (verbose)
426 fprintf(stderr, "Checking reflog %s->%s\n",
427 sha1_to_hex(osha1), sha1_to_hex(nsha1));
429 if (!is_null_sha1(osha1)) {
430 obj = lookup_object(osha1);
431 if (obj) {
432 obj->used = 1;
433 mark_object_reachable(obj);
436 obj = lookup_object(nsha1);
437 if (obj) {
438 obj->used = 1;
439 mark_object_reachable(obj);
441 return 0;
444 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
446 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
447 return 0;
450 static int is_branch(const char *refname)
452 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
455 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
457 struct object *obj;
459 obj = parse_object(sha1);
460 if (!obj) {
461 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
462 /* We'll continue with the rest despite the error.. */
463 return 0;
465 if (obj->type != OBJ_COMMIT && is_branch(refname))
466 error("%s: not a commit", refname);
467 default_refs++;
468 obj->used = 1;
469 mark_object_reachable(obj);
471 return 0;
474 static void get_default_heads(void)
476 for_each_ref(fsck_handle_ref, NULL);
477 if (include_reflogs)
478 for_each_reflog(fsck_handle_reflog, NULL);
481 * Not having any default heads isn't really fatal, but
482 * it does mean that "--unreachable" no longer makes any
483 * sense (since in this case everything will obviously
484 * be unreachable by definition.
486 * Showing dangling objects is valid, though (as those
487 * dangling objects are likely lost heads).
489 * So we just print a warning about it, and clear the
490 * "show_unreachable" flag.
492 if (!default_refs) {
493 fprintf(stderr, "notice: No default references\n");
494 show_unreachable = 0;
498 static void fsck_object_dir(const char *path)
500 int i;
502 if (verbose)
503 fprintf(stderr, "Checking object directory\n");
505 for (i = 0; i < 256; i++) {
506 static char dir[4096];
507 sprintf(dir, "%s/%02x", path, i);
508 fsck_dir(i, dir);
510 fsck_sha1_list();
513 static int fsck_head_link(void)
515 unsigned char sha1[20];
516 int flag;
517 int null_is_error = 0;
518 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
520 if (verbose)
521 fprintf(stderr, "Checking HEAD link\n");
523 if (!head_points_at)
524 return error("Invalid HEAD");
525 if (!strcmp(head_points_at, "HEAD"))
526 /* detached HEAD */
527 null_is_error = 1;
528 else if (prefixcmp(head_points_at, "refs/heads/"))
529 return error("HEAD points to something strange (%s)",
530 head_points_at);
531 if (is_null_sha1(sha1)) {
532 if (null_is_error)
533 return error("HEAD: detached HEAD points at nothing");
534 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
535 head_points_at + 11);
537 return 0;
540 static int fsck_cache_tree(struct cache_tree *it)
542 int i;
543 int err = 0;
545 if (verbose)
546 fprintf(stderr, "Checking cache tree\n");
548 if (0 <= it->entry_count) {
549 struct object *obj = parse_object(it->sha1);
550 if (!obj) {
551 error("%s: invalid sha1 pointer in cache-tree",
552 sha1_to_hex(it->sha1));
553 return 1;
555 mark_object_reachable(obj);
556 obj->used = 1;
557 if (obj->type != OBJ_TREE)
558 err |= objerror(obj, "non-tree in cache-tree");
560 for (i = 0; i < it->subtree_nr; i++)
561 err |= fsck_cache_tree(it->down[i]->cache_tree);
562 return err;
565 static char const * const fsck_usage[] = {
566 "git fsck [options] [<object>...]",
567 NULL
570 static struct option fsck_opts[] = {
571 OPT__VERBOSE(&verbose),
572 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
573 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
574 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
575 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
576 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
577 OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
578 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
579 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
580 "write dangling objects in .git/lost-found"),
581 OPT_END(),
584 int cmd_fsck(int argc, const char **argv, const char *prefix)
586 int i, heads;
588 errors_found = 0;
590 argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
591 if (write_lost_and_found) {
592 check_full = 1;
593 include_reflogs = 0;
596 fsck_head_link();
597 fsck_object_dir(get_object_directory());
598 if (check_full) {
599 struct alternate_object_database *alt;
600 struct packed_git *p;
601 prepare_alt_odb();
602 for (alt = alt_odb_list; alt; alt = alt->next) {
603 char namebuf[PATH_MAX];
604 int namelen = alt->name - alt->base;
605 memcpy(namebuf, alt->base, namelen);
606 namebuf[namelen - 1] = 0;
607 fsck_object_dir(namebuf);
609 prepare_packed_git();
610 for (p = packed_git; p; p = p->next)
611 /* verify gives error messages itself */
612 verify_pack(p);
614 for (p = packed_git; p; p = p->next) {
615 uint32_t j, num;
616 if (open_pack_index(p))
617 continue;
618 num = p->num_objects;
619 for (j = 0; j < num; j++)
620 fsck_sha1(nth_packed_object_sha1(p, j));
624 heads = 0;
625 for (i = 0; i < argc; i++) {
626 const char *arg = argv[i];
627 if (!get_sha1(arg, head_sha1)) {
628 struct object *obj = lookup_object(head_sha1);
630 /* Error is printed by lookup_object(). */
631 if (!obj)
632 continue;
634 obj->used = 1;
635 mark_object_reachable(obj);
636 heads++;
637 continue;
639 error("invalid parameter: expected sha1, got '%s'", arg);
643 * If we've not been given any explicit head information, do the
644 * default ones from .git/refs. We also consider the index file
645 * in this case (ie this implies --cache).
647 if (!heads) {
648 get_default_heads();
649 keep_cache_objects = 1;
652 if (keep_cache_objects) {
653 read_cache();
654 for (i = 0; i < active_nr; i++) {
655 unsigned int mode;
656 struct blob *blob;
657 struct object *obj;
659 mode = active_cache[i]->ce_mode;
660 if (S_ISGITLINK(mode))
661 continue;
662 blob = lookup_blob(active_cache[i]->sha1);
663 if (!blob)
664 continue;
665 obj = &blob->object;
666 obj->used = 1;
667 mark_object_reachable(obj);
669 if (active_cache_tree)
670 fsck_cache_tree(active_cache_tree);
673 check_connectivity();
674 return errors_found;