Skip to content

Commit 86b770b

Browse files
pks-tgitster
authored andcommitted
reftable/stack: stop using fsync_component() directly
We're executing `fsync_component()` directly in the reftable library so that we can fsync data to disk depending on "core.fsync". But as we're in the process of converting the reftable library to become standalone we cannot use that function in the library anymore. Refactor the code such that users of the library can inject a custom fsync function via the write options. This allows us to get rid of the dependency on "write-or-die.h". Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2f0823 commit 86b770b

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

refs/reftable-backend.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "../setup.h"
2525
#include "../strmap.h"
2626
#include "../trace2.h"
27+
#include "../write-or-die.h"
2728
#include "parse.h"
2829
#include "refs-internal.h"
2930

@@ -273,6 +274,11 @@ static int reftable_be_config(const char *var, const char *value,
273274
return 0;
274275
}
275276

277+
static int reftable_be_fsync(int fd)
278+
{
279+
return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
280+
}
281+
276282
static struct ref_store *reftable_be_init(struct repository *repo,
277283
const char *gitdir,
278284
unsigned int store_flags)
@@ -304,6 +310,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
304310
refs->write_options.disable_auto_compact =
305311
!git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1);
306312
refs->write_options.lock_timeout_ms = 100;
313+
refs->write_options.fsync = reftable_be_fsync;
307314

308315
git_config(reftable_be_config, &refs->write_options);
309316

reftable/reftable-writer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ struct reftable_write_options {
6262
* negative value will cause us to block indefinitely.
6363
*/
6464
long lock_timeout_ms;
65+
66+
/*
67+
* Optional callback used to fsync files to disk. Falls back to using
68+
* fsync(3P) when unset.
69+
*/
70+
int (*fsync)(int fd);
6571
};
6672

6773
/* reftable_block_stats holds statistics for a single block type */

reftable/stack.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ license that can be found in the LICENSE file or at
88

99
#include "stack.h"
1010

11-
#include "../write-or-die.h"
1211
#include "system.h"
1312
#include "constants.h"
1413
#include "merged.h"
@@ -43,17 +42,28 @@ static int stack_filename(struct reftable_buf *dest, struct reftable_stack *st,
4342
return 0;
4443
}
4544

46-
static ssize_t reftable_fd_write(void *arg, const void *data, size_t sz)
45+
static int stack_fsync(const struct reftable_write_options *opts, int fd)
4746
{
48-
int *fdp = (int *)arg;
49-
return write_in_full(*fdp, data, sz);
47+
if (opts->fsync)
48+
return opts->fsync(fd);
49+
return fsync(fd);
5050
}
5151

52-
static int reftable_fd_flush(void *arg)
52+
struct fd_writer {
53+
const struct reftable_write_options *opts;
54+
int fd;
55+
};
56+
57+
static ssize_t fd_writer_write(void *arg, const void *data, size_t sz)
5358
{
54-
int *fdp = (int *)arg;
59+
struct fd_writer *writer = arg;
60+
return write_in_full(writer->fd, data, sz);
61+
}
5562

56-
return fsync_component(FSYNC_COMPONENT_REFERENCE, *fdp);
63+
static int fd_writer_flush(void *arg)
64+
{
65+
struct fd_writer *writer = arg;
66+
return stack_fsync(writer->opts, writer->fd);
5767
}
5868

5969
int reftable_new_stack(struct reftable_stack **dest, const char *dir,
@@ -765,7 +775,7 @@ int reftable_addition_commit(struct reftable_addition *add)
765775
goto done;
766776
}
767777

768-
err = fsync_component(FSYNC_COMPONENT_REFERENCE, lock_file_fd);
778+
err = stack_fsync(&add->stack->opts, lock_file_fd);
769779
if (err < 0) {
770780
err = REFTABLE_IO_ERROR;
771781
goto done;
@@ -858,8 +868,10 @@ int reftable_addition_add(struct reftable_addition *add,
858868
struct reftable_buf next_name = REFTABLE_BUF_INIT;
859869
struct reftable_writer *wr = NULL;
860870
struct tempfile *tab_file = NULL;
871+
struct fd_writer writer = {
872+
.opts = &add->stack->opts,
873+
};
861874
int err = 0;
862-
int tab_fd;
863875

864876
reftable_buf_reset(&next_name);
865877

@@ -887,10 +899,10 @@ int reftable_addition_add(struct reftable_addition *add,
887899
goto done;
888900
}
889901
}
890-
tab_fd = get_tempfile_fd(tab_file);
891902

892-
err = reftable_writer_new(&wr, reftable_fd_write, reftable_fd_flush,
893-
&tab_fd, &add->stack->opts);
903+
writer.fd = get_tempfile_fd(tab_file);
904+
err = reftable_writer_new(&wr, fd_writer_write, fd_writer_flush,
905+
&writer, &add->stack->opts);
894906
if (err < 0)
895907
goto done;
896908

@@ -973,8 +985,11 @@ static int stack_compact_locked(struct reftable_stack *st,
973985
struct reftable_buf next_name = REFTABLE_BUF_INIT;
974986
struct reftable_buf tab_file_path = REFTABLE_BUF_INIT;
975987
struct reftable_writer *wr = NULL;
988+
struct fd_writer writer= {
989+
.opts = &st->opts,
990+
};
976991
struct tempfile *tab_file;
977-
int tab_fd, err = 0;
992+
int err = 0;
978993

979994
err = format_name(&next_name, reftable_reader_min_update_index(st->readers[first]),
980995
reftable_reader_max_update_index(st->readers[last]));
@@ -994,16 +1009,16 @@ static int stack_compact_locked(struct reftable_stack *st,
9941009
err = REFTABLE_IO_ERROR;
9951010
goto done;
9961011
}
997-
tab_fd = get_tempfile_fd(tab_file);
9981012

9991013
if (st->opts.default_permissions &&
10001014
chmod(get_tempfile_path(tab_file), st->opts.default_permissions) < 0) {
10011015
err = REFTABLE_IO_ERROR;
10021016
goto done;
10031017
}
10041018

1005-
err = reftable_writer_new(&wr, reftable_fd_write, reftable_fd_flush,
1006-
&tab_fd, &st->opts);
1019+
writer.fd = get_tempfile_fd(tab_file);
1020+
err = reftable_writer_new(&wr, fd_writer_write, fd_writer_flush,
1021+
&writer, &st->opts);
10071022
if (err < 0)
10081023
goto done;
10091024

@@ -1460,7 +1475,7 @@ static int stack_compact_range(struct reftable_stack *st,
14601475
goto done;
14611476
}
14621477

1463-
err = fsync_component(FSYNC_COMPONENT_REFERENCE, get_lock_file_fd(&tables_list_lock));
1478+
err = stack_fsync(&st->opts, get_lock_file_fd(&tables_list_lock));
14641479
if (err < 0) {
14651480
err = REFTABLE_IO_ERROR;
14661481
unlink(new_table_path.buf);

0 commit comments

Comments
 (0)