[Libguestfs] [PATCH 5/5] Add progress notification messages to upload and upload-offset APIs.

Richard W.M. Jones rjones at redhat.com
Wed Dec 1 14:04:52 UTC 2010


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://et.redhat.com/~rjones/virt-df/
-------------- next part --------------
>From 8022d46e5e2d9c3ab664ace6c9f185976e34dc20 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Wed, 1 Dec 2010 13:33:31 +0000
Subject: [PATCH 5/5] Add progress notification messages to upload and upload-offset APIs.

---
 daemon/upload.c                |   39 ++++++++++++++++++++++++++++-----------
 generator/generator_actions.ml |    4 ++--
 2 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/daemon/upload.c b/daemon/upload.c
index c026af8..e28bf96 100644
--- a/daemon/upload.c
+++ b/daemon/upload.c
@@ -31,25 +31,42 @@
 #include "daemon.h"
 #include "actions.h"
 
+struct write_cb_data {
+  int fd;                       /* file descriptor */
+  uint64_t written;             /* bytes written so far */
+};
+
 static int
-write_cb (void *fd_ptr, const void *buf, size_t len)
+write_cb (void *data_vp, const void *buf, size_t len)
 {
-  int fd = *(int *)fd_ptr;
-  return xwrite (fd, buf, len);
+  struct write_cb_data *data = data_vp;
+  int r;
+
+  r = xwrite (data->fd, buf, len);
+  if (r == -1)
+    return -1;
+
+  data->written += len;
+
+  if (progress_hint > 0)
+    notify_progress (data->written, progress_hint);
+
+  return 0;
 }
 
 /* Has one FileIn parameter. */
 static int
 upload (const char *filename, int flags, int64_t offset)
 {
-  int err, fd, r, is_dev;
+  struct write_cb_data data = { .written = 0 };
+  int err, r, is_dev;
 
   is_dev = STRPREFIX (filename, "/dev/");
 
   if (!is_dev) CHROOT_IN;
-  fd = open (filename, flags, 0666);
+  data.fd = open (filename, flags, 0666);
   if (!is_dev) CHROOT_OUT;
-  if (fd == -1) {
+  if (data.fd == -1) {
     err = errno;
     r = cancel_receive ();
     errno = err;
@@ -58,7 +75,7 @@ upload (const char *filename, int flags, int64_t offset)
   }
 
   if (offset) {
-    if (lseek (fd, offset, SEEK_SET) == -1) {
+    if (lseek (data.fd, offset, SEEK_SET) == -1) {
       err = errno;
       r = cancel_receive ();
       errno = err;
@@ -67,22 +84,22 @@ upload (const char *filename, int flags, int64_t offset)
     }
   }
 
-  r = receive_file (write_cb, &fd);
+  r = receive_file (write_cb, &data.fd);
   if (r == -1) {		/* write error */
     err = errno;
     r = cancel_receive ();
     errno = err;
     if (r != -2) reply_with_error ("write error: %s", filename);
-    close (fd);
+    close (data.fd);
     return -1;
   }
   if (r == -2) {		/* cancellation from library */
-    close (fd);
+    close (data.fd);
     /* Do NOT send any error. */
     return -1;
   }
 
-  if (close (fd) == -1) {
+  if (close (data.fd) == -1) {
     err = errno;
     if (r == -1)                /* if r == 0, file transfer ended already */
       r = cancel_receive ();
diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml
index 0d08f73..a405fd4 100644
--- a/generator/generator_actions.ml
+++ b/generator/generator_actions.ml
@@ -2359,7 +2359,7 @@ Reread the partition table on C<device>.
 
 This uses the L<blockdev(8)> command.");
 
-  ("upload", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"], []), 66, [],
+  ("upload", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"], []), 66, [Progress],
    [InitScratchFS, Always, TestOutput (
       (* Pick a file from cwd which isn't likely to change. *)
       [["mkdir"; "/upload"];
@@ -5510,7 +5510,7 @@ removes the partition number, returning the device name
 The named partition must exist, for example as a string returned
 from C<guestfs_list_partitions>.");
 
-  ("upload_offset", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"; Int64 "offset"], []), 273, [],
+  ("upload_offset", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"; Int64 "offset"], []), 273, [Progress],
    (let md5 = Digest.to_hex (Digest.file "COPYING.LIB") in
     [InitScratchFS, Always, TestOutput (
        [["upload_offset"; "../COPYING.LIB"; "/upload_offset"; "0"];
-- 
1.7.3.2



More information about the Libguestfs mailing list