mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 03:21:19 +00:00
livirt: Move URI alias matching to util
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.
This commit is contained in:
parent
dbecb87f94
commit
fb90fcc046
@ -908,82 +908,6 @@ virGetVersion(unsigned long *libVer, const char *type ATTRIBUTE_UNUSED,
|
|||||||
return -1;
|
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
|
static int
|
||||||
virConnectGetDefaultURI(virConfPtr conf,
|
virConnectGetDefaultURI(virConfPtr conf,
|
||||||
@ -1059,7 +983,7 @@ do_open(const char *name,
|
|||||||
name = "xen:///";
|
name = "xen:///";
|
||||||
|
|
||||||
if (!(flags & VIR_CONNECT_NO_ALIASES) &&
|
if (!(flags & VIR_CONNECT_NO_ALIASES) &&
|
||||||
virConnectOpenResolveURIAlias(conf, name, &alias) < 0)
|
virURIResolveAlias(conf, name, &alias) < 0)
|
||||||
goto failed;
|
goto failed;
|
||||||
|
|
||||||
if (!(ret->uri = virURIParse(alias ? alias : name))) {
|
if (!(ret->uri = virURIParse(alias ? alias : name))) {
|
||||||
|
@ -2344,6 +2344,7 @@ virURIFormat;
|
|||||||
virURIFormatParams;
|
virURIFormatParams;
|
||||||
virURIFree;
|
virURIFree;
|
||||||
virURIParse;
|
virURIParse;
|
||||||
|
virURIResolveAlias;
|
||||||
|
|
||||||
|
|
||||||
# util/virusb.h
|
# util/virusb.h
|
||||||
|
@ -25,10 +25,14 @@
|
|||||||
#include "viralloc.h"
|
#include "viralloc.h"
|
||||||
#include "virerror.h"
|
#include "virerror.h"
|
||||||
#include "virbuffer.h"
|
#include "virbuffer.h"
|
||||||
|
#include "virlog.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
|
#include "virutil.h"
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_URI
|
#define VIR_FROM_THIS VIR_FROM_URI
|
||||||
|
|
||||||
|
VIR_LOG_INIT("util.uri");
|
||||||
|
|
||||||
static int
|
static int
|
||||||
virURIParamAppend(virURIPtr uri,
|
virURIParamAppend(virURIPtr uri,
|
||||||
const char *name,
|
const char *name,
|
||||||
@ -311,3 +315,91 @@ void virURIFree(virURIPtr uri)
|
|||||||
|
|
||||||
VIR_FREE(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;
|
||||||
|
}
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
# include <libxml/uri.h>
|
# include <libxml/uri.h>
|
||||||
|
|
||||||
# include "internal.h"
|
# include "internal.h"
|
||||||
|
# include "virconf.h"
|
||||||
|
|
||||||
typedef struct _virURI virURI;
|
typedef struct _virURI virURI;
|
||||||
typedef virURI *virURIPtr;
|
typedef virURI *virURIPtr;
|
||||||
@ -59,6 +60,7 @@ char *virURIFormat(virURIPtr uri)
|
|||||||
char *virURIFormatParams(virURIPtr uri);
|
char *virURIFormatParams(virURIPtr uri);
|
||||||
|
|
||||||
void virURIFree(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")
|
# define VIR_URI_SERVER(uri) ((uri) && (uri)->server ? (uri)->server : "localhost")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user