From b4e04fb66e87a027c5f9c96bcbbba50719400169 Mon Sep 17 00:00:00 2001 From: Stefan Beller Date: Tue, 15 Dec 2015 16:04:08 -0800 Subject: strbuf: add strbuf_read_once to read without blocking 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 Signed-off-by: Junio C Hamano --- strbuf.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c index d76f0aed8..38686ffb6 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) -- cgit v1.2.1