Provide a wrapper to read(), similar to xread(), that restarts on EINTR but not EAGAIN (or EWOULDBLOCK). This enables the caller to handle polling itself, possibly polling multiple sockets or performing some other action. Helped-by: Jacob Keller <jacob.keller@xxxxxxxxx> Helped-by: Jeff King <peff@xxxxxxxx>, Helped-by: Junio C Hamano <gitster@xxxxxxxxx> Signed-off-by: Stefan Beller <sbeller@xxxxxxxxxx> Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx> --- git-compat-util.h | 1 + wrapper.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/git-compat-util.h b/git-compat-util.h index c6d391f..9ccea85 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -718,6 +718,7 @@ extern void *xcalloc(size_t nmemb, size_t size); extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); extern void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset); extern ssize_t xread(int fd, void *buf, size_t len); +extern ssize_t xread_nonblock(int fd, void *buf, size_t len); extern ssize_t xwrite(int fd, const void *buf, size_t len); extern ssize_t xpread(int fd, void *buf, size_t len, off_t offset); extern int xdup(int fd); diff --git a/wrapper.c b/wrapper.c index 5517928..41a21e1 100644 --- a/wrapper.c +++ b/wrapper.c @@ -217,6 +217,28 @@ ssize_t xread(int fd, void *buf, size_t len) } /* + * xread_nonblock() is the same a read(), but it automatically restarts read() + * interrupted operations (EINTR). xread_nonblock() DOES NOT GUARANTEE that + * "len" bytes is read. EWOULDBLOCK is turned into EAGAIN. + */ +ssize_t xread_nonblock(int fd, void *buf, size_t len) +{ + ssize_t nr; + if (len > MAX_IO_SIZE) + len = MAX_IO_SIZE; + while (1) { + nr = read(fd, buf, len); + if (nr < 0) { + if (errno == EINTR) + continue; + if (errno == EWOULDBLOCK) + errno = EAGAIN; + } + return nr; + } +} + +/* * xwrite() is the same a write(), but it automatically restarts write() * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT * GUARANTEE that "len" bytes is written even if the operation is successful. -- 2.5.0.273.g6fa2560.dirty -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html