git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] sequencer: factor out strbuf_read_file_or_whine()

From
Jeff King <peff@peff.net>
Date
Feb 23, 2018, 06:49 UTC
Message-ID
<20180223064952.GB19791@sigill.intra.peff.net>
In-Reply-To
<6b58885c-b0f6-1687-3f2d-4594aacff9ac@web.de>
On Thu, Feb 22, 2018 at 08:29:25PM +0100, René Scharfe wrote:
Show 9 quoted lines
> Reduce code duplication by factoring out a function that reads an entire
> file into a strbuf, or reports errors on stderr if something goes wrong.
> 
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
> The difference to using strbuf_read_file() is more detailed error
> messages for open(2) failures.  But I don't know if we need them -- or
> under which circumstances reading todo files could fail anyway.  When
> doing multiple rebases in parallel perhaps?

I'm fine with this patch, but FWIW I think reporting the result of strbuf_read_file with error_errno() would actually be an improvement. The errno values are generally sufficient to tell if the problem was in opening or reading, and then we'd get more information in the case of a failed read() call.

Thought note...
Show 22 quoted lines
> diff --git a/sequencer.c b/sequencer.c
> index e9baaf59bd..e34334f0ef 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -1869,22 +1869,31 @@ static int count_commands(struct todo_list *todo_list)
>  	return count;
>  }
>  
> +static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
> +{
> +	int fd;
> +	ssize_t len;
> +
> +	fd = open(path, O_RDONLY);
> +	if (fd < 0)
> +		return error_errno(_("could not open '%s'"), path);
> +	len = strbuf_read(sb, fd, 0);
> +	close(fd);
> +	if (len < 0)
> +		return error(_("could not read '%s'."), path);
> +	return len;
> +}

If we were to use error_errno() in the second conditional here, we should take care not to clobber errno during the close(). I think strbuf_read_file() actually has the same problem, which might be worth fixing.

-Peff
Previous: Junio C HamanoNext: Jeff King
Message 3 of 8 in “sequencer: factor out strbuf_read_file_or_whine()”
  1. sequencer: factor out strbuf_read_file_or_whine()René Scharfe, Feb 22, 2018
  2. Junio C HamanoFeb 22, 2018
  3. Jeff KingFeb 23, 2018
  4. strbuf_read_file(): preserve errno across close() callJeff King, Feb 23, 2018
  5. René ScharfeFeb 23, 2018
  6. Junio C HamanoFeb 23, 2018
  7. René ScharfeFeb 23, 2018
  8. Jeff KingFeb 26, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.