Restore functionality to allow proxies to cache objects
[git/dscho.git] / prune-packed.c
blob73f0f3a46277e9053d97590f8e2200477fe247e3
1 #include "cache.h"
3 static const char prune_packed_usage[] =
4 "git-prune-packed [-n]";
6 static int dryrun;
8 static void prune_dir(int i, DIR *dir, char *pathname, int len)
10 struct dirent *de;
11 char hex[40];
13 sprintf(hex, "%02x", i);
14 while ((de = readdir(dir)) != NULL) {
15 unsigned char sha1[20];
16 if (strlen(de->d_name) != 38)
17 continue;
18 memcpy(hex+2, de->d_name, 38);
19 if (get_sha1_hex(hex, sha1))
20 continue;
21 if (!has_sha1_pack(sha1))
22 continue;
23 memcpy(pathname + len, de->d_name, 38);
24 if (dryrun)
25 printf("rm -f %s\n", pathname);
26 else if (unlink(pathname) < 0)
27 error("unable to unlink %s", pathname);
29 pathname[len] = 0;
30 if (rmdir(pathname))
31 mkdir(pathname, 0777);
34 static void prune_packed_objects(void)
36 int i;
37 static char pathname[PATH_MAX];
38 const char *dir = get_object_directory();
39 int len = strlen(dir);
41 if (len > PATH_MAX - 42)
42 die("impossible object directory");
43 memcpy(pathname, dir, len);
44 if (len && pathname[len-1] != '/')
45 pathname[len++] = '/';
46 for (i = 0; i < 256; i++) {
47 DIR *d;
49 sprintf(pathname + len, "%02x/", i);
50 d = opendir(pathname);
51 if (!d)
52 continue;
53 prune_dir(i, d, pathname, len + 3);
54 closedir(d);
58 int main(int argc, char **argv)
60 int i;
62 for (i = 1; i < argc; i++) {
63 const char *arg = argv[i];
65 if (*arg == '-') {
66 if (!strcmp(arg, "-n"))
67 dryrun = 1;
68 else
69 usage(prune_packed_usage);
70 continue;
72 /* Handle arguments here .. */
73 usage(prune_packed_usage);
75 prune_packed_objects();
76 return 0;