[Pkg-libvirt-commits] [libguestfs] 64/233: launch: libvirt: Slightly simplify the XML generation code further.

Hilko Bengen bengen at moszumanska.debian.org
Wed Feb 19 21:10:55 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch experimental
in repository libguestfs.

commit 937c0313a070ab9e9b7d75ffd8e509412ca7d755
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Sat Jan 18 11:40:30 2014 +0000

    launch: libvirt: Slightly simplify the XML generation code further.
    
    This updates commit 96737fc5b7a0c9f92896f4a7d0f738a66c56d3b0.
---
 src/launch-libvirt.c | 74 +++++++++++++++++++++++++---------------------------
 1 file changed, 35 insertions(+), 39 deletions(-)

diff --git a/src/launch-libvirt.c b/src/launch-libvirt.c
index b9890a7..d6dafe8 100644
--- a/src/launch-libvirt.c
+++ b/src/launch-libvirt.c
@@ -837,9 +837,17 @@ static int construct_libvirt_xml_appliance (guestfs_h *g, const struct libvirt_x
 
 /* key=value attribute of the current element. */
 #define attribute(key,value)                                            \
-  if (xmlTextWriterWriteAttribute (xo, BAD_CAST (key), BAD_CAST (value)) == -1) { \
-    xml_error ("xmlTextWriterWriteAttribute"); \
-    return -1;                                 \
+  if (xmlTextWriterWriteAttribute (xo, BAD_CAST (key), BAD_CAST (value)) == -1){\
+    xml_error ("xmlTextWriterWriteAttribute");                          \
+    return -1;                                                          \
+  }
+
+/* key=value, but value is a printf-style format string. */
+#define attribute_format(key,fs,...)                                    \
+  if (xmlTextWriterWriteFormatAttribute (xo, BAD_CAST (key),            \
+                                         fs, ##__VA_ARGS__) == -1) {    \
+    xml_error ("xmlTextWriterWriteFormatAttribute");                    \
+    return -1;                                                          \
   }
 
 /* attribute with namespace. */
@@ -851,13 +859,15 @@ static int construct_libvirt_xml_appliance (guestfs_h *g, const struct libvirt_x
     return -1;                                                          \
   }
 
-#define write_string(str)                                               \
+/* A string, eg. within an element. */
+#define string(str)                                                     \
   if (xmlTextWriterWriteString (xo, BAD_CAST (str)) == -1) {            \
     xml_error ("xmlTextWriterWriteString");                             \
     return -1;                                                          \
   }
 
-#define write_format_string(fs,...)                                     \
+/* A string, using printf-style formatting. */
+#define string_format(fs,...)                                           \
   if (xmlTextWriterWriteFormatString (xo, fs, ##__VA_ARGS__) == -1) {   \
     xml_error ("xmlTextWriterWriteFormatString");                       \
     return -1;                                                          \
@@ -955,7 +965,7 @@ construct_libvirt_xml_name (guestfs_h *g,
                             xmlTextWriterPtr xo)
 {
   start_element ("name") {
-    write_string (params->data->name);
+    string (params->data->name);
   } end_element ();
 
   return 0;
@@ -969,12 +979,12 @@ construct_libvirt_xml_cpu (guestfs_h *g,
 {
   start_element ("memory") {
     attribute ("unit", "MiB");
-    write_format_string ("%d", g->memsize);
+    string_format ("%d", g->memsize);
   } end_element ();
 
   start_element ("currentMemory") {
     attribute ("unit", "MiB");
-    write_format_string ("%d", g->memsize);
+    string_format ("%d", g->memsize);
   } end_element ();
 
 #ifndef __arm__
@@ -994,7 +1004,7 @@ construct_libvirt_xml_cpu (guestfs_h *g,
 #endif
 
   start_element ("vcpu") {
-    write_format_string ("%d", g->smp);
+    string_format ("%d", g->smp);
   } end_element ();
 
   start_element ("clock") {
@@ -1028,25 +1038,25 @@ construct_libvirt_xml_boot (guestfs_h *g,
 #ifdef MACHINE_TYPE
       attribute ("machine", MACHINE_TYPE);
 #endif
-      write_string ("hvm");
+      string ("hvm");
     } end_element ();
 
     start_element ("kernel") {
-      write_string (params->kernel);
+      string (params->kernel);
     } end_element ();
 
     if (params->dtb) {
       start_element ("dtb") {
-        write_string (params->dtb);
+        string (params->dtb);
       } end_element ();
     }
 
     start_element ("initrd") {
-      write_string (params->initrd);
+      string (params->initrd);
     } end_element ();
 
     start_element ("cmdline") {
-      write_string (cmdline);
+      string (cmdline);
     } end_element ();
 
   } end_element ();
@@ -1075,10 +1085,10 @@ construct_libvirt_xml_seclabel (guestfs_h *g,
       attribute ("model", "selinux");
       attribute ("relabel", "yes");
       start_element ("label") {
-        write_string (params->data->selinux_label);
+        string (params->data->selinux_label);
       } end_element ();
       start_element ("imagelabel") {
-        write_string (params->data->selinux_imagelabel);
+        string (params->data->selinux_imagelabel);
       } end_element ();
     } end_element ();
   }
@@ -1093,7 +1103,7 @@ construct_libvirt_xml_lifecycle (guestfs_h *g,
                                  xmlTextWriterPtr xo)
 {
   start_element ("on_reboot") {
-    write_string ("destroy");
+    string ("destroy");
   } end_element ();
 
   return 0;
@@ -1115,7 +1125,7 @@ construct_libvirt_xml_devices (guestfs_h *g,
      */
     if (is_custom_hv (g)) {
       start_element ("emulator") {
-        write_string (g->hv);
+        string (g->hv);
       } end_element ();
     }
 #ifdef __arm__
@@ -1124,7 +1134,7 @@ construct_libvirt_xml_devices (guestfs_h *g,
      */
     else {
       start_element ("emulator") {
-        write_string (QEMU);
+        string (QEMU);
       } end_element ();
     }
 #endif
@@ -1358,7 +1368,7 @@ construct_libvirt_xml_disk (guestfs_h *g,
 
     if (drv->disk_label) {
       start_element ("serial") {
-        write_string (drv->disk_label);
+        string (drv->disk_label);
       } end_element ();
     }
 
@@ -1404,15 +1414,11 @@ static int
 construct_libvirt_xml_disk_address (guestfs_h *g, xmlTextWriterPtr xo,
                                     size_t drv_index)
 {
-  char scsi_target[64];
-
-  snprintf (scsi_target, sizeof scsi_target, "%zu", drv_index);
-
   start_element ("address") {
     attribute ("type", "drive");
     attribute ("controller", "0");
     attribute ("bus", "0");
-    attribute ("target", scsi_target);
+    attribute_format ("target", "%zu", drv_index);
     attribute ("unit", "0");
   } end_element ();
 
@@ -1431,25 +1437,15 @@ construct_libvirt_xml_disk_source_hosts (guestfs_h *g,
       switch (src->servers[i].transport) {
       case drive_transport_none:
       case drive_transport_tcp: {
-        const char *hostname = src->servers[i].u.hostname;
-        int port = src->servers[i].port;
-
-        attribute ("name", hostname);
-
-        if (port > 0) {
-          char port_str[64];
-
-          snprintf (port_str, sizeof port_str, "%d", port);
-          attribute ("port", port_str);
-        }
+        attribute ("name", src->servers[i].u.hostname);
+        if (src->servers[i].port > 0)
+          attribute_format ("port", "%d", src->servers[i].port);
         break;
       }
 
       case drive_transport_unix: {
-        const char *socket = src->servers[i].u.socket;
-
         attribute ("transport", "unix");
-        attribute ("socket", socket);
+        attribute ("socket", src->servers[i].u.socket);
         break;
       }
       }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list