diff options
author | Patrick Steinhardt <ps@pks.im> | 2024-11-18 16:33:57 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2024-11-19 04:23:10 +0100 |
commit | c2f08236ed786a48e50af33ecc5c0f951c14761b (patch) | |
tree | 2bf85fa99746b31ee4b1b3fc5d53bb5ce1c5ea6b /reftable | |
parent | reftable: explicitly handle hash format IDs (diff) | |
download | git-c2f08236ed786a48e50af33ecc5c0f951c14761b.tar.xz git-c2f08236ed786a48e50af33ecc5c0f951c14761b.zip |
reftable/system: stop depending on "hash.h"
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'reftable')
-rw-r--r-- | reftable/basics.c | 13 | ||||
-rw-r--r-- | reftable/basics.h | 2 | ||||
-rw-r--r-- | reftable/merged.c | 4 | ||||
-rw-r--r-- | reftable/merged.h | 3 | ||||
-rw-r--r-- | reftable/reader.c | 8 | ||||
-rw-r--r-- | reftable/reader.h | 4 | ||||
-rw-r--r-- | reftable/reftable-basics.h | 13 | ||||
-rw-r--r-- | reftable/reftable-merged.h | 4 | ||||
-rw-r--r-- | reftable/reftable-reader.h | 2 | ||||
-rw-r--r-- | reftable/reftable-record.h | 12 | ||||
-rw-r--r-- | reftable/reftable-writer.h | 2 | ||||
-rw-r--r-- | reftable/stack.c | 4 | ||||
-rw-r--r-- | reftable/system.h | 3 | ||||
-rw-r--r-- | reftable/writer.c | 8 |
14 files changed, 47 insertions, 35 deletions
diff --git a/reftable/basics.c b/reftable/basics.c index bc4fcc9144..7d84a5d62d 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -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(); } diff --git a/reftable/basics.h b/reftable/basics.h index bcab0b529b..36beda2c25 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -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 diff --git a/reftable/merged.c b/reftable/merged.c index 514d6facf4..5b93e20f42 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -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; @@ -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; } diff --git a/reftable/merged.h b/reftable/merged.h index 89bd0c4b35..0b7d939e92 100644 --- a/reftable/merged.h +++ b/reftable/merged.h @@ -10,11 +10,12 @@ https://developers.google.com/open-source/licenses/bsd #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. */ diff --git a/reftable/reader.c b/reftable/reader.c index 64eb6938ef..ea82955c9b 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -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; } @@ -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; diff --git a/reftable/reader.h b/reftable/reader.h index 010fbfe851..d2b48a4849 100644 --- a/reftable/reader.h +++ b/reftable/reader.h @@ -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; diff --git a/reftable/reftable-basics.h b/reftable/reftable-basics.h index 6e8e636b71..e0397ed583 100644 --- a/reftable/reftable-basics.h +++ b/reftable/reftable-basics.h @@ -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 *)); diff --git a/reftable/reftable-merged.h b/reftable/reftable-merged.h index a970d5dd89..f2d01c3ef8 100644 --- a/reftable/reftable-merged.h +++ b/reftable/reftable-merged.h @@ -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, @@ -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 diff --git a/reftable/reftable-reader.h b/reftable/reftable-reader.h index 6a2d0b693f..0085fbb903 100644 --- a/reftable/reftable-reader.h +++ b/reftable/reftable-reader.h @@ -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, diff --git a/reftable/reftable-record.h b/reftable/reftable-record.h index 2d42463c58..ddd48eb579 100644 --- a/reftable/reftable-record.h +++ b/reftable/reftable-record.h @@ -9,7 +9,7 @@ https://developers.google.com/open-source/licenses/bsd #ifndef REFTABLE_RECORD_H #define REFTABLE_RECORD_H -#include "hash.h" +#include "reftable-basics.h" #include <stdint.h> /* @@ -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; @@ -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; diff --git a/reftable/reftable-writer.h b/reftable/reftable-writer.h index e4fc953788..211860d08a 100644 --- a/reftable/reftable-writer.h +++ b/reftable/reftable-writer.h @@ -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; diff --git a/reftable/stack.c b/reftable/stack.c index c33979536e..9ae716ff37 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -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; @@ -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; diff --git a/reftable/system.h b/reftable/system.h index 8564213475..38d3534620 100644 --- a/reftable/system.h +++ b/reftable/system.h @@ -14,8 +14,5 @@ https://developers.google.com/open-source/licenses/bsd #include "git-compat-util.h" #include "lockfile.h" #include "tempfile.h" -#include "hash.h" /* hash ID, sizes.*/ - -int hash_size(uint32_t id); #endif diff --git a/reftable/writer.c b/reftable/writer.c index 9aa45de634..ea2f831fc5 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -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; @@ -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; } @@ -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: |