]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable/system: introduce `reftable_rand()`
authorPatrick Steinhardt <ps@pks.im>
Tue, 18 Feb 2025 09:20:47 +0000 (10:20 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 18 Feb 2025 18:55:38 +0000 (10:55 -0800)
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>
reftable/stack.c
reftable/system.c
reftable/system.h

index c33f0c3333accd05a9d1ca11079b74d5b63daafc..08893fd454fe54b0f6bcdecc452c9d1687878997 100644 (file)
@@ -523,7 +523,7 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
                close(fd);
                fd = -1;
 
-               delay = delay + (delay * git_rand(CSPRNG_BYTES_INSECURE)) / UINT32_MAX + 1;
+               delay = delay + (delay * reftable_rand()) / UINT32_MAX + 1;
                sleep_millisec(delay);
        }
 
@@ -688,7 +688,7 @@ int reftable_stack_add(struct reftable_stack *st,
 static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
 {
        char buf[100];
-       uint32_t rnd = git_rand(CSPRNG_BYTES_INSECURE);
+       uint32_t rnd = reftable_rand();
        snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
                 min, max, rnd);
        reftable_buf_reset(dest);
index adf8e4d30b823c7d13a58c8e7ed050412ed08cf1..e25ccc0da3ccb81e72cf896f05bd6ed336a9e808 100644 (file)
@@ -4,6 +4,11 @@
 #include "../lockfile.h"
 #include "../tempfile.h"
 
+uint32_t reftable_rand(void)
+{
+       return git_rand(CSPRNG_BYTES_INSECURE);
+}
+
 int tmpfile_from_pattern(struct reftable_tmpfile *out, const char *pattern)
 {
        struct tempfile *tempfile;
index d02eacea8f02dd1b1c3013e6bd14faa40aa66464..bb6a7e6285b8402c053a6c47b48757ea5dd19fd6 100644 (file)
@@ -14,6 +14,12 @@ https://developers.google.com/open-source/licenses/bsd
 #include "git-compat-util.h"
 #include "compat/zlib-compat.h"
 
+/*
+ * Return a random 32 bit integer. This function is expected to return
+ * pre-seeded data.
+ */
+uint32_t reftable_rand(void);
+
 /*
  * An implementation-specific temporary file. By making this specific to the
  * implementation it becomes possible to tie temporary files into any kind of