From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NQuvv-0006Tp-ES for qemu-devel@nongnu.org; Fri, 01 Jan 2010 22:45:43 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NQuvr-0006Sz-TS for qemu-devel@nongnu.org; Fri, 01 Jan 2010 22:45:43 -0500 Received: from [199.232.76.173] (port=53806 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NQuvr-0006Sw-Lg for qemu-devel@nongnu.org; Fri, 01 Jan 2010 22:45:39 -0500 Received: from mail-fx0-f222.google.com ([209.85.220.222]:55673) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NQuvr-00082i-E5 for qemu-devel@nongnu.org; Fri, 01 Jan 2010 22:45:39 -0500 Received: by fxm22 with SMTP id 22so16659813fxm.2 for ; Fri, 01 Jan 2010 19:45:37 -0800 (PST) From: "Kirill A. Shutemov" Date: Sat, 2 Jan 2010 05:45:19 +0200 Message-Id: <1262403933-26881-1-git-send-email-kirill@shutemov.name> Subject: [Qemu-devel] [PATCH 01/15] Introduce qemu_write_full() List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: "Kirill A. Shutemov" A variant of write(2) which handles partial write. Signed-off-by: Kirill A. Shutemov --- osdep.c | 27 +++++++++++++++++++++++++++ qemu-common.h | 2 ++ 2 files changed, 29 insertions(+), 0 deletions(-) diff --git a/osdep.c b/osdep.c index e4836e7..8ae48fe 100644 --- a/osdep.c +++ b/osdep.c @@ -243,6 +243,33 @@ int qemu_open(const char *name, int flags, ...) return ret; } +/* + * A variant of write(2) which handles partial write. + * + * Return the number of bytes transferred. + * Set errno if fewer than `count' bytes are written. + */ +size_t qemu_write_full(int fd, const void *buf, size_t count) +{ + ssize_t ret; + size_t total = 0; + + while (count) { + ret = write(fd, buf, count); + if (ret < 0) { + if (errno == EINTR) + continue; + break; + } + + count -= ret; + buf += ret; + total += ret; + } + + return total; +} + #ifndef _WIN32 /* * Creates a pipe with FD_CLOEXEC set on both file descriptors diff --git a/qemu-common.h b/qemu-common.h index 8630f8c..7231348 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -160,6 +160,8 @@ void qemu_mutex_lock_iothread(void); void qemu_mutex_unlock_iothread(void); int qemu_open(const char *name, int flags, ...); +size_t qemu_write_full(int fd, const void *buf, size_t count) + __attribute__ ((warn_unused_result)); void qemu_set_cloexec(int fd); #ifndef _WIN32 -- 1.6.5.7