Remove "tree->entries" tree-entry list from tree parser
[git/git.git] / fsck-objects.c
CommitLineData
20222118
LT
1#include <sys/types.h>
2#include <dirent.h>
3
4b182421 4#include "cache.h"
ff5ebe39
DB
5#include "commit.h"
6#include "tree.h"
7#include "blob.h"
c418eda4 8#include "tag.h"
944d8589 9#include "refs.h"
f9253394 10#include "pack.h"
53dc3f3e 11#include "cache-tree.h"
ff5ebe39
DB
12
13#define REACHABLE 0x0001
2d9c58c6 14#define SEEN 0x0002
d9839e03 15
ab7df187 16static int show_root = 0;
889262ea 17static int show_tags = 0;
d9839e03 18static int show_unreachable = 0;
8a498a05 19static int check_full = 0;
de2eb7f6 20static int check_strict = 0;
7aaa715d 21static int keep_cache_objects = 0;
d9839e03
LT
22static unsigned char head_sha1[20];
23
962554c6 24#ifdef NO_D_INO_IN_DIRENT
35a730f0
JH
25#define SORT_DIRENT 0
26#define DIRENT_SORT_HINT(de) 0
27#else
28#define SORT_DIRENT 1
29#define DIRENT_SORT_HINT(de) ((de)->d_ino)
30#endif
f1f0d088
PB
31
32static void objreport(struct object *obj, const char *severity,
33 const char *err, va_list params)
34{
35 fprintf(stderr, "%s in %s %s: ",
36 severity, obj->type, sha1_to_hex(obj->sha1));
37 vfprintf(stderr, err, params);
38 fputs("\n", stderr);
39}
40
a7928f8e 41static int objerror(struct object *obj, const char *err, ...)
f1f0d088
PB
42{
43 va_list params;
44 va_start(params, err);
45 objreport(obj, "error", err, params);
46 va_end(params);
47 return -1;
48}
49
a7928f8e 50static int objwarning(struct object *obj, const char *err, ...)
f1f0d088
PB
51{
52 va_list params;
53 va_start(params, err);
54 objreport(obj, "warning", err, params);
55 va_end(params);
56 return -1;
57}
58
59
8ba0bbb2
LT
60static void check_connectivity(void)
61{
62 int i;
63
8ba0bbb2 64 /* Look up all the requirements, warn about missing objects.. */
070879ca 65 for (i = 0; i < obj_allocs; i++) {
ff5ebe39 66 struct object *obj = objs[i];
8ba0bbb2 67
070879ca
JS
68 if (!obj)
69 continue;
70
3a6a23e6 71 if (!obj->parsed) {
7aaa715d 72 if (has_sha1_file(obj->sha1))
8a498a05
JH
73 ; /* it is in pack */
74 else
75 printf("missing %s %s\n",
76 obj->type, sha1_to_hex(obj->sha1));
3a6a23e6
LT
77 continue;
78 }
79
4a4e6fd7
SV
80 if (obj->refs) {
81 const struct object_refs *refs = obj->refs;
82 unsigned j;
83 for (j = 0; j < refs->count; j++) {
84 struct object *ref = refs->ref[j];
85 if (ref->parsed ||
7aaa715d 86 (has_sha1_file(ref->sha1)))
4a4e6fd7
SV
87 continue;
88 printf("broken link from %7s %s\n",
89 obj->type, sha1_to_hex(obj->sha1));
90 printf(" to %7s %s\n",
91 ref->type, sha1_to_hex(ref->sha1));
92 }
3a6a23e6
LT
93 }
94
ff5ebe39 95 if (show_unreachable && !(obj->flags & REACHABLE)) {
c4584ae3
JH
96 printf("unreachable %s %s\n",
97 obj->type, sha1_to_hex(obj->sha1));
8ba0bbb2 98 continue;
d9839e03 99 }
8ba0bbb2 100
ff5ebe39
DB
101 if (!obj->used) {
102 printf("dangling %s %s\n", obj->type,
103 sha1_to_hex(obj->sha1));
d9839e03 104 }
8ba0bbb2
LT
105 }
106}
107
85003492
LT
108/*
109 * The entries in a tree are ordered in the _path_ order,
110 * which means that a directory entry is ordered by adding
111 * a slash to the end of it.
112 *
113 * So a directory called "a" is ordered _after_ a file
114 * called "a.c", because "a/" sorts after "a.c".
115 */
a4f35a2d
JH
116#define TREE_UNORDERED (-1)
117#define TREE_HAS_DUPS (-2)
118
85003492
LT
119static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
120{
121 int len1 = strlen(a->name);
122 int len2 = strlen(b->name);
123 int len = len1 < len2 ? len1 : len2;
124 unsigned char c1, c2;
125 int cmp;
126
127 cmp = memcmp(a->name, b->name, len);
128 if (cmp < 0)
129 return 0;
130 if (cmp > 0)
a4f35a2d 131 return TREE_UNORDERED;
85003492
LT
132
133 /*
134 * Ok, the first <len> characters are the same.
135 * Now we need to order the next one, but turn
136 * a '\0' into a '/' for a directory entry.
137 */
138 c1 = a->name[len];
139 c2 = b->name[len];
a4f35a2d
JH
140 if (!c1 && !c2)
141 /*
142 * git-write-tree used to write out a nonsense tree that has
143 * entries with the same name, one blob and one tree. Make
144 * sure we do not have duplicate entries.
145 */
146 return TREE_HAS_DUPS;
85003492
LT
147 if (!c1 && a->directory)
148 c1 = '/';
149 if (!c2 && b->directory)
150 c2 = '/';
a4f35a2d 151 return c1 < c2 ? 0 : TREE_UNORDERED;
85003492
LT
152}
153
c418eda4 154static int fsck_tree(struct tree *item)
20222118 155{
64071805 156 int retval;
85003492 157 int has_full_path = 0;
64071805
LT
158 int has_zero_pad = 0;
159 int has_bad_modes = 0;
160 int has_dup_entries = 0;
161 int not_properly_sorted = 0;
85003492
LT
162 struct tree_entry_list *entry, *last;
163
164 last = NULL;
2d9c58c6 165 for (entry = create_tree_entry_list(item); entry; entry = entry->next) {
85003492
LT
166 if (strchr(entry->name, '/'))
167 has_full_path = 1;
64071805 168 has_zero_pad |= entry->zeropad;
85003492 169
42ea9cb2
LT
170 switch (entry->mode) {
171 /*
172 * Standard modes..
173 */
174 case S_IFREG | 0755:
175 case S_IFREG | 0644:
176 case S_IFLNK:
177 case S_IFDIR:
178 break;
179 /*
180 * This is nonstandard, but we had a few of these
181 * early on when we honored the full set of mode
182 * bits..
183 */
184 case S_IFREG | 0664:
de2eb7f6
LT
185 if (!check_strict)
186 break;
42ea9cb2 187 default:
64071805 188 has_bad_modes = 1;
42ea9cb2
LT
189 }
190
85003492 191 if (last) {
a4f35a2d
JH
192 switch (verify_ordered(last, entry)) {
193 case TREE_UNORDERED:
64071805
LT
194 not_properly_sorted = 1;
195 break;
a4f35a2d 196 case TREE_HAS_DUPS:
64071805
LT
197 has_dup_entries = 1;
198 break;
a4f35a2d
JH
199 default:
200 break;
85003492 201 }
545f229a 202 free(last);
85003492
LT
203 }
204
205 last = entry;
206 }
136f2e54 207 if (last)
545f229a 208 free(last);
136f2e54
LT
209 free(item->buffer);
210 item->buffer = NULL;
85003492 211
64071805 212 retval = 0;
85003492 213 if (has_full_path) {
f1f0d088 214 objwarning(&item->object, "contains full pathnames");
1ea34e36 215 }
64071805 216 if (has_zero_pad) {
f1f0d088 217 objwarning(&item->object, "contains zero-padded file modes");
64071805
LT
218 }
219 if (has_bad_modes) {
f1f0d088 220 objwarning(&item->object, "contains bad file modes");
64071805
LT
221 }
222 if (has_dup_entries) {
f1f0d088 223 retval = objerror(&item->object, "contains duplicate file entries");
64071805
LT
224 }
225 if (not_properly_sorted) {
f1f0d088 226 retval = objerror(&item->object, "not properly sorted");
64071805
LT
227 }
228 return retval;
1ea34e36
LT
229}
230
c418eda4 231static int fsck_commit(struct commit *commit)
1ea34e36 232{
de2eb7f6 233 char *buffer = commit->buffer;
f1f0d088 234 unsigned char tree_sha1[20], sha1[20];
de2eb7f6
LT
235
236 if (memcmp(buffer, "tree ", 5))
f1f0d088
PB
237 return objerror(&commit->object, "invalid format - expected 'tree' line");
238 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
239 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
de2eb7f6
LT
240 buffer += 46;
241 while (!memcmp(buffer, "parent ", 7)) {
242 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
f1f0d088 243 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
de2eb7f6
LT
244 buffer += 48;
245 }
246 if (memcmp(buffer, "author ", 7))
f1f0d088 247 return objerror(&commit->object, "invalid format - expected 'author' line");
bd1e17e2
LT
248 free(commit->buffer);
249 commit->buffer = NULL;
ff5ebe39 250 if (!commit->tree)
f1f0d088 251 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
ab7df187 252 if (!commit->parents && show_root)
c418eda4 253 printf("root %s\n", sha1_to_hex(commit->object.sha1));
e6948b6d 254 if (!commit->date)
c418eda4
DB
255 printf("bad commit date in %s\n",
256 sha1_to_hex(commit->object.sha1));
4728b861
LT
257 return 0;
258}
259
c418eda4 260static int fsck_tag(struct tag *tag)
ec4465ad 261{
92d4c85d
LT
262 struct object *tagged = tag->tagged;
263
264 if (!tagged) {
f1f0d088 265 return objerror(&tag->object, "could not load tagged object");
92d4c85d 266 }
889262ea
LT
267 if (!show_tags)
268 return 0;
269
92d4c85d
LT
270 printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
271 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
ff5ebe39 272 return 0;
20222118
LT
273}
274
7e8c174a 275static int fsck_sha1(unsigned char *sha1)
20222118 276{
7e8c174a
LT
277 struct object *obj = parse_object(sha1);
278 if (!obj)
f1f0d088 279 return error("%s: object not found", sha1_to_hex(sha1));
2d9c58c6
LT
280 if (obj->flags & SEEN)
281 return 0;
282 obj->flags |= SEEN;
7e8c174a
LT
283 if (obj->type == blob_type)
284 return 0;
285 if (obj->type == tree_type)
286 return fsck_tree((struct tree *) obj);
287 if (obj->type == commit_type)
288 return fsck_commit((struct commit *) obj);
289 if (obj->type == tag_type)
290 return fsck_tag((struct tag *) obj);
f1f0d088
PB
291 /* By now, parse_object() would've returned NULL instead. */
292 return objerror(obj, "unknown type '%s' (internal fsck error)", obj->type);
7e8c174a
LT
293}
294
295/*
296 * This is the sorting chunk size: make it reasonably
297 * big so that we can sort well..
298 */
299#define MAX_SHA1_ENTRIES (1024)
300
301struct sha1_entry {
302 unsigned long ino;
20222118 303 unsigned char sha1[20];
7e8c174a
LT
304};
305
306static struct {
307 unsigned long nr;
308 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
309} sha1_list;
310
311static int ino_compare(const void *_a, const void *_b)
312{
313 const struct sha1_entry *a = _a, *b = _b;
314 unsigned long ino1 = a->ino, ino2 = b->ino;
315 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
316}
317
318static void fsck_sha1_list(void)
319{
320 int i, nr = sha1_list.nr;
321
35a730f0
JH
322 if (SORT_DIRENT)
323 qsort(sha1_list.entry, nr,
324 sizeof(struct sha1_entry *), ino_compare);
7e8c174a
LT
325 for (i = 0; i < nr; i++) {
326 struct sha1_entry *entry = sha1_list.entry[i];
327 unsigned char *sha1 = entry->sha1;
328
329 sha1_list.entry[i] = NULL;
f1f0d088 330 fsck_sha1(sha1);
7e8c174a 331 free(entry);
20222118 332 }
7e8c174a
LT
333 sha1_list.nr = 0;
334}
335
336static void add_sha1_list(unsigned char *sha1, unsigned long ino)
337{
338 struct sha1_entry *entry = xmalloc(sizeof(*entry));
339 int nr;
340
341 entry->ino = ino;
342 memcpy(entry->sha1, sha1, 20);
343 nr = sha1_list.nr;
344 if (nr == MAX_SHA1_ENTRIES) {
345 fsck_sha1_list();
346 nr = 0;
347 }
348 sha1_list.entry[nr] = entry;
349 sha1_list.nr = ++nr;
20222118
LT
350}
351
352static int fsck_dir(int i, char *path)
353{
354 DIR *dir = opendir(path);
355 struct dirent *de;
356
230f1322
LT
357 if (!dir)
358 return 0;
20222118
LT
359
360 while ((de = readdir(dir)) != NULL) {
361 char name[100];
7e8c174a 362 unsigned char sha1[20];
20222118
LT
363 int len = strlen(de->d_name);
364
365 switch (len) {
366 case 2:
367 if (de->d_name[1] != '.')
368 break;
369 case 1:
370 if (de->d_name[0] != '.')
371 break;
372 continue;
373 case 38:
374 sprintf(name, "%02x", i);
375 memcpy(name+2, de->d_name, len+1);
7e8c174a
LT
376 if (get_sha1_hex(name, sha1) < 0)
377 break;
35a730f0 378 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
7e8c174a 379 continue;
20222118
LT
380 }
381 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
382 }
383 closedir(dir);
384 return 0;
385}
386
944d8589
LT
387static int default_refs = 0;
388
389static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
1024932f 390{
1024932f
LT
391 struct object *obj;
392
1024932f 393 obj = lookup_object(sha1);
8a498a05 394 if (!obj) {
7aaa715d 395 if (has_sha1_file(sha1)) {
659cacf5 396 default_refs++;
944d8589 397 return 0; /* it is in a pack */
659cacf5 398 }
944d8589
LT
399 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
400 /* We'll continue with the rest despite the error.. */
401 return 0;
8a498a05 402 }
944d8589 403 default_refs++;
1024932f
LT
404 obj->used = 1;
405 mark_reachable(obj, REACHABLE);
7c4d07c7 406 return 0;
1024932f
LT
407}
408
1024932f
LT
409static void get_default_heads(void)
410{
944d8589
LT
411 for_each_ref(fsck_handle_ref);
412 if (!default_refs)
477606f5 413 die("No default references");
1024932f
LT
414}
415
8a498a05
JH
416static void fsck_object_dir(const char *path)
417{
418 int i;
419 for (i = 0; i < 256; i++) {
420 static char dir[4096];
421 sprintf(dir, "%s/%02x", path, i);
422 fsck_dir(i, dir);
423 }
424 fsck_sha1_list();
425}
426
c3330383
LT
427static int fsck_head_link(void)
428{
c3330383 429 unsigned char sha1[20];
8098a178
JH
430 const char *git_HEAD = strdup(git_path("HEAD"));
431 const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 1);
432 int pfxlen = strlen(git_HEAD) - 4; /* strip .../.git/ part */
433
434 if (!git_refs_heads_master)
435 return error("HEAD is not a symbolic ref");
436 if (strncmp(git_refs_heads_master + pfxlen, "refs/heads/", 11))
437 return error("HEAD points to something strange (%s)",
438 git_refs_heads_master + pfxlen);
439 if (!memcmp(null_sha1, sha1, 20))
c3330383
LT
440 return error("HEAD: not a valid git pointer");
441 return 0;
442}
443
53dc3f3e
JH
444static int fsck_cache_tree(struct cache_tree *it)
445{
446 int i;
447 int err = 0;
448
449 if (0 <= it->entry_count) {
450 struct object *obj = parse_object(it->sha1);
6d60bbef
JH
451 if (!obj) {
452 error("%s: invalid sha1 pointer in cache-tree",
453 sha1_to_hex(it->sha1));
454 return 1;
455 }
cdc08b33
JH
456 mark_reachable(obj, REACHABLE);
457 obj->used = 1;
53dc3f3e
JH
458 if (obj->type != tree_type)
459 err |= objerror(obj, "non-tree in cache-tree");
460 }
461 for (i = 0; i < it->subtree_nr; i++)
462 err |= fsck_cache_tree(it->down[i]->cache_tree);
463 return err;
464}
465
20222118
LT
466int main(int argc, char **argv)
467{
bcee6fd8 468 int i, heads;
20222118 469
3a7c352b 470 track_object_refs = 1;
61e2b015
JH
471 setup_git_directory();
472
889262ea
LT
473 for (i = 1; i < argc; i++) {
474 const char *arg = argv[i];
475
476 if (!strcmp(arg, "--unreachable")) {
477 show_unreachable = 1;
478 continue;
479 }
480 if (!strcmp(arg, "--tags")) {
481 show_tags = 1;
482 continue;
483 }
ab7df187
LT
484 if (!strcmp(arg, "--root")) {
485 show_root = 1;
486 continue;
487 }
ae7c0c92
JH
488 if (!strcmp(arg, "--cache")) {
489 keep_cache_objects = 1;
490 continue;
491 }
8a498a05
JH
492 if (!strcmp(arg, "--full")) {
493 check_full = 1;
494 continue;
495 }
de2eb7f6
LT
496 if (!strcmp(arg, "--strict")) {
497 check_strict = 1;
498 continue;
499 }
889262ea 500 if (*arg == '-')
7aaa715d 501 usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
889262ea
LT
502 }
503
c3330383 504 fsck_head_link();
8a498a05
JH
505 fsck_object_dir(get_object_directory());
506 if (check_full) {
d5a63b99 507 struct alternate_object_database *alt;
8a498a05
JH
508 struct packed_git *p;
509 prepare_alt_odb();
d5a63b99 510 for (alt = alt_odb_list; alt; alt = alt->next) {
a3eb250f 511 char namebuf[PATH_MAX];
d5a63b99
JH
512 int namelen = alt->name - alt->base;
513 memcpy(namebuf, alt->base, namelen);
a3eb250f
JH
514 namebuf[namelen - 1] = 0;
515 fsck_object_dir(namebuf);
8a498a05
JH
516 }
517 prepare_packed_git();
f9253394
JH
518 for (p = packed_git; p; p = p->next)
519 /* verify gives error messages itself */
f3bf9224 520 verify_pack(p, 0);
f9253394 521
8a498a05
JH
522 for (p = packed_git; p; p = p->next) {
523 int num = num_packed_objects(p);
524 for (i = 0; i < num; i++) {
525 unsigned char sha1[20];
526 nth_packed_object_sha1(p, i, sha1);
f1f0d088 527 fsck_sha1(sha1);
8a498a05
JH
528 }
529 }
bcee6fd8
LT
530 }
531
532 heads = 0;
d9839e03 533 for (i = 1; i < argc; i++) {
889262ea
LT
534 const char *arg = argv[i];
535
536 if (*arg == '-')
d9839e03 537 continue;
889262ea 538
3c249c95 539 if (!get_sha1(arg, head_sha1)) {
770896e5 540 struct object *obj = lookup_object(head_sha1);
e1a1388d 541
770896e5
LT
542 /* Error is printed by lookup_object(). */
543 if (!obj)
e1a1388d
JF
544 continue;
545
ff5ebe39
DB
546 obj->used = 1;
547 mark_reachable(obj, REACHABLE);
bcee6fd8 548 heads++;
d9839e03
LT
549 continue;
550 }
f1f0d088 551 error("invalid parameter: expected sha1, got '%s'", arg);
d9839e03 552 }
d9839e03 553
1024932f 554 /*
d1af002d 555 * If we've not been given any explicit head information, do the
e7bd907d
LT
556 * default ones from .git/refs. We also consider the index file
557 * in this case (ie this implies --cache).
1024932f 558 */
e7bd907d 559 if (!heads) {
1024932f
LT
560 get_default_heads();
561 keep_cache_objects = 1;
562 }
563
ae7c0c92
JH
564 if (keep_cache_objects) {
565 int i;
566 read_cache();
567 for (i = 0; i < active_nr; i++) {
568 struct blob *blob = lookup_blob(active_cache[i]->sha1);
569 struct object *obj;
570 if (!blob)
571 continue;
572 obj = &blob->object;
573 obj->used = 1;
574 mark_reachable(obj, REACHABLE);
575 }
53dc3f3e
JH
576 if (active_cache_tree)
577 fsck_cache_tree(active_cache_tree);
ae7c0c92
JH
578 }
579
8ba0bbb2 580 check_connectivity();
20222118
LT
581 return 0;
582}