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

[PATCH 4/8] strbuf: add strbuf_read_once to read without blocking

From
Stefan Beller <sbeller@google.com>
Date
Sep 28, 2015, 23:14 UTC
Message-ID
<1443482046-25569-5-git-send-email-sbeller@google.com>
In-Reply-To
<1443482046-25569-1-git-send-email-sbeller@google.com>

The new call will read from a file descriptor into a strbuf once. The underlying call xread_nonblock is meant to execute without blocking if the file descriptor is set to O_NONBLOCK. It is a bug to call strbuf_read_once on a file descriptor which would block.

Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 strbuf.c | 11 +++++++++++
 strbuf.h |  9 +++++++++
 2 files changed, 20 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;
+}
+
 #define STRBUF_MAXLINK (2*PATH_MAX)
 
 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
diff --git a/strbuf.h b/strbuf.h
index aef2794..ea69665 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -367,6 +367,15 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
 extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
 
 /**
+ * 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.
+ */
+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.
  */
-- 
2.5.0.273.g6fa2560.dirty
Previous: Stefan BellerNext: Stefan Beller
Message 5 of 13 in “fetch submodules in parallel”
  1. 0/8 fetch submodules in parallelStefan Beller, Sep 28, 2015
  2. 1/8 submodule.c: write "Fetching submodule <foo>" to stderrStefan Beller, Sep 28, 2015
  3. 2/8 xread: poll on non blocking fdsStefan Beller, Sep 28, 2015
  4. 3/8 xread_nonblock: add functionality to read from fds without blockingStefan Beller, Sep 28, 2015
  5. 4/8 strbuf: add strbuf_read_once to read without blockingStefan Beller, Sep 28, 2015
  6. 5/8 sigchain: add command to pop all common signalsStefan Beller, Sep 28, 2015
  7. Junio C HamanoSep 30, 2015
  8. 6/8 run-command: add an asynchronous parallel child processorStefan Beller, Sep 28, 2015
  9. Junio C HamanoSep 30, 2015
  10. Stefan BellerSep 30, 2015
  11. Junio C HamanoSep 30, 2015
  12. 7/8 fetch_populated_submodules: use new parallel job processingStefan Beller, Sep 28, 2015
  13. 8/8 submodules: allow parallel fetching, add tests and documentationStefan Beller, Sep 28, 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.