summaryrefslogtreecommitdiff
path: root/midx.c
diff options
context:
space:
mode:
Diffstat (limited to 'midx.c')
-rw-r--r--midx.c108
1 files changed, 61 insertions, 47 deletions
diff --git a/midx.c b/midx.c
index 7e06e85975..837b46b2af 100644
--- a/midx.c
+++ b/midx.c
@@ -57,15 +57,15 @@ const unsigned char *get_midx_checksum(struct multi_pack_index *m)
return m->data + m->data_len - the_hash_algo->rawsz;
}
-char *get_midx_filename(const char *object_dir)
+void get_midx_filename(struct strbuf *out, const char *object_dir)
{
- return xstrfmt("%s/pack/multi-pack-index", object_dir);
+ strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
}
-char *get_midx_rev_filename(struct multi_pack_index *m)
+void get_midx_rev_filename(struct strbuf *out, struct multi_pack_index *m)
{
- return xstrfmt("%s/pack/multi-pack-index-%s.rev",
- m->object_dir, hash_to_hex(get_midx_checksum(m)));
+ get_midx_filename(out, m->object_dir);
+ strbuf_addf(out, "-%s.rev", hash_to_hex(get_midx_checksum(m)));
}
static int midx_read_oid_fanout(const unsigned char *chunk_start,
@@ -89,28 +89,30 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
size_t midx_size;
void *midx_map = NULL;
uint32_t hash_version;
- char *midx_name = get_midx_filename(object_dir);
+ struct strbuf midx_name = STRBUF_INIT;
uint32_t i;
const char *cur_pack_name;
struct chunkfile *cf = NULL;
- fd = git_open(midx_name);
+ get_midx_filename(&midx_name, object_dir);
+
+ fd = git_open(midx_name.buf);
if (fd < 0)
goto cleanup_fail;
if (fstat(fd, &st)) {
- error_errno(_("failed to read %s"), midx_name);
+ error_errno(_("failed to read %s"), midx_name.buf);
goto cleanup_fail;
}
midx_size = xsize_t(st.st_size);
if (midx_size < MIDX_MIN_SIZE) {
- error(_("multi-pack-index file %s is too small"), midx_name);
+ error(_("multi-pack-index file %s is too small"), midx_name.buf);
goto cleanup_fail;
}
- FREE_AND_NULL(midx_name);
+ strbuf_release(&midx_name);
midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
@@ -179,12 +181,13 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
+ free_chunkfile(cf);
return m;
cleanup_fail:
free(m);
- free(midx_name);
- free(cf);
+ strbuf_release(&midx_name);
+ free_chunkfile(cf);
if (midx_map)
munmap(midx_map, midx_size);
if (0 <= fd)
@@ -1107,6 +1110,22 @@ cleanup:
return ret;
}
+static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
+ const char *object_dir)
+{
+ struct multi_pack_index *cur;
+
+ /* Ensure the given object_dir is local, or a known alternate. */
+ find_odb(r, object_dir);
+
+ for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
+ if (!strcmp(object_dir, cur->object_dir))
+ return cur;
+ }
+
+ return NULL;
+}
+
static int write_midx_internal(const char *object_dir,
struct string_list *packs_to_include,
struct string_list *packs_to_drop,
@@ -1114,25 +1133,21 @@ static int write_midx_internal(const char *object_dir,
const char *refs_snapshot,
unsigned flags)
{
- char *midx_name;
+ struct strbuf midx_name = STRBUF_INIT;
unsigned char midx_hash[GIT_MAX_RAWSZ];
uint32_t i;
struct hashfile *f = NULL;
struct lock_file lk;
struct write_midx_context ctx = { 0 };
- struct multi_pack_index *cur;
int pack_name_concat_len = 0;
int dropped_packs = 0;
int result = 0;
struct chunkfile *cf;
- /* Ensure the given object_dir is local, or a known alternate. */
- find_odb(the_repository, object_dir);
-
- midx_name = get_midx_filename(object_dir);
- if (safe_create_leading_directories(midx_name))
+ get_midx_filename(&midx_name, object_dir);
+ if (safe_create_leading_directories(midx_name.buf))
die_errno(_("unable to create leading directories of %s"),
- midx_name);
+ midx_name.buf);
if (!packs_to_include) {
/*
@@ -1140,12 +1155,7 @@ static int write_midx_internal(const char *object_dir,
* packs to include, since all packs and objects are copied
* blindly from an existing MIDX if one is present.
*/
- for (cur = get_multi_pack_index(the_repository); cur; cur = cur->next) {
- if (!strcmp(object_dir, cur->object_dir)) {
- ctx.m = cur;
- break;
- }
- }
+ ctx.m = lookup_multi_pack_index(the_repository, object_dir);
}
if (ctx.m && !midx_checksum_valid(ctx.m)) {
@@ -1366,7 +1376,7 @@ static int write_midx_internal(const char *object_dir,
pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
(pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
- hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
+ hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
if (ctx.nr - dropped_packs == 0) {
@@ -1403,9 +1413,9 @@ static int write_midx_internal(const char *object_dir,
ctx.pack_order = midx_pack_order(&ctx);
if (flags & MIDX_WRITE_REV_INDEX)
- write_midx_reverse_index(midx_name, midx_hash, &ctx);
+ write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
if (flags & MIDX_WRITE_BITMAP) {
- if (write_midx_bitmap(midx_name, midx_hash, &ctx,
+ if (write_midx_bitmap(midx_name.buf, midx_hash, &ctx,
refs_snapshot, flags) < 0) {
error(_("could not write multi-pack bitmap"));
result = 1;
@@ -1416,7 +1426,8 @@ static int write_midx_internal(const char *object_dir,
if (ctx.m)
close_object_store(the_repository->objects);
- commit_lock_file(&lk);
+ if (commit_lock_file(&lk) < 0)
+ die_errno(_("could not write multi-pack-index"));
clear_midx_files_ext(object_dir, ".bitmap", midx_hash);
clear_midx_files_ext(object_dir, ".rev", midx_hash);
@@ -1434,7 +1445,7 @@ cleanup:
free(ctx.entries);
free(ctx.pack_perm);
free(ctx.pack_order);
- free(midx_name);
+ strbuf_release(&midx_name);
return result;
}
@@ -1498,20 +1509,22 @@ static void clear_midx_files_ext(const char *object_dir, const char *ext,
void clear_midx_file(struct repository *r)
{
- char *midx = get_midx_filename(r->objects->odb->path);
+ struct strbuf midx = STRBUF_INIT;
+
+ get_midx_filename(&midx, r->objects->odb->path);
if (r->objects && r->objects->multi_pack_index) {
close_midx(r->objects->multi_pack_index);
r->objects->multi_pack_index = NULL;
}
- if (remove_path(midx))
- die(_("failed to clear multi-pack-index at %s"), midx);
+ if (remove_path(midx.buf))
+ die(_("failed to clear multi-pack-index at %s"), midx.buf);
clear_midx_files_ext(r->objects->odb->path, ".bitmap", NULL);
clear_midx_files_ext(r->objects->odb->path, ".rev", NULL);
- free(midx);
+ strbuf_release(&midx);
}
static int verify_midx_error;
@@ -1564,12 +1577,15 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
if (!m) {
int result = 0;
struct stat sb;
- char *filename = get_midx_filename(object_dir);
- if (!stat(filename, &sb)) {
+ struct strbuf filename = STRBUF_INIT;
+
+ get_midx_filename(&filename, object_dir);
+
+ if (!stat(filename.buf, &sb)) {
error(_("multi-pack-index file exists, but failed to parse"));
result = 1;
}
- free(filename);
+ strbuf_release(&filename);
return result;
}
@@ -1602,7 +1618,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
* Remaining tests assume that we have objects, so we can
* return here.
*/
- return verify_midx_error;
+ goto cleanup;
}
if (flags & MIDX_PROGRESS)
@@ -1680,7 +1696,9 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
}
stop_progress(&progress);
+cleanup:
free(pairs);
+ close_midx(m);
return verify_midx_error;
}
@@ -1689,7 +1707,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
{
uint32_t i, *count, result = 0;
struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
- struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
+ struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
struct progress *progress = NULL;
if (!m)
@@ -1734,12 +1752,11 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
free(count);
- if (packs_to_drop.nr) {
+ if (packs_to_drop.nr)
result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
- m = NULL;
- }
string_list_clear(&packs_to_drop, 0);
+
return result;
}
@@ -1855,7 +1872,7 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
struct child_process cmd = CHILD_PROCESS_INIT;
FILE *cmd_in;
struct strbuf base_name = STRBUF_INIT;
- struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
+ struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
/*
* When updating the default for these configuration
@@ -1927,11 +1944,8 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
}
result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
- m = NULL;
cleanup:
- if (m)
- close_midx(m);
free(include_pack);
return result;
}