csum-file: stop depending on `the_repository`

There are multiple sites in "csum-file.c" where we use the global
`the_repository` variable, either explicitly or implicitly by using
`the_hash_algo`.

Refactor the code to stop using `the_repository` by adapting functions
to receive required data as parameters. Adapt callsites accordingly by
either using `the_repository->hash_algo`, or by using a context-provided
hash algorithm in case the subsystem already got rid of its dependency
on `the_repository`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Patrick Steinhardt 2025-03-10 08:13:20 +01:00 committed by Junio C Hamano
parent e969bc8759
commit 228457c9d9
14 changed files with 56 additions and 39 deletions

View File

@ -770,7 +770,7 @@ static void start_packfile(void)
p->pack_fd = pack_fd;
p->do_not_close = 1;
p->repo = the_repository;
pack_file = hashfd(pack_fd, p->pack_name);
pack_file = hashfd(the_repository->hash_algo, pack_fd, p->pack_name);

pack_data = p;
pack_size = write_pack_header(pack_file, 0);

View File

@ -1381,7 +1381,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
memset(objects + nr_objects + 1, 0,
nr_unresolved * sizeof(*objects));
f = hashfd(output_fd, curr_pack);
f = hashfd(the_repository->hash_algo, output_fd, curr_pack);
fix_unresolved_deltas(f);
strbuf_addf(&msg, Q_("completed with %d local object",
"completed with %d local objects",

View File

@ -1311,7 +1311,8 @@ static void write_pack_file(void)
char *pack_tmp_name = NULL;

if (pack_to_stdout)
f = hashfd_throughput(1, "<stdout>", progress_state);
f = hashfd_throughput(the_repository->hash_algo, 1,
"<stdout>", progress_state);
else
f = create_tmp_packfile(&pack_tmp_name);


View File

@ -2090,11 +2090,13 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
return -1;
}

f = hashfd(get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
f = hashfd(the_repository->hash_algo,
get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
} else {
hold_lock_file_for_update_mode(&lk, ctx->graph_name,
LOCK_DIE_ON_ERROR, 0444);
f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
f = hashfd(the_repository->hash_algo,
get_lock_file_fd(&lk), get_lock_file_path(&lk));
}

cf = init_chunkfile(f);
@ -2716,7 +2718,8 @@ static void graph_report(const char *fmt, ...)

static int commit_graph_checksum_valid(struct commit_graph *g)
{
return hashfile_checksum_valid(g->data, g->data_len);
return hashfile_checksum_valid(the_repository->hash_algo,
g->data, g->data_len);
}

static int verify_one_commit_graph(struct repository *r,

View File

@ -8,8 +8,6 @@
* able to verify hasn't been messed with afterwards.
*/

#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h"
#include "csum-file.h"
#include "git-zlib.h"
@ -148,21 +146,23 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
}
}

struct hashfile *hashfd_check(const char *name)
struct hashfile *hashfd_check(const struct git_hash_algo *algop,
const char *name)
{
int sink, check;
struct hashfile *f;

sink = xopen("/dev/null", O_WRONLY);
check = xopen(name, O_RDONLY);
f = hashfd(sink, name);
f = hashfd(algop, sink, name);
f->check_fd = check;
f->check_buffer = xmalloc(f->buffer_len);

return f;
}

static struct hashfile *hashfd_internal(int fd, const char *name,
static struct hashfile *hashfd_internal(const struct git_hash_algo *algop,
int fd, const char *name,
struct progress *tp,
size_t buffer_len)
{
@ -176,7 +176,7 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
f->do_crc = 0;
f->skip_hash = 0;

f->algop = unsafe_hash_algo(the_hash_algo);
f->algop = unsafe_hash_algo(algop);
f->algop->init_fn(&f->ctx);

f->buffer_len = buffer_len;
@ -186,17 +186,19 @@ static struct hashfile *hashfd_internal(int fd, const char *name,
return f;
}

struct hashfile *hashfd(int fd, const char *name)
struct hashfile *hashfd(const struct git_hash_algo *algop,
int fd, const char *name)
{
/*
* Since we are not going to use a progress meter to
* measure the rate of data passing through this hashfile,
* use a larger buffer size to reduce fsync() calls.
*/
return hashfd_internal(fd, name, NULL, 128 * 1024);
return hashfd_internal(algop, fd, name, NULL, 128 * 1024);
}

struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
int fd, const char *name, struct progress *tp)
{
/*
* Since we are expecting to report progress of the
@ -204,7 +206,7 @@ struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp
* size so the progress indicators arrive at a more
* frequent rate.
*/
return hashfd_internal(fd, name, tp, 8 * 1024);
return hashfd_internal(algop, fd, name, tp, 8 * 1024);
}

void hashfile_checkpoint_init(struct hashfile *f,
@ -246,13 +248,15 @@ uint32_t crc32_end(struct hashfile *f)
return f->crc32;
}

int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
int hashfile_checksum_valid(const struct git_hash_algo *algop,
const unsigned char *data, size_t total_len)
{
unsigned char got[GIT_MAX_RAWSZ];
struct git_hash_ctx ctx;
const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo);
size_t data_len = total_len - algop->rawsz;

algop = unsafe_hash_algo(algop);

if (total_len < algop->rawsz)
return 0; /* say "too short"? */


View File

@ -45,9 +45,12 @@ int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
#define CSUM_FSYNC 2
#define CSUM_HASH_IN_STREAM 4

struct hashfile *hashfd(int fd, const char *name);
struct hashfile *hashfd_check(const char *name);
struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
struct hashfile *hashfd(const struct git_hash_algo *algop,
int fd, const char *name);
struct hashfile *hashfd_check(const struct git_hash_algo *algop,
const char *name);
struct hashfile *hashfd_throughput(const struct git_hash_algo *algop,
int fd, const char *name, struct progress *tp);

/*
* Free the hashfile without flushing its contents to disk. This only
@ -66,7 +69,8 @@ void crc32_begin(struct hashfile *);
uint32_t crc32_end(struct hashfile *);

/* Verify checksum validity while reading. Returns non-zero on success. */
int hashfile_checksum_valid(const unsigned char *data, size_t len);
int hashfile_checksum_valid(const struct git_hash_algo *algop,
const unsigned char *data, size_t len);

/*
* Returns the total number of bytes fed to the hashfile so far (including ones

View File

@ -1342,10 +1342,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
return -1;
}

f = hashfd(get_tempfile_fd(incr), get_tempfile_path(incr));
f = hashfd(r->hash_algo, get_tempfile_fd(incr),
get_tempfile_path(incr));
} else {
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));
f = hashfd(r->hash_algo, get_lock_file_fd(&lk),
get_lock_file_path(&lk));
}

cf = init_chunkfile(f);

3
midx.c
View File

@ -747,7 +747,8 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i

int midx_checksum_valid(struct multi_pack_index *m)
{
return hashfile_checksum_valid(m->data, m->data_len);
return hashfile_checksum_valid(m->repo->hash_algo,
m->data, m->data_len);
}

struct clear_midx_data {

View File

@ -1030,7 +1030,7 @@ void bitmap_writer_finish(struct bitmap_writer *writer,
if (writer->pseudo_merges_nr)
options |= BITMAP_OPT_PSEUDO_MERGES;

f = hashfd(fd, tmp_file.buf);
f = hashfd(the_repository->hash_algo, fd, tmp_file.buf);

memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
header.version = htons(default_version);

View File

@ -3024,7 +3024,8 @@ int bitmap_is_preferred_refname(struct repository *r, const char *refname)
return 0;
}

static int verify_bitmap_file(const char *name)
static int verify_bitmap_file(const struct git_hash_algo *algop,
const char *name)
{
struct stat st;
unsigned char *data;
@ -3040,7 +3041,7 @@ static int verify_bitmap_file(const char *name)

data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (!hashfile_checksum_valid(data, st.st_size))
if (!hashfile_checksum_valid(algop, data, st.st_size))
res = error(_("bitmap file '%s' has invalid checksum"),
name);

@ -3055,14 +3056,14 @@ int verify_bitmap_files(struct repository *r)
for (struct multi_pack_index *m = get_multi_pack_index(r);
m; m = m->next) {
char *midx_bitmap_name = midx_bitmap_filename(m);
res |= verify_bitmap_file(midx_bitmap_name);
res |= verify_bitmap_file(r->hash_algo, midx_bitmap_name);
free(midx_bitmap_name);
}

for (struct packed_git *p = get_all_packs(r);
p; p = p->next) {
char *pack_bitmap_name = pack_bitmap_filename(p);
res |= verify_bitmap_file(pack_bitmap_name);
res |= verify_bitmap_file(r->hash_algo, pack_bitmap_name);
free(pack_bitmap_name);
}


View File

@ -180,7 +180,7 @@ int verify_pack_index(struct packed_git *p)
return error("packfile %s index not opened", p->pack_name);

/* Verify SHA1 sum of the index file */
if (!hashfile_checksum_valid(p->index_data, p->index_size))
if (!hashfile_checksum_valid(the_repository->hash_algo, p->index_data, p->index_size))
err = error("Packfile index for %s hash mismatch",
p->pack_name);
return err;

View File

@ -322,7 +322,8 @@ int verify_pack_revindex(struct packed_git *p)
if (!p->revindex_map || !p->revindex_data)
return res;

if (!hashfile_checksum_valid((const unsigned char *)p->revindex_map, p->revindex_size)) {
if (!hashfile_checksum_valid(the_repository->hash_algo,
(const unsigned char *)p->revindex_map, p->revindex_size)) {
error(_("invalid checksum"));
res = -1;
}

View File

@ -82,7 +82,7 @@ const char *write_idx_file(const struct git_hash_algo *hash_algo,

if (opts->flags & WRITE_IDX_VERIFY) {
assert(index_name);
f = hashfd_check(index_name);
f = hashfd_check(the_repository->hash_algo, index_name);
} else {
if (!index_name) {
struct strbuf tmp_file = STRBUF_INIT;
@ -92,7 +92,7 @@ const char *write_idx_file(const struct git_hash_algo *hash_algo,
unlink(index_name);
fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
}
f = hashfd(fd, index_name);
f = hashfd(the_repository->hash_algo, fd, index_name);
}

/* if last object's offset is >= 2^31 we should use index V2 */
@ -268,7 +268,7 @@ char *write_rev_file_order(const struct git_hash_algo *hash_algo,
fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
path = xstrdup(rev_name);
}
f = hashfd(fd, path);
f = hashfd(the_repository->hash_algo, fd, path);
} else if (flags & WRITE_REV_VERIFY) {
struct stat statbuf;
if (stat(rev_name, &statbuf)) {
@ -278,7 +278,7 @@ char *write_rev_file_order(const struct git_hash_algo *hash_algo,
} else
die_errno(_("could not stat: %s"), rev_name);
}
f = hashfd_check(rev_name);
f = hashfd_check(the_repository->hash_algo, rev_name);
path = xstrdup(rev_name);
} else {
return NULL;
@ -346,7 +346,7 @@ static char *write_mtimes_file(const struct git_hash_algo *hash_algo,

fd = odb_mkstemp(&tmp_file, "pack/tmp_mtimes_XXXXXX");
mtimes_name = strbuf_detach(&tmp_file, NULL);
f = hashfd(fd, mtimes_name);
f = hashfd(the_repository->hash_algo, fd, mtimes_name);

write_mtimes_header(hash_algo, f);
write_mtimes_objects(f, to_pack, objects, nr_objects);
@ -534,7 +534,7 @@ struct hashfile *create_tmp_packfile(char **pack_tmp_name)

fd = odb_mkstemp(&tmpname, "pack/tmp_pack_XXXXXX");
*pack_tmp_name = strbuf_detach(&tmpname, NULL);
return hashfd(fd, *pack_tmp_name);
return hashfd(the_repository->hash_algo, fd, *pack_tmp_name);
}

static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,

View File

@ -2848,7 +2848,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
struct strbuf sb = STRBUF_INIT;
int nr, nr_threads, ret;

f = hashfd(tempfile->fd, tempfile->filename.buf);
f = hashfd(the_repository->hash_algo, tempfile->fd, tempfile->filename.buf);

prepare_repo_settings(r);
f->skip_hash = r->settings.index_skip_hash;