reftable/stack: provide fsync(3p) via system header

Users of the reftable library are expected to provide their own function callback in cases they want to sync(3p) data to disk via the reftable write options. But if no such function was provided we end up calling fsync(3p) directly, which may not even be available on some systems. While dropping the explicit call to fsync(3p) would work, it would lead to an unsafe default behaviour where a project may have forgotten to set up the callback function, and that could lead to potential data loss. So this is not a great solution. Instead, drop the callback function and make it mandatory for the project to define fsync(3p). In the case of Git, we can then easily inject our custom implementation via the "reftable-system.h" header so that we continue to use `fsync_component()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 2, 2026 at 09:31 UTC b45ea595e6f6b03a749abc2c8e508504429a4cf3
5 files changed +12 -22
refs/reftable-backend.c
-6
@@ -366,11 +366,6 @@ static int reftable_be_config(const char *var, const char *value,
366 return 0;
367 }
368
369 -static int reftable_be_fsync(int fd)
370 -{
371 - return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
372 -}
373 -
369 static struct ref_store *reftable_be_init(struct repository *repo,
370 const char *payload,
371 const char *gitdir,
@@ -408,7 +403,6 @@ static struct ref_store *reftable_be_init(struct repository *repo,
403 refs->write_options.disable_auto_compact =
404 !git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1);
405 refs->write_options.lock_timeout_ms = 100;
411 - refs->write_options.fsync = reftable_be_fsync;
406
407 repo_config(the_repository, reftable_be_config, &refs->write_options);
408
reftable/reftable-system.h
+3
@@ -12,4 +12,7 @@
12 #include "compat/posix.h"
13 #include "compat/zlib-compat.h"
14
15 +int reftable_fsync(int fd);
16 +#define fsync(fd) reftable_fsync(fd)
17 +
18 #endif
reftable/reftable-writer.h
-6
@@ -61,12 +61,6 @@ struct reftable_write_options {
61 */
62 long lock_timeout_ms;
63
64 - /*
65 - * Optional callback used to fsync files to disk. Falls back to using
66 - * fsync(3P) when unset.
67 - */
68 - int (*fsync)(int fd);
69 -
64 /*
65 * Callback function to execute whenever the stack is being reloaded.
66 * This can be used e.g. to discard cached information that relies on
reftable/stack.c
+3 -10
@@ -29,13 +29,6 @@ static int stack_filename(struct reftable_buf *dest, struct reftable_stack *st,
29 return 0;
30 }
31
32 -static int stack_fsync(const struct reftable_write_options *opts, int fd)
33 -{
34 - if (opts->fsync)
35 - return opts->fsync(fd);
36 - return fsync(fd);
37 -}
38 -
32 static ssize_t reftable_write_data(int fd, const void *data, size_t size)
33 {
34 size_t total_written = 0;
@@ -69,7 +62,7 @@ static ssize_t fd_writer_write(void *arg, const void *data, size_t sz)
62 static int fd_writer_flush(void *arg)
63 {
64 struct fd_writer *writer = arg;
72 - return stack_fsync(writer->opts, writer->fd);
65 + return fsync(writer->fd);
66 }
67
68 static int fd_read_lines(int fd, char ***namesp)
@@ -812,7 +805,7 @@ int reftable_addition_commit(struct reftable_addition *add)
805 goto done;
806 }
807
815 - err = stack_fsync(&add->stack->opts, add->tables_list_lock.fd);
808 + err = fsync(add->tables_list_lock.fd);
809 if (err < 0) {
810 err = REFTABLE_IO_ERROR;
811 goto done;
@@ -1480,7 +1473,7 @@ static int stack_compact_range(struct reftable_stack *st,
1473 goto done;
1474 }
1475
1483 - err = stack_fsync(&st->opts, tables_list_lock.fd);
1476 + err = fsync(tables_list_lock.fd);
1477 if (err < 0) {
1478 err = REFTABLE_IO_ERROR;
1479 unlink(new_table_path.buf);
reftable/system.c
+6
@@ -5,6 +5,7 @@
5 #include "reftable-error.h"
6 #include "../lockfile.h"
7 #include "../tempfile.h"
8 +#include "../write-or-die.h"
9
10 uint32_t reftable_rand(void)
11 {
@@ -131,3 +132,8 @@ int flock_commit(struct reftable_flock *l)
132
133 return 0;
134 }
135 +
136 +int reftable_fsync(int fd)
137 +{
138 + return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
139 +}