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>
This commit is contained in:

committed by
Junio C Hamano

parent
1079c4be0b
commit
b4e04fb66e
11
strbuf.c
11
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)
|
||||
|
Reference in New Issue
Block a user