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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Mar 7, 2024 at 14:10 UTC
1920d17a99e83f48c0bad5200df9f6f0d8785518
1 file changed
+12
-17
reftable/stack.c
+12
-17
@@ -737,8 +737,9 @@ int reftable_addition_add(struct reftable_addition *add,
737
struct strbuf tab_file_name = STRBUF_INIT;
738
struct strbuf next_name = STRBUF_INIT;
739
struct reftable_writer *wr = NULL;
740
+ struct tempfile *tab_file = NULL;
741
int err = 0;
741
- int tab_fd = 0;
742
+ int tab_fd;
743
744
strbuf_reset(&next_name);
745
format_name(&next_name, add->next_update_index, add->next_update_index);
@@ -746,17 +747,20 @@ int reftable_addition_add(struct reftable_addition *add,
747
stack_filename(&temp_tab_file_name, add->stack, next_name.buf);
748
strbuf_addstr(&temp_tab_file_name, ".temp.XXXXXX");
749
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) {
752
err = REFTABLE_IO_ERROR;
753
goto done;
754
}
755
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)) {
758
err = REFTABLE_IO_ERROR;
759
goto done;
760
}
761
}
762
+ tab_fd = get_tempfile_fd(tab_file);
763
+
764
wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, &tab_fd,
765
&add->stack->config);
766
err = write_table(wr, arg);
@@ -771,14 +775,13 @@ int reftable_addition_add(struct reftable_addition *add,
775
if (err < 0)
776
goto done;
777
774
- err = close(tab_fd);
775
- tab_fd = 0;
778
+ err = close_tempfile_gently(tab_file);
779
if (err < 0) {
780
err = REFTABLE_IO_ERROR;
781
goto done;
782
}
783
781
- err = stack_check_addition(add->stack, temp_tab_file_name.buf);
784
+ err = stack_check_addition(add->stack, get_tempfile_path(tab_file));
785
if (err < 0)
786
goto done;
787
@@ -789,14 +792,13 @@ int reftable_addition_add(struct reftable_addition *add,
792
793
format_name(&next_name, wr->min_update_index, wr->max_update_index);
794
strbuf_addstr(&next_name, ".ref");
792
-
795
stack_filename(&tab_file_name, add->stack, next_name.buf);
796
797
/*
798
On windows, this relies on rand() picking a unique destination name.
799
Maybe we should do retry loop as well?
800
*/
799
- err = rename(temp_tab_file_name.buf, tab_file_name.buf);
801
+ err = rename_tempfile(&tab_file, tab_file_name.buf);
802
if (err < 0) {
803
err = REFTABLE_IO_ERROR;
804
goto done;
@@ -806,14 +808,7 @@ int reftable_addition_add(struct reftable_addition *add,
808
add->new_tables_cap);
809
add->new_tables[add->new_tables_len++] = strbuf_detach(&next_name, NULL);
810
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);
812
strbuf_release(&temp_tab_file_name);
813
strbuf_release(&tab_file_name);
814
strbuf_release(&next_name);