repack: refactor pack deletion for future use

The repack builtin deletes redundant pack-files and their associated .idx, .promisor, .bitmap, and .keep files. We will want to re-use this logic in the future for other types of repack, so pull the logic into 'unlink_pack_path()' in packfile.c. The 'ignore_keep' parameter is enabled for the use in repack, but will be important for a future caller. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 10, 2019 at 16:35 UTC 8434e85d5f9602a82f6b34ba82a9cb9e559a2d3b
3 files changed +37 -12
builtin/repack.c
+2 -12
@@ -129,19 +129,9 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list,
129
130 static void remove_redundant_pack(const char *dir_name, const char *base_name)
131 {
132 - const char *exts[] = {".pack", ".idx", ".keep", ".bitmap", ".promisor"};
133 - int i;
132 struct strbuf buf = STRBUF_INIT;
135 - size_t plen;
136 -
137 - strbuf_addf(&buf, "%s/%s", dir_name, base_name);
138 - plen = buf.len;
139 -
140 - for (i = 0; i < ARRAY_SIZE(exts); i++) {
141 - strbuf_setlen(&buf, plen);
142 - strbuf_addstr(&buf, exts[i]);
143 - unlink(buf.buf);
144 - }
133 + strbuf_addf(&buf, "%s/%s.pack", dir_name, base_name);
134 + unlink_pack_path(buf.buf, 1);
135 strbuf_release(&buf);
136 }
137
packfile.c
+28
@@ -352,6 +352,34 @@ void close_all_packs(struct raw_object_store *o)
352 }
353 }
354
355 +void unlink_pack_path(const char *pack_name, int force_delete)
356 +{
357 + static const char *exts[] = {".pack", ".idx", ".keep", ".bitmap", ".promisor"};
358 + int i;
359 + struct strbuf buf = STRBUF_INIT;
360 + size_t plen;
361 +
362 + strbuf_addstr(&buf, pack_name);
363 + strip_suffix_mem(buf.buf, &buf.len, ".pack");
364 + plen = buf.len;
365 +
366 + if (!force_delete) {
367 + strbuf_addstr(&buf, ".keep");
368 + if (!access(buf.buf, F_OK)) {
369 + strbuf_release(&buf);
370 + return;
371 + }
372 + }
373 +
374 + for (i = 0; i < ARRAY_SIZE(exts); i++) {
375 + strbuf_setlen(&buf, plen);
376 + strbuf_addstr(&buf, exts[i]);
377 + unlink(buf.buf);
378 + }
379 +
380 + strbuf_release(&buf);
381 +}
382 +
383 /*
384 * The LRU pack is the one with the oldest MRU window, preferring packs
385 * with no used windows, or the oldest mtime if it has no windows allocated.
packfile.h
+7
@@ -95,6 +95,13 @@ void unuse_pack(struct pack_window **);
95 void clear_delta_base_cache(void);
96 struct packed_git *add_packed_git(const char *path, size_t path_len, int local);
97
98 +/*
99 + * Unlink the .pack and associated extension files.
100 + * Does not unlink if 'force_delete' is false and the pack-file is
101 + * marked as ".keep".
102 + */
103 +extern void unlink_pack_path(const char *pack_name, int force_delete);
104 +
105 /*
106 * Make sure that a pointer access into an mmap'd index file is within bounds,
107 * and can provide at least 8 bytes of data.