From: Stefan Beller <sbeller@google.com>
To: gitster@pobox.com, git@vger.kernel.org
Cc: Stefan Beller <sbeller@google.com>,
ramsay@ramsayjones.plus.com, jacob.keller@gmail.com,
peff@peff.net, jrnieder@gmail.com, johannes.schindelin@gmail.com,
Jens.Lehmann@web.de, ericsunshine@gmail.com
Subject: [PATCHv6 4/8] strbuf: add strbuf_read_once to read without blocking
Date: Wed, 30 Sep 2015 18:54:12 -0700 [thread overview]
Message-ID: <1443664456-1307-5-git-send-email-sbeller@google.com> (raw)
In-Reply-To: <1443664456-1307-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.275.gf20166c.dirty
next prev parent reply other threads:[~2015-10-01 1:55 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-01 1:54 [PATCHv6 0/8] fetch submodules in parallel Stefan Beller
2015-10-01 1:54 ` [PATCHv6 1/8] submodule.c: write "Fetching submodule <foo>" to stderr Stefan Beller
2015-10-01 1:54 ` [PATCHv6 2/8] xread: poll on non blocking fds Stefan Beller
2015-10-01 1:54 ` [PATCHv6 3/8] xread_nonblock: add functionality to read from fds without blocking Stefan Beller
2015-10-01 1:54 ` Stefan Beller [this message]
2015-10-01 1:54 ` [PATCHv6 5/8] sigchain: add command to pop all common signals Stefan Beller
2015-10-01 1:54 ` [PATCHv6 6/8] run-command: add an asynchronous parallel child processor Stefan Beller
2015-10-01 1:54 ` [PATCHv6 7/8] fetch_populated_submodules: use new parallel job processing Stefan Beller
2015-10-01 1:54 ` [PATCHv6 8/8] submodules: allow parallel fetching, add tests and documentation Stefan Beller
2015-10-01 18:55 ` [PATCHv6 0/8] fetch submodules in parallel Ramsay Jones
2015-10-01 19:03 ` Stefan Beller
2015-10-02 18:48 ` Junio C Hamano
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=1443664456-1307-5-git-send-email-sbeller@google.com \
--to=sbeller@google.com \
--cc=Jens.Lehmann@web.de \
--cc=ericsunshine@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jacob.keller@gmail.com \
--cc=johannes.schindelin@gmail.com \
--cc=jrnieder@gmail.com \
--cc=peff@peff.net \
--cc=ramsay@ramsayjones.plus.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.