[PATCH] Let umask do its work upon filesystem object creation.
[git/gitweb.git] / prune-packed.c
blob41ee2a73bc325dd35831431ddfc176c1a19397df
1 #include "cache.h"
3 static const char prune_packed_usage[] = "git-prune-packed: usage: git-prune-packed";
5 static void prune_dir(int i, DIR *dir, char *pathname, int len)
7 struct dirent *de;
8 char hex[40];
10 sprintf(hex, "%02x", i);
11 while ((de = readdir(dir)) != NULL) {
12 unsigned char sha1[20];
13 if (strlen(de->d_name) != 38)
14 continue;
15 memcpy(hex+2, de->d_name, 38);
16 if (get_sha1_hex(hex, sha1))
17 continue;
18 if (!has_sha1_pack(sha1))
19 continue;
20 memcpy(pathname + len, de->d_name, 38);
21 if (unlink(pathname) < 0)
22 error("unable to unlink %s", pathname);
26 static void prune_packed_objects(void)
28 int i;
29 static char pathname[PATH_MAX];
30 const char *dir = get_object_directory();
31 int len = strlen(dir);
33 if (len > PATH_MAX - 42)
34 die("impossible object directory");
35 memcpy(pathname, dir, len);
36 if (len && pathname[len-1] != '/')
37 pathname[len++] = '/';
38 for (i = 0; i < 256; i++) {
39 DIR *d;
41 sprintf(pathname + len, "%02x/", i);
42 d = opendir(pathname);
43 if (!d)
44 die("unable to open %s", pathname);
45 prune_dir(i, d, pathname, len + 3);
46 closedir(d);
50 int main(int argc, char **argv)
52 int i;
54 for (i = 1; i < argc; i++) {
55 const char *arg = argv[i];
57 if (*arg == '-') {
58 /* Handle flags here .. */
59 usage(prune_packed_usage);
61 /* Handle arguments here .. */
62 usage(prune_packed_usage);
64 prune_packed_objects();
65 return 0;