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

[PATCHv2 3/7] strbuf: add strbuf_read_once to read without blocking

From
Stefan Beller <sbeller@google.com>
Date
Dec 16, 2015, 00:04 UTC
Message-ID
<1450224252-16833-4-git-send-email-sbeller@google.com>
In-Reply-To
<1450224252-16833-1-git-send-email-sbeller@google.com>

The new call will read from a file descriptor into a strbuf once. The underlying call xread is just run once. xread only reattempts reading in case of EINTR, which makes it suitable to use for a nonblocking read.

Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 strbuf.c | 11 +++++++++++
 strbuf.h |  8 ++++++++
 2 files changed, 19 insertions(+)
diff --git a/strbuf.c b/strbuf.c
index d76f0ae..38686ff 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(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 7123fca..2bf90e7 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -367,6 +367,14 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
 extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
 
 /**
+ * Read the contents of a given file descriptor partially by using only one
+ * attempt of xread. The third argument can be used to give a hint about the
+ * file size, to avoid reallocs. Returns the number of new bytes appended to
+ * the sb.
+ */
+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.6.4.443.ge094245.dirty
Previous: Jeff KingNext: Stefan Beller
Message 13 of 19 in “[PATCHv2 0/7] Rerolling sb/submodule-parallel-fetch for the time after 2.7”
  1. Stefan BellerDec 16, 2015
  2. 1/7 submodule.c: write "Fetching submodule <foo>" to stderrStefan Beller, Dec 16, 2015
  3. 2/7 xread: poll on non blocking fdsStefan Beller, Dec 16, 2015
  4. Torsten BögershausenDec 17, 2015
  5. Stefan BellerDec 17, 2015
  6. Torsten BögershausenDec 17, 2015
  7. Stefan BellerDec 17, 2015
  8. Jeff KingDec 18, 2015
  9. Jeff KingDec 18, 2015
  10. Torsten BögershausenDec 18, 2015
  11. Jeff KingDec 18, 2015
  12. Jeff KingDec 18, 2015
  13. 3/7 strbuf: add strbuf_read_once to read without blockingStefan Beller, Dec 16, 2015
  14. 4/7 sigchain: add command to pop all common signalsStefan Beller, Dec 16, 2015
  15. 5/7 run-command: add an asynchronous parallel child processorStefan Beller, Dec 16, 2015
  16. 6/7 fetch_populated_submodules: use new parallel job processingStefan Beller, Dec 16, 2015
  17. 7/7 submodules: allow parallel fetching, add tests and documentationStefan Beller, Dec 16, 2015
  18. Stefan BellerDec 16, 2015
  19. Junio C HamanoDec 16, 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.