From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Edward Thomson <ethomson@edwardthomson.com>,
Eric Sunshine <sunshine@sunshineco.com>,
Justin Tobler <jltobler@gmail.com>
Subject: [PATCH v2 2/7] reftable: explicitly handle hash format IDs
Date: Fri, 8 Nov 2024 09:17:12 +0100 [thread overview]
Message-ID: <38cfe85bf5b82c70848e4b295ba6cae33dcfd667.1731047193.git.ps@pks.im> (raw)
In-Reply-To: <cover.1731047193.git.ps@pks.im>
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 <ps@pks.im>
---
reftable/basics.h | 8 ++++++++
reftable/reader.c | 10 ++++++----
reftable/writer.c | 16 +++++++++++++++-
3 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/reftable/basics.h b/reftable/basics.h
index 7aa46d7c30d..bcab0b529b0 100644
--- a/reftable/basics.h
+++ b/reftable/basics.h
@@ -150,4 +150,12 @@ int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);
int hash_size(uint32_t id);
+/*
+ * Format IDs that identify the hash function used by a reftable. Note that
+ * these constants end up on disk and thus mustn't change. The format IDs are
+ * "sha1" and "s256" in big endian, respectively.
+ */
+#define REFTABLE_FORMAT_ID_SHA1 ((uint32_t) 0x73686131)
+#define REFTABLE_FORMAT_ID_SHA256 ((uint32_t) 0x73323536)
+
#endif
diff --git a/reftable/reader.c b/reftable/reader.c
index 90dc950b577..64eb6938efe 100644
--- a/reftable/reader.c
+++ b/reftable/reader.c
@@ -109,16 +109,18 @@ static int parse_footer(struct reftable_reader *r, uint8_t *footer,
if (r->version == 1) {
r->hash_id = GIT_SHA1_FORMAT_ID;
} else {
- r->hash_id = get_be32(f);
- switch (r->hash_id) {
- case GIT_SHA1_FORMAT_ID:
+ switch (get_be32(f)) {
+ case REFTABLE_FORMAT_ID_SHA1:
+ r->hash_id = GIT_SHA1_FORMAT_ID;
break;
- case GIT_SHA256_FORMAT_ID:
+ case REFTABLE_FORMAT_ID_SHA256:
+ r->hash_id = GIT_SHA256_FORMAT_ID;
break;
default:
err = REFTABLE_FORMAT_ERROR;
goto done;
}
+
f += 4;
}
diff --git a/reftable/writer.c b/reftable/writer.c
index fd136794d5a..9aa45de6340 100644
--- a/reftable/writer.c
+++ b/reftable/writer.c
@@ -103,8 +103,22 @@ static int writer_write_header(struct reftable_writer *w, uint8_t *dest)
put_be64(dest + 8, w->min_update_index);
put_be64(dest + 16, w->max_update_index);
if (writer_version(w) == 2) {
- put_be32(dest + 24, w->opts.hash_id);
+ uint32_t hash_id;
+
+ switch (w->opts.hash_id) {
+ case GIT_SHA1_FORMAT_ID:
+ hash_id = REFTABLE_FORMAT_ID_SHA1;
+ break;
+ case GIT_SHA256_FORMAT_ID:
+ hash_id = REFTABLE_FORMAT_ID_SHA256;
+ break;
+ default:
+ return -1;
+ }
+
+ put_be32(dest + 24, hash_id);
}
+
return header_size(writer_version(w));
}
--
2.47.0.229.g8f8d6eee53.dirty
next prev parent reply other threads:[~2024-11-08 8:17 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-23 9:55 [PATCH 0/7] reftable: stop using Git subsystems Patrick Steinhardt
2024-10-23 9:55 ` [PATCH 1/7] reftable/system: move "dir.h" to its only user Patrick Steinhardt
2024-10-24 4:18 ` Eric Sunshine
2024-10-23 9:55 ` [PATCH 2/7] reftable: explicitly handle hash format IDs Patrick Steinhardt
2024-11-07 23:11 ` Justin Tobler
2024-10-23 9:56 ` [PATCH 3/7] reftable/system: stop depending on "hash.h" Patrick Steinhardt
2024-11-08 1:10 ` Justin Tobler
2024-11-08 6:17 ` Patrick Steinhardt
2024-10-23 9:56 ` [PATCH 4/7] reftable/stack: stop using `fsync_component()` directly Patrick Steinhardt
2024-11-08 2:09 ` Justin Tobler
2024-11-08 6:17 ` Patrick Steinhardt
2024-10-23 9:56 ` [PATCH 5/7] reftable/system: provide thin wrapper for tempfile subsystem Patrick Steinhardt
2024-10-23 9:56 ` [PATCH 6/7] reftable/stack: drop only use of `get_locked_file_path()` Patrick Steinhardt
2024-10-23 9:56 ` [PATCH 7/7] reftable/system: provide thin wrapper for lockfile subsystem Patrick Steinhardt
2024-11-08 8:17 ` [PATCH v2 0/7] reftable: stop using Git subsystems Patrick Steinhardt
2024-11-08 8:17 ` [PATCH v2 1/7] reftable/system: move "dir.h" to its only user Patrick Steinhardt
2024-11-08 8:17 ` Patrick Steinhardt [this message]
2024-11-18 13:47 ` [PATCH v2 2/7] reftable: explicitly handle hash format IDs karthik nayak
2024-11-08 8:17 ` [PATCH v2 3/7] reftable/system: stop depending on "hash.h" Patrick Steinhardt
2024-11-12 5:53 ` Junio C Hamano
2024-11-18 13:54 ` karthik nayak
2024-11-08 8:17 ` [PATCH v2 4/7] reftable/stack: stop using `fsync_component()` directly Patrick Steinhardt
2024-11-18 14:02 ` karthik nayak
2024-11-18 15:26 ` Patrick Steinhardt
2024-11-08 8:17 ` [PATCH v2 5/7] reftable/system: provide thin wrapper for tempfile subsystem Patrick Steinhardt
2024-11-18 14:18 ` karthik nayak
2024-11-18 14:29 ` karthik nayak
2024-11-08 8:17 ` [PATCH v2 6/7] reftable/stack: drop only use of `get_locked_file_path()` Patrick Steinhardt
2024-11-08 8:17 ` [PATCH v2 7/7] reftable/system: provide thin wrapper for lockfile subsystem Patrick Steinhardt
2024-11-08 17:39 ` [PATCH v2 0/7] reftable: stop using Git subsystems Justin Tobler
2024-11-11 0:37 ` Junio C Hamano
2024-11-18 14:30 ` karthik nayak
2024-11-18 15:26 ` Patrick Steinhardt
2024-11-18 15:33 ` [PATCH v3 " Patrick Steinhardt
2024-11-18 15:33 ` [PATCH v3 1/7] reftable/system: move "dir.h" to its only user Patrick Steinhardt
2024-11-18 15:33 ` [PATCH v3 2/7] reftable: explicitly handle hash format IDs Patrick Steinhardt
2024-11-18 15:33 ` [PATCH v3 3/7] reftable/system: stop depending on "hash.h" Patrick Steinhardt
2024-11-18 15:34 ` [PATCH v3 4/7] reftable/stack: stop using `fsync_component()` directly Patrick Steinhardt
2024-11-18 15:34 ` [PATCH v3 5/7] reftable/system: provide thin wrapper for tempfile subsystem Patrick Steinhardt
2024-11-18 15:34 ` [PATCH v3 6/7] reftable/stack: drop only use of `get_locked_file_path()` Patrick Steinhardt
2024-11-18 15:34 ` [PATCH v3 7/7] reftable/system: provide thin wrapper for lockfile subsystem Patrick Steinhardt
2024-11-19 14:23 ` [PATCH v3 0/7] reftable: stop using Git subsystems karthik nayak
2024-11-20 1:09 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=38cfe85bf5b82c70848e4b295ba6cae33dcfd667.1731047193.git.ps@pks.im \
--to=ps@pks.im \
--cc=ethomson@edwardthomson.com \
--cc=git@vger.kernel.org \
--cc=jltobler@gmail.com \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).