Skip to content

Commit 88e2972

Browse files
pks-tgitster
authored andcommitted
reftable: explicitly handle hash format IDs
The hash format IDs are used for two different things across the reftable codebase: - They are used as a 32 bit unsigned integer when reading and writing the header in order to identify the hash function. - They are used internally to identify which hash function is in use. When one only considers the second usecase one might think that one can easily change the representation of those hash IDs. But because those IDs end up in the reftable header and footer on disk it is important that those never change. Create separate constants `REFTABLE_FORMAT_ID_*` and use them in contexts where we read or write reftable headers. This serves multiple purposes: - It allows us to more easily discern cases where we actually use those constants for the on-disk format. - It detangles us from the same constants that are defined in libgit.a, which is another required step to convert the reftable library to become standalone. - It makes the next step easier where we stop using `GIT_*_FORMAT_ID` constants in favor of a custom enum. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 17e8039 commit 88e2972

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

reftable/basics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,12 @@ int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);
150150

151151
int hash_size(uint32_t id);
152152

153+
/*
154+
* Format IDs that identify the hash function used by a reftable. Note that
155+
* these constants end up on disk and thus mustn't change. The format IDs are
156+
* "sha1" and "s256" in big endian, respectively.
157+
*/
158+
#define REFTABLE_FORMAT_ID_SHA1 ((uint32_t) 0x73686131)
159+
#define REFTABLE_FORMAT_ID_SHA256 ((uint32_t) 0x73323536)
160+
153161
#endif

reftable/reader.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,18 @@ static int parse_footer(struct reftable_reader *r, uint8_t *footer,
109109
if (r->version == 1) {
110110
r->hash_id = GIT_SHA1_FORMAT_ID;
111111
} else {
112-
r->hash_id = get_be32(f);
113-
switch (r->hash_id) {
114-
case GIT_SHA1_FORMAT_ID:
112+
switch (get_be32(f)) {
113+
case REFTABLE_FORMAT_ID_SHA1:
114+
r->hash_id = GIT_SHA1_FORMAT_ID;
115115
break;
116-
case GIT_SHA256_FORMAT_ID:
116+
case REFTABLE_FORMAT_ID_SHA256:
117+
r->hash_id = GIT_SHA256_FORMAT_ID;
117118
break;
118119
default:
119120
err = REFTABLE_FORMAT_ERROR;
120121
goto done;
121122
}
123+
122124
f += 4;
123125
}
124126

reftable/writer.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,22 @@ static int writer_write_header(struct reftable_writer *w, uint8_t *dest)
103103
put_be64(dest + 8, w->min_update_index);
104104
put_be64(dest + 16, w->max_update_index);
105105
if (writer_version(w) == 2) {
106-
put_be32(dest + 24, w->opts.hash_id);
106+
uint32_t hash_id;
107+
108+
switch (w->opts.hash_id) {
109+
case GIT_SHA1_FORMAT_ID:
110+
hash_id = REFTABLE_FORMAT_ID_SHA1;
111+
break;
112+
case GIT_SHA256_FORMAT_ID:
113+
hash_id = REFTABLE_FORMAT_ID_SHA256;
114+
break;
115+
default:
116+
return -1;
117+
}
118+
119+
put_be32(dest + 24, hash_id);
107120
}
121+
108122
return header_size(writer_version(w));
109123
}
110124

0 commit comments

Comments
 (0)