[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH] Update pwrite to write a full buffer



This patch changes pwrite such that it will never return a short write. This
is a backwards compatible API change, as checking for short writes remains
correct, but redundant.
---
 daemon/file.c    |   20 +++++++++++++-------
 src/generator.ml |    6 ------
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/daemon/file.c b/daemon/file.c
index da899b6..9ce0783 100644
--- a/daemon/file.c
+++ b/daemon/file.c
@@ -501,7 +501,6 @@ int
 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
 {
   int fd;
-  ssize_t r;
 
   CHROOT_IN;
   fd = open (path, O_WRONLY);
@@ -512,11 +511,18 @@ do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
     return -1;
   }
 
-  r = pwrite (fd, content, size, offset);
-  if (r == -1) {
-    reply_with_perror ("pwrite: %s", path);
-    close (fd);
-    return -1;
+  size_t written = 0;
+  while (written < size) {
+      ssize_t r = pwrite (fd, content, size - written, offset);
+
+      if (r == -1) {
+        reply_with_perror ("pwrite: %s", path);
+        close (fd);
+        return -1;
+      }
+
+      offset += r;
+      written += r;
   }
 
   if (close (fd) == -1) {
@@ -525,7 +531,7 @@ do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
     return -1;
   }
 
-  return r;
+  return size;
 }
 
 /* This runs the 'file' command. */
diff --git a/src/generator.ml b/src/generator.ml
index dc072ef..7dcd7b4 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -5120,12 +5120,6 @@ file is the string C<content> (which can contain any 8 bit data).");
 This command writes to part of a file.  It writes the data
 buffer C<content> to the file C<path> starting at offset C<offset>.
 
-This command implements the L<pwrite(2)> system call, and like
-that system call it may not write the full data requested.  The
-return value is the number of bytes that were actually written
-to the file.  This could even be 0, although short writes are
-unlikely for regular files in ordinary circumstances.
-
 See also C<guestfs_pread>.");
 
   ("resize2fs_size", (RErr, [Device "device"; Int64 "size"]), 248, [],
-- 
1.7.2.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]