midx: return success/failure in chunk write methods

Historically, the chunk-writing methods in midx.c have returned the
amount of data written so the writer method could compare this with the
table of contents. This presents with some interesting issues:

1. If a chunk writing method has a bug that miscalculates the written
   bytes, then we can satisfy the table of contents without actually
   writing the right amount of data to the hashfile. The commit-graph
   writing code checks the hashfile struct directly for a more robust
   verification.

2. There is no way for a chunk writing method to gracefully fail.
   Returning an int presents an opportunity to fail without a die().

3. The current pattern doesn't match chunk_write_fn type exactly, so we
   cannot share code with commit-graph.c

For these reasons, convert the midx chunk writer methods to return an
'int'. Since none of them fail at the moment, they all return 0.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Derrick Stolee
2021-02-18 14:07:31 +00:00
committed by Junio C Hamano
parent 980f525c3c
commit 0ccd713cb6

63
midx.c
View File

@ -650,7 +650,7 @@ static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
return deduplicated_entries; return deduplicated_entries;
} }
static size_t write_midx_pack_names(struct hashfile *f, void *data) static int write_midx_pack_names(struct hashfile *f, void *data)
{ {
struct write_midx_context *ctx = data; struct write_midx_context *ctx = data;
uint32_t i; uint32_t i;
@ -678,14 +678,13 @@ static size_t write_midx_pack_names(struct hashfile *f, void *data)
if (i < MIDX_CHUNK_ALIGNMENT) { if (i < MIDX_CHUNK_ALIGNMENT) {
memset(padding, 0, sizeof(padding)); memset(padding, 0, sizeof(padding));
hashwrite(f, padding, i); hashwrite(f, padding, i);
written += i;
} }
return written; return 0;
} }
static size_t write_midx_oid_fanout(struct hashfile *f, static int write_midx_oid_fanout(struct hashfile *f,
void *data) void *data)
{ {
struct write_midx_context *ctx = data; struct write_midx_context *ctx = data;
struct pack_midx_entry *list = ctx->entries; struct pack_midx_entry *list = ctx->entries;
@ -710,17 +709,16 @@ static size_t write_midx_oid_fanout(struct hashfile *f,
list = next; list = next;
} }
return MIDX_CHUNK_FANOUT_SIZE; return 0;
} }
static size_t write_midx_oid_lookup(struct hashfile *f, static int write_midx_oid_lookup(struct hashfile *f,
void *data) void *data)
{ {
struct write_midx_context *ctx = data; struct write_midx_context *ctx = data;
unsigned char hash_len = the_hash_algo->rawsz; unsigned char hash_len = the_hash_algo->rawsz;
struct pack_midx_entry *list = ctx->entries; struct pack_midx_entry *list = ctx->entries;
uint32_t i; uint32_t i;
size_t written = 0;
for (i = 0; i < ctx->entries_nr; i++) { for (i = 0; i < ctx->entries_nr; i++) {
struct pack_midx_entry *obj = list++; struct pack_midx_entry *obj = list++;
@ -734,19 +732,17 @@ static size_t write_midx_oid_lookup(struct hashfile *f,
} }
hashwrite(f, obj->oid.hash, (int)hash_len); hashwrite(f, obj->oid.hash, (int)hash_len);
written += hash_len;
} }
return written; return 0;
} }
static size_t write_midx_object_offsets(struct hashfile *f, static int write_midx_object_offsets(struct hashfile *f,
void *data) void *data)
{ {
struct write_midx_context *ctx = data; struct write_midx_context *ctx = data;
struct pack_midx_entry *list = ctx->entries; struct pack_midx_entry *list = ctx->entries;
uint32_t i, nr_large_offset = 0; uint32_t i, nr_large_offset = 0;
size_t written = 0;
for (i = 0; i < ctx->entries_nr; i++) { for (i = 0; i < ctx->entries_nr; i++) {
struct pack_midx_entry *obj = list++; struct pack_midx_entry *obj = list++;
@ -766,20 +762,17 @@ static size_t write_midx_object_offsets(struct hashfile *f,
obj->offset); obj->offset);
else else
hashwrite_be32(f, (uint32_t)obj->offset); hashwrite_be32(f, (uint32_t)obj->offset);
written += MIDX_CHUNK_OFFSET_WIDTH;
} }
return written; return 0;
} }
static size_t write_midx_large_offsets(struct hashfile *f, static int write_midx_large_offsets(struct hashfile *f,
void *data) void *data)
{ {
struct write_midx_context *ctx = data; struct write_midx_context *ctx = data;
struct pack_midx_entry *list = ctx->entries; struct pack_midx_entry *list = ctx->entries;
struct pack_midx_entry *end = ctx->entries + ctx->entries_nr; struct pack_midx_entry *end = ctx->entries + ctx->entries_nr;
size_t written = 0;
uint32_t nr_large_offset = ctx->num_large_offsets; uint32_t nr_large_offset = ctx->num_large_offsets;
while (nr_large_offset) { while (nr_large_offset) {
@ -795,12 +788,12 @@ static size_t write_midx_large_offsets(struct hashfile *f,
if (!(offset >> 31)) if (!(offset >> 31))
continue; continue;
written += hashwrite_be64(f, offset); hashwrite_be64(f, offset);
nr_large_offset--; nr_large_offset--;
} }
return written; return 0;
} }
static int write_midx_internal(const char *object_dir, struct multi_pack_index *m, static int write_midx_internal(const char *object_dir, struct multi_pack_index *m,
@ -812,7 +805,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
struct hashfile *f = NULL; struct hashfile *f = NULL;
struct lock_file lk; struct lock_file lk;
struct write_midx_context ctx = { 0 }; struct write_midx_context ctx = { 0 };
uint64_t written = 0; uint64_t header_size = 0;
uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1]; uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1]; uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
struct progress *progress = NULL; struct progress *progress = NULL;
@ -940,10 +933,10 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
goto cleanup; goto cleanup;
} }
written = write_midx_header(f, num_chunks, ctx.nr - dropped_packs); header_size = write_midx_header(f, num_chunks, ctx.nr - dropped_packs);
chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES; chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH; chunk_offsets[cur_chunk] = header_size + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
cur_chunk++; cur_chunk++;
chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT; chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
@ -981,39 +974,37 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
hashwrite_be32(f, chunk_ids[i]); hashwrite_be32(f, chunk_ids[i]);
hashwrite_be64(f, chunk_offsets[i]); hashwrite_be64(f, chunk_offsets[i]);
written += MIDX_CHUNKLOOKUP_WIDTH;
} }
if (flags & MIDX_PROGRESS) if (flags & MIDX_PROGRESS)
progress = start_delayed_progress(_("Writing chunks to multi-pack-index"), progress = start_delayed_progress(_("Writing chunks to multi-pack-index"),
num_chunks); num_chunks);
for (i = 0; i < num_chunks; i++) { for (i = 0; i < num_chunks; i++) {
if (written != chunk_offsets[i]) if (f->total + f->offset != chunk_offsets[i])
BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32, BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32,
chunk_offsets[i], chunk_offsets[i],
written, f->total + f->offset,
chunk_ids[i]); chunk_ids[i]);
switch (chunk_ids[i]) { switch (chunk_ids[i]) {
case MIDX_CHUNKID_PACKNAMES: case MIDX_CHUNKID_PACKNAMES:
written += write_midx_pack_names(f, &ctx); write_midx_pack_names(f, &ctx);
break; break;
case MIDX_CHUNKID_OIDFANOUT: case MIDX_CHUNKID_OIDFANOUT:
written += write_midx_oid_fanout(f, &ctx); write_midx_oid_fanout(f, &ctx);
break; break;
case MIDX_CHUNKID_OIDLOOKUP: case MIDX_CHUNKID_OIDLOOKUP:
written += write_midx_oid_lookup(f, &ctx); write_midx_oid_lookup(f, &ctx);
break; break;
case MIDX_CHUNKID_OBJECTOFFSETS: case MIDX_CHUNKID_OBJECTOFFSETS:
written += write_midx_object_offsets(f, &ctx); write_midx_object_offsets(f, &ctx);
break; break;
case MIDX_CHUNKID_LARGEOFFSETS: case MIDX_CHUNKID_LARGEOFFSETS:
written += write_midx_large_offsets(f, &ctx); write_midx_large_offsets(f, &ctx);
break; break;
default: default:
@ -1025,9 +1016,9 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
} }
stop_progress(&progress); stop_progress(&progress);
if (written != chunk_offsets[num_chunks]) if (hashfile_total(f) != chunk_offsets[num_chunks])
BUG("incorrect final offset %"PRIu64" != %"PRIu64, BUG("incorrect final offset %"PRIu64" != %"PRIu64,
written, hashfile_total(f),
chunk_offsets[num_chunks]); chunk_offsets[num_chunks]);
finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM); finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);