[PATCH] Simplify date handling and make it more reliable
[git/fastimport.git] / fsck-cache.c
blob3fbcd2c7e99fa5e27fa7edad0e2bd65d3b557fe7
1 #include "cache.h"
3 #include <sys/types.h>
4 #include <dirent.h>
6 #include "revision.h"
8 static int show_unreachable = 0;
9 static unsigned char head_sha1[20];
11 static void check_connectivity(void)
13 int i;
15 /* Look up all the requirements, warn about missing objects.. */
16 for (i = 0; i < nr_revs; i++) {
17 struct revision *rev = revs[i];
19 if (show_unreachable && !(rev->flags & REACHABLE)) {
20 printf("unreachable %s\n", sha1_to_hex(rev->sha1));
21 continue;
24 switch (rev->flags & (SEEN | USED)) {
25 case 0:
26 printf("bad %s\n", sha1_to_hex(rev->sha1));
27 break;
28 case USED:
29 printf("missing %s\n", sha1_to_hex(rev->sha1));
30 break;
31 case SEEN:
32 printf("dangling %s\n", sha1_to_hex(rev->sha1));
33 break;
38 static void mark_needs_sha1(unsigned char *parent, const char * tag, unsigned char *child)
40 struct revision * child_rev = add_relationship(lookup_rev(parent), child);
41 child_rev->flags |= USED;
44 static int mark_sha1_seen(unsigned char *sha1, char *tag)
46 struct revision *rev = lookup_rev(sha1);
48 rev->flags |= SEEN;
49 return 0;
52 static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
54 int warn_old_tree = 1;
56 while (size) {
57 int len = 1+strlen(data);
58 unsigned char *file_sha1 = data + len;
59 char *path = strchr(data, ' ');
60 unsigned int mode;
61 if (size < len + 20 || !path || sscanf(data, "%o", &mode) != 1)
62 return -1;
64 /* Warn about trees that don't do the recursive thing.. */
65 if (warn_old_tree && strchr(path, '/')) {
66 fprintf(stderr, "warning: fsck-cache: tree %s has full pathnames in it\n", sha1_to_hex(sha1));
67 warn_old_tree = 0;
70 data += len + 20;
71 size -= len + 20;
72 mark_needs_sha1(sha1, S_ISDIR(mode) ? "tree" : "blob", file_sha1);
74 return 0;
77 static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
79 int parents;
80 unsigned char tree_sha1[20];
81 unsigned char parent_sha1[20];
83 if (memcmp(data, "tree ", 5))
84 return -1;
85 if (get_sha1_hex(data + 5, tree_sha1) < 0)
86 return -1;
87 mark_needs_sha1(sha1, "tree", tree_sha1);
88 data += 5 + 40 + 1; /* "tree " + <hex sha1> + '\n' */
89 parents = 0;
90 while (!memcmp(data, "parent ", 7)) {
91 if (get_sha1_hex(data + 7, parent_sha1) < 0)
92 return -1;
93 mark_needs_sha1(sha1, "commit", parent_sha1);
94 data += 7 + 40 + 1; /* "parent " + <hex sha1> + '\n' */
95 parents++;
97 if (!parents)
98 printf("root %s\n", sha1_to_hex(sha1));
99 return 0;
102 static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)
104 if (!strcmp(tag, "blob")) {
105 /* Nothing to check */;
106 } else if (!strcmp(tag, "tree")) {
107 if (fsck_tree(sha1, data, size) < 0)
108 return -1;
109 } else if (!strcmp(tag, "commit")) {
110 if (fsck_commit(sha1, data, size) < 0)
111 return -1;
112 } else
113 return -1;
114 return mark_sha1_seen(sha1, tag);
117 static int fsck_name(char *hex)
119 unsigned char sha1[20];
120 if (!get_sha1_hex(hex, sha1)) {
121 unsigned long mapsize;
122 void *map = map_sha1_file(sha1, &mapsize);
123 if (map) {
124 char type[100];
125 unsigned long size;
126 void *buffer = NULL;
127 if (!check_sha1_signature(sha1, map, mapsize))
128 buffer = unpack_sha1_file(map, mapsize, type, &size);
129 munmap(map, mapsize);
130 if (buffer && !fsck_entry(sha1, type, buffer, size))
131 return 0;
134 return -1;
137 static int fsck_dir(int i, char *path)
139 DIR *dir = opendir(path);
140 struct dirent *de;
142 if (!dir) {
143 return error("missing sha1 directory '%s'", path);
146 while ((de = readdir(dir)) != NULL) {
147 char name[100];
148 int len = strlen(de->d_name);
150 switch (len) {
151 case 2:
152 if (de->d_name[1] != '.')
153 break;
154 case 1:
155 if (de->d_name[0] != '.')
156 break;
157 continue;
158 case 38:
159 sprintf(name, "%02x", i);
160 memcpy(name+2, de->d_name, len+1);
161 if (!fsck_name(name))
162 continue;
164 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
166 closedir(dir);
167 return 0;
170 int main(int argc, char **argv)
172 int i, heads;
173 char *sha1_dir;
175 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
176 for (i = 0; i < 256; i++) {
177 static char dir[4096];
178 sprintf(dir, "%s/%02x", sha1_dir, i);
179 fsck_dir(i, dir);
182 heads = 0;
183 for (i = 1; i < argc; i++) {
184 if (!strcmp(argv[i], "--unreachable")) {
185 show_unreachable = 1;
186 continue;
188 if (!get_sha1_hex(argv[i], head_sha1)) {
189 mark_reachable(lookup_rev(head_sha1));
190 heads++;
191 continue;
193 error("fsck-cache [[--unreachable] <head-sha1>*]");
196 if (!heads) {
197 if (show_unreachable) {
198 fprintf(stderr, "unable to do reachability without a head\n");
199 show_unreachable = 0;
201 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
204 check_connectivity();
205 return 0;