From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Edward Thomson <ethomson@edwardthomson.com>
Subject: [PATCH 09/10] reftable/stack: adapt `stack_filename()` to handle allocation failures
Date: Fri, 11 Oct 2024 08:54:35 +0200 [thread overview]
Message-ID: <d66d9e50e06ae6e16a4092667c561dccdbabfa08.1728629612.git.ps@pks.im> (raw)
In-Reply-To: <cover.1728629612.git.ps@pks.im>
The `stack_filename()` function cannot pass any errors to the caller as it
has a `void` return type. Adapt it and its callers such that we can
handle errors and start handling allocation failures.
There are two interesting edge cases in `reftable_stack_destroy()` and
`reftable_addition_close()`. Both of these are trying to tear down their
respective structures, and while doing so they try to unlink some of the
tables they have been keeping alive. Any earlier attempts to do that may
fail on Windows because it keeps us from deleting such tables while they
are still open, and thus we re-try on close. It's okay and even expected
that this can fail when the tables are still open by another process, so
we handle the allocation failures gracefully and just skip over any file
whose name we couldn't figure out.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
reftable/stack.c | 62 +++++++++++++++++++++++++++++++++++-------------
1 file changed, 45 insertions(+), 17 deletions(-)
diff --git a/reftable/stack.c b/reftable/stack.c
index e94eb3c4685..243b10715cc 100644
--- a/reftable/stack.c
+++ b/reftable/stack.c
@@ -31,13 +31,16 @@ static void reftable_addition_close(struct reftable_addition *add);
static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
int reuse_open);
-static void stack_filename(struct reftable_buf *dest, struct reftable_stack *st,
- const char *name)
+static int stack_filename(struct reftable_buf *dest, struct reftable_stack *st,
+ const char *name)
{
+ int err;
reftable_buf_reset(dest);
- reftable_buf_addstr(dest, st->reftable_dir);
- reftable_buf_addstr(dest, "/");
- reftable_buf_addstr(dest, name);
+ if ((err = reftable_buf_addstr(dest, st->reftable_dir)) < 0 ||
+ (err = reftable_buf_addstr(dest, "/")) < 0 ||
+ (err = reftable_buf_addstr(dest, name)) < 0)
+ return err;
+ return 0;
}
static ssize_t reftable_fd_write(void *arg, const void *data, size_t sz)
@@ -211,13 +214,16 @@ void reftable_stack_destroy(struct reftable_stack *st)
struct reftable_buf filename = REFTABLE_BUF_INIT;
for (i = 0; i < st->readers_len; i++) {
const char *name = reader_name(st->readers[i]);
+ int try_unlinking = 1;
+
reftable_buf_reset(&filename);
if (names && !has_name(names, name)) {
- stack_filename(&filename, st, name);
+ if (stack_filename(&filename, st, name) < 0)
+ try_unlinking = 0;
}
reftable_reader_decref(st->readers[i]);
- if (filename.len) {
+ if (try_unlinking && filename.len) {
/* On Windows, can only unlink after closing. */
unlink(filename.buf);
}
@@ -310,7 +316,10 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
if (!rd) {
struct reftable_block_source src = { NULL };
- stack_filename(&table_path, st, name);
+
+ err = stack_filename(&table_path, st, name);
+ if (err < 0)
+ goto done;
err = reftable_block_source_from_file(&src,
table_path.buf);
@@ -341,7 +350,11 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
for (i = 0; i < cur_len; i++) {
if (cur[i]) {
const char *name = reader_name(cur[i]);
- stack_filename(&table_path, st, name);
+
+ err = stack_filename(&table_path, st, name);
+ if (err < 0)
+ goto done;
+
reftable_reader_decref(cur[i]);
unlink(table_path.buf);
}
@@ -700,8 +713,8 @@ static void reftable_addition_close(struct reftable_addition *add)
size_t i;
for (i = 0; i < add->new_tables_len; i++) {
- stack_filename(&nm, add->stack, add->new_tables[i]);
- unlink(nm.buf);
+ if (!stack_filename(&nm, add->stack, add->new_tables[i]))
+ unlink(nm.buf);
reftable_free(add->new_tables[i]);
add->new_tables[i] = NULL;
}
@@ -851,7 +864,9 @@ int reftable_addition_add(struct reftable_addition *add,
if (err < 0)
goto done;
- stack_filename(&temp_tab_file_name, add->stack, next_name.buf);
+ err = stack_filename(&temp_tab_file_name, add->stack, next_name.buf);
+ if (err < 0)
+ goto done;
reftable_buf_addstr(&temp_tab_file_name, ".temp.XXXXXX");
tab_file = mks_tempfile(temp_tab_file_name.buf);
@@ -900,7 +915,10 @@ int reftable_addition_add(struct reftable_addition *add,
if (err < 0)
goto done;
reftable_buf_addstr(&next_name, ".ref");
- stack_filename(&tab_file_name, add->stack, next_name.buf);
+
+ err = stack_filename(&tab_file_name, add->stack, next_name.buf);
+ if (err < 0)
+ goto done;
/*
On windows, this relies on rand() picking a unique destination name.
@@ -954,7 +972,9 @@ static int stack_compact_locked(struct reftable_stack *st,
if (err < 0)
goto done;
- stack_filename(&tab_file_path, st, next_name.buf);
+ err = stack_filename(&tab_file_path, st, next_name.buf);
+ if (err < 0)
+ goto done;
reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX");
tab_file = mks_tempfile(tab_file_path.buf);
@@ -1174,7 +1194,9 @@ static int stack_compact_range(struct reftable_stack *st,
}
for (i = last + 1; i > first; i--) {
- stack_filename(&table_name, st, reader_name(st->readers[i - 1]));
+ err = stack_filename(&table_name, st, reader_name(st->readers[i - 1]));
+ if (err < 0)
+ goto done;
err = hold_lock_file_for_update(&table_locks[nlocks],
table_name.buf, LOCK_NO_DEREF);
@@ -1383,7 +1405,10 @@ static int stack_compact_range(struct reftable_stack *st,
goto done;
reftable_buf_addstr(&new_table_name, ".ref");
- stack_filename(&new_table_path, st, new_table_name.buf);
+
+ err = stack_filename(&new_table_path, st, new_table_name.buf);
+ if (err < 0)
+ goto done;
err = rename_tempfile(&new_table, new_table_path.buf);
if (err < 0) {
@@ -1677,7 +1702,10 @@ static void remove_maybe_stale_table(struct reftable_stack *st, uint64_t max,
struct reftable_block_source src = { NULL };
struct reftable_reader *rd = NULL;
struct reftable_buf table_path = REFTABLE_BUF_INIT;
- stack_filename(&table_path, st, name);
+
+ err = stack_filename(&table_path, st, name);
+ if (err < 0)
+ goto done;
err = reftable_block_source_from_file(&src, table_path.buf);
if (err < 0)
--
2.47.0.dirty
next prev parent reply other threads:[~2024-10-11 6:54 UTC|newest]
Thread overview: 63+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-11 6:54 [PATCH 00/10] reftable: stop using `struct strbuf` Patrick Steinhardt
2024-10-11 6:54 ` [PATCH 01/10] reftable: stop using `strbuf_addbuf()` Patrick Steinhardt
2024-10-11 6:54 ` [PATCH 02/10] reftable: stop using `strbuf_addf()` Patrick Steinhardt
2024-10-11 9:51 ` karthik nayak
2024-10-14 13:09 ` Patrick Steinhardt
2024-10-11 6:54 ` [PATCH 03/10] reftable/basics: provide new `reftable_buf` interface Patrick Steinhardt
2024-10-11 10:03 ` karthik nayak
2024-10-14 13:09 ` Patrick Steinhardt
2024-10-11 6:54 ` [PATCH 04/10] reftable: convert from `strbuf` to `reftable_buf` Patrick Steinhardt
2024-10-11 12:12 ` karthik nayak
2024-10-14 13:09 ` Patrick Steinhardt
2024-10-11 6:54 ` [PATCH 05/10] reftable/blocksource: adapt interface name Patrick Steinhardt
2024-10-11 6:54 ` [PATCH 06/10] t/unit-tests: check for `reftable_buf` allocation errors Patrick Steinhardt
2024-10-11 6:54 ` [PATCH 07/10] reftable/stack: adapt `format_name()` to handle allocation failures Patrick Steinhardt
2024-10-11 6:54 ` [PATCH 08/10] reftable/record: adapt `reftable_record_key()` " Patrick Steinhardt
2024-10-11 6:54 ` Patrick Steinhardt [this message]
2024-10-11 6:54 ` [PATCH 10/10] reftable: handle trivial `reftable_buf` errors Patrick Steinhardt
2024-10-11 12:18 ` [PATCH 00/10] reftable: stop using `struct strbuf` karthik nayak
2024-10-14 13:09 ` Patrick Steinhardt
2024-10-14 13:02 ` [PATCH v2 " Patrick Steinhardt
2024-10-14 13:02 ` [PATCH v2 01/10] reftable: stop using `strbuf_addbuf()` Patrick Steinhardt
2024-10-14 22:19 ` Taylor Blau
2024-10-14 13:02 ` [PATCH v2 02/10] reftable: stop using `strbuf_addf()` Patrick Steinhardt
2024-10-14 22:32 ` Taylor Blau
2024-10-15 4:37 ` Patrick Steinhardt
2024-10-15 19:26 ` Taylor Blau
2024-10-14 13:02 ` [PATCH v2 03/10] reftable/basics: provide new `reftable_buf` interface Patrick Steinhardt
2024-10-14 22:34 ` Taylor Blau
2024-10-15 4:38 ` Patrick Steinhardt
2024-10-15 5:10 ` Eric Sunshine
2024-10-15 19:27 ` Taylor Blau
2024-10-16 8:42 ` Patrick Steinhardt
2024-10-16 20:56 ` Taylor Blau
2024-10-17 4:54 ` Patrick Steinhardt
2024-10-17 20:59 ` Taylor Blau
2024-10-14 13:02 ` [PATCH v2 04/10] reftable: convert from `strbuf` to `reftable_buf` Patrick Steinhardt
2024-10-14 22:35 ` Taylor Blau
2024-10-14 13:02 ` [PATCH v2 05/10] reftable/blocksource: adapt interface name Patrick Steinhardt
2024-10-14 13:02 ` [PATCH v2 06/10] t/unit-tests: check for `reftable_buf` allocation errors Patrick Steinhardt
2024-10-14 13:02 ` [PATCH v2 07/10] reftable/stack: adapt `format_name()` to handle allocation failures Patrick Steinhardt
2024-10-14 22:41 ` Taylor Blau
2024-10-14 13:02 ` [PATCH v2 08/10] reftable/record: adapt `reftable_record_key()` " Patrick Steinhardt
2024-10-14 13:02 ` [PATCH v2 09/10] reftable/stack: adapt `stack_filename()` " Patrick Steinhardt
2024-10-14 13:02 ` [PATCH v2 10/10] reftable: handle trivial `reftable_buf` errors Patrick Steinhardt
2024-10-14 22:44 ` [PATCH v2 00/10] reftable: stop using `struct strbuf` Taylor Blau
2024-10-15 4:37 ` Patrick Steinhardt
2024-10-15 10:33 ` shejialuo
2024-10-15 10:44 ` Patrick Steinhardt
2024-10-15 11:23 ` shejialuo
2024-10-17 4:53 ` [PATCH v3 " Patrick Steinhardt
2024-10-17 4:53 ` [PATCH v3 01/10] reftable: stop using `strbuf_addbuf()` Patrick Steinhardt
2024-10-17 4:53 ` [PATCH v3 02/10] reftable: stop using `strbuf_addf()` Patrick Steinhardt
2024-10-17 4:53 ` [PATCH v3 03/10] reftable/basics: provide new `reftable_buf` interface Patrick Steinhardt
2024-10-17 4:53 ` [PATCH v3 04/10] reftable: convert from `strbuf` to `reftable_buf` Patrick Steinhardt
2024-10-17 4:53 ` [PATCH v3 05/10] reftable/blocksource: adapt interface name Patrick Steinhardt
2024-10-17 4:54 ` [PATCH v3 06/10] t/unit-tests: check for `reftable_buf` allocation errors Patrick Steinhardt
2024-10-17 4:54 ` [PATCH v3 07/10] reftable/stack: adapt `format_name()` to handle allocation failures Patrick Steinhardt
2024-10-17 4:54 ` [PATCH v3 08/10] reftable/record: adapt `reftable_record_key()` " Patrick Steinhardt
2024-10-17 4:54 ` [PATCH v3 09/10] reftable/stack: adapt `stack_filename()` " Patrick Steinhardt
2024-10-17 4:54 ` [PATCH v3 10/10] reftable: handle trivial `reftable_buf` errors Patrick Steinhardt
2024-10-17 21:00 ` [PATCH v3 00/10] reftable: stop using `struct strbuf` Taylor Blau
2024-10-18 7:46 ` karthik nayak
2024-10-18 21:41 ` Taylor Blau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d66d9e50e06ae6e16a4092667c561dccdbabfa08.1728629612.git.ps@pks.im \
--to=ps@pks.im \
--cc=ethomson@edwardthomson.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).