@@ -827,51 +827,56 @@ uint64_t reftable_stack_next_update_index(struct reftable_stack *st)
827
827
828
828
static int stack_compact_locked (struct reftable_stack * st ,
829
829
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 )
832
832
{
833
833
struct strbuf next_name = STRBUF_INIT ;
834
- int tab_fd = -1 ;
834
+ struct strbuf tab_file_path = STRBUF_INIT ;
835
835
struct reftable_writer * wr = NULL ;
836
- int err = 0 ;
836
+ struct tempfile * tab_file ;
837
+ int tab_fd , err = 0 ;
837
838
838
839
format_name (& next_name ,
839
840
reftable_reader_min_update_index (st -> readers [first ]),
840
841
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" );
841
844
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 );
844
851
845
- tab_fd = mkstemp (temp_tab -> buf );
846
852
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 ) {
848
854
err = REFTABLE_IO_ERROR ;
849
855
goto done ;
850
856
}
851
857
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 );
854
860
err = stack_write_compact (st , wr , first , last , config );
855
861
if (err < 0 )
856
862
goto done ;
863
+
857
864
err = reftable_writer_close (wr );
858
865
if (err < 0 )
859
866
goto done ;
860
867
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 ;
863
874
864
875
done :
876
+ delete_tempfile (& tab_file );
865
877
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
- }
874
878
strbuf_release (& next_name );
879
+ strbuf_release (& tab_file_path );
875
880
return err ;
876
881
}
877
882
@@ -979,12 +984,12 @@ static int stack_compact_range(struct reftable_stack *st,
979
984
struct reftable_log_expiry_config * expiry )
980
985
{
981
986
struct strbuf tables_list_buf = STRBUF_INIT ;
982
- struct strbuf new_table_temp_path = STRBUF_INIT ;
983
987
struct strbuf new_table_name = STRBUF_INIT ;
984
988
struct strbuf new_table_path = STRBUF_INIT ;
985
989
struct strbuf table_name = STRBUF_INIT ;
986
990
struct lock_file tables_list_lock = LOCK_INIT ;
987
991
struct lock_file * table_locks = NULL ;
992
+ struct tempfile * new_table = NULL ;
988
993
int is_empty_table = 0 , err = 0 ;
989
994
size_t i ;
990
995
@@ -1059,7 +1064,7 @@ static int stack_compact_range(struct reftable_stack *st,
1059
1064
* these tables may end up with an empty new table in case tombstones
1060
1065
* end up cancelling out all refs in that range.
1061
1066
*/
1062
- err = stack_compact_locked (st , first , last , & new_table_temp_path , expiry );
1067
+ err = stack_compact_locked (st , first , last , expiry , & new_table );
1063
1068
if (err < 0 ) {
1064
1069
if (err != REFTABLE_EMPTY_TABLE_ERROR )
1065
1070
goto done ;
@@ -1099,7 +1104,7 @@ static int stack_compact_range(struct reftable_stack *st,
1099
1104
strbuf_addstr (& new_table_name , ".ref" );
1100
1105
stack_filename (& new_table_path , st , new_table_name .buf );
1101
1106
1102
- err = rename ( new_table_temp_path . buf , new_table_path .buf );
1107
+ err = rename_tempfile ( & new_table , new_table_path .buf );
1103
1108
if (err < 0 ) {
1104
1109
err = REFTABLE_IO_ERROR ;
1105
1110
goto done ;
@@ -1166,9 +1171,10 @@ static int stack_compact_range(struct reftable_stack *st,
1166
1171
rollback_lock_file (& table_locks [i - first ]);
1167
1172
reftable_free (table_locks );
1168
1173
1174
+ delete_tempfile (& new_table );
1169
1175
strbuf_release (& new_table_name );
1170
1176
strbuf_release (& new_table_path );
1171
- strbuf_release ( & new_table_temp_path );
1177
+
1172
1178
strbuf_release (& tables_list_buf );
1173
1179
strbuf_release (& table_name );
1174
1180
return err ;
0 commit comments