7 * These two functions should build up a graph in memory about
8 * what objects we've referenced, and found, and types..
10 * Right now we don't do that kind of reachability checking. Yet.
12 static void mark_needs_sha1(unsigned char *parent
, const char * type
, unsigned char *child
)
16 static int mark_sha1_seen(unsigned char *sha1
, char *tag
)
21 static int fsck_tree(unsigned char *sha1
, void *data
, unsigned long 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
)
31 mark_needs_sha1(sha1
, "blob", file_sha1
);
36 static int fsck_commit(unsigned char *sha1
, void *data
, unsigned long size
)
38 unsigned char tree_sha1
[20];
39 unsigned char parent_sha1
[20];
41 if (memcmp(data
, "tree ", 5))
43 if (get_sha1_hex(data
+ 5, tree_sha1
) < 0)
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)
50 mark_needs_sha1(sha1
, "commit", parent_sha1
);
51 data
+= 7 + 40 + 1; /* "parent " + <hex sha1> + '\n' */
56 static int fsck_entry(unsigned char *sha1
, char *tag
, void *data
, unsigned long size
)
58 if (!strcmp(tag
, "blob")) {
59 /* Nothing to check */;
60 } else if (!strcmp(tag
, "tree")) {
61 if (fsck_tree(sha1
, data
, size
) < 0)
63 } else if (!strcmp(tag
, "commit")) {
64 if (fsck_commit(sha1
, data
, size
) < 0)
68 return mark_sha1_seen(sha1
, tag
);
71 static int fsck_name(char *hex
)
73 unsigned char sha1
[20];
74 if (!get_sha1_hex(hex
, sha1
)) {
75 unsigned long mapsize
;
76 void *map
= map_sha1_file(sha1
, &mapsize
);
81 if (!check_sha1_signature(sha1
, map
, mapsize
))
82 buffer
= unpack_sha1_file(map
, mapsize
, type
, &size
);
84 if (buffer
&& !fsck_entry(sha1
, type
, buffer
, size
))
91 static int fsck_dir(int i
, char *path
)
93 DIR *dir
= opendir(path
);
97 fprintf(stderr
, "missing sha1 directory '%s'", path
);
101 while ((de
= readdir(dir
)) != NULL
) {
103 int len
= strlen(de
->d_name
);
107 if (de
->d_name
[1] != '.')
110 if (de
->d_name
[0] != '.')
114 sprintf(name
, "%02x", i
);
115 memcpy(name
+2, de
->d_name
, len
+1);
116 if (!fsck_name(name
))
119 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
125 int main(int argc
, char **argv
)
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
);