1123b6b7e4312a9ab47071479d071a5856fc8134
   1#include "cache.h"
   2
   3#include <sys/types.h>
   4#include <dirent.h>
   5
   6/*
   7 * These two functions should build up a graph in memory about
   8 * what objects we've referenced, and found, and types..
   9 *
  10 * Right now we don't do that kind of reachability checking. Yet.
  11 */
  12static void mark_needs_sha1(unsigned char *parent, const char * type, unsigned char *child)
  13{
  14}
  15
  16static int mark_sha1_seen(unsigned char *sha1, char *tag)
  17{
  18        return 0;
  19}
  20
  21static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
  22{
  23        while (size) {
  24                int len = 1+strlen(data);
  25                unsigned char *file_sha1 = data + len;
  26                char *path = strchr(data, ' ');
  27                unsigned int mode;
  28                if (size < len + 20 || !path || sscanf(data, "%o", &mode) != 1)
  29                        return -1;
  30                data += len + 20;
  31                size -= len + 20;
  32                mark_needs_sha1(sha1, S_ISDIR(mode) ? "tree" : "blob", file_sha1);
  33        }
  34        return 0;
  35}
  36
  37static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
  38{
  39        int parents;
  40        unsigned char tree_sha1[20];
  41        unsigned char parent_sha1[20];
  42
  43        if (memcmp(data, "tree ", 5))
  44                return -1;
  45        if (get_sha1_hex(data + 5, tree_sha1) < 0)
  46                return -1;
  47        mark_needs_sha1(sha1, "tree", tree_sha1);
  48        data += 5 + 40 + 1;     /* "tree " + <hex sha1> + '\n' */
  49        parents = 0;
  50        while (!memcmp(data, "parent ", 7)) {
  51                if (get_sha1_hex(data + 7, parent_sha1) < 0)
  52                        return -1;
  53                mark_needs_sha1(sha1, "commit", parent_sha1);
  54                data += 7 + 40 + 1;     /* "parent " + <hex sha1> + '\n' */
  55                parents++;
  56        }
  57        if (!parents)
  58                printf("root: %s\n", sha1_to_hex(sha1));
  59        return 0;
  60}
  61
  62static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)
  63{
  64        if (!strcmp(tag, "blob")) {
  65                /* Nothing to check */;
  66        } else if (!strcmp(tag, "tree")) {
  67                if (fsck_tree(sha1, data, size) < 0)
  68                        return -1;
  69        } else if (!strcmp(tag, "commit")) {
  70                if (fsck_commit(sha1, data, size) < 0)
  71                        return -1;
  72        } else
  73                return -1;
  74        return mark_sha1_seen(sha1, tag);
  75}
  76
  77static int fsck_name(char *hex)
  78{
  79        unsigned char sha1[20];
  80        if (!get_sha1_hex(hex, sha1)) {
  81                unsigned long mapsize;
  82                void *map = map_sha1_file(sha1, &mapsize);
  83                if (map) {
  84                        char type[100];
  85                        unsigned long size;
  86                        void *buffer = NULL;
  87                        if (!check_sha1_signature(sha1, map, mapsize))
  88                                buffer = unpack_sha1_file(map, mapsize, type, &size);
  89                        munmap(map, mapsize);
  90                        if (buffer && !fsck_entry(sha1, type, buffer, size))
  91                                return 0;
  92                }
  93        }
  94        return -1;
  95}
  96
  97static int fsck_dir(int i, char *path)
  98{
  99        DIR *dir = opendir(path);
 100        struct dirent *de;
 101
 102        if (!dir) {
 103                fprintf(stderr, "missing sha1 directory '%s'", path);
 104                return -1;
 105        }
 106
 107        while ((de = readdir(dir)) != NULL) {
 108                char name[100];
 109                int len = strlen(de->d_name);
 110
 111                switch (len) {
 112                case 2:
 113                        if (de->d_name[1] != '.')
 114                                break;
 115                case 1:
 116                        if (de->d_name[0] != '.')
 117                                break;
 118                        continue;
 119                case 38:
 120                        sprintf(name, "%02x", i);
 121                        memcpy(name+2, de->d_name, len+1);
 122                        if (!fsck_name(name))
 123                                continue;
 124                }
 125                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
 126        }
 127        closedir(dir);
 128        return 0;
 129}
 130
 131int main(int argc, char **argv)
 132{
 133        int i;
 134        char *sha1_dir;
 135
 136        if (argc != 1)
 137                usage("fsck-cache");
 138        sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
 139        for (i = 0; i < 256; i++) {
 140                static char dir[4096];
 141                sprintf(dir, "%s/%02x", sha1_dir, i);
 142                fsck_dir(i, dir);
 143        }
 144        return 0;
 145}