From patchwork Thu Dec 31 01:33:11 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 41946 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1E42C1007D2 for ; Thu, 31 Dec 2009 12:34:24 +1100 (EST) Received: from localhost ([127.0.0.1]:40931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NQ9vg-0005xa-QS for incoming@patchwork.ozlabs.org; Wed, 30 Dec 2009 20:34:20 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NQ9uw-0005mb-3U for qemu-devel@nongnu.org; Wed, 30 Dec 2009 20:33:34 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NQ9ur-0005ki-E1 for qemu-devel@nongnu.org; Wed, 30 Dec 2009 20:33:33 -0500 Received: from [199.232.76.173] (port=40607 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NQ9ur-0005kf-Bp for qemu-devel@nongnu.org; Wed, 30 Dec 2009 20:33:29 -0500 Received: from mail-fx0-f222.google.com ([209.85.220.222]:55327) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NQ9ur-00031j-0T for qemu-devel@nongnu.org; Wed, 30 Dec 2009 20:33:29 -0500 Received: by fxm22 with SMTP id 22so15607316fxm.2 for ; Wed, 30 Dec 2009 17:33:27 -0800 (PST) Received: by 10.223.82.23 with SMTP id z23mr4947322fak.9.1262223206977; Wed, 30 Dec 2009 17:33:26 -0800 (PST) Received: from localhost.localdomain (a88-114-220-92.elisa-laajakaista.fi [88.114.220.92]) by mx.google.com with ESMTPS id 18sm20568832fks.34.2009.12.30.17.33.25 (version=SSLv3 cipher=RC4-MD5); Wed, 30 Dec 2009 17:33:25 -0800 (PST) From: "Kirill A. Shutemov" To: qemu-devel@nongnu.org Date: Thu, 31 Dec 2009 03:33:11 +0200 Message-Id: <1262223199-19062-1-git-send-email-kirill@shutemov.name> X-Mailer: git-send-email 1.6.5.7 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) Cc: "Kirill A. Shutemov" Subject: [Qemu-devel] [PATCH 01/14] Introduce qemu_write_full() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org A variant of write(2) which handles partial write. Signed-off-by: Kirill A. Shutemov --- osdep.c | 27 +++++++++++++++++++++++++++ qemu-common.h | 1 + 2 files changed, 28 insertions(+), 0 deletions(-) diff --git a/osdep.c b/osdep.c index e4836e7..d2406f2 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. + */ +ssize_t qemu_write_full(int fd, const void *buf, size_t count) +{ + ssize_t ret = 0; + ssize_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..a8144cb 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -160,6 +160,7 @@ void qemu_mutex_lock_iothread(void); void qemu_mutex_unlock_iothread(void); int qemu_open(const char *name, int flags, ...); +ssize_t qemu_write_full(int fd, const void *buf, size_t count); void qemu_set_cloexec(int fd); #ifndef _WIN32