Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | |
Junio C Hamano | 51890a6 | 2005-08-19 21:38:36 -0700 | [diff] [blame] | 3 | static const char prune_packed_usage[] = |
| 4 | "git-prune-packed [-n]"; |
| 5 | |
| 6 | static int dryrun; |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 7 | |
| 8 | static void prune_dir(int i, DIR *dir, char *pathname, int len) |
| 9 | { |
| 10 | struct dirent *de; |
| 11 | char hex[40]; |
| 12 | |
| 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); |
Junio C Hamano | 51890a6 | 2005-08-19 21:38:36 -0700 | [diff] [blame] | 24 | if (dryrun) |
| 25 | printf("rm -f %s\n", pathname); |
| 26 | else if (unlink(pathname) < 0) |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 27 | error("unable to unlink %s", pathname); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | static void prune_packed_objects(void) |
| 32 | { |
| 33 | int i; |
| 34 | static char pathname[PATH_MAX]; |
| 35 | const char *dir = get_object_directory(); |
| 36 | int len = strlen(dir); |
| 37 | |
| 38 | if (len > PATH_MAX - 42) |
| 39 | die("impossible object directory"); |
| 40 | memcpy(pathname, dir, len); |
| 41 | if (len && pathname[len-1] != '/') |
| 42 | pathname[len++] = '/'; |
| 43 | for (i = 0; i < 256; i++) { |
| 44 | DIR *d; |
| 45 | |
| 46 | sprintf(pathname + len, "%02x/", i); |
| 47 | d = opendir(pathname); |
| 48 | if (!d) |
| 49 | die("unable to open %s", pathname); |
| 50 | prune_dir(i, d, pathname, len + 3); |
| 51 | closedir(d); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | int main(int argc, char **argv) |
| 56 | { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 1; i < argc; i++) { |
| 60 | const char *arg = argv[i]; |
| 61 | |
| 62 | if (*arg == '-') { |
Junio C Hamano | 51890a6 | 2005-08-19 21:38:36 -0700 | [diff] [blame] | 63 | if (!strcmp(arg, "-n")) |
| 64 | dryrun = 1; |
| 65 | else |
| 66 | usage(prune_packed_usage); |
| 67 | continue; |
Linus Torvalds | 2396ec8 | 2005-07-03 14:27:34 -0700 | [diff] [blame] | 68 | } |
| 69 | /* Handle arguments here .. */ |
| 70 | usage(prune_packed_usage); |
| 71 | } |
| 72 | prune_packed_objects(); |
| 73 | return 0; |
| 74 | } |