builtin/prune.c: use strbuf to avoid having to worry about PATH_MAX
While at it, rename prune_tmp_object(), which used to be a helper to remove temporary files that were created to become loose object files, to prune_tmp_file(), as the function is also used to remove any random cruft whose name begins with tmp_ directly in .git/object or .git/object/pack directories these days. Noticed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Dec 17, 2013 at 18:22 UTC
4454e9cb5994467fb1a6b60b3b2637fd4ff76ac0
1 file changed
+24
-14
builtin/prune.c
+24
-14
@@ -17,9 +17,8 @@ static int verbose;
17
static unsigned long expire;
18
static int show_progress = -1;
19
20
-static int prune_tmp_object(const char *path, const char *filename)
20
+static int prune_tmp_file(const char *fullpath)
21
{
22
- const char *fullpath = mkpath("%s/%s", path, filename);
22
struct stat st;
23
if (lstat(fullpath, &st))
24
return error("Could not stat '%s'", fullpath);
@@ -32,9 +31,8 @@ static int prune_tmp_object(const char *path, const char *filename)
31
return 0;
32
}
33
35
-static int prune_object(char *path, const char *filename, const unsigned char *sha1)
34
+static int prune_object(const char *fullpath, const unsigned char *sha1)
35
{
37
- const char *fullpath = mkpath("%s/%s", path, filename);
36
struct stat st;
37
if (lstat(fullpath, &st))
38
return error("Could not stat '%s'", fullpath);
@@ -50,9 +48,10 @@ static int prune_object(char *path, const char *filename, const unsigned char *s
48
return 0;
49
}
50
53
-static int prune_dir(int i, char *path)
51
+static int prune_dir(int i, struct strbuf *path)
52
{
55
- DIR *dir = opendir(path);
53
+ size_t baselen = path->len;
54
+ DIR *dir = opendir(path->buf);
55
struct dirent *de;
56
57
if (!dir)
@@ -77,28 +76,39 @@ static int prune_dir(int i, char *path)
76
if (lookup_object(sha1))
77
continue;
78
80
- prune_object(path, de->d_name, sha1);
79
+ strbuf_addf(path, "/%s", de->d_name);
80
+ prune_object(path->buf, sha1);
81
+ strbuf_setlen(path, baselen);
82
continue;
83
}
84
if (!prefixcmp(de->d_name, "tmp_obj_")) {
84
- prune_tmp_object(path, de->d_name);
85
+ strbuf_addf(path, "/%s", de->d_name);
86
+ prune_tmp_file(path->buf);
87
+ strbuf_setlen(path, baselen);
88
continue;
89
}
87
- fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
90
+ fprintf(stderr, "bad sha1 file: %s/%s\n", path->buf, de->d_name);
91
}
92
closedir(dir);
93
if (!show_only)
91
- rmdir(path);
94
+ rmdir(path->buf);
95
return 0;
96
}
97
98
static void prune_object_dir(const char *path)
99
{
100
+ struct strbuf buf = STRBUF_INIT;
101
+ size_t baselen;
102
int i;
103
+
104
+ strbuf_addstr(&buf, path);
105
+ strbuf_addch(&buf, '/');
106
+ baselen = buf.len;
107
+
108
for (i = 0; i < 256; i++) {
99
- static char dir[4096];
100
- sprintf(dir, "%s/%02x", path, i);
101
- prune_dir(i, dir);
109
+ strbuf_addf(&buf, "%02x", i);
110
+ prune_dir(i, &buf);
111
+ strbuf_setlen(&buf, baselen);
112
}
113
}
114
@@ -120,7 +130,7 @@ static void remove_temporary_files(const char *path)
130
}
131
while ((de = readdir(dir)) != NULL)
132
if (!prefixcmp(de->d_name, "tmp_"))
123
- prune_tmp_object(path, de->d_name);
133
+ prune_tmp_file(mkpath("%s/%s", path, de->d_name));
134
closedir(dir);
135
}
136