Make some of fwrite/fclose/write/close failures visible
[git/dscho.git] / builtin-fsck.c
blobafded5e68db35cb5eca789e948bc1cb2ef2de0bc
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"
14 #define REACHABLE 0x0001
15 #define SEEN 0x0002
17 static int show_root;
18 static int show_tags;
19 static int show_unreachable;
20 static int include_reflogs = 1;
21 static int check_full;
22 static int check_strict;
23 static int keep_cache_objects;
24 static unsigned char head_sha1[20];
25 static int errors_found;
26 static int write_lost_and_found;
27 static int verbose;
28 #define ERROR_OBJECT 01
29 #define ERROR_REACHABLE 02
31 #ifdef NO_D_INO_IN_DIRENT
32 #define SORT_DIRENT 0
33 #define DIRENT_SORT_HINT(de) 0
34 #else
35 #define SORT_DIRENT 1
36 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
37 #endif
39 static void objreport(struct object *obj, const char *severity,
40 const char *err, va_list params)
42 fprintf(stderr, "%s in %s %s: ",
43 severity, typename(obj->type), sha1_to_hex(obj->sha1));
44 vfprintf(stderr, err, params);
45 fputs("\n", stderr);
48 static int objerror(struct object *obj, const char *err, ...)
50 va_list params;
51 va_start(params, err);
52 errors_found |= ERROR_OBJECT;
53 objreport(obj, "error", err, params);
54 va_end(params);
55 return -1;
58 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
60 va_list params;
61 va_start(params, err);
62 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
63 va_end(params);
64 return (type == FSCK_WARN) ? 0 : 1;
67 static int mark_object(struct object *obj, int type, void *data)
69 struct tree *tree = NULL;
70 struct object *parent = data;
71 int result;
73 if (!obj) {
74 printf("broken link from %7s %s\n",
75 typename(parent->type), sha1_to_hex(parent->sha1));
76 printf("broken link from %7s %s\n",
77 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
78 errors_found |= ERROR_REACHABLE;
79 return 1;
82 if (type != OBJ_ANY && obj->type != type)
83 objerror(parent, "wrong object type in link");
85 if (obj->flags & REACHABLE)
86 return 0;
87 obj->flags |= REACHABLE;
88 if (!obj->parsed) {
89 if (parent && !has_sha1_file(obj->sha1)) {
90 printf("broken link from %7s %s\n",
91 typename(parent->type), sha1_to_hex(parent->sha1));
92 printf(" to %7s %s\n",
93 typename(obj->type), sha1_to_hex(obj->sha1));
94 errors_found |= ERROR_REACHABLE;
96 return 1;
99 if (obj->type == OBJ_TREE) {
100 obj->parsed = 0;
101 tree = (struct tree *)obj;
102 if (parse_tree(tree) < 0)
103 return 1; /* error already displayed */
105 result = fsck_walk(obj, mark_object, obj);
106 if (tree) {
107 free(tree->buffer);
108 tree->buffer = NULL;
110 if (result < 0)
111 result = 1;
113 return result;
116 static void mark_object_reachable(struct object *obj)
118 mark_object(obj, OBJ_ANY, 0);
121 static int mark_used(struct object *obj, int type, void *data)
123 if (!obj)
124 return 1;
125 obj->used = 1;
126 return 0;
130 * Check a single reachable object
132 static void check_reachable_object(struct object *obj)
135 * We obviously want the object to be parsed,
136 * except if it was in a pack-file and we didn't
137 * do a full fsck
139 if (!obj->parsed) {
140 if (has_sha1_pack(obj->sha1, NULL))
141 return; /* it is in pack - forget about it */
142 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
143 errors_found |= ERROR_REACHABLE;
144 return;
149 * Check a single unreachable object
151 static void check_unreachable_object(struct object *obj)
154 * Missing unreachable object? Ignore it. It's not like
155 * we miss it (since it can't be reached), nor do we want
156 * to complain about it being unreachable (since it does
157 * not exist).
159 if (!obj->parsed)
160 return;
163 * Unreachable object that exists? Show it if asked to,
164 * since this is something that is prunable.
166 if (show_unreachable) {
167 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
168 return;
172 * "!used" means that nothing at all points to it, including
173 * other unreachable objects. In other words, it's the "tip"
174 * of some set of unreachable objects, usually a commit that
175 * got dropped.
177 * Such starting points are more interesting than some random
178 * set of unreachable objects, so we show them even if the user
179 * hasn't asked for _all_ unreachable objects. If you have
180 * deleted a branch by mistake, this is a prime candidate to
181 * start looking at, for example.
183 if (!obj->used) {
184 printf("dangling %s %s\n", typename(obj->type),
185 sha1_to_hex(obj->sha1));
186 if (write_lost_and_found) {
187 char *filename = git_path("lost-found/%s/%s",
188 obj->type == OBJ_COMMIT ? "commit" : "other",
189 sha1_to_hex(obj->sha1));
190 FILE *f;
192 if (safe_create_leading_directories(filename)) {
193 error("Could not create lost-found");
194 return;
196 if (!(f = fopen(filename, "w")))
197 die("Could not open %s", filename);
198 if (obj->type == OBJ_BLOB) {
199 enum object_type type;
200 unsigned long size;
201 char *buf = read_sha1_file(obj->sha1,
202 &type, &size);
203 if (buf) {
204 if (fwrite(buf, size, 1, f) != 1)
205 die("Could not write %s: %s",
206 filename, strerror(errno));
207 free(buf);
209 } else
210 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
211 if (fclose(f))
212 die("Could not finish %s: %s",
213 filename, strerror(errno));
215 return;
219 * Otherwise? It's there, it's unreachable, and some other unreachable
220 * object points to it. Ignore it - it's not interesting, and we showed
221 * all the interesting cases above.
225 static void check_object(struct object *obj)
227 if (verbose)
228 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
230 if (obj->flags & REACHABLE)
231 check_reachable_object(obj);
232 else
233 check_unreachable_object(obj);
236 static void check_connectivity(void)
238 int i, max;
240 /* Look up all the requirements, warn about missing objects.. */
241 max = get_max_object_index();
242 if (verbose)
243 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
245 for (i = 0; i < max; i++) {
246 struct object *obj = get_indexed_object(i);
248 if (obj)
249 check_object(obj);
253 static int fsck_sha1(const unsigned char *sha1)
255 struct object *obj = parse_object(sha1);
256 if (!obj) {
257 errors_found |= ERROR_OBJECT;
258 return error("%s: object corrupt or missing",
259 sha1_to_hex(sha1));
261 if (obj->flags & SEEN)
262 return 0;
263 obj->flags |= SEEN;
265 if (verbose)
266 fprintf(stderr, "Checking %s %s\n",
267 typename(obj->type), sha1_to_hex(obj->sha1));
269 if (fsck_walk(obj, mark_used, 0))
270 objerror(obj, "broken links");
271 if (fsck_object(obj, check_strict, fsck_error_func))
272 return -1;
274 if (obj->type == OBJ_TREE) {
275 struct tree *item = (struct tree *) obj;
277 free(item->buffer);
278 item->buffer = NULL;
281 if (obj->type == OBJ_COMMIT) {
282 struct commit *commit = (struct commit *) obj;
284 free(commit->buffer);
285 commit->buffer = NULL;
287 if (!commit->parents && show_root)
288 printf("root %s\n", sha1_to_hex(commit->object.sha1));
291 if (obj->type == OBJ_TAG) {
292 struct tag *tag = (struct tag *) obj;
294 if (show_tags && tag->tagged) {
295 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
296 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
300 return 0;
304 * This is the sorting chunk size: make it reasonably
305 * big so that we can sort well..
307 #define MAX_SHA1_ENTRIES (1024)
309 struct sha1_entry {
310 unsigned long ino;
311 unsigned char sha1[20];
314 static struct {
315 unsigned long nr;
316 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
317 } sha1_list;
319 static int ino_compare(const void *_a, const void *_b)
321 const struct sha1_entry *a = _a, *b = _b;
322 unsigned long ino1 = a->ino, ino2 = b->ino;
323 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
326 static void fsck_sha1_list(void)
328 int i, nr = sha1_list.nr;
330 if (SORT_DIRENT)
331 qsort(sha1_list.entry, nr,
332 sizeof(struct sha1_entry *), ino_compare);
333 for (i = 0; i < nr; i++) {
334 struct sha1_entry *entry = sha1_list.entry[i];
335 unsigned char *sha1 = entry->sha1;
337 sha1_list.entry[i] = NULL;
338 fsck_sha1(sha1);
339 free(entry);
341 sha1_list.nr = 0;
344 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
346 struct sha1_entry *entry = xmalloc(sizeof(*entry));
347 int nr;
349 entry->ino = ino;
350 hashcpy(entry->sha1, sha1);
351 nr = sha1_list.nr;
352 if (nr == MAX_SHA1_ENTRIES) {
353 fsck_sha1_list();
354 nr = 0;
356 sha1_list.entry[nr] = entry;
357 sha1_list.nr = ++nr;
360 static void fsck_dir(int i, char *path)
362 DIR *dir = opendir(path);
363 struct dirent *de;
365 if (!dir)
366 return;
368 if (verbose)
369 fprintf(stderr, "Checking directory %s\n", path);
371 while ((de = readdir(dir)) != NULL) {
372 char name[100];
373 unsigned char sha1[20];
374 int len = strlen(de->d_name);
376 switch (len) {
377 case 2:
378 if (de->d_name[1] != '.')
379 break;
380 case 1:
381 if (de->d_name[0] != '.')
382 break;
383 continue;
384 case 38:
385 sprintf(name, "%02x", i);
386 memcpy(name+2, de->d_name, len+1);
387 if (get_sha1_hex(name, sha1) < 0)
388 break;
389 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
390 continue;
392 if (!prefixcmp(de->d_name, "tmp_obj_"))
393 continue;
394 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
396 closedir(dir);
399 static int default_refs;
401 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
402 const char *email, unsigned long timestamp, int tz,
403 const char *message, void *cb_data)
405 struct object *obj;
407 if (verbose)
408 fprintf(stderr, "Checking reflog %s->%s\n",
409 sha1_to_hex(osha1), sha1_to_hex(nsha1));
411 if (!is_null_sha1(osha1)) {
412 obj = lookup_object(osha1);
413 if (obj) {
414 obj->used = 1;
415 mark_object_reachable(obj);
418 obj = lookup_object(nsha1);
419 if (obj) {
420 obj->used = 1;
421 mark_object_reachable(obj);
423 return 0;
426 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
428 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
429 return 0;
432 static int is_branch(const char *refname)
434 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
437 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
439 struct object *obj;
441 obj = parse_object(sha1);
442 if (!obj) {
443 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
444 /* We'll continue with the rest despite the error.. */
445 return 0;
447 if (obj->type != OBJ_COMMIT && is_branch(refname))
448 error("%s: not a commit", refname);
449 default_refs++;
450 obj->used = 1;
451 mark_object_reachable(obj);
453 return 0;
456 static void get_default_heads(void)
458 for_each_ref(fsck_handle_ref, NULL);
459 if (include_reflogs)
460 for_each_reflog(fsck_handle_reflog, NULL);
463 * Not having any default heads isn't really fatal, but
464 * it does mean that "--unreachable" no longer makes any
465 * sense (since in this case everything will obviously
466 * be unreachable by definition.
468 * Showing dangling objects is valid, though (as those
469 * dangling objects are likely lost heads).
471 * So we just print a warning about it, and clear the
472 * "show_unreachable" flag.
474 if (!default_refs) {
475 fprintf(stderr, "notice: No default references\n");
476 show_unreachable = 0;
480 static void fsck_object_dir(const char *path)
482 int i;
484 if (verbose)
485 fprintf(stderr, "Checking object directory\n");
487 for (i = 0; i < 256; i++) {
488 static char dir[4096];
489 sprintf(dir, "%s/%02x", path, i);
490 fsck_dir(i, dir);
492 fsck_sha1_list();
495 static int fsck_head_link(void)
497 unsigned char sha1[20];
498 int flag;
499 int null_is_error = 0;
500 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
502 if (verbose)
503 fprintf(stderr, "Checking HEAD link\n");
505 if (!head_points_at)
506 return error("Invalid HEAD");
507 if (!strcmp(head_points_at, "HEAD"))
508 /* detached HEAD */
509 null_is_error = 1;
510 else if (prefixcmp(head_points_at, "refs/heads/"))
511 return error("HEAD points to something strange (%s)",
512 head_points_at);
513 if (is_null_sha1(sha1)) {
514 if (null_is_error)
515 return error("HEAD: detached HEAD points at nothing");
516 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
517 head_points_at + 11);
519 return 0;
522 static int fsck_cache_tree(struct cache_tree *it)
524 int i;
525 int err = 0;
527 if (verbose)
528 fprintf(stderr, "Checking cache tree\n");
530 if (0 <= it->entry_count) {
531 struct object *obj = parse_object(it->sha1);
532 if (!obj) {
533 error("%s: invalid sha1 pointer in cache-tree",
534 sha1_to_hex(it->sha1));
535 return 1;
537 mark_object_reachable(obj);
538 obj->used = 1;
539 if (obj->type != OBJ_TREE)
540 err |= objerror(obj, "non-tree in cache-tree");
542 for (i = 0; i < it->subtree_nr; i++)
543 err |= fsck_cache_tree(it->down[i]->cache_tree);
544 return err;
547 static char const * const fsck_usage[] = {
548 "git fsck [options] [<object>...]",
549 NULL
552 static struct option fsck_opts[] = {
553 OPT__VERBOSE(&verbose),
554 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
555 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
556 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
557 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
558 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
559 OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
560 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
561 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
562 "write dangling objects in .git/lost-found"),
563 OPT_END(),
566 int cmd_fsck(int argc, const char **argv, const char *prefix)
568 int i, heads;
570 errors_found = 0;
572 argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
573 if (write_lost_and_found) {
574 check_full = 1;
575 include_reflogs = 0;
578 fsck_head_link();
579 fsck_object_dir(get_object_directory());
580 if (check_full) {
581 struct alternate_object_database *alt;
582 struct packed_git *p;
583 prepare_alt_odb();
584 for (alt = alt_odb_list; alt; alt = alt->next) {
585 char namebuf[PATH_MAX];
586 int namelen = alt->name - alt->base;
587 memcpy(namebuf, alt->base, namelen);
588 namebuf[namelen - 1] = 0;
589 fsck_object_dir(namebuf);
591 prepare_packed_git();
592 for (p = packed_git; p; p = p->next)
593 /* verify gives error messages itself */
594 verify_pack(p);
596 for (p = packed_git; p; p = p->next) {
597 uint32_t j, num;
598 if (open_pack_index(p))
599 continue;
600 num = p->num_objects;
601 for (j = 0; j < num; j++)
602 fsck_sha1(nth_packed_object_sha1(p, j));
606 heads = 0;
607 for (i = 1; i < argc; i++) {
608 const char *arg = argv[i];
609 if (!get_sha1(arg, head_sha1)) {
610 struct object *obj = lookup_object(head_sha1);
612 /* Error is printed by lookup_object(). */
613 if (!obj)
614 continue;
616 obj->used = 1;
617 mark_object_reachable(obj);
618 heads++;
619 continue;
621 error("invalid parameter: expected sha1, got '%s'", arg);
625 * If we've not been given any explicit head information, do the
626 * default ones from .git/refs. We also consider the index file
627 * in this case (ie this implies --cache).
629 if (!heads) {
630 get_default_heads();
631 keep_cache_objects = 1;
634 if (keep_cache_objects) {
635 read_cache();
636 for (i = 0; i < active_nr; i++) {
637 unsigned int mode;
638 struct blob *blob;
639 struct object *obj;
641 mode = active_cache[i]->ce_mode;
642 if (S_ISGITLINK(mode))
643 continue;
644 blob = lookup_blob(active_cache[i]->sha1);
645 if (!blob)
646 continue;
647 obj = &blob->object;
648 obj->used = 1;
649 mark_object_reachable(obj);
651 if (active_cache_tree)
652 fsck_cache_tree(active_cache_tree);
655 check_connectivity();
656 return errors_found;