[Libguestfs] [PATCH 2/4] Add progress messages to copy-size command.

Richard W.M. Jones rjones at redhat.com
Sat Aug 28 12:03:38 UTC 2010


This is an example of how to add progress notifications to one of the
daemon commands.  Item (1) on the to-do list is to go through the
remaining daemon commands and add progress notifications to the easy
ones.  (Item (94c) is to add it to the hard ones ...)

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
New in Fedora 11: Fedora Windows cross-compiler. Compile Windows
programs, test, and build Windows installers. Over 70 libraries supprt'd
http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
-------------- next part --------------
>From 2d790f65c6591ab751641b59ce27389edde88b64 Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at redhat.com>
Date: Sat, 28 Aug 2010 11:11:32 +0100
Subject: [PATCH 2/4] Add progress messages to copy-size command.

---
 daemon/dd.c      |   19 +++++++++++++++----
 src/generator.ml |   11 ++++++++++-
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/daemon/dd.c b/daemon/dd.c
index 2bbe855..c004d92 100644
--- a/daemon/dd.c
+++ b/daemon/dd.c
@@ -73,7 +73,7 @@ do_dd (const char *src, const char *dest)
 }
 
 int
-do_copy_size (const char *src, const char *dest, int64_t size)
+do_copy_size (const char *src, const char *dest, int64_t ssize)
 {
   char *buf;
   int src_fd, dest_fd;
@@ -112,9 +112,19 @@ do_copy_size (const char *src, const char *dest, int64_t size)
     return -1;
   }
 
-  while (size > 0) {
+  uint64_t position = 0, size = (uint64_t) ssize;
+
+  while (position < size) {
     char buf[1024*1024];
-    size_t n = size > (int64_t) (sizeof buf) ? sizeof buf : (size_t) size;
+
+    /* Calculate bytes to copy. */
+    uint64_t n64 = size - position;
+    size_t n;
+    if (n64 > sizeof buf)
+      n = sizeof buf;
+    else
+      n = (size_t) n64; /* safe because of if condition */
+
     ssize_t r = read (src_fd, buf, n);
     if (r == -1) {
       reply_with_perror ("%s: read", src);
@@ -136,7 +146,8 @@ do_copy_size (const char *src, const char *dest, int64_t size)
       return -1;
     }
 
-    size -= r;
+    position += r;
+    notify_progress ((uint64_t) position, (uint64_t) size);
   }
 
   if (close (src_fd) == -1) {
diff --git a/src/generator.ml b/src/generator.ml
index bbf313a..ec93850 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -192,6 +192,7 @@ type flags =
   | NotInDocs		  (* do not add this function to documentation *)
   | DeprecatedBy of string (* function is deprecated, use .. instead *)
   | Optional of string	  (* function is part of an optional group *)
+  | Progress              (* function can generate progress messages *)
 
 and fish_output_t =
   | FishOutputOctal       (* for int return, print in octal *)
@@ -4875,7 +4876,7 @@ calls to associate logical volumes and volume groups.
 
 See also C<guestfs_vgpvuuids>.");
 
-  ("copy_size", (RErr, [Dev_or_Path "src"; Dev_or_Path "dest"; Int64 "size"]), 227, [],
+  ("copy_size", (RErr, [Dev_or_Path "src"; Dev_or_Path "dest"; Int64 "size"]), 227, [Progress],
    [InitBasicFS, Always, TestOutputBuffer (
       [["write"; "/src"; "hello, world"];
        ["copy_size"; "/src"; "/dest"; "5"];
@@ -5799,6 +5800,12 @@ let seq_of_test = function
   | TestLastFail s -> s
 
 (* Handling for function flags. *)
+let progress_message =
+  "This long-running command can generate progress notification messages
+so that the caller can display a progress bar or indicator.
+To receive these messages, the caller must register a progress
+callback.  See L<guestfs(3)/guestfs_set_progress_callback>."
+
 let protocol_limit_warning =
   "Because of the message protocol, there is a transfer limit
 of somewhere between 2MB and 4MB.  See L<guestfs(3)/PROTOCOL LIMITS>."
@@ -6133,6 +6140,8 @@ I<The caller must free the strings and the array after use>.\n\n"
 The size of the returned buffer is written to C<*size_r>.
 I<The caller must free the returned buffer after use>.\n\n"
         );
+        if List.mem Progress flags then
+          pr "%s\n\n" progress_message;
         if List.mem ProtocolLimitWarning flags then
           pr "%s\n\n" protocol_limit_warning;
         if List.mem DangerWillRobinson flags then
-- 
1.7.1



More information about the Libguestfs mailing list