From nobody Sun May 5 17:48:08 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503588148995345.9604066543253; Thu, 24 Aug 2017 08:22:28 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E7CCA68B5; Thu, 24 Aug 2017 15:22:26 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A32907EE61; Thu, 24 Aug 2017 15:22:26 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 2BB881806106; Thu, 24 Aug 2017 15:22:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7OFMJPo014767 for ; Thu, 24 Aug 2017 11:22:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id E0950700B4; Thu, 24 Aug 2017 15:22:19 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 674217F48C for ; Thu, 24 Aug 2017 15:22:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E7CCA68B5 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 24 Aug 2017 17:22:06 +0200 Message-Id: <8c71aaa7e14dabb7e7b152aca404fe5612eb62ae.1503588082.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/3] util: introduce virBufferSetChildIndent macro X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 24 Aug 2017 15:22:27 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina Reviewed-by: John Ferlan --- 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 =3D 0; size_t j =3D 0; - int indent =3D virBufferGetIndent(buf, false); virBuffer controlBuf =3D VIR_BUFFER_INITIALIZER; =20 if (!ncaches) @@ -903,7 +902,7 @@ virCapabilitiesFormatCaches(virBufferPtr buf, cpus_str); VIR_FREE(cpus_str); =20 - virBufferAdjustIndent(&controlBuf, indent + 4); + virBufferSetChildIndent(&controlBuf, buf); for (j =3D 0; j < bank->ncontrols; j++) { bool min_kilos =3D !(bank->controls[j]->granularity % 1024); =20 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 =3D -1; virBuffer attributeBuf =3D VIR_BUFFER_INITIALIZER; virBuffer childrenBuf =3D VIR_BUFFER_INITIALIZER; - int indent =3D virBufferGetIndent(buf, false); =20 if (!def) return 0; @@ -636,7 +635,7 @@ virCPUDefFormatBufFull(virBufferPtr buf, } =20 /* Format children */ - virBufferAdjustIndent(&childrenBuf, indent + 2); + virBufferSetChildIndent(&childrenBuf, buf); if (def->type =3D=3D VIR_CPU_TYPE_HOST && def->arch) virBufferAsprintf(&childrenBuf, "%s\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 =3D NULL; virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; =20 - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); =20 if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23287,7 +23287,7 @@ virDomainSmartcardDefFormat(virBufferPtr buf, virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; size_t i; =20 - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); =20 if (!mode) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23396,7 +23396,7 @@ virDomainSoundDefFormat(virBufferPtr buf, virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; size_t i; =20 - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); =20 if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23432,7 +23432,6 @@ virDomainMemballoonDefFormat(virBufferPtr buf, { const char *model =3D virDomainMemballoonModelTypeToString(def->model); virBuffer childrenBuf =3D VIR_BUFFER_INITIALIZER; - int indent =3D virBufferGetIndent(buf, false); =20 if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23446,7 +23445,7 @@ virDomainMemballoonDefFormat(virBufferPtr buf, virBufferAsprintf(buf, " autodeflate=3D'%s'", virTristateSwitchTypeToString(def->autodeflate)); =20 - virBufferAdjustIndent(&childrenBuf, indent + 2); + virBufferSetChildIndent(&childrenBuf, buf); =20 if (def->period) virBufferAsprintf(&childrenBuf, "\n", def->p= eriod); @@ -23508,7 +23507,7 @@ virDomainWatchdogDefFormat(virBufferPtr buf, const char *action =3D virDomainWatchdogActionTypeToString(def->action= ); virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; =20 - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); =20 if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -23545,7 +23544,6 @@ static int virDomainPanicDefFormat(virBufferPtr buf, virDomainPanicDefPtr def) { virBuffer childrenBuf =3D VIR_BUFFER_INITIALIZER; - int indent =3D virBufferGetIndent(buf, false); =20 virBufferAddLit(buf, "model)); =20 - virBufferAdjustIndent(&childrenBuf, indent + 2); + virBufferSetChildIndent(&childrenBuf, buf); virDomainDeviceInfoFormat(&childrenBuf, &def->info, 0); =20 if (virBufferCheckError(&childrenBuf) < 0) @@ -23888,7 +23886,7 @@ virDomainInputDefFormat(virBufferPtr buf, virBufferAsprintf(buf, "virtio); if (virBufferCheckError(&driverBuf) < 0) return -1; @@ -24538,7 +24536,7 @@ virDomainHubDefFormat(virBufferPtr buf, const char *type =3D virDomainHubTypeToString(def->type); virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; =20 - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); =20 if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -24751,7 +24749,7 @@ virDomainCputuneDefFormat(virBufferPtr buf, virBuffer childrenBuf =3D VIR_BUFFER_INITIALIZER; int ret =3D -1; =20 - virBufferAdjustIndent(&childrenBuf, virBufferGetIndent(buf, false) + 2= ); + virBufferSetChildIndent(&childrenBuf, buf); =20 if (def->cputune.sharesSpecified) virBufferAsprintf(&childrenBuf, "%llu\n", @@ -24933,7 +24931,7 @@ virDomainIOMMUDefFormat(virBufferPtr buf, { virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; =20 - virBufferAdjustIndent(&childBuf, virBufferGetIndent(buf, false) + 2); + virBufferSetChildIndent(&childBuf, buf); =20 if (iommu->intremap !=3D VIR_TRISTATE_SWITCH_ABSENT || iommu->caching_mode !=3D VIR_TRISTATE_SWITCH_ABSENT || @@ -24987,7 +24985,6 @@ virDomainDefFormatInternal(virDomainDefPtr def, int n; size_t i; virBuffer childrenBuf =3D VIR_BUFFER_INITIALIZER; - int indent; char *netprefix =3D NULL; =20 virCheckFlags(VIR_DOMAIN_DEF_FORMAT_COMMON_FLAGS | @@ -25065,8 +25062,7 @@ virDomainDefFormatInternal(virDomainDefPtr def, def->mem.cur_balloon); =20 /* start format blkiotune */ - indent =3D virBufferGetIndent(buf, false); - virBufferAdjustIndent(&childrenBuf, indent + 2); + virBufferSetChildIndent(&childrenBuf, buf); if (def->blkio.weight) virBufferAsprintf(&childrenBuf, "%u\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); =20 +/** + * 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); =20 void virBufferTrim(virBufferPtr buf, const char *trim, int len); --=20 2.13.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun May 5 17:48:09 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503588152100423.31159191739187; Thu, 24 Aug 2017 08:22:32 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 589628B134; Thu, 24 Aug 2017 15:22:30 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 388C27F8D7; Thu, 24 Aug 2017 15:22:30 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 023EF3FA58; Thu, 24 Aug 2017 15:22:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7OFMShv014784 for ; Thu, 24 Aug 2017 11:22:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0BD6B7009C; Thu, 24 Aug 2017 15:22:28 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 86FE07F474 for ; Thu, 24 Aug 2017 15:22:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 589628B134 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 24 Aug 2017 17:22:07 +0200 Message-Id: <0c0632fbcd54c71c0a947bfec03be7085ca93ff2.1503588082.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/3] util: introduce virXMLFormatElement helper X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 24 Aug 2017 15:22:30 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: John Ferlan --- 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) =3D=3D 0) && + (!childBuf || virBufferUse(childBuf) =3D=3D 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, "\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); =20 +int +virXMLFormatElement(virBufferPtr buf, + const char *name, + virBufferPtr attrBuf, + virBufferPtr childBuf); + #endif /* __VIR_XML_H__ */ --=20 2.13.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun May 5 17:48:09 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1503588168035827.5040048958656; Thu, 24 Aug 2017 08:22:48 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2EDE97F3F3; Thu, 24 Aug 2017 15:22:46 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C5E3B7EA4C; Thu, 24 Aug 2017 15:22:45 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 86BD24EE50; Thu, 24 Aug 2017 15:22:45 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7OFMis4015141 for ; Thu, 24 Aug 2017 11:22:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5114C7009C; Thu, 24 Aug 2017 15:22:44 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD4187FE86 for ; Thu, 24 Aug 2017 15:22:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2EDE97F3F3 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 24 Aug 2017 17:22:08 +0200 Message-Id: <1e392c8547f396ae640f5db0e7dfa67f3e503e72.1503588082.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/3] conf: don't close the source element inside different function X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 24 Aug 2017 15:22:46 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: John Ferlan --- 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; =20 if (nseclabels && !skipSeclables) { - virBufferAddLit(buf, ">\n"); - virBufferAdjustIndent(buf, 2); for (n =3D 0; n < nseclabels; n++) virSecurityDeviceLabelDefFormat(buf, seclabels[n], flags); - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); - } else { - virBufferAddLit(buf, "/>\n"); } } =20 @@ -21569,6 +21563,10 @@ virDomainDiskSourceFormatInternal(virBufferPtr buf, bool skipSeclabels) { const char *startupPolicy =3D NULL; + virBuffer attrBuf =3D VIR_BUFFER_INITIALIZER; + virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; + + virBufferSetChildIndent(&childBuf, buf); =20 if (policy) startupPolicy =3D virDomainStartupPolicyTypeToString(policy); @@ -21576,51 +21574,45 @@ virDomainDiskSourceFormatInternal(virBufferPtr bu= f, if (src->path || src->nhosts > 0 || src->srcpool || startupPolicy) { switch ((virStorageType)src->type) { case VIR_STORAGE_TYPE_FILE: - virBufferAddLit(buf, "path); - virBufferEscapeString(buf, " startupPolicy=3D'%s'", startupPol= icy); + virBufferEscapeString(&attrBuf, " file=3D'%s'", src->path); + virBufferEscapeString(&attrBuf, " startupPolicy=3D'%s'", start= upPolicy); =20 - virDomainDiskSourceDefFormatSeclabel(buf, src->nseclabels, + virDomainDiskSourceDefFormatSeclabel(&childBuf, src->nseclabel= s, src->seclabels, flags, skipSeclabels); break; =20 case VIR_STORAGE_TYPE_BLOCK: - virBufferAddLit(buf, "path); - virBufferEscapeString(buf, " startupPolicy=3D'%s'", startupPol= icy); + virBufferEscapeString(&attrBuf, " dev=3D'%s'", src->path); + virBufferEscapeString(&attrBuf, " startupPolicy=3D'%s'", start= upPolicy); =20 - virDomainDiskSourceDefFormatSeclabel(buf, src->nseclabels, + virDomainDiskSourceDefFormatSeclabel(&childBuf, src->nseclabel= s, src->seclabels, flags, skipSeclabels); break; =20 case VIR_STORAGE_TYPE_DIR: - virBufferAddLit(buf, "path); - virBufferEscapeString(buf, " startupPolicy=3D'%s'", startupPol= icy); - virBufferAddLit(buf, "/>\n"); + virBufferEscapeString(&attrBuf, " dir=3D'%s'", src->path); + virBufferEscapeString(&attrBuf, " startupPolicy=3D'%s'", start= upPolicy); break; =20 case VIR_STORAGE_TYPE_NETWORK: if (virDomainDiskSourceFormatNetwork(buf, src) < 0) - return -1; + goto error; break; =20 case VIR_STORAGE_TYPE_VOLUME: - virBufferAddLit(buf, "srcpool) { - virBufferEscapeString(buf, " pool=3D'%s'", src->srcpool->p= ool); - virBufferEscapeString(buf, " volume=3D'%s'", + virBufferEscapeString(&attrBuf, " pool=3D'%s'", src->srcpo= ol->pool); + virBufferEscapeString(&attrBuf, " volume=3D'%s'", src->srcpool->volume); if (src->srcpool->mode) - virBufferAsprintf(buf, " mode=3D'%s'", + virBufferAsprintf(&attrBuf, " mode=3D'%s'", virStorageSourcePoolModeTypeToString= (src->srcpool->mode)); } - virBufferEscapeString(buf, " startupPolicy=3D'%s'", startupPol= icy); + virBufferEscapeString(&attrBuf, " startupPolicy=3D'%s'", start= upPolicy); =20 - virDomainDiskSourceDefFormatSeclabel(buf, src->nseclabels, + virDomainDiskSourceDefFormatSeclabel(&childBuf, src->nseclabel= s, src->seclabels, flags, skipSeclabels); break; @@ -21629,11 +21621,19 @@ virDomainDiskSourceFormatInternal(virBufferPtr bu= f, 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; } =20 return 0; + + error: + virBufferFreeAndReset(&attrBuf); + virBufferFreeAndReset(&childBuf); + return -1; } =20 =20 @@ -23059,11 +23059,15 @@ virDomainChrAttrsDefFormat(virBufferPtr buf, return 0; } =20 -static void +static int virDomainChrSourceDefFormat(virBufferPtr buf, virDomainChrSourceDefPtr def, unsigned int flags) { + virBuffer attrBuf =3D VIR_BUFFER_INITIALIZER; + virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; + + virBufferSetChildIndent(&childBuf, buf); =20 switch ((virDomainChrType)def->type) { case VIR_DOMAIN_CHR_TYPE_NULL: @@ -23081,14 +23085,17 @@ virDomainChrSourceDefFormat(virBufferPtr buf, if (def->type !=3D VIR_DOMAIN_CHR_TYPE_PTY || (def->data.file.path && !(flags & VIR_DOMAIN_DEF_FORMAT_INACTIVE))) { - virBufferEscapeString(buf, "data.file.path); if (def->type =3D=3D VIR_DOMAIN_CHR_TYPE_FILE && def->data.file.append !=3D VIR_TRISTATE_SWITCH_ABSENT) - virBufferAsprintf(buf, " append=3D'%s'", + virBufferAsprintf(&attrBuf, " append=3D'%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; =20 @@ -23119,19 +23126,21 @@ virDomainChrSourceDefFormat(virBufferPtr buf, break; =20 case VIR_DOMAIN_CHR_TYPE_TCP: - virBufferAsprintf(buf, "data.tcp.listen ? "bind" : "connect"); - virBufferEscapeString(buf, "host=3D'%s' ", def->data.tcp.host); - virBufferEscapeString(buf, "service=3D'%s'", def->data.tcp.service= ); + virBufferEscapeString(&attrBuf, "host=3D'%s' ", def->data.tcp.host= ); + virBufferEscapeString(&attrBuf, "service=3D'%s'", def->data.tcp.se= rvice); if (def->data.tcp.haveTLS !=3D VIR_TRISTATE_BOOL_ABSENT && !(flags & VIR_DOMAIN_DEF_FORMAT_MIGRATABLE && def->data.tcp.tlsFromConfig)) - virBufferAsprintf(buf, " tls=3D'%s'", + virBufferAsprintf(&attrBuf, " tls=3D'%s'", virTristateBoolTypeToString(def->data.tcp.haveTLS)); if (flags & VIR_DOMAIN_DEF_FORMAT_STATUS) - virBufferAsprintf(buf, " tlsFromConfig=3D'%d'", + virBufferAsprintf(&attrBuf, " tlsFromConfig=3D'%d'", def->data.tcp.tlsFromConfig); - virBufferAddLit(buf, "/>\n"); + + if (virXMLFormatElement(buf, "source", &attrBuf, &childBuf) < 0) + goto error; =20 virBufferAsprintf(buf, "\n", virDomainChrTcpProtocolTypeToString( @@ -23140,11 +23149,14 @@ virDomainChrSourceDefFormat(virBufferPtr buf, =20 case VIR_DOMAIN_CHR_TYPE_UNIX: if (def->data.nix.path) { - virBufferAsprintf(buf, "data.nix.listen ? "bind" : "connect"); - virBufferEscapeString(buf, " path=3D'%s'", def->data.nix.path); - virDomainSourceDefFormatSeclabel(buf, def->nseclabels, + virBufferEscapeString(&attrBuf, " path=3D'%s'", def->data.nix.= path); + virDomainSourceDefFormatSeclabel(&childBuf, def->nseclabels, def->seclabels, flags); + + if (virXMLFormatElement(buf, "source", &attrBuf, &childBuf) < = 0) + goto error; } break; =20 @@ -23164,7 +23176,12 @@ virDomainChrSourceDefFormat(virBufferPtr buf, virBufferAddLit(buf, "/>\n"); } =20 - return; + return 0; + + error: + virBufferFreeAndReset(&attrBuf); + virBufferFreeAndReset(&childBuf); + return -1; } =20 static int diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-seclabel-dynamic-label= skip.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-seclabel-dynamic-labelsk= ip.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 @@ /usr/bin/qemu-system-i686 - - +
--=20 2.13.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list