reftable/stack: reorder code to avoid forward declarations
We have a couple of forward declarations in the stack-related code of the reftable library. These declarations aren't really required, but are simply caused by unfortunate ordering. Reorder the code and remove the forward declarations. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Aug 12, 2025 at 11:54 UTC
5ed5f5dc01636ac8590a499bb1d63b26789c73aa
1 file changed
+176
-188
reftable/stack.c
+176
-188
@@ -17,18 +17,6 @@
17
#include "table.h"
18
#include "writer.h"
19
20
-static int stack_try_add(struct reftable_stack *st,
21
- int (*write_table)(struct reftable_writer *wr,
22
- void *arg),
23
- void *arg);
24
-static int stack_write_compact(struct reftable_stack *st,
25
- struct reftable_writer *wr,
26
- size_t first, size_t last,
27
- struct reftable_log_expiry_config *config);
28
-static void reftable_addition_close(struct reftable_addition *add);
29
-static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
30
- int reuse_open);
31
-
20
static int stack_filename(struct reftable_buf *dest, struct reftable_stack *st,
21
const char *name)
22
{
@@ -84,54 +72,6 @@ static int fd_writer_flush(void *arg)
72
return stack_fsync(writer->opts, writer->fd);
73
}
74
87
-int reftable_new_stack(struct reftable_stack **dest, const char *dir,
88
- const struct reftable_write_options *_opts)
89
-{
90
- struct reftable_buf list_file_name = REFTABLE_BUF_INIT;
91
- struct reftable_write_options opts = { 0 };
92
- struct reftable_stack *p;
93
- int err;
94
-
95
- p = reftable_calloc(1, sizeof(*p));
96
- if (!p) {
97
- err = REFTABLE_OUT_OF_MEMORY_ERROR;
98
- goto out;
99
- }
100
-
101
- if (_opts)
102
- opts = *_opts;
103
- if (opts.hash_id == 0)
104
- opts.hash_id = REFTABLE_HASH_SHA1;
105
-
106
- *dest = NULL;
107
-
108
- reftable_buf_reset(&list_file_name);
109
- if ((err = reftable_buf_addstr(&list_file_name, dir)) < 0 ||
110
- (err = reftable_buf_addstr(&list_file_name, "/tables.list")) < 0)
111
- goto out;
112
-
113
- p->list_file = reftable_buf_detach(&list_file_name);
114
- p->list_fd = -1;
115
- p->opts = opts;
116
- p->reftable_dir = reftable_strdup(dir);
117
- if (!p->reftable_dir) {
118
- err = REFTABLE_OUT_OF_MEMORY_ERROR;
119
- goto out;
120
- }
121
-
122
- err = reftable_stack_reload_maybe_reuse(p, 1);
123
- if (err < 0)
124
- goto out;
125
-
126
- *dest = p;
127
- err = 0;
128
-
129
-out:
130
- if (err < 0)
131
- reftable_stack_destroy(p);
132
- return err;
133
-}
134
-
75
static int fd_read_lines(int fd, char ***namesp)
76
{
77
char *buf = NULL;
@@ -591,6 +531,54 @@ out:
531
return err;
532
}
533
534
+int reftable_new_stack(struct reftable_stack **dest, const char *dir,
535
+ const struct reftable_write_options *_opts)
536
+{
537
+ struct reftable_buf list_file_name = REFTABLE_BUF_INIT;
538
+ struct reftable_write_options opts = { 0 };
539
+ struct reftable_stack *p;
540
+ int err;
541
+
542
+ p = reftable_calloc(1, sizeof(*p));
543
+ if (!p) {
544
+ err = REFTABLE_OUT_OF_MEMORY_ERROR;
545
+ goto out;
546
+ }
547
+
548
+ if (_opts)
549
+ opts = *_opts;
550
+ if (opts.hash_id == 0)
551
+ opts.hash_id = REFTABLE_HASH_SHA1;
552
+
553
+ *dest = NULL;
554
+
555
+ reftable_buf_reset(&list_file_name);
556
+ if ((err = reftable_buf_addstr(&list_file_name, dir)) < 0 ||
557
+ (err = reftable_buf_addstr(&list_file_name, "/tables.list")) < 0)
558
+ goto out;
559
+
560
+ p->list_file = reftable_buf_detach(&list_file_name);
561
+ p->list_fd = -1;
562
+ p->opts = opts;
563
+ p->reftable_dir = reftable_strdup(dir);
564
+ if (!p->reftable_dir) {
565
+ err = REFTABLE_OUT_OF_MEMORY_ERROR;
566
+ goto out;
567
+ }
568
+
569
+ err = reftable_stack_reload_maybe_reuse(p, 1);
570
+ if (err < 0)
571
+ goto out;
572
+
573
+ *dest = p;
574
+ err = 0;
575
+
576
+out:
577
+ if (err < 0)
578
+ reftable_stack_destroy(p);
579
+ return err;
580
+}
581
+
582
/* -1 = error
583
0 = up to date
584
1 = changed. */
@@ -667,34 +655,6 @@ int reftable_stack_reload(struct reftable_stack *st)
655
return err;
656
}
657
670
-int reftable_stack_add(struct reftable_stack *st,
671
- int (*write)(struct reftable_writer *wr, void *arg),
672
- void *arg)
673
-{
674
- int err = stack_try_add(st, write, arg);
675
- if (err < 0) {
676
- if (err == REFTABLE_OUTDATED_ERROR) {
677
- /* Ignore error return, we want to propagate
678
- REFTABLE_OUTDATED_ERROR.
679
- */
680
- reftable_stack_reload(st);
681
- }
682
- return err;
683
- }
684
-
685
- return 0;
686
-}
687
-
688
-static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
689
-{
690
- char buf[100];
691
- uint32_t rnd = reftable_rand();
692
- snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
693
- min, max, rnd);
694
- reftable_buf_reset(dest);
695
- return reftable_buf_addstr(dest, buf);
696
-}
697
-
658
struct reftable_addition {
659
struct reftable_flock tables_list_lock;
660
struct reftable_stack *stack;
@@ -706,6 +666,26 @@ struct reftable_addition {
666
667
#define REFTABLE_ADDITION_INIT {0}
668
669
+static void reftable_addition_close(struct reftable_addition *add)
670
+{
671
+ struct reftable_buf nm = REFTABLE_BUF_INIT;
672
+ size_t i;
673
+
674
+ for (i = 0; i < add->new_tables_len; i++) {
675
+ if (!stack_filename(&nm, add->stack, add->new_tables[i]))
676
+ unlink(nm.buf);
677
+ reftable_free(add->new_tables[i]);
678
+ add->new_tables[i] = NULL;
679
+ }
680
+ reftable_free(add->new_tables);
681
+ add->new_tables = NULL;
682
+ add->new_tables_len = 0;
683
+ add->new_tables_cap = 0;
684
+
685
+ flock_release(&add->tables_list_lock);
686
+ reftable_buf_release(&nm);
687
+}
688
+
689
static int reftable_stack_init_addition(struct reftable_addition *add,
690
struct reftable_stack *st,
691
unsigned int flags)
@@ -754,24 +734,52 @@ done:
734
return err;
735
}
736
757
-static void reftable_addition_close(struct reftable_addition *add)
737
+static int stack_try_add(struct reftable_stack *st,
738
+ int (*write_table)(struct reftable_writer *wr,
739
+ void *arg),
740
+ void *arg)
741
{
759
- struct reftable_buf nm = REFTABLE_BUF_INIT;
760
- size_t i;
742
+ struct reftable_addition add = REFTABLE_ADDITION_INIT;
743
+ int err = reftable_stack_init_addition(&add, st, 0);
744
+ if (err < 0)
745
+ goto done;
746
762
- for (i = 0; i < add->new_tables_len; i++) {
763
- if (!stack_filename(&nm, add->stack, add->new_tables[i]))
764
- unlink(nm.buf);
765
- reftable_free(add->new_tables[i]);
766
- add->new_tables[i] = NULL;
747
+ err = reftable_addition_add(&add, write_table, arg);
748
+ if (err < 0)
749
+ goto done;
750
+
751
+ err = reftable_addition_commit(&add);
752
+done:
753
+ reftable_addition_close(&add);
754
+ return err;
755
+}
756
+
757
+int reftable_stack_add(struct reftable_stack *st,
758
+ int (*write)(struct reftable_writer *wr, void *arg),
759
+ void *arg)
760
+{
761
+ int err = stack_try_add(st, write, arg);
762
+ if (err < 0) {
763
+ if (err == REFTABLE_OUTDATED_ERROR) {
764
+ /* Ignore error return, we want to propagate
765
+ REFTABLE_OUTDATED_ERROR.
766
+ */
767
+ reftable_stack_reload(st);
768
+ }
769
+ return err;
770
}
768
- reftable_free(add->new_tables);
769
- add->new_tables = NULL;
770
- add->new_tables_len = 0;
771
- add->new_tables_cap = 0;
771
773
- flock_release(&add->tables_list_lock);
774
- reftable_buf_release(&nm);
772
+ return 0;
773
+}
774
+
775
+static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max)
776
+{
777
+ char buf[100];
778
+ uint32_t rnd = reftable_rand();
779
+ snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x",
780
+ min, max, rnd);
781
+ reftable_buf_reset(dest);
782
+ return reftable_buf_addstr(dest, buf);
783
}
784
785
void reftable_addition_destroy(struct reftable_addition *add)
@@ -874,26 +882,6 @@ int reftable_stack_new_addition(struct reftable_addition **dest,
882
return err;
883
}
884
877
-static int stack_try_add(struct reftable_stack *st,
878
- int (*write_table)(struct reftable_writer *wr,
879
- void *arg),
880
- void *arg)
881
-{
882
- struct reftable_addition add = REFTABLE_ADDITION_INIT;
883
- int err = reftable_stack_init_addition(&add, st, 0);
884
- if (err < 0)
885
- goto done;
886
-
887
- err = reftable_addition_add(&add, write_table, arg);
888
- if (err < 0)
889
- goto done;
890
-
891
- err = reftable_addition_commit(&add);
892
-done:
893
- reftable_addition_close(&add);
894
- return err;
895
-}
896
-
885
int reftable_addition_add(struct reftable_addition *add,
886
int (*write_table)(struct reftable_writer *wr,
887
void *arg),
@@ -1007,72 +995,6 @@ uint64_t reftable_stack_next_update_index(struct reftable_stack *st)
995
return 1;
996
}
997
1010
-static int stack_compact_locked(struct reftable_stack *st,
1011
- size_t first, size_t last,
1012
- struct reftable_log_expiry_config *config,
1013
- struct reftable_tmpfile *tab_file_out)
1014
-{
1015
- struct reftable_buf next_name = REFTABLE_BUF_INIT;
1016
- struct reftable_buf tab_file_path = REFTABLE_BUF_INIT;
1017
- struct reftable_writer *wr = NULL;
1018
- struct fd_writer writer= {
1019
- .opts = &st->opts,
1020
- };
1021
- struct reftable_tmpfile tab_file = REFTABLE_TMPFILE_INIT;
1022
- int err = 0;
1023
-
1024
- err = format_name(&next_name, reftable_table_min_update_index(st->tables[first]),
1025
- reftable_table_max_update_index(st->tables[last]));
1026
- if (err < 0)
1027
- goto done;
1028
-
1029
- err = stack_filename(&tab_file_path, st, next_name.buf);
1030
- if (err < 0)
1031
- goto done;
1032
-
1033
- err = reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX");
1034
- if (err < 0)
1035
- goto done;
1036
-
1037
- err = tmpfile_from_pattern(&tab_file, tab_file_path.buf);
1038
- if (err < 0)
1039
- goto done;
1040
-
1041
- if (st->opts.default_permissions &&
1042
- chmod(tab_file.path, st->opts.default_permissions) < 0) {
1043
- err = REFTABLE_IO_ERROR;
1044
- goto done;
1045
- }
1046
-
1047
- writer.fd = tab_file.fd;
1048
- err = reftable_writer_new(&wr, fd_writer_write, fd_writer_flush,
1049
- &writer, &st->opts);
1050
- if (err < 0)
1051
- goto done;
1052
-
1053
- err = stack_write_compact(st, wr, first, last, config);
1054
- if (err < 0)
1055
- goto done;
1056
-
1057
- err = reftable_writer_close(wr);
1058
- if (err < 0)
1059
- goto done;
1060
-
1061
- err = tmpfile_close(&tab_file);
1062
- if (err < 0)
1063
- goto done;
1064
-
1065
- *tab_file_out = tab_file;
1066
- tab_file = REFTABLE_TMPFILE_INIT;
1067
-
1068
-done:
1069
- tmpfile_delete(&tab_file);
1070
- reftable_writer_free(wr);
1071
- reftable_buf_release(&next_name);
1072
- reftable_buf_release(&tab_file_path);
1073
- return err;
1074
-}
1075
-
998
static int stack_write_compact(struct reftable_stack *st,
999
struct reftable_writer *wr,
1000
size_t first, size_t last,
@@ -1172,6 +1094,72 @@ done:
1094
return err;
1095
}
1096
1097
+static int stack_compact_locked(struct reftable_stack *st,
1098
+ size_t first, size_t last,
1099
+ struct reftable_log_expiry_config *config,
1100
+ struct reftable_tmpfile *tab_file_out)
1101
+{
1102
+ struct reftable_buf next_name = REFTABLE_BUF_INIT;
1103
+ struct reftable_buf tab_file_path = REFTABLE_BUF_INIT;
1104
+ struct reftable_writer *wr = NULL;
1105
+ struct fd_writer writer= {
1106
+ .opts = &st->opts,
1107
+ };
1108
+ struct reftable_tmpfile tab_file = REFTABLE_TMPFILE_INIT;
1109
+ int err = 0;
1110
+
1111
+ err = format_name(&next_name, reftable_table_min_update_index(st->tables[first]),
1112
+ reftable_table_max_update_index(st->tables[last]));
1113
+ if (err < 0)
1114
+ goto done;
1115
+
1116
+ err = stack_filename(&tab_file_path, st, next_name.buf);
1117
+ if (err < 0)
1118
+ goto done;
1119
+
1120
+ err = reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX");
1121
+ if (err < 0)
1122
+ goto done;
1123
+
1124
+ err = tmpfile_from_pattern(&tab_file, tab_file_path.buf);
1125
+ if (err < 0)
1126
+ goto done;
1127
+
1128
+ if (st->opts.default_permissions &&
1129
+ chmod(tab_file.path, st->opts.default_permissions) < 0) {
1130
+ err = REFTABLE_IO_ERROR;
1131
+ goto done;
1132
+ }
1133
+
1134
+ writer.fd = tab_file.fd;
1135
+ err = reftable_writer_new(&wr, fd_writer_write, fd_writer_flush,
1136
+ &writer, &st->opts);
1137
+ if (err < 0)
1138
+ goto done;
1139
+
1140
+ err = stack_write_compact(st, wr, first, last, config);
1141
+ if (err < 0)
1142
+ goto done;
1143
+
1144
+ err = reftable_writer_close(wr);
1145
+ if (err < 0)
1146
+ goto done;
1147
+
1148
+ err = tmpfile_close(&tab_file);
1149
+ if (err < 0)
1150
+ goto done;
1151
+
1152
+ *tab_file_out = tab_file;
1153
+ tab_file = REFTABLE_TMPFILE_INIT;
1154
+
1155
+done:
1156
+ tmpfile_delete(&tab_file);
1157
+ reftable_writer_free(wr);
1158
+ reftable_buf_release(&next_name);
1159
+ reftable_buf_release(&tab_file_path);
1160
+ return err;
1161
+}
1162
+
1163
enum stack_compact_range_flags {
1164
/*
1165
* Perform a best-effort compaction. That is, even if we cannot lock