@@ -17,6 +17,8 @@ license that can be found in the LICENSE file or at
17
17
#include "reftable-merged.h"
18
18
#include "writer.h"
19
19
20
+ #include "tempfile.h"
21
+
20
22
static int stack_try_add (struct reftable_stack * st ,
21
23
int (* write_table )(struct reftable_writer * wr ,
22
24
void * arg ),
@@ -440,33 +442,27 @@ static void format_name(struct strbuf *dest, uint64_t min, uint64_t max)
440
442
}
441
443
442
444
struct reftable_addition {
443
- int lock_file_fd ;
444
- struct strbuf lock_file_name ;
445
+ struct tempfile * lock_file ;
445
446
struct reftable_stack * stack ;
446
447
447
448
char * * new_tables ;
448
449
int new_tables_len ;
449
450
uint64_t next_update_index ;
450
451
};
451
452
452
- #define REFTABLE_ADDITION_INIT \
453
- { \
454
- .lock_file_name = STRBUF_INIT \
455
- }
453
+ #define REFTABLE_ADDITION_INIT {0}
456
454
457
455
static int reftable_stack_init_addition (struct reftable_addition * add ,
458
456
struct reftable_stack * st )
459
457
{
458
+ struct strbuf lock_file_name = STRBUF_INIT ;
460
459
int err = 0 ;
461
460
add -> stack = st ;
462
461
463
- strbuf_reset (& add -> lock_file_name );
464
- strbuf_addstr (& add -> lock_file_name , st -> list_file );
465
- strbuf_addstr (& add -> lock_file_name , ".lock" );
462
+ strbuf_addf (& lock_file_name , "%s.lock" , st -> list_file );
466
463
467
- add -> lock_file_fd = open (add -> lock_file_name .buf ,
468
- O_EXCL | O_CREAT | O_WRONLY , 0666 );
469
- if (add -> lock_file_fd < 0 ) {
464
+ add -> lock_file = create_tempfile (lock_file_name .buf );
465
+ if (!add -> lock_file ) {
470
466
if (errno == EEXIST ) {
471
467
err = REFTABLE_LOCK_ERROR ;
472
468
} else {
@@ -475,7 +471,7 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
475
471
goto done ;
476
472
}
477
473
if (st -> config .default_permissions ) {
478
- if (chmod (add -> lock_file_name .buf , st -> config .default_permissions ) < 0 ) {
474
+ if (chmod (add -> lock_file -> filename .buf , st -> config .default_permissions ) < 0 ) {
479
475
err = REFTABLE_IO_ERROR ;
480
476
goto done ;
481
477
}
@@ -495,6 +491,7 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
495
491
if (err ) {
496
492
reftable_addition_close (add );
497
493
}
494
+ strbuf_release (& lock_file_name );
498
495
return err ;
499
496
}
500
497
@@ -512,15 +509,7 @@ static void reftable_addition_close(struct reftable_addition *add)
512
509
add -> new_tables = NULL ;
513
510
add -> new_tables_len = 0 ;
514
511
515
- if (add -> lock_file_fd > 0 ) {
516
- close (add -> lock_file_fd );
517
- add -> lock_file_fd = 0 ;
518
- }
519
- if (add -> lock_file_name .len > 0 ) {
520
- unlink (add -> lock_file_name .buf );
521
- strbuf_release (& add -> lock_file_name );
522
- }
523
-
512
+ delete_tempfile (& add -> lock_file );
524
513
strbuf_release (& nm );
525
514
}
526
515
@@ -536,8 +525,10 @@ void reftable_addition_destroy(struct reftable_addition *add)
536
525
int reftable_addition_commit (struct reftable_addition * add )
537
526
{
538
527
struct strbuf table_list = STRBUF_INIT ;
528
+ int lock_file_fd = get_tempfile_fd (add -> lock_file );
539
529
int i = 0 ;
540
530
int err = 0 ;
531
+
541
532
if (add -> new_tables_len == 0 )
542
533
goto done ;
543
534
@@ -550,28 +541,20 @@ int reftable_addition_commit(struct reftable_addition *add)
550
541
strbuf_addstr (& table_list , "\n" );
551
542
}
552
543
553
- err = write_in_full (add -> lock_file_fd , table_list .buf , table_list .len );
544
+ err = write_in_full (lock_file_fd , table_list .buf , table_list .len );
554
545
strbuf_release (& table_list );
555
546
if (err < 0 ) {
556
547
err = REFTABLE_IO_ERROR ;
557
548
goto done ;
558
549
}
559
550
560
- err = close (add -> lock_file_fd );
561
- add -> lock_file_fd = 0 ;
562
- if (err < 0 ) {
563
- err = REFTABLE_IO_ERROR ;
564
- goto done ;
565
- }
566
-
567
- err = rename (add -> lock_file_name .buf , add -> stack -> list_file );
551
+ err = rename_tempfile (& add -> lock_file , add -> stack -> list_file );
568
552
if (err < 0 ) {
569
553
err = REFTABLE_IO_ERROR ;
570
554
goto done ;
571
555
}
572
556
573
557
/* success, no more state to clean up. */
574
- strbuf_release (& add -> lock_file_name );
575
558
for (i = 0 ; i < add -> new_tables_len ; i ++ ) {
576
559
reftable_free (add -> new_tables [i ]);
577
560
}
0 commit comments