On Wed, Nov 24, 2010 at 5:18 PM, M. Mohan Kumar <mo...@in.ibm.com> wrote: > Add qemu_read_full function > > Signed-off-by: M. Mohan Kumar <mo...@in.ibm.com> > --- > osdep.c | 29 +++++++++++++++++++++++++++++ > qemu-common.h | 2 ++ > 2 files changed, 31 insertions(+), 0 deletions(-) > > diff --git a/osdep.c b/osdep.c > index 327583b..7046b32 100644 > --- a/osdep.c > +++ b/osdep.c > @@ -127,6 +127,35 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t > count) > } > > /* > + * A variant of read(2) which handles interrupted read. > + * Simlar to qemu_write_full function > + * > + * Return the number of bytes read. > + * > + */
Two points worth noting in the comment (stolen from qemu_write_full): 1. "This function don't work with non-blocking fd's." 2. "Set errno if fewer than `count' bytes are read." Normal QEMU style would be to return -errno, so it's important to point out this exception to the rule. > +ssize_t qemu_read_full(int fd, void *buf, size_t count) > +{ > + ssize_t ret = 0; > + ssize_t total = 0; > + > + while (count) { > + ret = read(fd, buf, count); > + if (ret < 0) { > + if (errno == EINTR) { > + continue; > + } > + break; > + } > + > + count -= ret; > + buf += ret; > + total += ret; > + } > + > + return total; > +} On EOF read(2) will return 0. Right now we don't handle this case and go into an infinite loop. Stefan