5dca6dbc04b0bf40cd654f05766f7daa6fac75b4
   1#include "cache.h"
   2
   3#include <sys/types.h>
   4#include <dirent.h>
   5
   6struct needs {
   7        unsigned char parent[20];
   8        unsigned char needs[20];
   9        char tag[10];
  10};
  11
  12struct seen {
  13        unsigned char sha1[20];
  14        char tag[10];
  15        unsigned needed;
  16};
  17
  18static struct needs *needs;
  19static struct seen *seen;
  20
  21static int nr_seen, alloc_seen, nr_needs, alloc_needs;
  22
  23/*
  24 * These two functions build up a graph in memory about
  25 * what objects we've referenced, and found, and types..
  26 */
  27static int compare_seen(const void *s1, const void *s2)
  28{
  29        return memcmp(s1, s2, 20);
  30}
  31
  32static int lookup_seen(unsigned char *sha1, char *tag)
  33{
  34        int first = 0, last = nr_seen;
  35
  36        while (last > first) {
  37                int next = (last + first) / 2;
  38                struct seen *s = seen + next;
  39                int cmp = memcmp(sha1, s->sha1, 20);
  40
  41                if (cmp < 0) {
  42                        last = next;
  43                        continue;
  44                }
  45                if (cmp > 0) {
  46                        first = next+1;
  47                        continue;
  48                }
  49                if (strcmp(tag, s->tag))
  50                        break;
  51                s->needed++;
  52                return 1;
  53        }
  54        return 0;
  55}
  56
  57static void check_connectivity(void)
  58{
  59        int i;
  60
  61        /* Sort the "seen" tags for quicker lookup */
  62        qsort(seen, nr_seen, sizeof(struct seen), compare_seen);
  63
  64        /* Look up all the requirements, warn about missing objects.. */
  65        for (i = 0; i < nr_needs; i++) {
  66                struct needs *n = needs + i;
  67                char hex[60];
  68
  69                if (lookup_seen(n->needs, n->tag))
  70                        continue;
  71                strcpy(hex, sha1_to_hex(n->parent));
  72        }
  73
  74        /* Tell the user about things not referenced.. */
  75        for (i = 0; i < nr_seen; i++) {
  76                struct seen *s = seen + i;
  77
  78                if (s->needed)
  79                        continue;
  80                printf("unreferenced %s: %s\n", s->tag, sha1_to_hex(s->sha1));
  81        }
  82}
  83
  84static void mark_needs_sha1(unsigned char *parent, const char * tag, unsigned char *child)
  85{
  86        struct needs *n;
  87
  88        if (nr_needs == alloc_needs) {
  89                alloc_needs = alloc_nr(alloc_needs);
  90                needs = realloc(needs, alloc_needs*sizeof(struct needs));
  91        }
  92        n = needs + nr_needs;
  93        nr_needs++;
  94        memcpy(n->parent, parent, 20);
  95        memcpy(n->needs, child, 20);
  96        strncpy(n->tag, tag, sizeof(n->tag));
  97}
  98
  99static int mark_sha1_seen(unsigned char *sha1, char *tag)
 100{
 101        struct seen *s;
 102
 103        if (nr_seen == alloc_seen) {
 104                alloc_seen = alloc_nr(alloc_seen);
 105                seen = realloc(seen, alloc_seen*sizeof(struct seen));
 106        }
 107        s = seen + nr_seen;
 108        memset(s, 0, sizeof(s));
 109        nr_seen++;
 110        memcpy(s->sha1, sha1, 20);
 111        strncpy(s->tag, tag, sizeof(s->tag));
 112        
 113        return 0;
 114}
 115
 116static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
 117{
 118        int warn_old_tree = 1;
 119
 120        while (size) {
 121                int len = 1+strlen(data);
 122                unsigned char *file_sha1 = data + len;
 123                char *path = strchr(data, ' ');
 124                unsigned int mode;
 125                if (size < len + 20 || !path || sscanf(data, "%o", &mode) != 1)
 126                        return -1;
 127
 128                /* Warn about trees that don't do the recursive thing.. */
 129                if (warn_old_tree && strchr(path, '/')) {
 130                        fprintf(stderr, "warning: fsck-cache: tree %s has full pathnames in it\n", sha1_to_hex(sha1));
 131                        warn_old_tree = 0;
 132                }
 133
 134                data += len + 20;
 135                size -= len + 20;
 136                mark_needs_sha1(sha1, S_ISDIR(mode) ? "tree" : "blob", file_sha1);
 137        }
 138        return 0;
 139}
 140
 141static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
 142{
 143        int parents;
 144        unsigned char tree_sha1[20];
 145        unsigned char parent_sha1[20];
 146
 147        if (memcmp(data, "tree ", 5))
 148                return -1;
 149        if (get_sha1_hex(data + 5, tree_sha1) < 0)
 150                return -1;
 151        mark_needs_sha1(sha1, "tree", tree_sha1);
 152        data += 5 + 40 + 1;     /* "tree " + <hex sha1> + '\n' */
 153        parents = 0;
 154        while (!memcmp(data, "parent ", 7)) {
 155                if (get_sha1_hex(data + 7, parent_sha1) < 0)
 156                        return -1;
 157                mark_needs_sha1(sha1, "commit", parent_sha1);
 158                data += 7 + 40 + 1;     /* "parent " + <hex sha1> + '\n' */
 159                parents++;
 160        }
 161        if (!parents)
 162                printf("root: %s\n", sha1_to_hex(sha1));
 163        return 0;
 164}
 165
 166static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)
 167{
 168        if (!strcmp(tag, "blob")) {
 169                /* Nothing to check */;
 170        } else if (!strcmp(tag, "tree")) {
 171                if (fsck_tree(sha1, data, size) < 0)
 172                        return -1;
 173        } else if (!strcmp(tag, "commit")) {
 174                if (fsck_commit(sha1, data, size) < 0)
 175                        return -1;
 176        } else
 177                return -1;
 178        return mark_sha1_seen(sha1, tag);
 179}
 180
 181static int fsck_name(char *hex)
 182{
 183        unsigned char sha1[20];
 184        if (!get_sha1_hex(hex, sha1)) {
 185                unsigned long mapsize;
 186                void *map = map_sha1_file(sha1, &mapsize);
 187                if (map) {
 188                        char type[100];
 189                        unsigned long size;
 190                        void *buffer = NULL;
 191                        if (!check_sha1_signature(sha1, map, mapsize))
 192                                buffer = unpack_sha1_file(map, mapsize, type, &size);
 193                        munmap(map, mapsize);
 194                        if (buffer && !fsck_entry(sha1, type, buffer, size))
 195                                return 0;
 196                }
 197        }
 198        return -1;
 199}
 200
 201static int fsck_dir(int i, char *path)
 202{
 203        DIR *dir = opendir(path);
 204        struct dirent *de;
 205
 206        if (!dir) {
 207                fprintf(stderr, "missing sha1 directory '%s'", path);
 208                return -1;
 209        }
 210
 211        while ((de = readdir(dir)) != NULL) {
 212                char name[100];
 213                int len = strlen(de->d_name);
 214
 215                switch (len) {
 216                case 2:
 217                        if (de->d_name[1] != '.')
 218                                break;
 219                case 1:
 220                        if (de->d_name[0] != '.')
 221                                break;
 222                        continue;
 223                case 38:
 224                        sprintf(name, "%02x", i);
 225                        memcpy(name+2, de->d_name, len+1);
 226                        if (!fsck_name(name))
 227                                continue;
 228                }
 229                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
 230        }
 231        closedir(dir);
 232        return 0;
 233}
 234
 235int main(int argc, char **argv)
 236{
 237        int i;
 238        char *sha1_dir;
 239
 240        if (argc != 1)
 241                usage("fsck-cache");
 242        sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
 243        for (i = 0; i < 256; i++) {
 244                static char dir[4096];
 245                sprintf(dir, "%s/%02x", sha1_dir, i);
 246                fsck_dir(i, dir);
 247        }
 248        check_connectivity();
 249        return 0;
 250}