Fix missing return values and some error tests for empty index files
[git.git] / fsck-cache.c
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  */
12 static void mark_needs_sha1(unsigned char *parent, const char * type, unsigned char *child)
13 {
14 }
15
16 static int mark_sha1_seen(unsigned char *sha1, char *tag)
17 {
18         return 0;
19 }
20
21 static 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                 if (size < len + 20 || !path)
28                         return -1;
29                 data += len + 20;
30                 size -= len + 20;
31                 mark_needs_sha1(sha1, "blob", file_sha1);
32         }
33         return 0;
34 }
35
36 static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
37 {
38         unsigned char tree_sha1[20];
39         unsigned char parent_sha1[20];
40
41         if (memcmp(data, "tree ", 5))
42                 return -1;
43         if (get_sha1_hex(data + 5, tree_sha1) < 0)
44                 return -1;
45         mark_needs_sha1(sha1, "tree", tree_sha1);
46         data += 5 + 40 + 1;     /* "tree " + <hex sha1> + '\n' */
47         while (!memcmp(data, "parent ", 7)) {
48                 if (get_sha1_hex(data + 7, parent_sha1) < 0)
49                         return -1;
50                 mark_needs_sha1(sha1, "commit", parent_sha1);
51                 data += 7 + 40 + 1;     /* "parent " + <hex sha1> + '\n' */
52         }
53         return 0;
54 }
55
56 static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)
57 {
58         if (!strcmp(tag, "blob")) {
59                 /* Nothing to check */;
60         } else if (!strcmp(tag, "tree")) {
61                 if (fsck_tree(sha1, data, size) < 0)
62                         return -1;
63         } else if (!strcmp(tag, "commit")) {
64                 if (fsck_commit(sha1, data, size) < 0)
65                         return -1;
66         } else
67                 return -1;
68         return mark_sha1_seen(sha1, tag);
69 }
70
71 static int fsck_name(char *hex)
72 {
73         unsigned char sha1[20];
74         if (!get_sha1_hex(hex, sha1)) {
75                 unsigned long mapsize;
76                 void *map = map_sha1_file(sha1, &mapsize);
77                 if (map) {
78                         char type[100];
79                         unsigned long size;
80                         void *buffer = NULL;
81                         if (!check_sha1_signature(sha1, map, mapsize))
82                                 buffer = unpack_sha1_file(map, mapsize, type, &size);
83                         munmap(map, mapsize);
84                         if (buffer && !fsck_entry(sha1, type, buffer, size))
85                                 return 0;
86                 }
87         }
88         return -1;
89 }
90
91 static int fsck_dir(int i, char *path)
92 {
93         DIR *dir = opendir(path);
94         struct dirent *de;
95
96         if (!dir) {
97                 fprintf(stderr, "missing sha1 directory '%s'", path);
98                 return -1;
99         }
100
101         while ((de = readdir(dir)) != NULL) {
102                 char name[100];
103                 int len = strlen(de->d_name);
104
105                 switch (len) {
106                 case 2:
107                         if (de->d_name[1] != '.')
108                                 break;
109                 case 1:
110                         if (de->d_name[0] != '.')
111                                 break;
112                         continue;
113                 case 38:
114                         sprintf(name, "%02x", i);
115                         memcpy(name+2, de->d_name, len+1);
116                         if (!fsck_name(name))
117                                 continue;
118                 }
119                 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
120         }
121         closedir(dir);
122         return 0;
123 }
124
125 int main(int argc, char **argv)
126 {
127         int i;
128         char *sha1_dir;
129
130         if (argc != 1)
131                 usage("fsck-cache");
132         sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
133         for (i = 0; i < 256; i++) {
134                 static char dir[4096];
135                 sprintf(dir, "%s/%02x", sha1_dir, i);
136                 fsck_dir(i, dir);
137         }
138         return 0;
139 }