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 <ps@pks.im>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
This commit is contained in:
Patrick Steinhardt
2024-10-17 06:53:47 +02:00
committed by Taylor Blau
parent 409f04995e
commit 7fa7e14ebe
4 changed files with 50 additions and 37 deletions

View File

@ -105,7 +105,6 @@ static int write_test_ref(struct reftable_writer *wr, void *arg)
static void write_n_ref_tables(struct reftable_stack *st,
size_t n)
{
struct strbuf buf = STRBUF_INIT;
int disable_auto_compact;
int err;
@ -117,10 +116,10 @@ static void write_n_ref_tables(struct reftable_stack *st,
.update_index = reftable_stack_next_update_index(st),
.value_type = REFTABLE_REF_VAL1,
};
char buf[128];
strbuf_reset(&buf);
strbuf_addf(&buf, "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i);
ref.refname = buf.buf;
snprintf(buf, sizeof(buf), "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i);
ref.refname = buf;
t_reftable_set_hash(ref.value.val1, i, GIT_SHA1_FORMAT_ID);
err = reftable_stack_add(st, &write_test_ref, &ref);
@ -128,7 +127,6 @@ static void write_n_ref_tables(struct reftable_stack *st,
}
st->opts.disable_auto_compact = disable_auto_compact;
strbuf_release(&buf);
}
struct write_log_arg {
@ -434,7 +432,10 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void)
* Adding a new table to the stack should not be impacted by this, even
* though auto-compaction will now fail.
*/
strbuf_addf(&table_path, "%s/%s.lock", dir, st->readers[0]->name);
strbuf_addstr(&table_path, dir);
strbuf_addstr(&table_path, "/");
strbuf_addstr(&table_path, st->readers[0]->name);
strbuf_addstr(&table_path, ".lock");
write_file_buf(table_path.buf, "", 0);
ref.update_index = 2;
@ -1077,8 +1078,10 @@ static void t_reftable_stack_auto_compaction_with_locked_tables(void)
* size, we expect that auto-compaction will want to compact all of the
* tables. Locking any of the tables will keep it from doing so.
*/
strbuf_reset(&buf);
strbuf_addf(&buf, "%s/%s.lock", dir, st->readers[2]->name);
strbuf_addstr(&buf, dir);
strbuf_addstr(&buf, "/");
strbuf_addstr(&buf, st->readers[2]->name);
strbuf_addstr(&buf, ".lock");
write_file_buf(buf.buf, "", 0);
/*
@ -1101,7 +1104,6 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
{
struct reftable_write_options opts = { 0 };
struct reftable_stack *st = NULL;
struct strbuf refname = STRBUF_INIT;
char *dir = get_tmp_dir(__LINE__);
int err;
size_t i, n = 20;
@ -1115,6 +1117,7 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
.value_type = REFTABLE_REF_SYMREF,
.value.symref = (char *) "master",
};
char buf[128];
/*
* 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)
*/
st->opts.disable_auto_compact = i != n;
strbuf_reset(&refname);
strbuf_addf(&refname, "branch-%04"PRIuMAX, (uintmax_t)i);
ref.refname = refname.buf;
snprintf(buf, sizeof(buf), "branch-%04"PRIuMAX, (uintmax_t)i);
ref.refname = buf;
err = reftable_stack_add(st, write_test_ref, &ref);
check(!err);
@ -1142,7 +1144,6 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
}
reftable_stack_destroy(st);
strbuf_release(&refname);
clear_dir(dir);
}
@ -1163,8 +1164,10 @@ static void t_reftable_stack_compaction_with_locked_tables(void)
check_int(st->merged->readers_len, ==, 3);
/* Lock one of the tables that we're about to compact. */
strbuf_reset(&buf);
strbuf_addf(&buf, "%s/%s.lock", dir, st->readers[1]->name);
strbuf_addstr(&buf, dir);
strbuf_addstr(&buf, "/");
strbuf_addstr(&buf, st->readers[1]->name);
strbuf_addstr(&buf, ".lock");
write_file_buf(buf.buf, "", 0);
/*
@ -1321,10 +1324,13 @@ static void t_reftable_stack_reload_with_missing_table(void)
* our old readers. This should trigger a partial reload of the stack,
* where we try to reuse our old readers.
*/
strbuf_addf(&content, "%s\n", st->readers[0]->name);
strbuf_addf(&content, "%s\n", st->readers[1]->name);
strbuf_addstr(&content, st->readers[0]->name);
strbuf_addstr(&content, "\n");
strbuf_addstr(&content, st->readers[1]->name);
strbuf_addstr(&content, "\n");
strbuf_addstr(&content, "garbage\n");
strbuf_addf(&table_path, "%s.lock", st->list_file);
strbuf_addstr(&table_path, st->list_file);
strbuf_addstr(&table_path, ".lock");
write_file_buf(table_path.buf, content.buf, content.len);
err = rename(table_path.buf, st->list_file);
check(!err);