]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
livirt: Move URI alias matching to util
authorErik Skultety <eskultet@redhat.com>
Fri, 6 Nov 2015 09:44:53 +0000 (10:44 +0100)
committerErik Skultety <eskultet@redhat.com>
Mon, 30 Nov 2015 08:44:28 +0000 (09:44 +0100)
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
src/libvirt_private.syms
src/util/viruri.c
src/util/viruri.h

index cde22d254142bacf8078704bec784ecc4174caf9..dd58e9c23d14cdf6bcaed7ab0b65395a2dba67ba 100644 (file)
@@ -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))) {
index d6187cba1202c0378adc0a53fc0076d1ded5c284..360fb2dd9bbabc9174f5d7e6b8baf0649d5b335f 100644 (file)
@@ -2344,6 +2344,7 @@ virURIFormat;
 virURIFormatParams;
 virURIFree;
 virURIParse;
+virURIResolveAlias;
 
 
 # util/virusb.h
index 6166c372b048063eebf11a4ba21712784be348d0..a491e3446b1e5841add3cf073e855313eb0b6c89 100644 (file)
 #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;
+}
index 3cfc7d39d9e493684a03f5d68a2f32028f2b876d..1e53abb0bc21c3d3d5c8f65d386c279bb8c20473 100644 (file)
@@ -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")