Skip to content

Commit 60c4c42

Browse files
pks-tgitster
authored andcommitted
reftable/stack: register compacted tables as tempfiles
We do not register tables resulting from stack compaction with the tempfile API. Those tables will thus not be deleted in case Git gets killed. Refactor the code to register compacted tables as tempfiles. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3a60f6a commit 60c4c42

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

reftable/stack.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -827,51 +827,56 @@ uint64_t reftable_stack_next_update_index(struct reftable_stack *st)
827827

828828
static int stack_compact_locked(struct reftable_stack *st,
829829
size_t first, size_t last,
830-
struct strbuf *temp_tab,
831-
struct reftable_log_expiry_config *config)
830+
struct reftable_log_expiry_config *config,
831+
struct tempfile **tab_file_out)
832832
{
833833
struct strbuf next_name = STRBUF_INIT;
834-
int tab_fd = -1;
834+
struct strbuf tab_file_path = STRBUF_INIT;
835835
struct reftable_writer *wr = NULL;
836-
int err = 0;
836+
struct tempfile *tab_file;
837+
int tab_fd, err = 0;
837838

838839
format_name(&next_name,
839840
reftable_reader_min_update_index(st->readers[first]),
840841
reftable_reader_max_update_index(st->readers[last]));
842+
stack_filename(&tab_file_path, st, next_name.buf);
843+
strbuf_addstr(&tab_file_path, ".temp.XXXXXX");
841844

842-
stack_filename(temp_tab, st, next_name.buf);
843-
strbuf_addstr(temp_tab, ".temp.XXXXXX");
845+
tab_file = mks_tempfile(tab_file_path.buf);
846+
if (!tab_file) {
847+
err = REFTABLE_IO_ERROR;
848+
goto done;
849+
}
850+
tab_fd = get_tempfile_fd(tab_file);
844851

845-
tab_fd = mkstemp(temp_tab->buf);
846852
if (st->config.default_permissions &&
847-
chmod(temp_tab->buf, st->config.default_permissions) < 0) {
853+
chmod(get_tempfile_path(tab_file), st->config.default_permissions) < 0) {
848854
err = REFTABLE_IO_ERROR;
849855
goto done;
850856
}
851857

852-
wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, &tab_fd, &st->config);
853-
858+
wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush,
859+
&tab_fd, &st->config);
854860
err = stack_write_compact(st, wr, first, last, config);
855861
if (err < 0)
856862
goto done;
863+
857864
err = reftable_writer_close(wr);
858865
if (err < 0)
859866
goto done;
860867

861-
err = close(tab_fd);
862-
tab_fd = 0;
868+
err = close_tempfile_gently(tab_file);
869+
if (err < 0)
870+
goto done;
871+
872+
*tab_file_out = tab_file;
873+
tab_file = NULL;
863874

864875
done:
876+
delete_tempfile(&tab_file);
865877
reftable_writer_free(wr);
866-
if (tab_fd > 0) {
867-
close(tab_fd);
868-
tab_fd = 0;
869-
}
870-
if (err != 0 && temp_tab->len > 0) {
871-
unlink(temp_tab->buf);
872-
strbuf_release(temp_tab);
873-
}
874878
strbuf_release(&next_name);
879+
strbuf_release(&tab_file_path);
875880
return err;
876881
}
877882

@@ -979,12 +984,12 @@ static int stack_compact_range(struct reftable_stack *st,
979984
struct reftable_log_expiry_config *expiry)
980985
{
981986
struct strbuf tables_list_buf = STRBUF_INIT;
982-
struct strbuf new_table_temp_path = STRBUF_INIT;
983987
struct strbuf new_table_name = STRBUF_INIT;
984988
struct strbuf new_table_path = STRBUF_INIT;
985989
struct strbuf table_name = STRBUF_INIT;
986990
struct lock_file tables_list_lock = LOCK_INIT;
987991
struct lock_file *table_locks = NULL;
992+
struct tempfile *new_table = NULL;
988993
int is_empty_table = 0, err = 0;
989994
size_t i;
990995

@@ -1059,7 +1064,7 @@ static int stack_compact_range(struct reftable_stack *st,
10591064
* these tables may end up with an empty new table in case tombstones
10601065
* end up cancelling out all refs in that range.
10611066
*/
1062-
err = stack_compact_locked(st, first, last, &new_table_temp_path, expiry);
1067+
err = stack_compact_locked(st, first, last, expiry, &new_table);
10631068
if (err < 0) {
10641069
if (err != REFTABLE_EMPTY_TABLE_ERROR)
10651070
goto done;
@@ -1099,7 +1104,7 @@ static int stack_compact_range(struct reftable_stack *st,
10991104
strbuf_addstr(&new_table_name, ".ref");
11001105
stack_filename(&new_table_path, st, new_table_name.buf);
11011106

1102-
err = rename(new_table_temp_path.buf, new_table_path.buf);
1107+
err = rename_tempfile(&new_table, new_table_path.buf);
11031108
if (err < 0) {
11041109
err = REFTABLE_IO_ERROR;
11051110
goto done;
@@ -1166,9 +1171,10 @@ static int stack_compact_range(struct reftable_stack *st,
11661171
rollback_lock_file(&table_locks[i - first]);
11671172
reftable_free(table_locks);
11681173

1174+
delete_tempfile(&new_table);
11691175
strbuf_release(&new_table_name);
11701176
strbuf_release(&new_table_path);
1171-
strbuf_release(&new_table_temp_path);
1177+
11721178
strbuf_release(&tables_list_buf);
11731179
strbuf_release(&table_name);
11741180
return err;

0 commit comments

Comments
 (0)