New API: pwrite-device
authorRichard W.M. Jones <rjones@redhat.com>
Sun, 26 Sep 2010 17:00:11 +0000 (18:00 +0100)
committerRichard W.M. Jones <rjones@redhat.com>
Sun, 26 Sep 2010 21:21:36 +0000 (22:21 +0100)
This is the same as the existing 'pwrite' API call, but allows you
to write to a device.

daemon/file.c
generator/generator_actions.ml
src/MAX_PROC_NR

index 0849456..9403100 100644 (file)
@@ -463,11 +463,32 @@ do_pread (const char *path, int count, int64_t offset, size_t *size_r)
   return buf;
 }
 
   return buf;
 }
 
+static int
+pwrite_fd (int fd, const char *content, size_t size, int64_t offset,
+           const char *display_path)
+{
+  ssize_t r;
+
+  r = pwrite (fd, content, size, offset);
+  if (r == -1) {
+    reply_with_perror ("pwrite: %s", display_path);
+    close (fd);
+    return -1;
+  }
+
+  if (close (fd) == -1) {
+    reply_with_perror ("close: %s", display_path);
+    close (fd);
+    return -1;
+  }
+
+  return r;
+}
+
 int
 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
 {
   int fd;
 int
 do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
 {
   int fd;
-  ssize_t r;
 
   if (offset < 0) {
     reply_with_error ("offset is negative");
 
   if (offset < 0) {
     reply_with_error ("offset is negative");
@@ -483,20 +504,25 @@ do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
     return -1;
   }
 
     return -1;
   }
 
-  r = pwrite (fd, content, size, offset);
-  if (r == -1) {
-    reply_with_perror ("pwrite: %s", path);
-    close (fd);
+  return pwrite_fd (fd, content, size, offset, path);
+}
+
+int
+do_pwrite_device (const char *device, const char *content, size_t size,
+                  int64_t offset)
+{
+  if (offset < 0) {
+    reply_with_error ("offset is negative");
     return -1;
   }
 
     return -1;
   }
 
-  if (close (fd) == -1) {
-    reply_with_perror ("close: %s", path);
-    close (fd);
+  int fd = open (device, O_WRONLY);
+  if (fd == -1) {
+    reply_with_perror ("open: %s", device);
     return -1;
   }
 
     return -1;
   }
 
-  return r;
+  return pwrite_fd (fd, content, size, offset, device);
 }
 
 /* This runs the 'file' command. */
 }
 
 /* This runs the 'file' command. */
index ac8dab2..e94fcbd 100644 (file)
@@ -4812,7 +4812,7 @@ 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.
 
 to the file.  This could even be 0, although short writes are
 unlikely for regular files in ordinary circumstances.
 
-See also C<guestfs_pread>.");
+See also C<guestfs_pread>, C<guestfs_pwrite_device>.");
 
   ("resize2fs_size", (RErr, [Device "device"; Int64 "size"]), 248, [],
    [],
 
   ("resize2fs_size", (RErr, [Device "device"; Int64 "size"]), 248, [],
    [],
@@ -5166,6 +5166,23 @@ error occurs.
 
 See also C<guestfs_download>, C<guestfs_pread>.");
 
 
 See also C<guestfs_download>, C<guestfs_pread>.");
 
+  ("pwrite_device", (RInt "nbytes", [Device "device"; BufferIn "content"; Int64 "offset"]), 275, [ProtocolLimitWarning],
+   [InitPartition, Always, TestOutputList (
+      [["pwrite_device"; "/dev/sda"; "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"; "446"];
+       ["blockdev_rereadpt"; "/dev/sda"];
+       ["list_partitions"]], [])],
+   "write to part of a device",
+   "\
+This command writes to part of a device.  It writes the data
+buffer C<content> to C<device> 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
+(although short writes to disk devices and partitions are
+probably impossible with standard Linux kernels).
+
+See also C<guestfs_pwrite>.");
+
 ]
 
 let all_functions = non_daemon_functions @ daemon_functions
 ]
 
 let all_functions = non_daemon_functions @ daemon_functions
index d4d5a4b..4c738e3 100644 (file)
@@ -1 +1 @@
-274
+275