fsck-cache.con commit Start of "git-send-pack", the local part of sending off a pack (6122147)
   1#include <sys/types.h>
   2#include <dirent.h>
   3
   4#include "cache.h"
   5#include "commit.h"
   6#include "tree.h"
   7#include "blob.h"
   8#include "tag.h"
   9#include "pack.h"
  10
  11#define REACHABLE 0x0001
  12
  13static int show_root = 0;
  14static int show_tags = 0;
  15static int show_unreachable = 0;
  16static int standalone = 0;
  17static int check_full = 0;
  18static int keep_cache_objects = 0; 
  19static unsigned char head_sha1[20];
  20
  21static void check_connectivity(void)
  22{
  23        int i;
  24
  25        /* Look up all the requirements, warn about missing objects.. */
  26        for (i = 0; i < nr_objs; i++) {
  27                struct object *obj = objs[i];
  28                struct object_list *refs;
  29
  30                if (!obj->parsed) {
  31                        if (!standalone && has_sha1_file(obj->sha1))
  32                                ; /* it is in pack */
  33                        else
  34                                printf("missing %s %s\n",
  35                                       obj->type, sha1_to_hex(obj->sha1));
  36                        continue;
  37                }
  38
  39                for (refs = obj->refs; refs; refs = refs->next) {
  40                        if (refs->item->parsed ||
  41                            (!standalone && has_sha1_file(refs->item->sha1)))
  42                                continue;
  43                        printf("broken link from %7s %s\n",
  44                               obj->type, sha1_to_hex(obj->sha1));
  45                        printf("              to %7s %s\n",
  46                               refs->item->type, sha1_to_hex(refs->item->sha1));
  47                }
  48
  49                if (show_unreachable && !(obj->flags & REACHABLE)) {
  50                        printf("unreachable %s %s\n",
  51                               obj->type, sha1_to_hex(obj->sha1));
  52                        continue;
  53                }
  54
  55                if (!obj->used) {
  56                        printf("dangling %s %s\n", obj->type, 
  57                               sha1_to_hex(obj->sha1));
  58                }
  59        }
  60}
  61
  62/*
  63 * The entries in a tree are ordered in the _path_ order,
  64 * which means that a directory entry is ordered by adding
  65 * a slash to the end of it.
  66 *
  67 * So a directory called "a" is ordered _after_ a file
  68 * called "a.c", because "a/" sorts after "a.c".
  69 */
  70#define TREE_UNORDERED (-1)
  71#define TREE_HAS_DUPS  (-2)
  72
  73static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
  74{
  75        int len1 = strlen(a->name);
  76        int len2 = strlen(b->name);
  77        int len = len1 < len2 ? len1 : len2;
  78        unsigned char c1, c2;
  79        int cmp;
  80
  81        cmp = memcmp(a->name, b->name, len);
  82        if (cmp < 0)
  83                return 0;
  84        if (cmp > 0)
  85                return TREE_UNORDERED;
  86
  87        /*
  88         * Ok, the first <len> characters are the same.
  89         * Now we need to order the next one, but turn
  90         * a '\0' into a '/' for a directory entry.
  91         */
  92        c1 = a->name[len];
  93        c2 = b->name[len];
  94        if (!c1 && !c2)
  95                /*
  96                 * git-write-tree used to write out a nonsense tree that has
  97                 * entries with the same name, one blob and one tree.  Make
  98                 * sure we do not have duplicate entries.
  99                 */
 100                return TREE_HAS_DUPS;
 101        if (!c1 && a->directory)
 102                c1 = '/';
 103        if (!c2 && b->directory)
 104                c2 = '/';
 105        return c1 < c2 ? 0 : TREE_UNORDERED;
 106}
 107
 108static int fsck_tree(struct tree *item)
 109{
 110        int has_full_path = 0;
 111        struct tree_entry_list *entry, *last;
 112
 113        last = NULL;
 114        for (entry = item->entries; entry; entry = entry->next) {
 115                if (strchr(entry->name, '/'))
 116                        has_full_path = 1;
 117
 118                switch (entry->mode) {
 119                /*
 120                 * Standard modes.. 
 121                 */
 122                case S_IFREG | 0755:
 123                case S_IFREG | 0644:
 124                case S_IFLNK:
 125                case S_IFDIR:
 126                        break;
 127                /*
 128                 * This is nonstandard, but we had a few of these
 129                 * early on when we honored the full set of mode
 130                 * bits..
 131                 */
 132                case S_IFREG | 0664:
 133                        break;
 134                default:
 135                        printf("tree %s has entry %o %s\n",
 136                                sha1_to_hex(item->object.sha1),
 137                                entry->mode, entry->name);
 138                }
 139
 140                if (last) {
 141                        switch (verify_ordered(last, entry)) {
 142                        case TREE_UNORDERED:
 143                                fprintf(stderr, "tree %s not ordered\n",
 144                                        sha1_to_hex(item->object.sha1));
 145                                return -1;
 146                        case TREE_HAS_DUPS:
 147                                fprintf(stderr, "tree %s has duplicate entries for '%s'\n",
 148                                        sha1_to_hex(item->object.sha1),
 149                                        entry->name);
 150                                return -1;
 151                        default:
 152                                break;
 153                        }
 154                }
 155
 156                last = entry;
 157        }
 158
 159        if (has_full_path) {
 160                fprintf(stderr, "warning: git-fsck-cache: tree %s "
 161                        "has full pathnames in it\n", 
 162                        sha1_to_hex(item->object.sha1));
 163        }
 164
 165        return 0;
 166}
 167
 168static int fsck_commit(struct commit *commit)
 169{
 170        free(commit->buffer);
 171        commit->buffer = NULL;
 172        if (!commit->tree)
 173                return -1;
 174        if (!commit->parents && show_root)
 175                printf("root %s\n", sha1_to_hex(commit->object.sha1));
 176        if (!commit->date)
 177                printf("bad commit date in %s\n", 
 178                       sha1_to_hex(commit->object.sha1));
 179        return 0;
 180}
 181
 182static int fsck_tag(struct tag *tag)
 183{
 184        struct object *tagged = tag->tagged;
 185
 186        if (!tagged) {
 187                printf("bad object in tag %s\n", sha1_to_hex(tag->object.sha1));
 188                return -1;
 189        }
 190        if (!show_tags)
 191                return 0;
 192
 193        printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
 194        printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
 195        return 0;
 196}
 197
 198static int fsck_sha1(unsigned char *sha1)
 199{
 200        struct object *obj = parse_object(sha1);
 201        if (!obj)
 202                return -1;
 203        if (obj->type == blob_type)
 204                return 0;
 205        if (obj->type == tree_type)
 206                return fsck_tree((struct tree *) obj);
 207        if (obj->type == commit_type)
 208                return fsck_commit((struct commit *) obj);
 209        if (obj->type == tag_type)
 210                return fsck_tag((struct tag *) obj);
 211        return -1;
 212}
 213
 214/*
 215 * This is the sorting chunk size: make it reasonably
 216 * big so that we can sort well..
 217 */
 218#define MAX_SHA1_ENTRIES (1024)
 219
 220struct sha1_entry {
 221        unsigned long ino;
 222        unsigned char sha1[20];
 223};
 224
 225static struct {
 226        unsigned long nr;
 227        struct sha1_entry *entry[MAX_SHA1_ENTRIES];
 228} sha1_list;
 229
 230static int ino_compare(const void *_a, const void *_b)
 231{
 232        const struct sha1_entry *a = _a, *b = _b;
 233        unsigned long ino1 = a->ino, ino2 = b->ino;
 234        return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
 235}
 236
 237static void fsck_sha1_list(void)
 238{
 239        int i, nr = sha1_list.nr;
 240
 241        qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
 242        for (i = 0; i < nr; i++) {
 243                struct sha1_entry *entry = sha1_list.entry[i];
 244                unsigned char *sha1 = entry->sha1;
 245
 246                sha1_list.entry[i] = NULL;
 247                if (fsck_sha1(sha1) < 0)
 248                        fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
 249                free(entry);
 250        }
 251        sha1_list.nr = 0;
 252}
 253
 254static void add_sha1_list(unsigned char *sha1, unsigned long ino)
 255{
 256        struct sha1_entry *entry = xmalloc(sizeof(*entry));
 257        int nr;
 258
 259        entry->ino = ino;
 260        memcpy(entry->sha1, sha1, 20);
 261        nr = sha1_list.nr;
 262        if (nr == MAX_SHA1_ENTRIES) {
 263                fsck_sha1_list();
 264                nr = 0;
 265        }
 266        sha1_list.entry[nr] = entry;
 267        sha1_list.nr = ++nr;
 268}
 269
 270static int fsck_dir(int i, char *path)
 271{
 272        DIR *dir = opendir(path);
 273        struct dirent *de;
 274
 275        if (!dir) {
 276                return error("missing sha1 directory '%s'", path);
 277        }
 278
 279        while ((de = readdir(dir)) != NULL) {
 280                char name[100];
 281                unsigned char sha1[20];
 282                int len = strlen(de->d_name);
 283
 284                switch (len) {
 285                case 2:
 286                        if (de->d_name[1] != '.')
 287                                break;
 288                case 1:
 289                        if (de->d_name[0] != '.')
 290                                break;
 291                        continue;
 292                case 38:
 293                        sprintf(name, "%02x", i);
 294                        memcpy(name+2, de->d_name, len+1);
 295                        if (get_sha1_hex(name, sha1) < 0)
 296                                break;
 297                        add_sha1_list(sha1, de->d_ino);
 298                        continue;
 299                }
 300                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
 301        }
 302        closedir(dir);
 303        return 0;
 304}
 305
 306static int read_sha1_reference(const char *path)
 307{
 308        char hexname[60];
 309        unsigned char sha1[20];
 310        int fd = open(path, O_RDONLY), len;
 311        struct object *obj;
 312
 313        if (fd < 0)
 314                return -1;
 315
 316        len = read(fd, hexname, sizeof(hexname));
 317        close(fd);
 318        if (len < 40)
 319                return -1;
 320
 321        if (get_sha1_hex(hexname, sha1) < 0)
 322                return -1;
 323
 324        obj = lookup_object(sha1);
 325        if (!obj) {
 326                if (!standalone && has_sha1_file(sha1))
 327                        return 0; /* it is in pack */
 328                return error("%s: invalid sha1 pointer %.40s", path, hexname);
 329        }
 330
 331        obj->used = 1;
 332        mark_reachable(obj, REACHABLE);
 333        return 0;
 334}
 335
 336static int find_file_objects(const char *base, const char *name)
 337{
 338        int baselen = strlen(base);
 339        int namelen = strlen(name);
 340        char *path = xmalloc(baselen + namelen + 2);
 341        struct stat st;
 342
 343        memcpy(path, base, baselen);
 344        path[baselen] = '/';
 345        memcpy(path + baselen + 1, name, namelen+1);
 346        if (stat(path, &st) < 0)
 347                return 0;
 348
 349        /*
 350         * Recurse into directories
 351         */
 352        if (S_ISDIR(st.st_mode)) {
 353                int count = 0;
 354                DIR *dir = opendir(path);
 355                if (dir) {
 356                        struct dirent *de;
 357                        while ((de = readdir(dir)) != NULL) {
 358                                if (de->d_name[0] == '.')
 359                                        continue;
 360                                count += find_file_objects(path, de->d_name);
 361                        }
 362                        closedir(dir);
 363                }
 364                return count;
 365        }
 366        if (S_ISREG(st.st_mode))
 367                return read_sha1_reference(path) == 0;
 368        return 0;
 369}
 370
 371static void get_default_heads(void)
 372{
 373        char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
 374        int count = find_file_objects(git_dir, "refs");
 375        if (!count)
 376                die("No default references");
 377}
 378
 379static void fsck_object_dir(const char *path)
 380{
 381        int i;
 382        for (i = 0; i < 256; i++) {
 383                static char dir[4096];
 384                sprintf(dir, "%s/%02x", path, i);
 385                fsck_dir(i, dir);
 386        }
 387        fsck_sha1_list();
 388}
 389
 390int main(int argc, char **argv)
 391{
 392        int i, heads;
 393
 394        for (i = 1; i < argc; i++) {
 395                const char *arg = argv[i];
 396
 397                if (!strcmp(arg, "--unreachable")) {
 398                        show_unreachable = 1;
 399                        continue;
 400                }
 401                if (!strcmp(arg, "--tags")) {
 402                        show_tags = 1;
 403                        continue;
 404                }
 405                if (!strcmp(arg, "--root")) {
 406                        show_root = 1;
 407                        continue;
 408                }
 409                if (!strcmp(arg, "--cache")) {
 410                        keep_cache_objects = 1;
 411                        continue;
 412                }
 413                if (!strcmp(arg, "--standalone")) {
 414                        standalone = 1;
 415                        continue;
 416                }
 417                if (!strcmp(arg, "--full")) {
 418                        check_full = 1;
 419                        continue;
 420                }
 421                if (*arg == '-')
 422                        usage("git-fsck-cache [--tags] [[--unreachable] [--cache] [--standalone | --full] <head-sha1>*]");
 423        }
 424
 425        if (standalone && check_full)
 426                die("Only one of --standalone or --full can be used.");
 427        if (standalone)
 428                unsetenv("GIT_ALTERNATE_OBJECT_DIRECTORIES");
 429
 430        fsck_object_dir(get_object_directory());
 431        if (check_full) {
 432                int j;
 433                struct packed_git *p;
 434                prepare_alt_odb();
 435                for (j = 0; alt_odb[j].base; j++) {
 436                        alt_odb[j].name[-1] = 0; /* was slash */
 437                        fsck_object_dir(alt_odb[j].base);
 438                        alt_odb[j].name[-1] = '/';
 439                }
 440                prepare_packed_git();
 441                for (p = packed_git; p; p = p->next)
 442                        /* verify gives error messages itself */
 443                        verify_pack(p); 
 444
 445                for (p = packed_git; p; p = p->next) {
 446                        int num = num_packed_objects(p);
 447                        for (i = 0; i < num; i++) {
 448                                unsigned char sha1[20];
 449                                nth_packed_object_sha1(p, i, sha1);
 450                                if (fsck_sha1(sha1) < 0)
 451                                        fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
 452
 453                        }
 454                }
 455        }
 456
 457        heads = 0;
 458        for (i = 1; i < argc; i++) {
 459                const char *arg = argv[i]; 
 460
 461                if (*arg == '-')
 462                        continue;
 463
 464                if (!get_sha1(arg, head_sha1)) {
 465                        struct object *obj = lookup_object(head_sha1);
 466
 467                        /* Error is printed by lookup_object(). */
 468                        if (!obj)
 469                                continue;
 470
 471                        obj->used = 1;
 472                        mark_reachable(obj, REACHABLE);
 473                        heads++;
 474                        continue;
 475                }
 476                error("expected sha1, got %s", arg);
 477        }
 478
 479        /*
 480         * If we've not been given any explicit head information, do the
 481         * default ones from .git/refs. We also consider the index file
 482         * in this case (ie this implies --cache).
 483         */
 484        if (!heads) {
 485                get_default_heads();
 486                keep_cache_objects = 1;
 487        }
 488
 489        if (keep_cache_objects) {
 490                int i;
 491                read_cache();
 492                for (i = 0; i < active_nr; i++) {
 493                        struct blob *blob = lookup_blob(active_cache[i]->sha1);
 494                        struct object *obj;
 495                        if (!blob)
 496                                continue;
 497                        obj = &blob->object;
 498                        obj->used = 1;
 499                        mark_reachable(obj, REACHABLE);
 500                }
 501        }
 502
 503        check_connectivity();
 504        return 0;
 505}