Skip to content

Commit 128b9aa

Browse files
pks-tgitster
authored andcommitted
reftable/stack: use lock_file when adding table to "tables.list"
When modifying "tables.list", we need to lock the list before updating it to ensure that no concurrent writers modify the list at the same point in time. While we do this via the `lock_file` subsystem when compacting the stack, we manually handle the lock when adding a new table to it. While not wrong, it is at least inconsistent. Refactor the code to consistently lock "tables.list" via the `lock_file` subsytem. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7ee307d commit 128b9aa

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

reftable/stack.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ static void format_name(struct strbuf *dest, uint64_t min, uint64_t max)
567567
}
568568

569569
struct reftable_addition {
570-
struct tempfile *lock_file;
570+
struct lock_file tables_list_lock;
571571
struct reftable_stack *stack;
572572

573573
char **new_tables;
@@ -581,13 +581,13 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
581581
struct reftable_stack *st)
582582
{
583583
struct strbuf lock_file_name = STRBUF_INIT;
584-
int err = 0;
585-
add->stack = st;
584+
int err;
586585

587-
strbuf_addf(&lock_file_name, "%s.lock", st->list_file);
586+
add->stack = st;
588587

589-
add->lock_file = create_tempfile(lock_file_name.buf);
590-
if (!add->lock_file) {
588+
err = hold_lock_file_for_update(&add->tables_list_lock, st->list_file,
589+
LOCK_NO_DEREF);
590+
if (err < 0) {
591591
if (errno == EEXIST) {
592592
err = REFTABLE_LOCK_ERROR;
593593
} else {
@@ -596,7 +596,8 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
596596
goto done;
597597
}
598598
if (st->opts.default_permissions) {
599-
if (chmod(add->lock_file->filename.buf, st->opts.default_permissions) < 0) {
599+
if (chmod(get_lock_file_path(&add->tables_list_lock),
600+
st->opts.default_permissions) < 0) {
600601
err = REFTABLE_IO_ERROR;
601602
goto done;
602603
}
@@ -635,7 +636,7 @@ static void reftable_addition_close(struct reftable_addition *add)
635636
add->new_tables_len = 0;
636637
add->new_tables_cap = 0;
637638

638-
delete_tempfile(&add->lock_file);
639+
rollback_lock_file(&add->tables_list_lock);
639640
strbuf_release(&nm);
640641
}
641642

@@ -651,7 +652,7 @@ void reftable_addition_destroy(struct reftable_addition *add)
651652
int reftable_addition_commit(struct reftable_addition *add)
652653
{
653654
struct strbuf table_list = STRBUF_INIT;
654-
int lock_file_fd = get_tempfile_fd(add->lock_file);
655+
int lock_file_fd = get_lock_file_fd(&add->tables_list_lock);
655656
int err = 0;
656657
size_t i;
657658

@@ -680,7 +681,7 @@ int reftable_addition_commit(struct reftable_addition *add)
680681
goto done;
681682
}
682683

683-
err = rename_tempfile(&add->lock_file, add->stack->list_file);
684+
err = commit_lock_file(&add->tables_list_lock);
684685
if (err < 0) {
685686
err = REFTABLE_IO_ERROR;
686687
goto done;

0 commit comments

Comments
 (0)