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