mingw: always chmod(, 0666) before unlink()
[git/mingw/4msysgit.git] / builtin-prune-packed.c
blob639fbed2fcb200ecb7895e83028c1fbe7f9f61af
1 #include "builtin.h"
2 #include "cache.h"
4 static const char prune_packed_usage[] =
5 "git-prune-packed [-n] [-q]";
7 #define DRY_RUN 01
8 #define VERBOSE 02
10 static void prune_dir(int i, DIR *dir, char *pathname, int len, int opts)
12 struct dirent *de;
13 char hex[40];
15 sprintf(hex, "%02x", i);
16 while ((de = readdir(dir)) != NULL) {
17 unsigned char sha1[20];
18 if (strlen(de->d_name) != 38)
19 continue;
20 memcpy(hex+2, de->d_name, 38);
21 if (get_sha1_hex(hex, sha1))
22 continue;
23 if (!has_sha1_pack(sha1, NULL))
24 continue;
25 memcpy(pathname + len, de->d_name, 38);
26 if (opts & DRY_RUN)
27 printf("rm -f %s\n", pathname);
28 else {
29 #ifdef __MINGW32__
30 /* read-only files cannot be removed */
31 chmod(pathname, 0666);
32 #endif
33 if (unlink(pathname) < 0)
34 error("unable to unlink %s", pathname);
37 pathname[len] = 0;
38 rmdir(pathname);
41 void prune_packed_objects(int opts)
43 int i;
44 static char pathname[PATH_MAX];
45 const char *dir = get_object_directory();
46 int len = strlen(dir);
48 if (len > PATH_MAX - 42)
49 die("impossible object directory");
50 memcpy(pathname, dir, len);
51 if (len && pathname[len-1] != '/')
52 pathname[len++] = '/';
53 for (i = 0; i < 256; i++) {
54 DIR *d;
56 sprintf(pathname + len, "%02x/", i);
57 d = opendir(pathname);
58 if (opts == VERBOSE && (d || i == 255))
59 fprintf(stderr, "Removing unused objects %d%%...\015",
60 ((i+1) * 100) / 256);
61 if (!d)
62 continue;
63 prune_dir(i, d, pathname, len + 3, opts);
64 closedir(d);
66 if (opts == VERBOSE)
67 fprintf(stderr, "\nDone.\n");
70 int cmd_prune_packed(int argc, const char **argv, const char *prefix)
72 int i;
73 int opts = VERBOSE;
75 for (i = 1; i < argc; i++) {
76 const char *arg = argv[i];
78 if (*arg == '-') {
79 if (!strcmp(arg, "-n"))
80 opts |= DRY_RUN;
81 else if (!strcmp(arg, "-q"))
82 opts &= ~VERBOSE;
83 else
84 usage(prune_packed_usage);
85 continue;
87 /* Handle arguments here .. */
88 usage(prune_packed_usage);
90 sync();
91 prune_packed_objects(opts);
92 return 0;