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