[PATCH v3 07/11] livirt: Move URI alias matching to util

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



As we need to provide support for URI aliases in libvirt-admin as well, URI
alias matching needs to be internally visible. Since
virConnectOpenResolveURIAlias does have a compatible signature, it could be
easily reused by libvirt-admin. This patch moves URI alias matching to util,
renaming it accordingly.
---
 src/libvirt.c            | 78 +---------------------------------------
 src/libvirt_private.syms |  1 +
 src/util/viruri.c        | 92 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/viruri.h        |  2 ++
 4 files changed, 96 insertions(+), 77 deletions(-)

diff --git a/src/libvirt.c b/src/libvirt.c
index e3c35e4..2dd94bf 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -908,82 +908,6 @@ virGetVersion(unsigned long *libVer, const char *type ATTRIBUTE_UNUSED,
     return -1;
 }
 
-#define URI_ALIAS_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
-
-
-static int
-virConnectOpenFindURIAliasMatch(virConfValuePtr value, const char *alias,
-                                char **uri)
-{
-    virConfValuePtr entry;
-    size_t alias_len;
-
-    if (value->type != VIR_CONF_LIST) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Expected a list for 'uri_aliases' config parameter"));
-        return -1;
-    }
-
-    entry = value->list;
-    alias_len = strlen(alias);
-    while (entry) {
-        char *offset;
-        size_t safe;
-
-        if (entry->type != VIR_CONF_STRING) {
-            virReportError(VIR_ERR_CONF_SYNTAX, "%s",
-                           _("Expected a string for 'uri_aliases' config parameter list entry"));
-            return -1;
-        }
-
-        if (!(offset = strchr(entry->str, '='))) {
-            virReportError(VIR_ERR_CONF_SYNTAX,
-                           _("Malformed 'uri_aliases' config entry '%s', expected 'alias=uri://host/path'"),
-                            entry->str);
-            return -1;
-        }
-
-        safe  = strspn(entry->str, URI_ALIAS_CHARS);
-        if (safe < (offset - entry->str)) {
-            virReportError(VIR_ERR_CONF_SYNTAX,
-                           _("Malformed 'uri_aliases' config entry '%s', aliases may only contain 'a-Z, 0-9, _, -'"),
-                            entry->str);
-            return -1;
-        }
-
-        if (alias_len == (offset - entry->str) &&
-            STREQLEN(entry->str, alias, alias_len)) {
-            VIR_DEBUG("Resolved alias '%s' to '%s'",
-                      alias, offset+1);
-            return VIR_STRDUP(*uri, offset+1);
-        }
-
-        entry = entry->next;
-    }
-
-    VIR_DEBUG("No alias found for '%s', passing through to drivers",
-              alias);
-    return 0;
-}
-
-
-static int
-virConnectOpenResolveURIAlias(virConfPtr conf,
-                              const char *alias, char **uri)
-{
-    int ret = -1;
-    virConfValuePtr value = NULL;
-
-    *uri = NULL;
-
-    if ((value = virConfGetValue(conf, "uri_aliases")))
-        ret = virConnectOpenFindURIAliasMatch(value, alias, uri);
-    else
-        ret = 0;
-
-    return ret;
-}
-
 
 static int
 virConnectGetDefaultURI(virConfPtr conf,
@@ -1059,7 +983,7 @@ do_open(const char *name,
             name = "xen:///";
 
         if (!(flags & VIR_CONNECT_NO_ALIASES) &&
-            virConnectOpenResolveURIAlias(conf, name, &alias) < 0)
+            virURIResolveAlias(conf, name, &alias) < 0)
             goto failed;
 
         if (!(ret->uri = virURIParse(alias ? alias : name))) {
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 98f3351..02a8d7e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2322,6 +2322,7 @@ virURIFormat;
 virURIFormatParams;
 virURIFree;
 virURIParse;
+virURIResolveAlias;
 
 
 # util/virusb.h
diff --git a/src/util/viruri.c b/src/util/viruri.c
index 6166c37..16d27db 100644
--- a/src/util/viruri.c
+++ b/src/util/viruri.c
@@ -25,10 +25,14 @@
 #include "viralloc.h"
 #include "virerror.h"
 #include "virbuffer.h"
+#include "virlog.h"
 #include "virstring.h"
+#include "virutil.h"
 
 #define VIR_FROM_THIS VIR_FROM_URI
 
+VIR_LOG_INIT("util.uri");
+
 static int
 virURIParamAppend(virURIPtr uri,
                   const char *name,
@@ -311,3 +315,91 @@ void virURIFree(virURIPtr uri)
 
     VIR_FREE(uri);
 }
+
+
+#define URI_ALIAS_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
+
+static int
+virURIFindAliasMatch(virConfValuePtr value, const char *alias,
+                     char **uri)
+{
+    virConfValuePtr entry;
+    size_t alias_len;
+
+    if (value->type != VIR_CONF_LIST) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Expected a list for 'uri_aliases' config parameter"));
+        return -1;
+    }
+
+    entry = value->list;
+    alias_len = strlen(alias);
+    while (entry) {
+        char *offset;
+        size_t safe;
+
+        if (entry->type != VIR_CONF_STRING) {
+            virReportError(VIR_ERR_CONF_SYNTAX, "%s",
+                           _("Expected a string for 'uri_aliases' config "
+                             "parameter list entry"));
+            return -1;
+        }
+
+        if (!(offset = strchr(entry->str, '='))) {
+            virReportError(VIR_ERR_CONF_SYNTAX,
+                           _("Malformed 'uri_aliases' config entry '%s', "
+                             "expected 'alias=uri://host/path'"), entry->str);
+            return -1;
+        }
+
+        safe  = strspn(entry->str, URI_ALIAS_CHARS);
+        if (safe < (offset - entry->str)) {
+            virReportError(VIR_ERR_CONF_SYNTAX,
+                           _("Malformed 'uri_aliases' config entry '%s', "
+                             "aliases may only contain 'a-Z, 0-9, _, -'"),
+                            entry->str);
+            return -1;
+        }
+
+        if (alias_len == (offset - entry->str) &&
+            STREQLEN(entry->str, alias, alias_len)) {
+            VIR_DEBUG("Resolved alias '%s' to '%s'",
+                      alias, offset+1);
+            return VIR_STRDUP(*uri, offset+1);
+        }
+
+        entry = entry->next;
+    }
+
+    VIR_DEBUG("No alias found for '%s', continuing...",
+              alias);
+    return 0;
+}
+
+
+/**
+ * virURIResolveAlias:
+ * @conf: configuration file handler
+ * @alias: URI alias to be resolved
+ * @uri: URI object reference where the resolved URI should be stored
+ *
+ * Resolves @alias to a canonical URI according to our configuration
+ * file.
+ *
+ * Returns 0 on success, -1 on error.
+ */
+int
+virURIResolveAlias(virConfPtr conf, const char *alias, char **uri)
+{
+    int ret = -1;
+    virConfValuePtr value = NULL;
+
+    *uri = NULL;
+
+    if ((value = virConfGetValue(conf, "uri_aliases")))
+        ret = virURIFindAliasMatch(value, alias, uri);
+    else
+        ret = 0;
+
+    return ret;
+}
diff --git a/src/util/viruri.h b/src/util/viruri.h
index 3cfc7d3..1e53abb 100644
--- a/src/util/viruri.h
+++ b/src/util/viruri.h
@@ -24,6 +24,7 @@
 # include <libxml/uri.h>
 
 # include "internal.h"
+# include "virconf.h"
 
 typedef struct _virURI virURI;
 typedef virURI *virURIPtr;
@@ -59,6 +60,7 @@ char *virURIFormat(virURIPtr uri)
 char *virURIFormatParams(virURIPtr uri);
 
 void virURIFree(virURIPtr uri);
+int virURIResolveAlias(virConfPtr conf, const char *alias, char **uri);
 
 # define VIR_URI_SERVER(uri) ((uri) && (uri)->server ? (uri)->server : "localhost")
 
-- 
2.4.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]