Skip to content

Commit 1920d17

Browse files
pks-tgitster
authored andcommitted
reftable/stack: register new tables as tempfiles
We do not register new tables which we're about to add to the stack with the tempfile API. Those tables will thus not be deleted in case Git gets killed. Refactor the code to register tables as tempfiles. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4ae540d commit 1920d17

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

reftable/stack.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -737,26 +737,30 @@ int reftable_addition_add(struct reftable_addition *add,
737737
struct strbuf tab_file_name = STRBUF_INIT;
738738
struct strbuf next_name = STRBUF_INIT;
739739
struct reftable_writer *wr = NULL;
740+
struct tempfile *tab_file = NULL;
740741
int err = 0;
741-
int tab_fd = 0;
742+
int tab_fd;
742743

743744
strbuf_reset(&next_name);
744745
format_name(&next_name, add->next_update_index, add->next_update_index);
745746

746747
stack_filename(&temp_tab_file_name, add->stack, next_name.buf);
747748
strbuf_addstr(&temp_tab_file_name, ".temp.XXXXXX");
748749

749-
tab_fd = mkstemp(temp_tab_file_name.buf);
750-
if (tab_fd < 0) {
750+
tab_file = mks_tempfile(temp_tab_file_name.buf);
751+
if (!tab_file) {
751752
err = REFTABLE_IO_ERROR;
752753
goto done;
753754
}
754755
if (add->stack->config.default_permissions) {
755-
if (chmod(temp_tab_file_name.buf, add->stack->config.default_permissions)) {
756+
if (chmod(get_tempfile_path(tab_file),
757+
add->stack->config.default_permissions)) {
756758
err = REFTABLE_IO_ERROR;
757759
goto done;
758760
}
759761
}
762+
tab_fd = get_tempfile_fd(tab_file);
763+
760764
wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, &tab_fd,
761765
&add->stack->config);
762766
err = write_table(wr, arg);
@@ -771,14 +775,13 @@ int reftable_addition_add(struct reftable_addition *add,
771775
if (err < 0)
772776
goto done;
773777

774-
err = close(tab_fd);
775-
tab_fd = 0;
778+
err = close_tempfile_gently(tab_file);
776779
if (err < 0) {
777780
err = REFTABLE_IO_ERROR;
778781
goto done;
779782
}
780783

781-
err = stack_check_addition(add->stack, temp_tab_file_name.buf);
784+
err = stack_check_addition(add->stack, get_tempfile_path(tab_file));
782785
if (err < 0)
783786
goto done;
784787

@@ -789,14 +792,13 @@ int reftable_addition_add(struct reftable_addition *add,
789792

790793
format_name(&next_name, wr->min_update_index, wr->max_update_index);
791794
strbuf_addstr(&next_name, ".ref");
792-
793795
stack_filename(&tab_file_name, add->stack, next_name.buf);
794796

795797
/*
796798
On windows, this relies on rand() picking a unique destination name.
797799
Maybe we should do retry loop as well?
798800
*/
799-
err = rename(temp_tab_file_name.buf, tab_file_name.buf);
801+
err = rename_tempfile(&tab_file, tab_file_name.buf);
800802
if (err < 0) {
801803
err = REFTABLE_IO_ERROR;
802804
goto done;
@@ -806,14 +808,7 @@ int reftable_addition_add(struct reftable_addition *add,
806808
add->new_tables_cap);
807809
add->new_tables[add->new_tables_len++] = strbuf_detach(&next_name, NULL);
808810
done:
809-
if (tab_fd > 0) {
810-
close(tab_fd);
811-
tab_fd = 0;
812-
}
813-
if (temp_tab_file_name.len > 0) {
814-
unlink(temp_tab_file_name.buf);
815-
}
816-
811+
delete_tempfile(&tab_file);
817812
strbuf_release(&temp_tab_file_name);
818813
strbuf_release(&tab_file_name);
819814
strbuf_release(&next_name);

0 commit comments

Comments
 (0)