Blame SOURCES/0135-v2v-domainxml-factor-out-connect-and-pool-loading.patch

ffd6ed
From a71a1ecad6aa3b64616b35ba2bc2a671db80f1d5 Mon Sep 17 00:00:00 2001
ffd6ed
From: Pino Toscano <ptoscano@redhat.com>
ffd6ed
Date: Tue, 14 Apr 2015 15:16:01 +0200
ffd6ed
Subject: [PATCH] v2v: domainxml: factor out connect and pool loading
ffd6ed
ffd6ed
Factor out the connection and pool loading out of v2v_pool_dumpxml, so
ffd6ed
it can be used in later implementations requiring a pool.
ffd6ed
ffd6ed
Should be just code motion.
ffd6ed
ffd6ed
(cherry picked from commit 9001f61a402dc1820fd4c441e5cc78197c39b73b)
ffd6ed
---
ffd6ed
 v2v/domainxml-c.c | 101 ++++++++++++++++++++++++++++++++----------------------
ffd6ed
 1 file changed, 60 insertions(+), 41 deletions(-)
ffd6ed
ffd6ed
diff --git a/v2v/domainxml-c.c b/v2v/domainxml-c.c
ffd6ed
index c9ed8c5..ba2a5bc 100644
ffd6ed
--- a/v2v/domainxml-c.c
ffd6ed
+++ b/v2v/domainxml-c.c
ffd6ed
@@ -106,6 +106,63 @@ libvirt_auth_default_wrapper (virConnectCredentialPtr cred,
ffd6ed
   }
ffd6ed
 }
ffd6ed
 
ffd6ed
+virStoragePoolPtr
ffd6ed
+connect_and_load_pool (value connv, value poolnamev)
ffd6ed
+{
ffd6ed
+  CAMLparam2 (connv, poolnamev);
ffd6ed
+  const char *conn_uri = NULL;
ffd6ed
+  const char *poolname;
ffd6ed
+  /* We have to assemble the error on the stack because a dynamic
ffd6ed
+   * string couldn't be freed.
ffd6ed
+   */
ffd6ed
+  char errmsg[256];
ffd6ed
+  virErrorPtr err;
ffd6ed
+  virConnectPtr conn;
ffd6ed
+  virStoragePoolPtr pool;
ffd6ed
+
ffd6ed
+  if (connv != Val_int (0))
ffd6ed
+    conn_uri = String_val (Field (connv, 0)); /* Some conn */
ffd6ed
+
ffd6ed
+  /* We have to call the default authentication handler, not least
ffd6ed
+   * since it handles all the PolicyKit crap.  However it also makes
ffd6ed
+   * coding this simpler.
ffd6ed
+   */
ffd6ed
+  conn = virConnectOpenAuth (conn_uri, virConnectAuthPtrDefault,
ffd6ed
+                             VIR_CONNECT_RO);
ffd6ed
+  if (conn == NULL) {
ffd6ed
+    if (conn_uri)
ffd6ed
+      snprintf (errmsg, sizeof errmsg,
ffd6ed
+                _("cannot open libvirt connection '%s'"), conn_uri);
ffd6ed
+    else
ffd6ed
+      snprintf (errmsg, sizeof errmsg, _("cannot open libvirt connection"));
ffd6ed
+    caml_invalid_argument (errmsg);
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  /* Suppress default behaviour of printing errors to stderr.  Note
ffd6ed
+   * you can't set this to NULL to ignore errors; setting it to NULL
ffd6ed
+   * restores the default error handler ...
ffd6ed
+   */
ffd6ed
+  virConnSetErrorFunc (conn, NULL, ignore_errors);
ffd6ed
+
ffd6ed
+  /* Look up the pool. */
ffd6ed
+  poolname = String_val (poolnamev);
ffd6ed
+
ffd6ed
+  pool = virStoragePoolLookupByUUIDString (conn, poolname);
ffd6ed
+
ffd6ed
+  if (!pool)
ffd6ed
+    pool = virStoragePoolLookupByName (conn, poolname);
ffd6ed
+
ffd6ed
+  if (!pool) {
ffd6ed
+    err = virGetLastError ();
ffd6ed
+    snprintf (errmsg, sizeof errmsg,
ffd6ed
+              _("cannot find libvirt pool '%s': %s"), poolname, err->message);
ffd6ed
+    virConnectClose (conn);
ffd6ed
+    caml_invalid_argument (errmsg);
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  CAMLreturnT (virStoragePoolPtr, pool);
ffd6ed
+}
ffd6ed
+
ffd6ed
 value
ffd6ed
 v2v_dumpxml (value passwordv, value connv, value domnamev)
ffd6ed
 {
ffd6ed
@@ -216,8 +273,6 @@ v2v_pool_dumpxml (value connv, value poolnamev)
ffd6ed
 {
ffd6ed
   CAMLparam2 (connv, poolnamev);
ffd6ed
   CAMLlocal1 (retv);
ffd6ed
-  const char *conn_uri = NULL;
ffd6ed
-  const char *poolname;
ffd6ed
   /* We have to assemble the error on the stack because a dynamic
ffd6ed
    * string couldn't be freed.
ffd6ed
    */
ffd6ed
@@ -227,52 +282,16 @@ v2v_pool_dumpxml (value connv, value poolnamev)
ffd6ed
   virStoragePoolPtr pool;
ffd6ed
   char *xml;
ffd6ed
 
ffd6ed
-  if (connv != Val_int (0))
ffd6ed
-    conn_uri = String_val (Field (connv, 0)); /* Some conn */
ffd6ed
-
ffd6ed
-  /* We have to call the default authentication handler, not least
ffd6ed
-   * since it handles all the PolicyKit crap.  However it also makes
ffd6ed
-   * coding this simpler.
ffd6ed
-   */
ffd6ed
-  conn = virConnectOpenAuth (conn_uri, virConnectAuthPtrDefault,
ffd6ed
-                             VIR_CONNECT_RO);
ffd6ed
-  if (conn == NULL) {
ffd6ed
-    if (conn_uri)
ffd6ed
-      snprintf (errmsg, sizeof errmsg,
ffd6ed
-                _("cannot open libvirt connection '%s'"), conn_uri);
ffd6ed
-    else
ffd6ed
-      snprintf (errmsg, sizeof errmsg, _("cannot open libvirt connection"));
ffd6ed
-    caml_invalid_argument (errmsg);
ffd6ed
-  }
ffd6ed
-
ffd6ed
-  /* Suppress default behaviour of printing errors to stderr.  Note
ffd6ed
-   * you can't set this to NULL to ignore errors; setting it to NULL
ffd6ed
-   * restores the default error handler ...
ffd6ed
-   */
ffd6ed
-  virConnSetErrorFunc (conn, NULL, ignore_errors);
ffd6ed
-
ffd6ed
   /* Look up the pool. */
ffd6ed
-  poolname = String_val (poolnamev);
ffd6ed
-
ffd6ed
-  pool = virStoragePoolLookupByUUIDString (conn, poolname);
ffd6ed
-
ffd6ed
-  if (!pool)
ffd6ed
-    pool = virStoragePoolLookupByName (conn, poolname);
ffd6ed
-
ffd6ed
-  if (!pool) {
ffd6ed
-    err = virGetLastError ();
ffd6ed
-    snprintf (errmsg, sizeof errmsg,
ffd6ed
-              _("cannot find libvirt pool '%s': %s"), poolname, err->message);
ffd6ed
-    virConnectClose (conn);
ffd6ed
-    caml_invalid_argument (errmsg);
ffd6ed
-  }
ffd6ed
+  pool = connect_and_load_pool (connv, poolnamev);
ffd6ed
+  conn = virStoragePoolGetConnect (pool);
ffd6ed
 
ffd6ed
   xml = virStoragePoolGetXMLDesc (pool, 0);
ffd6ed
   if (xml == NULL) {
ffd6ed
     err = virGetLastError ();
ffd6ed
     snprintf (errmsg, sizeof errmsg,
ffd6ed
               _("cannot fetch XML description of pool '%s': %s"),
ffd6ed
-              poolname, err->message);
ffd6ed
+              String_val (poolnamev), err->message);
ffd6ed
     virStoragePoolFree (pool);
ffd6ed
     virConnectClose (conn);
ffd6ed
     caml_invalid_argument (errmsg);
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed