[libvirt] [PATCH 0/3] chardev and disk source format cleanup

Pavel Hrdina (3): util: introduce virBufferSetChildIndent macro util: introduce virXMLFormatElement helper conf: don't close the source element inside different function src/conf/capabilities.c | 3 +- src/conf/cpu_conf.c | 3 +- src/conf/domain_conf.c | 125 ++++++++++++--------- src/libvirt_private.syms | 1 + src/util/virbuffer.h | 9 ++ src/util/virxml.c | 47 ++++++++ src/util/virxml.h | 6 + .../qemuxml2xmlout-seclabel-dynamic-labelskip.xml | 3 +- 8 files changed, 135 insertions(+), 62 deletions(-) -- 2.13.5

Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/conf/capabilities.c | 3 +-- src/conf/cpu_conf.c | 3 +-- src/conf/domain_conf.c | 26 +++++++++++--------------- src/util/virbuffer.h | 9 +++++++++ 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index 69af3911a0..ba554535ae 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -872,7 +872,6 @@ virCapabilitiesFormatCaches(virBufferPtr buf, { size_t i = 0; size_t j = 0; - int indent = virBufferGetIndent(buf, false); virBuffer controlBuf = VIR_BUFFER_INITIALIZER; if (!ncaches) @@ -903,7 +902,7 @@ virCapabilitiesFormatCaches(virBufferPtr buf, cpus_str); VIR_FREE(cpus_str); - virBufferAdjustIndent(&controlBuf, indent + 4); + virBufferSetChildIndent(&controlBuf, buf); for (j = 0; j < bank->ncontrols; j++) { bool min_kilos = !(bank->controls[j]->granularity % 1024); diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c index cdb8583647..c21d11d244 100644 --- a/src/conf/cpu_conf.c +++ b/src/conf/cpu_conf.c @@ -599,7 +599,6 @@ virCPUDefFormatBufFull(virBufferPtr buf, int ret = -1; virBuffer attributeBuf = VIR_BUFFER_INITIALIZER; virBuffer childrenBuf = VIR_BUFFER_INITIALIZER; - int indent = virBufferGetIndent(buf, false); if (!def) return 0; @@ -636,7 +635,7 @@ virCPUDefFormatBufFull(virBufferPtr buf, } /* Format children */ - virBufferAdjustIndent(&childrenBuf, indent + 2); + virBufferSetChildIndent(&childrenBuf, buf); if (def->type == VIR_CPU_TYPE_HOST && def->arch) virBufferAsprintf(&childrenBuf, "<arch>%s</arch>\n", virArchToString(def->arch)); diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 478feb92d8..b8867fd1ea 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -22004,7 +22004,7 @@ virDomainControllerDefFormat(virBufferPtr buf, const char *modelName = NULL; virBuffer childBuf = VIR_BUFFER_INITIALIZER; - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23287,7 +23287,7 @@ virDomainSmartcardDefFormat(virBufferPtr buf, virBuffer childBuf = VIR_BUFFER_INITIALIZER; size_t i; - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); if (!mode) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23396,7 +23396,7 @@ virDomainSoundDefFormat(virBufferPtr buf, virBuffer childBuf = VIR_BUFFER_INITIALIZER; size_t i; - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23432,7 +23432,6 @@ virDomainMemballoonDefFormat(virBufferPtr buf, { const char *model = virDomainMemballoonModelTypeToString(def->model); virBuffer childrenBuf = VIR_BUFFER_INITIALIZER; - int indent = virBufferGetIndent(buf, false); if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23446,7 +23445,7 @@ virDomainMemballoonDefFormat(virBufferPtr buf, virBufferAsprintf(buf, " autodeflate='%s'", virTristateSwitchTypeToString(def->autodeflate)); - virBufferAdjustIndent(&childrenBuf, indent + 2); + virBufferSetChildIndent(&childrenBuf, buf); if (def->period) virBufferAsprintf(&childrenBuf, "<stats period='%i'/>\n", def->period); @@ -23508,7 +23507,7 @@ virDomainWatchdogDefFormat(virBufferPtr buf, const char *action = virDomainWatchdogActionTypeToString(def->action); virBuffer childBuf = VIR_BUFFER_INITIALIZER; - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23545,7 +23544,6 @@ static int virDomainPanicDefFormat(virBufferPtr buf, virDomainPanicDefPtr def) { virBuffer childrenBuf = VIR_BUFFER_INITIALIZER; - int indent = virBufferGetIndent(buf, false); virBufferAddLit(buf, "<panic"); @@ -23553,7 +23551,7 @@ static int virDomainPanicDefFormat(virBufferPtr buf, virBufferAsprintf(buf, " model='%s'", virDomainPanicModelTypeToString(def->model)); - virBufferAdjustIndent(&childrenBuf, indent + 2); + virBufferSetChildIndent(&childrenBuf, buf); virDomainDeviceInfoFormat(&childrenBuf, &def->info, 0); if (virBufferCheckError(&childrenBuf) < 0) @@ -23888,7 +23886,7 @@ virDomainInputDefFormat(virBufferPtr buf, virBufferAsprintf(buf, "<input type='%s' bus='%s'", type, bus); - virBufferAdjustIndent(&childbuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childbuf, buf); virDomainVirtioOptionsFormat(&driverBuf, def->virtio); if (virBufferCheckError(&driverBuf) < 0) return -1; @@ -24538,7 +24536,7 @@ virDomainHubDefFormat(virBufferPtr buf, const char *type = virDomainHubTypeToString(def->type); virBuffer childBuf = VIR_BUFFER_INITIALIZER; - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -24751,7 +24749,7 @@ virDomainCputuneDefFormat(virBufferPtr buf, virBuffer childrenBuf = VIR_BUFFER_INITIALIZER; int ret = -1; - virBufferAdjustIndent(&childrenBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childrenBuf, buf); if (def->cputune.sharesSpecified) virBufferAsprintf(&childrenBuf, "<shares>%llu</shares>\n", @@ -24933,7 +24931,7 @@ virDomainIOMMUDefFormat(virBufferPtr buf, { virBuffer childBuf = VIR_BUFFER_INITIALIZER; - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); if (iommu->intremap != VIR_TRISTATE_SWITCH_ABSENT || iommu->caching_mode != VIR_TRISTATE_SWITCH_ABSENT || @@ -24987,7 +24985,6 @@ virDomainDefFormatInternal(virDomainDefPtr def, int n; size_t i; virBuffer childrenBuf = VIR_BUFFER_INITIALIZER; - int indent; char *netprefix = NULL; virCheckFlags(VIR_DOMAIN_DEF_FORMAT_COMMON_FLAGS | @@ -25065,8 +25062,7 @@ virDomainDefFormatInternal(virDomainDefPtr def, def->mem.cur_balloon); /* start format blkiotune */ - indent = virBufferGetIndent(buf, false); - virBufferAdjustIndent(&childrenBuf, indent + 2); + virBufferSetChildIndent(&childrenBuf, buf); if (def->blkio.weight) virBufferAsprintf(&childrenBuf, "<weight>%u</weight>\n", def->blkio.weight); diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index f34217968b..6ad9c3ec54 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -102,6 +102,15 @@ void virBufferURIEncodeString(virBufferPtr buf, const char *str); void virBufferAdjustIndent(virBufferPtr buf, int indent); void virBufferSetIndent(virBufferPtr, int indent); +/** + * virBufferSetChildIndent + * + * Gets the parent indentation, increments it by 2 and sets it to + * child buffer. + */ +# define virBufferSetChildIndent(childBuf_, parentBuf_) \ + virBufferAdjustIndent(childBuf_, virBufferGetIndent(parentBuf_, false) + 2) + int virBufferGetIndent(const virBuffer *buf, bool dynamic); void virBufferTrim(virBufferPtr buf, const char *trim, int len); -- 2.13.5

This helper allows you to better structurize the code if some element may or may not contains attributes and/or child elements. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 ++++++ 3 files changed, 54 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 2149b11b74..12adf27285 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2955,6 +2955,7 @@ virVHBAPathExists; virXMLCheckIllegalChars; virXMLChildElementCount; virXMLExtractNamespaceXML; +virXMLFormatElement; virXMLNodeContentString; virXMLNodeNameEqual; virXMLNodeSanitizeNamespaces; diff --git a/src/util/virxml.c b/src/util/virxml.c index 562a4bf3ba..885f59bceb 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1354,3 +1354,50 @@ virXMLValidatorFree(virXMLValidatorPtr validator) xmlRelaxNGFree(validator->rng); VIR_FREE(validator); } + + +/** + * virXMLFormatElement + * @buf: the parent buffer where the element will be placed + * @name: the name of the element + * @attrBuf: buffer with attributes for element, may be NULL + * @childBuf: buffer with child elements, may be NULL + * + * Helper to format element where attributes or child elements + * are optional and may not be formatted. If both @attrBuf and + * @childBuf are NULL or are empty buffers the element is not + * formatted. + * + * Returns 0 on success, -1 on error. + */ +int +virXMLFormatElement(virBufferPtr buf, + const char *name, + virBufferPtr attrBuf, + virBufferPtr childBuf) +{ + if ((!attrBuf || virBufferUse(attrBuf) == 0) && + (!childBuf || virBufferUse(childBuf) == 0)) { + return 0; + } + + if ((attrBuf && virBufferCheckError(attrBuf) < 0) || + (childBuf && virBufferCheckError(childBuf) < 0)) { + return -1; + } + + virBufferAsprintf(buf, "<%s", name); + + if (attrBuf && virBufferUse(attrBuf) > 0) + virBufferAddBuffer(buf, attrBuf); + + if (childBuf && virBufferUse(childBuf) > 0) { + virBufferAddLit(buf, ">\n"); + virBufferAddBuffer(buf, childBuf); + virBufferAsprintf(buf, "</%s>\n", name); + } else { + virBufferAddLit(buf, "/>\n"); + } + + return 0; +} diff --git a/src/util/virxml.h b/src/util/virxml.h index 86baeb37a7..1a7d61a8bc 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -215,4 +215,10 @@ virXMLValidateAgainstSchema(const char *schemafile, void virXMLValidatorFree(virXMLValidatorPtr validator); +int +virXMLFormatElement(virBufferPtr buf, + const char *name, + virBufferPtr attrBuf, + virBufferPtr childBuf); + #endif /* __VIR_XML_H__ */ -- 2.13.5

While formatting disk or chardev element they both uses virDomainDiskSourceDefFormatSeclabel() function which also closes the source element. This is not extendable. Use the new virXMLFormatElement() to properly format the source element with possible child elements. As a side effect it fixes a bug in disk source formatting. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/conf/domain_conf.c | 99 +++++++++++++--------- .../qemuxml2xmlout-seclabel-dynamic-labelskip.xml | 3 +- 2 files changed, 59 insertions(+), 43 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index b8867fd1ea..640702f723 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -21489,14 +21489,8 @@ virDomainDiskSourceDefFormatSeclabel(virBufferPtr buf, size_t n; if (nseclabels && !skipSeclables) { - virBufferAddLit(buf, ">\n"); - virBufferAdjustIndent(buf, 2); for (n = 0; n < nseclabels; n++) virSecurityDeviceLabelDefFormat(buf, seclabels[n], flags); - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "</source>\n"); - } else { - virBufferAddLit(buf, "/>\n"); } } @@ -21569,6 +21563,10 @@ virDomainDiskSourceFormatInternal(virBufferPtr buf, bool skipSeclabels) { const char *startupPolicy = NULL; + virBuffer attrBuf = VIR_BUFFER_INITIALIZER; + virBuffer childBuf = VIR_BUFFER_INITIALIZER; + + virBufferSetChildIndent(&childBuf, buf); if (policy) startupPolicy = virDomainStartupPolicyTypeToString(policy); @@ -21576,51 +21574,45 @@ virDomainDiskSourceFormatInternal(virBufferPtr buf, if (src->path || src->nhosts > 0 || src->srcpool || startupPolicy) { switch ((virStorageType)src->type) { case VIR_STORAGE_TYPE_FILE: - virBufferAddLit(buf, "<source"); - virBufferEscapeString(buf, " file='%s'", src->path); - virBufferEscapeString(buf, " startupPolicy='%s'", startupPolicy); + virBufferEscapeString(&attrBuf, " file='%s'", src->path); + virBufferEscapeString(&attrBuf, " startupPolicy='%s'", startupPolicy); - virDomainDiskSourceDefFormatSeclabel(buf, src->nseclabels, + virDomainDiskSourceDefFormatSeclabel(&childBuf, src->nseclabels, src->seclabels, flags, skipSeclabels); break; case VIR_STORAGE_TYPE_BLOCK: - virBufferAddLit(buf, "<source"); - virBufferEscapeString(buf, " dev='%s'", src->path); - virBufferEscapeString(buf, " startupPolicy='%s'", startupPolicy); + virBufferEscapeString(&attrBuf, " dev='%s'", src->path); + virBufferEscapeString(&attrBuf, " startupPolicy='%s'", startupPolicy); - virDomainDiskSourceDefFormatSeclabel(buf, src->nseclabels, + virDomainDiskSourceDefFormatSeclabel(&childBuf, src->nseclabels, src->seclabels, flags, skipSeclabels); break; case VIR_STORAGE_TYPE_DIR: - virBufferAddLit(buf, "<source"); - virBufferEscapeString(buf, " dir='%s'", src->path); - virBufferEscapeString(buf, " startupPolicy='%s'", startupPolicy); - virBufferAddLit(buf, "/>\n"); + virBufferEscapeString(&attrBuf, " dir='%s'", src->path); + virBufferEscapeString(&attrBuf, " startupPolicy='%s'", startupPolicy); break; case VIR_STORAGE_TYPE_NETWORK: if (virDomainDiskSourceFormatNetwork(buf, src) < 0) - return -1; + goto error; break; case VIR_STORAGE_TYPE_VOLUME: - virBufferAddLit(buf, "<source"); - if (src->srcpool) { - virBufferEscapeString(buf, " pool='%s'", src->srcpool->pool); - virBufferEscapeString(buf, " volume='%s'", + virBufferEscapeString(&attrBuf, " pool='%s'", src->srcpool->pool); + virBufferEscapeString(&attrBuf, " volume='%s'", src->srcpool->volume); if (src->srcpool->mode) - virBufferAsprintf(buf, " mode='%s'", + virBufferAsprintf(&attrBuf, " mode='%s'", virStorageSourcePoolModeTypeToString(src->srcpool->mode)); } - virBufferEscapeString(buf, " startupPolicy='%s'", startupPolicy); + virBufferEscapeString(&attrBuf, " startupPolicy='%s'", startupPolicy); - virDomainDiskSourceDefFormatSeclabel(buf, src->nseclabels, + virDomainDiskSourceDefFormatSeclabel(&childBuf, src->nseclabels, src->seclabels, flags, skipSeclabels); break; @@ -21629,11 +21621,19 @@ virDomainDiskSourceFormatInternal(virBufferPtr buf, case VIR_STORAGE_TYPE_LAST: virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected disk type %d"), src->type); - return -1; + goto error; } + + if (virXMLFormatElement(buf, "source", &attrBuf, &childBuf) < 0) + goto error; } return 0; + + error: + virBufferFreeAndReset(&attrBuf); + virBufferFreeAndReset(&childBuf); + return -1; } @@ -23059,11 +23059,15 @@ virDomainChrAttrsDefFormat(virBufferPtr buf, return 0; } -static void +static int virDomainChrSourceDefFormat(virBufferPtr buf, virDomainChrSourceDefPtr def, unsigned int flags) { + virBuffer attrBuf = VIR_BUFFER_INITIALIZER; + virBuffer childBuf = VIR_BUFFER_INITIALIZER; + + virBufferSetChildIndent(&childBuf, buf); switch ((virDomainChrType)def->type) { case VIR_DOMAIN_CHR_TYPE_NULL: @@ -23081,14 +23085,17 @@ virDomainChrSourceDefFormat(virBufferPtr buf, if (def->type != VIR_DOMAIN_CHR_TYPE_PTY || (def->data.file.path && !(flags & VIR_DOMAIN_DEF_FORMAT_INACTIVE))) { - virBufferEscapeString(buf, "<source path='%s'", + virBufferEscapeString(&attrBuf, " path='%s'", def->data.file.path); if (def->type == VIR_DOMAIN_CHR_TYPE_FILE && def->data.file.append != VIR_TRISTATE_SWITCH_ABSENT) - virBufferAsprintf(buf, " append='%s'", + virBufferAsprintf(&attrBuf, " append='%s'", virTristateSwitchTypeToString(def->data.file.append)); - virDomainSourceDefFormatSeclabel(buf, def->nseclabels, + virDomainSourceDefFormatSeclabel(&childBuf, def->nseclabels, def->seclabels, flags); + + if (virXMLFormatElement(buf, "source", &attrBuf, &childBuf) < 0) + goto error; } break; @@ -23119,19 +23126,21 @@ virDomainChrSourceDefFormat(virBufferPtr buf, break; case VIR_DOMAIN_CHR_TYPE_TCP: - virBufferAsprintf(buf, "<source mode='%s' ", + virBufferAsprintf(&attrBuf, " mode='%s' ", def->data.tcp.listen ? "bind" : "connect"); - virBufferEscapeString(buf, "host='%s' ", def->data.tcp.host); - virBufferEscapeString(buf, "service='%s'", def->data.tcp.service); + virBufferEscapeString(&attrBuf, "host='%s' ", def->data.tcp.host); + virBufferEscapeString(&attrBuf, "service='%s'", def->data.tcp.service); if (def->data.tcp.haveTLS != VIR_TRISTATE_BOOL_ABSENT && !(flags & VIR_DOMAIN_DEF_FORMAT_MIGRATABLE && def->data.tcp.tlsFromConfig)) - virBufferAsprintf(buf, " tls='%s'", + virBufferAsprintf(&attrBuf, " tls='%s'", virTristateBoolTypeToString(def->data.tcp.haveTLS)); if (flags & VIR_DOMAIN_DEF_FORMAT_STATUS) - virBufferAsprintf(buf, " tlsFromConfig='%d'", + virBufferAsprintf(&attrBuf, " tlsFromConfig='%d'", def->data.tcp.tlsFromConfig); - virBufferAddLit(buf, "/>\n"); + + if (virXMLFormatElement(buf, "source", &attrBuf, &childBuf) < 0) + goto error; virBufferAsprintf(buf, "<protocol type='%s'/>\n", virDomainChrTcpProtocolTypeToString( @@ -23140,11 +23149,14 @@ virDomainChrSourceDefFormat(virBufferPtr buf, case VIR_DOMAIN_CHR_TYPE_UNIX: if (def->data.nix.path) { - virBufferAsprintf(buf, "<source mode='%s'", + virBufferAsprintf(&attrBuf, " mode='%s'", def->data.nix.listen ? "bind" : "connect"); - virBufferEscapeString(buf, " path='%s'", def->data.nix.path); - virDomainSourceDefFormatSeclabel(buf, def->nseclabels, + virBufferEscapeString(&attrBuf, " path='%s'", def->data.nix.path); + virDomainSourceDefFormatSeclabel(&childBuf, def->nseclabels, def->seclabels, flags); + + if (virXMLFormatElement(buf, "source", &attrBuf, &childBuf) < 0) + goto error; } break; @@ -23164,7 +23176,12 @@ virDomainChrSourceDefFormat(virBufferPtr buf, virBufferAddLit(buf, "/>\n"); } - return; + return 0; + + error: + virBufferFreeAndReset(&attrBuf); + virBufferFreeAndReset(&childBuf); + return -1; } static int diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-seclabel-dynamic-labelskip.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-seclabel-dynamic-labelskip.xml index 6688d98eb6..3f4ff0aadf 100644 --- a/tests/qemuxml2xmloutdata/qemuxml2xmlout-seclabel-dynamic-labelskip.xml +++ b/tests/qemuxml2xmloutdata/qemuxml2xmlout-seclabel-dynamic-labelskip.xml @@ -15,8 +15,7 @@ <devices> <emulator>/usr/bin/qemu-system-i686</emulator> <disk type='block' device='disk'> - <source dev='/dev/HostVG/QEMUGuest1'> - </source> + <source dev='/dev/HostVG/QEMUGuest1'/> <target dev='hda' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> -- 2.13.5

On 08/24/2017 11:22 AM, Pavel Hrdina wrote:
Pavel Hrdina (3): util: introduce virBufferSetChildIndent macro util: introduce virXMLFormatElement helper conf: don't close the source element inside different function
src/conf/capabilities.c | 3 +- src/conf/cpu_conf.c | 3 +- src/conf/domain_conf.c | 125 ++++++++++++--------- src/libvirt_private.syms | 1 + src/util/virbuffer.h | 9 ++ src/util/virxml.c | 47 ++++++++ src/util/virxml.h | 6 + .../qemuxml2xmlout-seclabel-dynamic-labelskip.xml | 3 +- 8 files changed, 135 insertions(+), 62 deletions(-)
Reviewed-by: John Ferlan <jferlan@redhat.com> (series) John
participants (2)
-
John Ferlan
-
Pavel Hrdina