diff mbox

[01/15] Introduce qemu_write_full()

Message ID 1262403933-26881-1-git-send-email-kirill@shutemov.name
State New
Headers show

Commit Message

Kirill A . Shutemov Jan. 2, 2010, 3:45 a.m. UTC
A variant of write(2) which handles partial write.

Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
---
 osdep.c       |   27 +++++++++++++++++++++++++++
 qemu-common.h |    2 ++
 2 files changed, 29 insertions(+), 0 deletions(-)

Comments

Paolo Bonzini Jan. 2, 2010, 10:34 a.m. UTC | #1
On 01/02/2010 04:45 AM, Kirill A. Shutemov wrote:
> A variant of write(2) which handles partial write.

Next time please add a cover letter 00/15 to explain what you changed, 
and a v3 (or v4?) marker in the subjects.  Thanks for this work though!

Paolo
diff mbox

Patch

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