[Libguestfs] [PATCH 1/4] lib: qemu: Refactor guestfs_int_test_qemu so it doesn't return qemu version.

Richard W.M. Jones rjones at redhat.com
Mon Sep 11 18:24:15 UTC 2017


Rather unnecessarily this function returned the parsed qemu version.
This complicates further refactoring, so I have changed the function
not to return this, and instead there is a separate function you have
to call to get the version struct (‘guestfs_int_qemu_version’).

Apart from a tiny amount of extra copying this is simply refactoring
of the interface between the direct-mode backend and the qemu query
functions.
---
 lib/guestfs-internal.h |  3 ++-
 lib/launch-direct.c    |  3 ++-
 lib/qemu.c             | 29 +++++++++++++++++++----------
 3 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/lib/guestfs-internal.h b/lib/guestfs-internal.h
index 7269fbeba..2ca258cb3 100644
--- a/lib/guestfs-internal.h
+++ b/lib/guestfs-internal.h
@@ -979,7 +979,8 @@ void guestfs_int_init_unix_backend (void) __attribute__((constructor));
 
 /* qemu.c */
 struct qemu_data;
-extern struct qemu_data *guestfs_int_test_qemu (guestfs_h *g, struct version *qemu_version);
+extern struct qemu_data *guestfs_int_test_qemu (guestfs_h *g);
+extern struct version guestfs_int_qemu_version (guestfs_h *g, struct qemu_data *);
 extern int guestfs_int_qemu_supports (guestfs_h *g, const struct qemu_data *, const char *option);
 extern int guestfs_int_qemu_supports_device (guestfs_h *g, const struct qemu_data *, const char *device_name);
 extern char *guestfs_int_drive_source_qemu_param (guestfs_h *g, const struct drive_source *src);
diff --git a/lib/launch-direct.c b/lib/launch-direct.c
index 3b848165c..685e42468 100644
--- a/lib/launch-direct.c
+++ b/lib/launch-direct.c
@@ -402,9 +402,10 @@ launch_direct (guestfs_h *g, void *datav, const char *arg)
 
   /* Get qemu help text and version. */
   if (data->qemu_data == NULL) {
-    data->qemu_data = guestfs_int_test_qemu (g, &data->qemu_version);
+    data->qemu_data = guestfs_int_test_qemu (g);
     if (data->qemu_data == NULL)
       goto cleanup0;
+    data->qemu_version = guestfs_int_qemu_version (g, data->qemu_data);
   }
 
   /* Using virtio-serial, we need to create a local Unix domain socket
diff --git a/lib/qemu.c b/lib/qemu.c
index 41098a20b..b4f4c84f6 100644
--- a/lib/qemu.c
+++ b/lib/qemu.c
@@ -46,9 +46,12 @@
 struct qemu_data {
   char *qemu_help;              /* Output of qemu -help. */
   char *qemu_devices;           /* Output of qemu -device ? */
+
+  /* The following fields are derived from the fields above. */
+  struct version qemu_version;  /* Parsed qemu version number. */
 };
 
-static int test_qemu (guestfs_h *g, struct qemu_data *data, struct version *qemu_version);
+static int test_qemu (guestfs_h *g, struct qemu_data *data);
 static void parse_qemu_version (guestfs_h *g, const char *, struct version *qemu_version);
 static void read_all (guestfs_h *g, void *retv, const char *buf, size_t len);
 
@@ -64,14 +67,11 @@ static void read_all (guestfs_h *g, void *retv, const char *buf, size_t len);
  * the version of qemu what options this qemu supports, and
  * C<qemu -device ?> so we know what devices are available.
  *
- * The version number of qemu (from the C<-help> output) is saved in
- * C<&qemu_version>.
- *
  * This caches the results in the cachedir so that as long as the qemu
  * binary does not change, calling this is effectively free.
  */
 struct qemu_data *
-guestfs_int_test_qemu (guestfs_h *g, struct version *qemu_version)
+guestfs_int_test_qemu (guestfs_h *g)
 {
   struct qemu_data *data;
   struct stat statbuf;
@@ -128,7 +128,7 @@ guestfs_int_test_qemu (guestfs_h *g, struct version *qemu_version)
       return NULL;
     }
 
-    parse_qemu_version (g, data->qemu_help, qemu_version);
+    parse_qemu_version (g, data->qemu_help, &data->qemu_version);
 
     if (guestfs_int_read_whole_file (g, qemu_devices_filename,
                                      &data->qemu_devices, NULL) == -1) {
@@ -142,11 +142,13 @@ guestfs_int_test_qemu (guestfs_h *g, struct version *qemu_version)
  do_test:
   data = safe_calloc (g, 1, sizeof *data);
 
-  if (test_qemu (g, data, qemu_version) == -1) {
+  if (test_qemu (g, data) == -1) {
     guestfs_int_free_qemu_data (data);
     return NULL;
   }
 
+  parse_qemu_version (g, data->qemu_help, &data->qemu_version);
+
   /* Now memoize the qemu output in the cache directory. */
   debug (g, "saving test results");
 
@@ -203,7 +205,7 @@ guestfs_int_test_qemu (guestfs_h *g, struct version *qemu_version)
 }
 
 static int
-test_qemu (guestfs_h *g, struct qemu_data *data, struct version *qemu_version)
+test_qemu (guestfs_h *g, struct qemu_data *data)
 {
   CLEANUP_CMD_CLOSE struct command *cmd1 = guestfs_int_new_command (g);
   CLEANUP_CMD_CLOSE struct command *cmd2 = guestfs_int_new_command (g);
@@ -219,8 +221,6 @@ test_qemu (guestfs_h *g, struct qemu_data *data, struct version *qemu_version)
   if (r == -1 || !WIFEXITED (r) || WEXITSTATUS (r) != 0)
     goto error;
 
-  parse_qemu_version (g, data->qemu_help, qemu_version);
-
   guestfs_int_cmd_add_arg (cmd2, g->hv);
   guestfs_int_cmd_add_arg (cmd2, "-display");
   guestfs_int_cmd_add_arg (cmd2, "none");
@@ -278,6 +278,15 @@ read_all (guestfs_h *g, void *retv, const char *buf, size_t len)
 }
 
 /**
+ * Return the parsed version of qemu.
+ */
+struct version
+guestfs_int_qemu_version (guestfs_h *g, struct qemu_data *data)
+{
+  return data->qemu_version;
+}
+
+/**
  * Test if option is supported by qemu command line (just by grepping
  * the help text).
  */
-- 
2.13.2




More information about the Libguestfs mailing list