提交 89513286 编写于 作者: S Stefan Berger 提交者: Stefan Berger

Extend NWFilter parameter parser to cope with lists of values

This patch modifies the NWFilter parameter parser to support multiple
elements with the same name and to internally build a list of items.
An example of the XML looks like this:

        <parameter name='TEST' value='10.1.2.3'/>
        <parameter name='TEST' value='10.2.3.4'/>
        <parameter name='TEST' value='10.1.1.1'/>

The list of values is then stored in the newly introduced data type
virNWFilterVarValue.

The XML formatter is also adapted to print out all items in alphabetical
order sorted by 'name'.

This patch also fixes a bug in the XML schema on the way.
Signed-off-by: NStefan Berger <stefanb@linux.vnet.ibm.com>
上级 c80296e2
...@@ -312,7 +312,7 @@ ...@@ -312,7 +312,7 @@
<attribute name="filter"> <attribute name="filter">
<data type="NCName"/> <data type="NCName"/>
</attribute> </attribute>
<optional> <zeroOrMore>
<element name="parameter"> <element name="parameter">
<attribute name="name"> <attribute name="name">
<ref name="filter-param-name"/> <ref name="filter-param-name"/>
...@@ -321,7 +321,7 @@ ...@@ -321,7 +321,7 @@
<ref name="filter-param-value"/> <ref name="filter-param-value"/>
</attribute> </attribute>
</element> </element>
</optional> </zeroOrMore>
</define> </define>
<define name="rule-node-attributes"> <define name="rule-node-attributes">
......
...@@ -604,7 +604,7 @@ isValidVarName(const char *var) ...@@ -604,7 +604,7 @@ isValidVarName(const char *var)
static bool static bool
isValidVarValue(const char *value) isValidVarValue(const char *value)
{ {
return value[strspn(value, VALID_VARVALUE)] == 0; return (value[strspn(value, VALID_VARVALUE)] == 0) && (strlen(value) != 0);
} }
static virNWFilterVarValuePtr static virNWFilterVarValuePtr
...@@ -636,15 +636,22 @@ virNWFilterParseParamAttributes(xmlNodePtr cur) ...@@ -636,15 +636,22 @@ virNWFilterParseParamAttributes(xmlNodePtr cur)
if (nam != NULL && val != NULL) { if (nam != NULL && val != NULL) {
if (!isValidVarName(nam)) if (!isValidVarName(nam))
goto skip_entry; goto skip_entry;
if (!isValidVarValue(val))
goto skip_entry;
value = virHashLookup(table->hashTable, nam);
if (value) {
/* add value to existing value -> list */
if (virNWFilterVarValueAddValue(value, val) < 0) {
value = NULL;
goto err_exit;
}
val = NULL;
} else {
value = virNWFilterParseVarValue(val); value = virNWFilterParseVarValue(val);
if (!value) if (!value)
goto skip_entry; goto skip_entry;
if (virNWFilterHashTablePut(table, nam, value, 1)) { if (virNWFilterHashTablePut(table, nam, value, 1))
VIR_FREE(nam); goto err_exit;
VIR_FREE(val);
virNWFilterVarValueFree(value);
virNWFilterHashTableFree(table);
return NULL;
} }
value = NULL; value = NULL;
} }
...@@ -657,39 +664,66 @@ skip_entry: ...@@ -657,39 +664,66 @@ skip_entry:
cur = cur->next; cur = cur->next;
} }
return table; return table;
err_exit:
VIR_FREE(nam);
VIR_FREE(val);
virNWFilterVarValueFree(value);
virNWFilterHashTableFree(table);
return NULL;
} }
static void static int
_formatParameterAttrs(void *payload, const void *name, void *data) virNWFilterFormatParameterNameSorter(const virHashKeyValuePairPtr a,
const virHashKeyValuePairPtr b)
{ {
virBufferPtr buf = data; return strcmp((const char *)a->key, (const char *)b->key);
virBufferAsprintf(buf, " <parameter name='%s' value='%s'/>\n",
(const char *)name,
(char *)payload);
} }
int int
virNWFilterFormatParamAttributes(virBufferPtr buf, virNWFilterFormatParamAttributes(virBufferPtr buf,
virNWFilterHashTablePtr table, virNWFilterHashTablePtr table,
const char *filterref) const char *filterref)
{ {
int count = virHashSize(table->hashTable); virHashKeyValuePairPtr items;
int i, j, card, numKeys;
if (count < 0) { numKeys = virHashSize(table->hashTable);
if (numKeys < 0) {
virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s", virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("missing filter parameter table")); _("missing filter parameter table"));
return -1; return -1;
} }
items = virHashGetItems(table->hashTable,
virNWFilterFormatParameterNameSorter);
if (!items)
return -1;
virBufferAsprintf(buf, "<filterref filter='%s'", filterref); virBufferAsprintf(buf, "<filterref filter='%s'", filterref);
if (count) { if (numKeys) {
virBufferAddLit(buf, ">\n"); virBufferAddLit(buf, ">\n");
virHashForEach(table->hashTable, _formatParameterAttrs, buf); for (i = 0; i < numKeys; i++) {
const virNWFilterVarValuePtr value =
(const virNWFilterVarValuePtr)items[i].value;
card = virNWFilterVarValueGetCardinality(value);
for (j = 0; j < card; j++)
virBufferAsprintf(buf,
" <parameter name='%s' value='%s'/>\n",
(const char *)items[i].key,
virNWFilterVarValueGetNthValue(value, j));
}
virBufferAddLit(buf, "</filterref>\n"); virBufferAddLit(buf, "</filterref>\n");
} else { } else {
virBufferAddLit(buf, "/>\n"); virBufferAddLit(buf, "/>\n");
} }
VIR_FREE(items);
return 0; return 0;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册