Skip to content

Commit 7992378

Browse files
pks-tgitster
authored andcommitted
reftable: pass opts as constant pointer
We sometimes pass the refatble write options as value and sometimes as a pointer. This is quite confusing and makes the reader wonder whether the options get modified sometimes. In fact, `reftable_new_writer()` does cause the caller-provided options to get updated when some values aren't set up. This is quite unexpected, but didn't cause any harm until now. Adapt the code so that we do not modify the caller-provided values anymore. While at it, refactor the code to code to consistently pass the options as a constant pointer to clarify that the caller-provided opts will not ever get modified. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4d35bb2 commit 7992378

File tree

7 files changed

+46
-38
lines changed

7 files changed

+46
-38
lines changed

refs/reftable-backend.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static struct reftable_stack *stack_for(struct reftable_ref_store *store,
129129
store->base.repo->commondir, wtname_buf.buf);
130130

131131
store->err = reftable_new_stack(&stack, wt_dir.buf,
132-
store->write_options);
132+
&store->write_options);
133133
assert(store->err != REFTABLE_API_ERROR);
134134
strmap_put(&store->worktree_stacks, wtname_buf.buf, stack);
135135
}
@@ -263,7 +263,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
263263
}
264264
strbuf_addstr(&path, "/reftable");
265265
refs->err = reftable_new_stack(&refs->main_stack, path.buf,
266-
refs->write_options);
266+
&refs->write_options);
267267
if (refs->err)
268268
goto done;
269269

@@ -280,7 +280,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
280280
strbuf_addf(&path, "%s/reftable", gitdir);
281281

282282
refs->err = reftable_new_stack(&refs->worktree_stack, path.buf,
283-
refs->write_options);
283+
&refs->write_options);
284284
if (refs->err)
285285
goto done;
286286
}

reftable/dump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static int compact_stack(const char *stackdir)
2929
struct reftable_stack *stack = NULL;
3030
struct reftable_write_options opts = { 0 };
3131

32-
int err = reftable_new_stack(&stack, stackdir, opts);
32+
int err = reftable_new_stack(&stack, stackdir, &opts);
3333
if (err < 0)
3434
goto done;
3535

reftable/reftable-stack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct reftable_stack;
2929
* stored in 'dir'. Typically, this should be .git/reftables.
3030
*/
3131
int reftable_new_stack(struct reftable_stack **dest, const char *dir,
32-
struct reftable_write_options opts);
32+
const struct reftable_write_options *opts);
3333

3434
/* returns the update_index at which a next table should be written. */
3535
uint64_t reftable_stack_next_update_index(struct reftable_stack *st);

reftable/reftable-writer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct reftable_stats {
8888
struct reftable_writer *
8989
reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
9090
int (*flush_func)(void *),
91-
void *writer_arg, struct reftable_write_options *opts);
91+
void *writer_arg, const struct reftable_write_options *opts);
9292

9393
/* Set the range of update indices for the records we will add. When writing a
9494
table into a stack, the min should be at least

reftable/stack.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ static int reftable_fd_flush(void *arg)
5454
}
5555

5656
int reftable_new_stack(struct reftable_stack **dest, const char *dir,
57-
struct reftable_write_options opts)
57+
const struct reftable_write_options *_opts)
5858
{
5959
struct reftable_stack *p = reftable_calloc(1, sizeof(*p));
6060
struct strbuf list_file_name = STRBUF_INIT;
61+
struct reftable_write_options opts = {0};
6162
int err = 0;
6263

64+
if (_opts)
65+
opts = *_opts;
6366
if (opts.hash_id == 0)
6467
opts.hash_id = GIT_SHA1_FORMAT_ID;
6568

@@ -1438,7 +1441,7 @@ int reftable_stack_print_directory(const char *stackdir, uint32_t hash_id)
14381441
struct reftable_merged_table *merged = NULL;
14391442
struct reftable_table table = { NULL };
14401443

1441-
int err = reftable_new_stack(&stack, stackdir, opts);
1444+
int err = reftable_new_stack(&stack, stackdir, &opts);
14421445
if (err < 0)
14431446
goto done;
14441447

reftable/stack_test.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static void test_reftable_stack_add_one(void)
163163
};
164164
struct reftable_ref_record dest = { NULL };
165165
struct stat stat_result = { 0 };
166-
err = reftable_new_stack(&st, dir, opts);
166+
err = reftable_new_stack(&st, dir, &opts);
167167
EXPECT_ERR(err);
168168

169169
err = reftable_stack_add(st, &write_test_ref, &ref);
@@ -232,10 +232,10 @@ static void test_reftable_stack_uptodate(void)
232232
/* simulate multi-process access to the same stack
233233
by creating two stacks for the same directory.
234234
*/
235-
err = reftable_new_stack(&st1, dir, opts);
235+
err = reftable_new_stack(&st1, dir, &opts);
236236
EXPECT_ERR(err);
237237

238-
err = reftable_new_stack(&st2, dir, opts);
238+
err = reftable_new_stack(&st2, dir, &opts);
239239
EXPECT_ERR(err);
240240

241241
err = reftable_stack_add(st1, &write_test_ref, &ref1);
@@ -270,7 +270,7 @@ static void test_reftable_stack_transaction_api(void)
270270
};
271271
struct reftable_ref_record dest = { NULL };
272272

273-
err = reftable_new_stack(&st, dir, opts);
273+
err = reftable_new_stack(&st, dir, &opts);
274274
EXPECT_ERR(err);
275275

276276
reftable_addition_destroy(add);
@@ -304,7 +304,7 @@ static void test_reftable_stack_transaction_api_performs_auto_compaction(void)
304304
struct reftable_stack *st = NULL;
305305
int i, n = 20, err;
306306

307-
err = reftable_new_stack(&st, dir, opts);
307+
err = reftable_new_stack(&st, dir, &opts);
308308
EXPECT_ERR(err);
309309

310310
for (i = 0; i <= n; i++) {
@@ -365,7 +365,7 @@ static void test_reftable_stack_auto_compaction_fails_gracefully(void)
365365
char *dir = get_tmp_dir(__LINE__);
366366
int err;
367367

368-
err = reftable_new_stack(&st, dir, opts);
368+
err = reftable_new_stack(&st, dir, &opts);
369369
EXPECT_ERR(err);
370370

371371
err = reftable_stack_add(st, write_test_ref, &ref);
@@ -418,7 +418,7 @@ static void test_reftable_stack_update_index_check(void)
418418
.value.symref = "master",
419419
};
420420

421-
err = reftable_new_stack(&st, dir, opts);
421+
err = reftable_new_stack(&st, dir, &opts);
422422
EXPECT_ERR(err);
423423

424424
err = reftable_stack_add(st, &write_test_ref, &ref1);
@@ -437,7 +437,7 @@ static void test_reftable_stack_lock_failure(void)
437437
struct reftable_stack *st = NULL;
438438
int err, i;
439439

440-
err = reftable_new_stack(&st, dir, opts);
440+
err = reftable_new_stack(&st, dir, &opts);
441441
EXPECT_ERR(err);
442442
for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--) {
443443
err = reftable_stack_add(st, &write_error, &i);
@@ -465,7 +465,7 @@ static void test_reftable_stack_add(void)
465465
struct stat stat_result;
466466
int N = ARRAY_SIZE(refs);
467467

468-
err = reftable_new_stack(&st, dir, opts);
468+
err = reftable_new_stack(&st, dir, &opts);
469469
EXPECT_ERR(err);
470470

471471
for (i = 0; i < N; i++) {
@@ -575,7 +575,7 @@ static void test_reftable_stack_log_normalize(void)
575575
.update_index = 1,
576576
};
577577

578-
err = reftable_new_stack(&st, dir, opts);
578+
err = reftable_new_stack(&st, dir, &opts);
579579
EXPECT_ERR(err);
580580

581581
input.value.update.message = "one\ntwo";
@@ -617,7 +617,7 @@ static void test_reftable_stack_tombstone(void)
617617
struct reftable_ref_record dest = { NULL };
618618
struct reftable_log_record log_dest = { NULL };
619619

620-
err = reftable_new_stack(&st, dir, opts);
620+
err = reftable_new_stack(&st, dir, &opts);
621621
EXPECT_ERR(err);
622622

623623
/* even entries add the refs, odd entries delete them. */
@@ -701,18 +701,18 @@ static void test_reftable_stack_hash_id(void)
701701
struct reftable_stack *st_default = NULL;
702702
struct reftable_ref_record dest = { NULL };
703703

704-
err = reftable_new_stack(&st, dir, opts);
704+
err = reftable_new_stack(&st, dir, &opts);
705705
EXPECT_ERR(err);
706706

707707
err = reftable_stack_add(st, &write_test_ref, &ref);
708708
EXPECT_ERR(err);
709709

710710
/* can't read it with the wrong hash ID. */
711-
err = reftable_new_stack(&st32, dir, opts32);
711+
err = reftable_new_stack(&st32, dir, &opts32);
712712
EXPECT(err == REFTABLE_FORMAT_ERROR);
713713

714714
/* check that we can read it back with default opts too. */
715-
err = reftable_new_stack(&st_default, dir, opts_default);
715+
err = reftable_new_stack(&st_default, dir, &opts_default);
716716
EXPECT_ERR(err);
717717

718718
err = reftable_stack_read_ref(st_default, "master", &dest);
@@ -756,7 +756,7 @@ static void test_reflog_expire(void)
756756
};
757757
struct reftable_log_record log = { NULL };
758758

759-
err = reftable_new_stack(&st, dir, opts);
759+
err = reftable_new_stack(&st, dir, &opts);
760760
EXPECT_ERR(err);
761761

762762
for (i = 1; i <= N; i++) {
@@ -825,13 +825,13 @@ static void test_empty_add(void)
825825
char *dir = get_tmp_dir(__LINE__);
826826
struct reftable_stack *st2 = NULL;
827827

828-
err = reftable_new_stack(&st, dir, opts);
828+
err = reftable_new_stack(&st, dir, &opts);
829829
EXPECT_ERR(err);
830830

831831
err = reftable_stack_add(st, &write_nothing, NULL);
832832
EXPECT_ERR(err);
833833

834-
err = reftable_new_stack(&st2, dir, opts);
834+
err = reftable_new_stack(&st2, dir, &opts);
835835
EXPECT_ERR(err);
836836
clear_dir(dir);
837837
reftable_stack_destroy(st);
@@ -858,7 +858,7 @@ static void test_reftable_stack_auto_compaction(void)
858858
int err, i;
859859
int N = 100;
860860

861-
err = reftable_new_stack(&st, dir, opts);
861+
err = reftable_new_stack(&st, dir, &opts);
862862
EXPECT_ERR(err);
863863

864864
for (i = 0; i < N; i++) {
@@ -894,7 +894,7 @@ static void test_reftable_stack_add_performs_auto_compaction(void)
894894
char *dir = get_tmp_dir(__LINE__);
895895
int err, i, n = 20;
896896

897-
err = reftable_new_stack(&st, dir, opts);
897+
err = reftable_new_stack(&st, dir, &opts);
898898
EXPECT_ERR(err);
899899

900900
for (i = 0; i <= n; i++) {
@@ -942,7 +942,7 @@ static void test_reftable_stack_compaction_concurrent(void)
942942
int err, i;
943943
int N = 3;
944944

945-
err = reftable_new_stack(&st1, dir, opts);
945+
err = reftable_new_stack(&st1, dir, &opts);
946946
EXPECT_ERR(err);
947947

948948
for (i = 0; i < N; i++) {
@@ -959,7 +959,7 @@ static void test_reftable_stack_compaction_concurrent(void)
959959
EXPECT_ERR(err);
960960
}
961961

962-
err = reftable_new_stack(&st2, dir, opts);
962+
err = reftable_new_stack(&st2, dir, &opts);
963963
EXPECT_ERR(err);
964964

965965
err = reftable_stack_compact_all(st1, NULL);
@@ -991,7 +991,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
991991
int err, i;
992992
int N = 3;
993993

994-
err = reftable_new_stack(&st1, dir, opts);
994+
err = reftable_new_stack(&st1, dir, &opts);
995995
EXPECT_ERR(err);
996996

997997
for (i = 0; i < N; i++) {
@@ -1008,7 +1008,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
10081008
EXPECT_ERR(err);
10091009
}
10101010

1011-
err = reftable_new_stack(&st2, dir, opts);
1011+
err = reftable_new_stack(&st2, dir, &opts);
10121012
EXPECT_ERR(err);
10131013

10141014
err = reftable_stack_compact_all(st1, NULL);
@@ -1017,7 +1017,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
10171017
unclean_stack_close(st1);
10181018
unclean_stack_close(st2);
10191019

1020-
err = reftable_new_stack(&st3, dir, opts);
1020+
err = reftable_new_stack(&st3, dir, &opts);
10211021
EXPECT_ERR(err);
10221022

10231023
err = reftable_stack_clean(st3);

reftable/writer.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,25 @@ static struct strbuf reftable_empty_strbuf = STRBUF_INIT;
122122
struct reftable_writer *
123123
reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t),
124124
int (*flush_func)(void *),
125-
void *writer_arg, struct reftable_write_options *opts)
125+
void *writer_arg, const struct reftable_write_options *_opts)
126126
{
127127
struct reftable_writer *wp = reftable_calloc(1, sizeof(*wp));
128-
strbuf_init(&wp->block_writer_data.last_key, 0);
129-
options_set_defaults(opts);
130-
if (opts->block_size >= (1 << 24)) {
128+
struct reftable_write_options opts = {0};
129+
130+
if (_opts)
131+
opts = *_opts;
132+
options_set_defaults(&opts);
133+
if (opts.block_size >= (1 << 24)) {
131134
/* TODO - error return? */
132135
abort();
133136
}
137+
138+
strbuf_init(&wp->block_writer_data.last_key, 0);
134139
wp->last_key = reftable_empty_strbuf;
135-
REFTABLE_CALLOC_ARRAY(wp->block, opts->block_size);
140+
REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size);
136141
wp->write = writer_func;
137142
wp->write_arg = writer_arg;
138-
wp->opts = *opts;
143+
wp->opts = opts;
139144
wp->flush = flush_func;
140145
writer_reinit_block_writer(wp, BLOCK_TYPE_REF);
141146

0 commit comments

Comments
 (0)