Skip to content

Commit

Permalink
reftable/system: stop depending on "hash.h"
Browse files Browse the repository at this point in the history
We include "hash.h" in "reftable/system.h" such that we can use hash
format IDs as well as the raw size of SHA1 and SHA256. As we are in the
process of converting the reftable library to become standalone we of
course cannot rely on those constants anymore.

Introduce a new `enum reftable_hash` to replace internal uses of the
hash format IDs and new constants that replace internal uses of the hash
size. Adapt the reftable backend to set up the correct hash function.

Signed-off-by: Patrick Steinhardt <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
pks-t authored and gitster committed Nov 19, 2024
1 parent 88e2972 commit c2f0823
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 143 deletions.
12 changes: 11 additions & 1 deletion refs/reftable-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "../object.h"
#include "../path.h"
#include "../refs.h"
#include "../reftable/reftable-basics.h"
#include "../reftable/reftable-stack.h"
#include "../reftable/reftable-record.h"
#include "../reftable/reftable-error.h"
Expand Down Expand Up @@ -289,7 +290,16 @@ static struct ref_store *reftable_be_init(struct repository *repo,
refs->store_flags = store_flags;
refs->log_all_ref_updates = repo_settings_get_log_all_ref_updates(repo);

refs->write_options.hash_id = repo->hash_algo->format_id;
switch (repo->hash_algo->format_id) {
case GIT_SHA1_FORMAT_ID:
refs->write_options.hash_id = REFTABLE_HASH_SHA1;
break;
case GIT_SHA256_FORMAT_ID:
refs->write_options.hash_id = REFTABLE_HASH_SHA256;
break;
default:
BUG("unknown hash algorithm %d", repo->hash_algo->format_id);
}
refs->write_options.default_permissions = calc_shared_perm(0666 & ~mask);
refs->write_options.disable_auto_compact =
!git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1);
Expand Down
13 changes: 7 additions & 6 deletions reftable/basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,15 @@ int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b)
return p;
}

int hash_size(uint32_t id)
int hash_size(enum reftable_hash id)
{
if (!id)
return REFTABLE_HASH_SIZE_SHA1;
switch (id) {
case 0:
case GIT_SHA1_FORMAT_ID:
return GIT_SHA1_RAWSZ;
case GIT_SHA256_FORMAT_ID:
return GIT_SHA256_RAWSZ;
case REFTABLE_HASH_SHA1:
return REFTABLE_HASH_SIZE_SHA1;
case REFTABLE_HASH_SHA256:
return REFTABLE_HASH_SIZE_SHA256;
}
abort();
}
2 changes: 1 addition & 1 deletion reftable/basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ char *reftable_strdup(const char *str);
/* Find the longest shared prefix size of `a` and `b` */
int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);

int hash_size(uint32_t id);
int hash_size(enum reftable_hash id);

/*
* Format IDs that identify the hash function used by a reftable. Note that
Expand Down
4 changes: 2 additions & 2 deletions reftable/merged.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ static void iterator_from_merged_iter(struct reftable_iterator *it,

int reftable_merged_table_new(struct reftable_merged_table **dest,
struct reftable_reader **readers, size_t n,
uint32_t hash_id)
enum reftable_hash hash_id)
{
struct reftable_merged_table *m = NULL;
uint64_t last_max = 0;
Expand Down Expand Up @@ -293,7 +293,7 @@ int reftable_merged_table_init_log_iterator(struct reftable_merged_table *mt,
return merged_table_init_iter(mt, it, BLOCK_TYPE_LOG);
}

uint32_t reftable_merged_table_hash_id(struct reftable_merged_table *mt)
enum reftable_hash reftable_merged_table_hash_id(struct reftable_merged_table *mt)
{
return mt->hash_id;
}
3 changes: 2 additions & 1 deletion reftable/merged.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ license that can be found in the LICENSE file or at
#define MERGED_H

#include "system.h"
#include "reftable-basics.h"

struct reftable_merged_table {
struct reftable_reader **readers;
size_t readers_len;
uint32_t hash_id;
enum reftable_hash hash_id;

/* If unset, produce deletions. This is useful for compaction. For the
* full stack, deletions should be produced. */
Expand Down
8 changes: 4 additions & 4 deletions reftable/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static int reader_get_block(struct reftable_reader *r,
return block_source_read_block(&r->source, dest, off, sz);
}

uint32_t reftable_reader_hash_id(struct reftable_reader *r)
enum reftable_hash reftable_reader_hash_id(struct reftable_reader *r)
{
return r->hash_id;
}
Expand Down Expand Up @@ -107,14 +107,14 @@ static int parse_footer(struct reftable_reader *r, uint8_t *footer,
f += 8;

if (r->version == 1) {
r->hash_id = GIT_SHA1_FORMAT_ID;
r->hash_id = REFTABLE_HASH_SHA1;
} else {
switch (get_be32(f)) {
case REFTABLE_FORMAT_ID_SHA1:
r->hash_id = GIT_SHA1_FORMAT_ID;
r->hash_id = REFTABLE_HASH_SHA1;
break;
case REFTABLE_FORMAT_ID_SHA256:
r->hash_id = GIT_SHA256_FORMAT_ID;
r->hash_id = REFTABLE_HASH_SHA256;
break;
default:
err = REFTABLE_FORMAT_ERROR;
Expand Down
4 changes: 2 additions & 2 deletions reftable/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ struct reftable_reader {
/* Size of the file, excluding the footer. */
uint64_t size;

/* 'sha1' for SHA1, 's256' for SHA-256 */
uint32_t hash_id;
/* The hash function used for ref records. */
enum reftable_hash hash_id;

uint32_t block_size;
uint64_t min_update_index;
Expand Down
13 changes: 13 additions & 0 deletions reftable/reftable-basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@

#include <stddef.h>

/*
* Hash functions understood by the reftable library. Note that the values are
* arbitrary and somewhat random such that we can easily detect cases where the
* hash hasn't been properly set up.
*/
enum reftable_hash {
REFTABLE_HASH_SHA1 = 89,
REFTABLE_HASH_SHA256 = 247,
};
#define REFTABLE_HASH_SIZE_SHA1 20
#define REFTABLE_HASH_SIZE_SHA256 32
#define REFTABLE_HASH_SIZE_MAX REFTABLE_HASH_SIZE_SHA256

/* Overrides the functions to use for memory management. */
void reftable_set_alloc(void *(*malloc)(size_t),
void *(*realloc)(void *, size_t), void (*free)(void *));
Expand Down
4 changes: 2 additions & 2 deletions reftable/reftable-merged.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct reftable_reader;
*/
int reftable_merged_table_new(struct reftable_merged_table **dest,
struct reftable_reader **readers, size_t n,
uint32_t hash_id);
enum reftable_hash hash_id);

/* Initialize a merged table iterator for reading refs. */
int reftable_merged_table_init_ref_iterator(struct reftable_merged_table *mt,
Expand All @@ -56,6 +56,6 @@ reftable_merged_table_min_update_index(struct reftable_merged_table *mt);
void reftable_merged_table_free(struct reftable_merged_table *m);

/* return the hash ID of the merged table. */
uint32_t reftable_merged_table_hash_id(struct reftable_merged_table *m);
enum reftable_hash reftable_merged_table_hash_id(struct reftable_merged_table *m);

#endif
2 changes: 1 addition & 1 deletion reftable/reftable-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int reftable_reader_init_log_iterator(struct reftable_reader *r,
struct reftable_iterator *it);

/* returns the hash ID used in this table. */
uint32_t reftable_reader_hash_id(struct reftable_reader *r);
enum reftable_hash reftable_reader_hash_id(struct reftable_reader *r);

/* return an iterator for the refs pointing to `oid`. */
int reftable_reader_refs_for(struct reftable_reader *r,
Expand Down
12 changes: 6 additions & 6 deletions reftable/reftable-record.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license that can be found in the LICENSE file or at
#ifndef REFTABLE_RECORD_H
#define REFTABLE_RECORD_H

#include "hash.h"
#include "reftable-basics.h"
#include <stdint.h>

/*
Expand Down Expand Up @@ -40,10 +40,10 @@ struct reftable_ref_record {
#define REFTABLE_NR_REF_VALUETYPES 4
} value_type;
union {
unsigned char val1[GIT_MAX_RAWSZ];
unsigned char val1[REFTABLE_HASH_SIZE_MAX];
struct {
unsigned char value[GIT_MAX_RAWSZ]; /* first hash */
unsigned char target_value[GIT_MAX_RAWSZ]; /* second hash */
unsigned char value[REFTABLE_HASH_SIZE_MAX]; /* first hash */
unsigned char target_value[REFTABLE_HASH_SIZE_MAX]; /* second hash */
} val2;
char *symref; /* referent, malloced 0-terminated string */
} value;
Expand Down Expand Up @@ -85,8 +85,8 @@ struct reftable_log_record {

union {
struct {
unsigned char new_hash[GIT_MAX_RAWSZ];
unsigned char old_hash[GIT_MAX_RAWSZ];
unsigned char new_hash[REFTABLE_HASH_SIZE_MAX];
unsigned char old_hash[REFTABLE_HASH_SIZE_MAX];
char *name;
char *email;
uint64_t time;
Expand Down
2 changes: 1 addition & 1 deletion reftable/reftable-writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct reftable_write_options {
/* 4-byte identifier ("sha1", "s256") of the hash.
* Defaults to SHA1 if unset
*/
uint32_t hash_id;
enum reftable_hash hash_id;

/* Default mode for creating files. If unset, use 0666 (+umask) */
unsigned int default_permissions;
Expand Down
4 changes: 2 additions & 2 deletions reftable/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir,
if (_opts)
opts = *_opts;
if (opts.hash_id == 0)
opts.hash_id = GIT_SHA1_FORMAT_ID;
opts.hash_id = REFTABLE_HASH_SHA1;

*dest = NULL;

Expand Down Expand Up @@ -1603,7 +1603,7 @@ struct segment suggest_compaction_segment(uint64_t *sizes, size_t n,

static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack *st)
{
int version = (st->opts.hash_id == GIT_SHA1_FORMAT_ID) ? 1 : 2;
int version = (st->opts.hash_id == REFTABLE_HASH_SHA1) ? 1 : 2;
int overhead = header_size(version) - 1;
uint64_t *sizes;

Expand Down
3 changes: 0 additions & 3 deletions reftable/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ license that can be found in the LICENSE file or at
#include "git-compat-util.h"
#include "lockfile.h"
#include "tempfile.h"
#include "hash.h" /* hash ID, sizes.*/

int hash_size(uint32_t id);

#endif
8 changes: 4 additions & 4 deletions reftable/writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void options_set_defaults(struct reftable_write_options *opts)
}

if (opts->hash_id == 0) {
opts->hash_id = GIT_SHA1_FORMAT_ID;
opts->hash_id = REFTABLE_HASH_SHA1;
}
if (opts->block_size == 0) {
opts->block_size = DEFAULT_BLOCK_SIZE;
Expand All @@ -88,7 +88,7 @@ static void options_set_defaults(struct reftable_write_options *opts)

static int writer_version(struct reftable_writer *w)
{
return (w->opts.hash_id == 0 || w->opts.hash_id == GIT_SHA1_FORMAT_ID) ?
return (w->opts.hash_id == 0 || w->opts.hash_id == REFTABLE_HASH_SHA1) ?
1 :
2;
}
Expand All @@ -106,10 +106,10 @@ static int writer_write_header(struct reftable_writer *w, uint8_t *dest)
uint32_t hash_id;

switch (w->opts.hash_id) {
case GIT_SHA1_FORMAT_ID:
case REFTABLE_HASH_SHA1:
hash_id = REFTABLE_FORMAT_ID_SHA1;
break;
case GIT_SHA256_FORMAT_ID:
case REFTABLE_HASH_SHA256:
hash_id = REFTABLE_FORMAT_ID_SHA256;
break;
default:
Expand Down
4 changes: 2 additions & 2 deletions t/helper/test-reftable.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ int cmd__dump_reftable(int argc, const char **argv)
int opt_dump_blocks = 0;
int opt_dump_table = 0;
int opt_dump_stack = 0;
uint32_t opt_hash_id = GIT_SHA1_FORMAT_ID;
uint32_t opt_hash_id = REFTABLE_HASH_SHA1;
const char *arg = NULL, *argv0 = argv[0];

for (; argc > 1; argv++, argc--)
Expand All @@ -167,7 +167,7 @@ int cmd__dump_reftable(int argc, const char **argv)
else if (!strcmp("-t", argv[1]))
opt_dump_table = 1;
else if (!strcmp("-6", argv[1]))
opt_hash_id = GIT_SHA256_FORMAT_ID;
opt_hash_id = REFTABLE_HASH_SHA256;
else if (!strcmp("-s", argv[1]))
opt_dump_stack = 1;
else if (!strcmp("-?", argv[1]) || !strcmp("-h", argv[1])) {
Expand Down
4 changes: 2 additions & 2 deletions t/unit-tests/lib-reftable.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "reftable/constants.h"
#include "reftable/writer.h"

void t_reftable_set_hash(uint8_t *p, int i, uint32_t id)
void t_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id)
{
memset(p, (uint8_t)i, hash_size(id));
}
Expand Down Expand Up @@ -82,7 +82,7 @@ void t_reftable_write_to_buf(struct reftable_buf *buf,
size_t off = i * (opts.block_size ? opts.block_size
: DEFAULT_BLOCK_SIZE);
if (!off)
off = header_size(opts.hash_id == GIT_SHA256_FORMAT_ID ? 2 : 1);
off = header_size(opts.hash_id == REFTABLE_HASH_SHA256 ? 2 : 1);
check_char(buf->buf[off], ==, 'r');
}

Expand Down
2 changes: 1 addition & 1 deletion t/unit-tests/lib-reftable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

struct reftable_buf;

void t_reftable_set_hash(uint8_t *p, int i, uint32_t id);
void t_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id);

struct reftable_writer *t_reftable_strbuf_writer(struct reftable_buf *buf,
struct reftable_write_options *opts);
Expand Down
Loading

0 comments on commit c2f0823

Please sign in to comment.