Patchwork [01/17] Introduce qemu_write_full()

login
register
mail settings
Submitter Juan Quintela
Date Jan. 20, 2010, 8:13 p.m.
Message ID <46b00fd53656455ae5847ca4584da47bc0cef21c.1264017981.git.quintela@redhat.com>
Download mbox | patch
Permalink /patch/43345/
State New
Headers show

Comments

Juan Quintela - Jan. 20, 2010, 8:13 p.m.
From: Kirill A. Shutemov <kirill@shutemov.name>

A variant of write(2) which handles partial write.

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

Patch

diff --git a/osdep.c b/osdep.c
index 1310684..c5d608e 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 written = 0;
+    const char *mem = buf;
+
+    while (count) {
+        ssize_t ret = write(fd, mem, count);
+        if (ret < 0) {
+            if (errno == EINTR || errno == EAGAIN)
+                continue;
+            break;
+        }
+
+        count -= ret;
+        mem += ret;
+        written += ret;
+    }
+
+    return written;
+}
+
 #ifndef _WIN32
 /*
  * Creates a pipe with FD_CLOEXEC set on both file descriptors
diff --git a/qemu-common.h b/qemu-common.h
index d96060a..a2731e1 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -161,6 +161,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