diff options
author | Stefan Beller <sbeller@google.com> | 2015-12-15 16:04:08 -0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2015-12-16 12:06:08 -0800 |
commit | b4e04fb66e87a027c5f9c96bcbbba50719400169 (patch) | |
tree | 3a5eee3f453bf76dc8c3e0da0eaefba3817b76c2 /strbuf.c | |
parent | 1079c4be0b72003668df647f8a520fa137c7e158 (diff) | |
download | git-b4e04fb66e87a027c5f9c96bcbbba50719400169.tar.gz |
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 <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 11 |
1 files changed, 11 insertions, 0 deletions
@@ -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) |