reftable/system: introduce `reftable_rand()`

Introduce a new system-level `reftable_rand()` function that generates a single unsigned integer for us. The implementation of this function is to be provided by the calling codebase, which allows us to more easily hook into pre-seeded random number generators. Adapt the two callsites where we generated random data. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 18, 2025 at 10:20 UTC 712f6cfe5411fdbbf46688c22a40fac7e0a8839b
3 files changed +13 -2
reftable/stack.c
+2 -2
@@ -523,7 +523,7 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
523 close(fd);
524 fd = -1;
525
526 - delay = delay + (delay * git_rand(CSPRNG_BYTES_INSECURE)) / UINT32_MAX + 1;
526 + delay = delay + (delay * reftable_rand()) / UINT32_MAX + 1;
527 sleep_millisec(delay);
528 }
529
@@ -688,7 +688,7 @@ int reftable_stack_add(struct reftable_stack *st,
688 static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
689 {
690 char buf[100];
691 - uint32_t rnd = git_rand(CSPRNG_BYTES_INSECURE);
691 + uint32_t rnd = reftable_rand();
692 snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
693 min, max, rnd);
694 reftable_buf_reset(dest);
reftable/system.c
+5
@@ -4,6 +4,11 @@
4 #include "../lockfile.h"
5 #include "../tempfile.h"
6
7 +uint32_t reftable_rand(void)
8 +{
9 + return git_rand(CSPRNG_BYTES_INSECURE);
10 +}
11 +
12 int tmpfile_from_pattern(struct reftable_tmpfile *out, const char *pattern)
13 {
14 struct tempfile *tempfile;
reftable/system.h
+6
@@ -14,6 +14,12 @@ https://developers.google.com/open-source/licenses/bsd
14 #include "git-compat-util.h"
15 #include "compat/zlib-compat.h"
16
17 +/*
18 + * Return a random 32 bit integer. This function is expected to return
19 + * pre-seeded data.
20 + */
21 +uint32_t reftable_rand(void);
22 +
23 /*
24 * An implementation-specific temporary file. By making this specific to the
25 * implementation it becomes possible to tie temporary files into any kind of