[libvirt] [PATCH 0/2] Make a generic version of virDomainDefMetadataSanitize()

Peter pointed out that Brandon's patch to add metadata to the network XML should "sanitize" it (to remove duplicate namespaces) in the same manner as we do (thanks to Peter!) for the domain metadata. In order to make that simpler, I made a patch that turns virDomainDefMetadataSanitize() into the generic function virXMLNodeSanitizeNamespaces(), then (in a separate patch) changed the domain parser to use that new generic function. I will also add a call to that function into Brandon's patch before pushing it. Laine Stump (2): util: new function virXMLNodeSanitizeNamespaces() qemu: use virXMLNodeSanitizeNamespaces() src/conf/domain_conf.c | 56 ++---------------------------------------------- src/libvirt_private.syms | 1 + src/util/virxml.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 2 ++ 4 files changed, 57 insertions(+), 54 deletions(-) -- 2.5.5

This is a generic version of virDomainDefMetadataSanitize() - the same functionality is now needed for network metadata. --- src/libvirt_private.syms | 1 + src/util/virxml.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 2 ++ 3 files changed, 55 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 501c23e..3632148 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2580,6 +2580,7 @@ virUUIDParse; # util/virxml.h virXMLChildElementCount; virXMLExtractNamespaceXML; +virXMLNodeSanitizeNamespaces; virXMLNodeToString; virXMLParseHelper; virXMLPickShellSafeComment; diff --git a/src/util/virxml.c b/src/util/virxml.c index aa97940..37f0817 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1083,6 +1083,58 @@ virXMLInjectNamespace(xmlNodePtr node, return 0; } +/** + * virXMLNodeSanitizeNamespaces() + * @node: Sanitize the namespaces for this node + * + * This function removes subnodes in node that share the namespace. + * The first metadata entry of every duplicate namespace is kept. Additionally + * nodes with no namespace are deleted. + */ +void +virXMLNodeSanitizeNamespaces(xmlNodePtr node) +{ + xmlNodePtr child; + xmlNodePtr next; + xmlNodePtr dupl; + + if (!node) + return; + + child = node->children; + while (child) { + /* remove metadata entries that don't have any namespace at all */ + if (!child->ns || !child->ns->href) { + dupl = child; + child = child->next; + + xmlUnlinkNode(dupl); + xmlFreeNode(dupl); + continue; + } + + /* check that every other child of @root doesn't share the namespace of + * the current one and delete them possibly */ + next = child->next; + while (next) { + dupl = NULL; + + if (child->ns && next->ns && + STREQ_NULLABLE((const char *) child->ns->href, + (const char *) next->ns->href)) + dupl = next; + + next = next->next; + if (dupl) { + xmlUnlinkNode(dupl); + xmlFreeNode(dupl); + } + } + child = child->next; + } +} + + static void catchRNGError(void *ctx, const char *msg, ...) diff --git a/src/util/virxml.h b/src/util/virxml.h index 7a89518..7a0a1da 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -179,6 +179,8 @@ int virXMLInjectNamespace(xmlNodePtr node, const char *uri, const char *key); +void virXMLNodeSanitizeNamespaces(xmlNodePtr node); + struct _virXMLValidator { xmlRelaxNGParserCtxtPtr rngParser; xmlRelaxNGPtr rng; -- 2.5.5

On Fri, Jun 24, 2016 at 11:33:50 -0400, Laine Stump wrote:
This is a generic version of virDomainDefMetadataSanitize() - the same functionality is now needed for network metadata. --- src/libvirt_private.syms | 1 + src/util/virxml.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 2 ++ 3 files changed, 55 insertions(+)
[...]
diff --git a/src/util/virxml.c b/src/util/virxml.c index aa97940..37f0817 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1083,6 +1083,58 @@ virXMLInjectNamespace(xmlNodePtr node, return 0; }
+/** + * virXMLNodeSanitizeNamespaces() + * @node: Sanitize the namespaces for this node + * + * This function removes subnodes in node that share the namespace. + * The first metadata entry of every duplicate namespace is kept. Additionally
This is a little inconsistent since the sentence still hints to 'metadata'.
+ * nodes with no namespace are deleted.
Maybe you can state here that this is mostly used to sanitize metadata.
+ */ +void +virXMLNodeSanitizeNamespaces(xmlNodePtr node) +{
ACK

Replace the virDomainDef-specific virDomainDefMetadataSanitize() with virXMLNodeSanitizeNamespaces(). --- src/conf/domain_conf.c | 56 ++------------------------------------------------ 1 file changed, 2 insertions(+), 54 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 75ad03f..efc95f7 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -3653,59 +3653,6 @@ virDomainDefRejectDuplicatePanics(virDomainDefPtr def) return 0; } -/** - * virDomainDefMetadataSanitize: - * @def: Sanitize metadata for this def - * - * This function removes metadata elements in @def that share the namespace. - * The first metadata entry of every duplicate namespace is kept. Additionally - * elements with no namespace are deleted. - */ -static void -virDomainDefMetadataSanitize(virDomainDefPtr def) -{ - xmlNodePtr child; - xmlNodePtr next; - xmlNodePtr dupl; - - if (!def || !def->metadata) - return; - - child = def->metadata->children; - while (child) { - /* remove metadata entries that don't have any namespace at all */ - if (!child->ns || !child->ns->href) { - dupl = child; - child = child->next; - - xmlUnlinkNode(dupl); - xmlFreeNode(dupl); - continue; - } - - /* check that every other child of @root doesn't share the namespace of - * the current one and delete them possibly */ - next = child->next; - while (next) { - dupl = NULL; - - if (child->ns && next->ns && - STREQ_NULLABLE((const char *) child->ns->href, - (const char *) next->ns->href)) - dupl = next; - - next = next->next; - - if (dupl) { - xmlUnlinkNode(dupl); - xmlFreeNode(dupl); - } - } - - child = child->next; - } -} - static int virDomainDefPostParseMemory(virDomainDefPtr def, @@ -4504,7 +4451,8 @@ virDomainDefPostParseInternal(virDomainDefPtr def, } /* clean up possibly duplicated metadata entries */ - virDomainDefMetadataSanitize(def); + if (def->metadata) + virXMLNodeSanitizeNamespaces(def->metadata); virDomainDefPostParseGraphics(def); -- 2.5.5

On Fri, Jun 24, 2016 at 11:33:51 -0400, Laine Stump wrote:
Replace the virDomainDef-specific virDomainDefMetadataSanitize() with virXMLNodeSanitizeNamespaces(). --- src/conf/domain_conf.c | 56 ++------------------------------------------------ 1 file changed, 2 insertions(+), 54 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 75ad03f..efc95f7 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c
[...]
@@ -4504,7 +4451,8 @@ virDomainDefPostParseInternal(virDomainDefPtr def, }
/* clean up possibly duplicated metadata entries */ - virDomainDefMetadataSanitize(def); + if (def->metadata)
This condition is integrated into the funciton so you can call it directly ACK if you squash this to the previous patch. Code movement patches are generally okay together. Peter
participants (2)
-
Laine Stump
-
Peter Krempa