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

Re: [PATCHv3 04/13] strbuf: add strbuf_read_once to read without blocking

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 22, 2015, 00:17 UTC
Message-ID
<xmqqoagv71es.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<1442875159-13027-5-git-send-email-sbeller@google.com>
Stefan Beller <sbeller@google.com> writes:
> The new call will read a fd into a strbuf once. The underlying call
"read from a fd"
> xread_nonblock is meant to execute non blockingly if the fd is set to
> O_NONBLOCK.

The latter sentence adds more questions than it answers. If the file descriptor is not set to non-blocking, what happens? Is it a bug in the caller, and if so do we give help to diagnose such a bug?

Show 23 quoted lines
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  strbuf.c | 11 +++++++++++
>  strbuf.h |  6 ++++++
>  2 files changed, 17 insertions(+)
>
> diff --git a/strbuf.c b/strbuf.c
> index cce5eed..35e71b8 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -384,6 +384,17 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
>  	return sb->len - oldlen;
>  }
>  
> +ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
> +{
> +	ssize_t cnt;
> +
> +	strbuf_grow(sb, hint ? hint : 8192);
> +	cnt = xread_nonblock(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
> +	if (cnt > 0)
> +		strbuf_setlen(sb, sb->len + cnt);
> +	return cnt;

OK. So the caller that receives a negative value can check errno to see if we got EAGAIN. How would the caller tell when it got an EOF?

>  /**
> + * Same as strbuf_read, just returns non-blockingly by ignoring EAGAIN.

I do not think you want to say "same as" for this one. strbuf_read() is about reading thru to the end, but this is about making some progress without blocking.

    Read from a file descriptor that is marked as O_NONBLOCK without
    blocking.  Returns the number of new bytes appended to the sb.
    Negative return value signals there was an error returned from
    underlying read(2), in which case the caller should check errno.
    e.g. errno == EAGAIN when the read may have blocked.
or something?  Again, how would a caller tell when it got an EOF?
Show 8 quoted lines
> + * The fd must have set O_NONBLOCK.
> + */
> +extern ssize_t strbuf_read_once(struct strbuf *, int fd, size_t hint);
> +
> +/**
>   * Read the contents of a file, specified by its path. The third argument
>   * can be used to give a hint about the file size, to avoid reallocs.
>   */
Previous: Stefan BellerNext: Jacob Keller
Message 27 of 48 in “[PATCHv3 00/13] fetch submodules in parallel and a preview on parallel "submodule update"”
  1. Stefan BellerSep 21, 2015
  2. 01/13 Sending "Fetching submodule <foo>" output to stderrStefan Beller, Sep 21, 2015
  3. Junio C HamanoSep 21, 2015
  4. 02/13 xread: poll on non blocking fdsStefan Beller, Sep 21, 2015
  5. Junio C HamanoSep 21, 2015
  6. Torsten BögershausenSep 22, 2015
  7. Jacob KellerSep 22, 2015
  8. Torsten BögershausenSep 22, 2015
  9. Junio C HamanoSep 22, 2015
  10. Jeff KingSep 22, 2015
  11. Junio C HamanoSep 22, 2015
  12. Stefan BellerSep 23, 2015
  13. Junio C HamanoSep 23, 2015
  14. Jeff KingSep 23, 2015
  15. Eric SunshineSep 21, 2015
  16. Junio C HamanoSep 22, 2015
  17. Stefan BellerSep 22, 2015
  18. Junio C HamanoSep 22, 2015
  19. Stefan BellerSep 22, 2015
  20. 03/13 xread_nonblock: add functionality to read from fds nonblockinglyStefan Beller, Sep 21, 2015
  21. Junio C HamanoSep 22, 2015
  22. Junio C HamanoSep 22, 2015
  23. Jacob KellerSep 22, 2015
  24. Jacob KellerSep 22, 2015
  25. Junio C HamanoSep 22, 2015
  26. 04/13 strbuf: add strbuf_read_once to read without blockingStefan Beller, Sep 21, 2015
  27. Junio C HamanoSep 22, 2015
  28. Jacob KellerSep 22, 2015
  29. 05/13 run-command: factor out return value computationStefan Beller, Sep 21, 2015
  30. Junio C HamanoSep 22, 2015
  31. 06/13 run-command: add an asynchronous parallel child processorStefan Beller, Sep 21, 2015
  32. Junio C HamanoSep 22, 2015
  33. Stefan BellerSep 22, 2015
  34. Junio C HamanoSep 22, 2015
  35. Stefan BellerSep 22, 2015
  36. Junio C HamanoSep 22, 2015
  37. Stefan BellerSep 22, 2015
  38. Junio C HamanoSep 22, 2015
  39. 07/13 fetch_populated_submodules: use new parallel job processingStefan Beller, Sep 21, 2015
  40. Junio C HamanoSep 22, 2015
  41. 08/13 submodules: allow parallel fetching, add tests and documentationStefan Beller, Sep 21, 2015
  42. 09/13 submodule config: keep update strategy aroundStefan Beller, Sep 21, 2015
  43. Eric SunshineSep 22, 2015
  44. Stefan BellerSep 22, 2015
  45. 10/13 git submodule update: cmd_update_recursiveStefan Beller, Sep 21, 2015
  46. 11/13 git submodule update: cmd_update_cloneStefan Beller, Sep 21, 2015
  47. 12/13 git submodule update: cmd_update_fetchStefan Beller, Sep 21, 2015
  48. 13/13 Rewrite submodule update in CStefan Beller, Sep 21, 2015

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.