Skip to content

Commit 7fa7e14

Browse files
pks-tttaylorr
authored andcommitted
reftable: stop using strbuf_addf()
We're about to introduce our own `reftable_buf` type to replace `strbuf`. One function we'll have to convert is `strbuf_addf()`, which is used in a handful of places. This function uses `snprintf()` internally, which makes porting it a bit more involved: - It is not available on all platforms. - Some platforms like Windows have broken implementations. So by using `snprintf()` we'd also push the burden on downstream users of the reftable library to make available a properly working version of it. Most callsites of `strbuf_addf()` are trivial to convert to not using it. We do end up using `snprintf()` in our unit tests, but that isn't much of a problem for downstream users of the reftable library. While at it, remove a useless call to `strbuf_reset()` in `t_reftable_stack_auto_compaction_with_locked_tables()`. We don't write to the buffer before this and initialize it with `STRBUF_INIT`, so there is no need to reset anything. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 409f049 commit 7fa7e14

File tree

4 files changed

+50
-37
lines changed

4 files changed

+50
-37
lines changed

reftable/stack.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,12 +1387,18 @@ static int stack_compact_range(struct reftable_stack *st,
13871387
* have just written. In case the compacted table became empty we
13881388
* simply skip writing it.
13891389
*/
1390-
for (i = 0; i < first_to_replace; i++)
1391-
strbuf_addf(&tables_list_buf, "%s\n", names[i]);
1392-
if (!is_empty_table)
1393-
strbuf_addf(&tables_list_buf, "%s\n", new_table_name.buf);
1394-
for (i = last_to_replace + 1; names[i]; i++)
1395-
strbuf_addf(&tables_list_buf, "%s\n", names[i]);
1390+
for (i = 0; i < first_to_replace; i++) {
1391+
strbuf_addstr(&tables_list_buf, names[i]);
1392+
strbuf_addstr(&tables_list_buf, "\n");
1393+
}
1394+
if (!is_empty_table) {
1395+
strbuf_addstr(&tables_list_buf, new_table_name.buf);
1396+
strbuf_addstr(&tables_list_buf, "\n");
1397+
}
1398+
for (i = last_to_replace + 1; names[i]; i++) {
1399+
strbuf_addstr(&tables_list_buf, names[i]);
1400+
strbuf_addstr(&tables_list_buf, "\n");
1401+
}
13961402

13971403
err = write_in_full(get_lock_file_fd(&tables_list_lock),
13981404
tables_list_buf.buf, tables_list_buf.len);

t/unit-tests/t-reftable-block.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,13 @@ static void t_index_block_read_write(void)
308308
check(!ret);
309309

310310
for (i = 0; i < N; i++) {
311-
strbuf_init(&recs[i].u.idx.last_key, 9);
311+
char buf[128];
312+
313+
snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
312314

315+
strbuf_init(&recs[i].u.idx.last_key, 9);
313316
recs[i].type = BLOCK_TYPE_INDEX;
314-
strbuf_addf(&recs[i].u.idx.last_key, "branch%02"PRIuMAX, (uintmax_t)i);
317+
strbuf_addstr(&recs[i].u.idx.last_key, buf);
315318
recs[i].u.idx.offset = i;
316319

317320
ret = block_writer_add(&bw, &recs[i]);

t/unit-tests/t-reftable-readwrite.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -753,12 +753,13 @@ static void t_write_multiple_indices(void)
753753
struct reftable_write_options opts = {
754754
.block_size = 100,
755755
};
756-
struct strbuf writer_buf = STRBUF_INIT, buf = STRBUF_INIT;
756+
struct strbuf writer_buf = STRBUF_INIT;
757757
struct reftable_block_source source = { 0 };
758758
struct reftable_iterator it = { 0 };
759759
const struct reftable_stats *stats;
760760
struct reftable_writer *writer;
761761
struct reftable_reader *reader;
762+
char buf[128];
762763
int err, i;
763764

764765
writer = t_reftable_strbuf_writer(&writer_buf, &opts);
@@ -770,9 +771,8 @@ static void t_write_multiple_indices(void)
770771
.value.val1 = {i},
771772
};
772773

773-
strbuf_reset(&buf);
774-
strbuf_addf(&buf, "refs/heads/%04d", i);
775-
ref.refname = buf.buf,
774+
snprintf(buf, sizeof(buf), "refs/heads/%04d", i);
775+
ref.refname = buf;
776776

777777
err = reftable_writer_add_ref(writer, &ref);
778778
check(!err);
@@ -788,9 +788,8 @@ static void t_write_multiple_indices(void)
788788
},
789789
};
790790

791-
strbuf_reset(&buf);
792-
strbuf_addf(&buf, "refs/heads/%04d", i);
793-
log.refname = buf.buf,
791+
snprintf(buf, sizeof(buf), "refs/heads/%04d", i);
792+
log.refname = buf;
794793

795794
err = reftable_writer_add_log(writer, &log);
796795
check(!err);
@@ -824,7 +823,6 @@ static void t_write_multiple_indices(void)
824823
reftable_writer_free(writer);
825824
reftable_reader_decref(reader);
826825
strbuf_release(&writer_buf);
827-
strbuf_release(&buf);
828826
}
829827

830828
static void t_write_multi_level_index(void)
@@ -848,10 +846,10 @@ static void t_write_multi_level_index(void)
848846
.value_type = REFTABLE_REF_VAL1,
849847
.value.val1 = {i},
850848
};
849+
char buf[128];
851850

852-
strbuf_reset(&buf);
853-
strbuf_addf(&buf, "refs/heads/%03" PRIuMAX, (uintmax_t)i);
854-
ref.refname = buf.buf,
851+
snprintf(buf, sizeof(buf), "refs/heads/%03" PRIuMAX, (uintmax_t)i);
852+
ref.refname = buf;
855853

856854
err = reftable_writer_add_ref(writer, &ref);
857855
check(!err);

t/unit-tests/t-reftable-stack.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ static int write_test_ref(struct reftable_writer *wr, void *arg)
105105
static void write_n_ref_tables(struct reftable_stack *st,
106106
size_t n)
107107
{
108-
struct strbuf buf = STRBUF_INIT;
109108
int disable_auto_compact;
110109
int err;
111110

@@ -117,18 +116,17 @@ static void write_n_ref_tables(struct reftable_stack *st,
117116
.update_index = reftable_stack_next_update_index(st),
118117
.value_type = REFTABLE_REF_VAL1,
119118
};
119+
char buf[128];
120120

121-
strbuf_reset(&buf);
122-
strbuf_addf(&buf, "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i);
123-
ref.refname = buf.buf;
121+
snprintf(buf, sizeof(buf), "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i);
122+
ref.refname = buf;
124123
t_reftable_set_hash(ref.value.val1, i, GIT_SHA1_FORMAT_ID);
125124

126125
err = reftable_stack_add(st, &write_test_ref, &ref);
127126
check(!err);
128127
}
129128

130129
st->opts.disable_auto_compact = disable_auto_compact;
131-
strbuf_release(&buf);
132130
}
133131

134132
struct write_log_arg {
@@ -434,7 +432,10 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void)
434432
* Adding a new table to the stack should not be impacted by this, even
435433
* though auto-compaction will now fail.
436434
*/
437-
strbuf_addf(&table_path, "%s/%s.lock", dir, st->readers[0]->name);
435+
strbuf_addstr(&table_path, dir);
436+
strbuf_addstr(&table_path, "/");
437+
strbuf_addstr(&table_path, st->readers[0]->name);
438+
strbuf_addstr(&table_path, ".lock");
438439
write_file_buf(table_path.buf, "", 0);
439440

440441
ref.update_index = 2;
@@ -1077,8 +1078,10 @@ static void t_reftable_stack_auto_compaction_with_locked_tables(void)
10771078
* size, we expect that auto-compaction will want to compact all of the
10781079
* tables. Locking any of the tables will keep it from doing so.
10791080
*/
1080-
strbuf_reset(&buf);
1081-
strbuf_addf(&buf, "%s/%s.lock", dir, st->readers[2]->name);
1081+
strbuf_addstr(&buf, dir);
1082+
strbuf_addstr(&buf, "/");
1083+
strbuf_addstr(&buf, st->readers[2]->name);
1084+
strbuf_addstr(&buf, ".lock");
10821085
write_file_buf(buf.buf, "", 0);
10831086

10841087
/*
@@ -1101,7 +1104,6 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
11011104
{
11021105
struct reftable_write_options opts = { 0 };
11031106
struct reftable_stack *st = NULL;
1104-
struct strbuf refname = STRBUF_INIT;
11051107
char *dir = get_tmp_dir(__LINE__);
11061108
int err;
11071109
size_t i, n = 20;
@@ -1115,6 +1117,7 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
11151117
.value_type = REFTABLE_REF_SYMREF,
11161118
.value.symref = (char *) "master",
11171119
};
1120+
char buf[128];
11181121

11191122
/*
11201123
* Disable auto-compaction for all but the last runs. Like this
@@ -1123,9 +1126,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
11231126
*/
11241127
st->opts.disable_auto_compact = i != n;
11251128

1126-
strbuf_reset(&refname);
1127-
strbuf_addf(&refname, "branch-%04"PRIuMAX, (uintmax_t)i);
1128-
ref.refname = refname.buf;
1129+
snprintf(buf, sizeof(buf), "branch-%04"PRIuMAX, (uintmax_t)i);
1130+
ref.refname = buf;
11291131

11301132
err = reftable_stack_add(st, write_test_ref, &ref);
11311133
check(!err);
@@ -1142,7 +1144,6 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
11421144
}
11431145

11441146
reftable_stack_destroy(st);
1145-
strbuf_release(&refname);
11461147
clear_dir(dir);
11471148
}
11481149

@@ -1163,8 +1164,10 @@ static void t_reftable_stack_compaction_with_locked_tables(void)
11631164
check_int(st->merged->readers_len, ==, 3);
11641165

11651166
/* Lock one of the tables that we're about to compact. */
1166-
strbuf_reset(&buf);
1167-
strbuf_addf(&buf, "%s/%s.lock", dir, st->readers[1]->name);
1167+
strbuf_addstr(&buf, dir);
1168+
strbuf_addstr(&buf, "/");
1169+
strbuf_addstr(&buf, st->readers[1]->name);
1170+
strbuf_addstr(&buf, ".lock");
11681171
write_file_buf(buf.buf, "", 0);
11691172

11701173
/*
@@ -1321,10 +1324,13 @@ static void t_reftable_stack_reload_with_missing_table(void)
13211324
* our old readers. This should trigger a partial reload of the stack,
13221325
* where we try to reuse our old readers.
13231326
*/
1324-
strbuf_addf(&content, "%s\n", st->readers[0]->name);
1325-
strbuf_addf(&content, "%s\n", st->readers[1]->name);
1327+
strbuf_addstr(&content, st->readers[0]->name);
1328+
strbuf_addstr(&content, "\n");
1329+
strbuf_addstr(&content, st->readers[1]->name);
1330+
strbuf_addstr(&content, "\n");
13261331
strbuf_addstr(&content, "garbage\n");
1327-
strbuf_addf(&table_path, "%s.lock", st->list_file);
1332+
strbuf_addstr(&table_path, st->list_file);
1333+
strbuf_addstr(&table_path, ".lock");
13281334
write_file_buf(table_path.buf, content.buf, content.len);
13291335
err = rename(table_path.buf, st->list_file);
13301336
check(!err);

0 commit comments

Comments
 (0)