Skip to content

Commit b45ea59

Browse files
pks-tgitster
authored andcommitted
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>
1 parent 34c17b8 commit b45ea59

5 files changed

Lines changed: 12 additions & 22 deletions

File tree

refs/reftable-backend.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,6 @@ static int reftable_be_config(const char *var, const char *value,
366366
return 0;
367367
}
368368

369-
static int reftable_be_fsync(int fd)
370-
{
371-
return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
372-
}
373-
374369
static struct ref_store *reftable_be_init(struct repository *repo,
375370
const char *payload,
376371
const char *gitdir,
@@ -408,7 +403,6 @@ static struct ref_store *reftable_be_init(struct repository *repo,
408403
refs->write_options.disable_auto_compact =
409404
!git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1);
410405
refs->write_options.lock_timeout_ms = 100;
411-
refs->write_options.fsync = reftable_be_fsync;
412406

413407
repo_config(the_repository, reftable_be_config, &refs->write_options);
414408

reftable/reftable-system.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
#include "compat/posix.h"
1313
#include "compat/zlib-compat.h"
1414

15+
int reftable_fsync(int fd);
16+
#define fsync(fd) reftable_fsync(fd)
17+
1518
#endif

reftable/reftable-writer.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ struct reftable_write_options {
6161
*/
6262
long lock_timeout_ms;
6363

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-
7064
/*
7165
* Callback function to execute whenever the stack is being reloaded.
7266
* This can be used e.g. to discard cached information that relies on

reftable/stack.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ static int stack_filename(struct reftable_buf *dest, struct reftable_stack *st,
2929
return 0;
3030
}
3131

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-
3932
static ssize_t reftable_write_data(int fd, const void *data, size_t size)
4033
{
4134
size_t total_written = 0;
@@ -69,7 +62,7 @@ static ssize_t fd_writer_write(void *arg, const void *data, size_t sz)
6962
static int fd_writer_flush(void *arg)
7063
{
7164
struct fd_writer *writer = arg;
72-
return stack_fsync(writer->opts, writer->fd);
65+
return fsync(writer->fd);
7366
}
7467

7568
static int fd_read_lines(int fd, char ***namesp)
@@ -812,7 +805,7 @@ int reftable_addition_commit(struct reftable_addition *add)
812805
goto done;
813806
}
814807

815-
err = stack_fsync(&add->stack->opts, add->tables_list_lock.fd);
808+
err = fsync(add->tables_list_lock.fd);
816809
if (err < 0) {
817810
err = REFTABLE_IO_ERROR;
818811
goto done;
@@ -1480,7 +1473,7 @@ static int stack_compact_range(struct reftable_stack *st,
14801473
goto done;
14811474
}
14821475

1483-
err = stack_fsync(&st->opts, tables_list_lock.fd);
1476+
err = fsync(tables_list_lock.fd);
14841477
if (err < 0) {
14851478
err = REFTABLE_IO_ERROR;
14861479
unlink(new_table_path.buf);

reftable/system.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "reftable-error.h"
66
#include "../lockfile.h"
77
#include "../tempfile.h"
8+
#include "../write-or-die.h"
89

910
uint32_t reftable_rand(void)
1011
{
@@ -131,3 +132,8 @@ int flock_commit(struct reftable_flock *l)
131132

132133
return 0;
133134
}
135+
136+
int reftable_fsync(int fd)
137+
{
138+
return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
139+
}

0 commit comments

Comments
 (0)