From nobody Sun Feb 8 21:33:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621936138; cv=none; d=zohomail.com; s=zohoarc; b=EKgYXZ1TUpgvmTX+Xl/lGpyczFzdHz0Th8mmDrGDk+LaEU8z0fcGAEm1y5gBzi4ZDhZo/Doivz1xYq8ukHE59K7Li+HpPIp2ccnAp3PE2bv//DEvWt9LExxwrtva1BjR5pGwxb60U3hubsBzNc0zCq6XychVaz7qSuwmYt/8urA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621936138; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=MRpPj8M2wY9RcMQHwzrBjlb9Xy1AC7w+Ut1LgKn/l9Y=; b=eVYvDBqBi1ZMCnuKcJQ+Zi8HYBvGOrTcieOJmiKPdBtc5zRu49twthxIuWt5Jlvd+N6W9UIf2PgCincR3/YzJ/NZxVX29wV4OWohepT+4U0c7SdFBM3wzONFLdZ8N8ollgRbx/LdDVnkJjmafzeg/ZM8K/bHA8pTrzI3hfWCQKU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1621936138331260.6843748187882; Tue, 25 May 2021 02:48:58 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-570-o-khSElOMqOzQYqwRBIeQA-1; Tue, 25 May 2021 05:48:55 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 87359180FD69; Tue, 25 May 2021 09:48:50 +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 5F17F18026; Tue, 25 May 2021 09:48:50 +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 282CC180B463; Tue, 25 May 2021 09:48:50 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14P9mYf3031349 for ; Tue, 25 May 2021 05:48:34 -0400 Received: by smtp.corp.redhat.com (Postfix) id CD76B6E6F5; Tue, 25 May 2021 09:48:34 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.195.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id 525C86C332 for ; Tue, 25 May 2021 09:48:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621936137; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=MRpPj8M2wY9RcMQHwzrBjlb9Xy1AC7w+Ut1LgKn/l9Y=; b=CaBaJWNT3hEEg9IVDl0Ul4eh09GQbvl26vHuvblkiwA3zRC4rROQt7AsZ0VVkc3YHWwEXS uX1d3kTh9U09uzCaBZoE762bakBEJFfeeYWDEP+2JfjwKrKL+Z5tZe1ehlrvUGZHBbIrem a7v2H0R7GQbrl+4eyj1QjU4qAU7V8C4= X-MC-Unique: o-khSElOMqOzQYqwRBIeQA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 1/3] virxml: Introduce and use virXMLFormatMetadata() Date: Tue, 25 May 2021 11:48:26 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" So far, we have to places where we format into XMLs: domain and network. Bot places share the same code. Move it into a helper function and just call it from those places. Signed-off-by: Michal Privoznik --- src/conf/domain_conf.c | 23 ++------------------- src/conf/network_conf.c | 23 ++------------------- src/libvirt_private.syms | 1 + src/util/virxml.c | 43 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 3 +++ 5 files changed, 51 insertions(+), 42 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 4f78b7b43d..413c44ac61 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -27806,27 +27806,8 @@ virDomainDefFormatInternalSetRootName(virDomainDef= *def, virBufferEscapeString(buf, "%s\n", def->description); =20 - if (def->metadata) { - g_autoptr(xmlBuffer) xmlbuf =3D NULL; - int oldIndentTreeOutput =3D xmlIndentTreeOutput; - - /* Indentation on output requires that we previously set - * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2 - * spaces per level of indentation of intermediate elements, - * but no leading indentation before the starting element. - * Thankfully, libxml maps what looks like globals into - * thread-local uses, so we are thread-safe. */ - xmlIndentTreeOutput =3D 1; - xmlbuf =3D virXMLBufferCreate(); - - if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata, - virBufferGetIndent(buf) / 2, 1) < 0) { - xmlIndentTreeOutput =3D oldIndentTreeOutput; - return -1; - } - virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); - xmlIndentTreeOutput =3D oldIndentTreeOutput; - } + if (virXMLFormatMetadata(buf, def->metadata) < 0) + return -1; =20 if (virDomainDefHasMemoryHotplug(def)) { virBufferAsprintf(buf, diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index a9eadff29c..b10ff5c7a8 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -2486,27 +2486,8 @@ virNetworkDefFormatBuf(virBuffer *buf, virUUIDFormat(uuid, uuidstr); virBufferAsprintf(buf, "%s\n", uuidstr); =20 - if (def->metadata) { - g_autoptr(xmlBuffer) xmlbuf =3D NULL; - int oldIndentTreeOutput =3D xmlIndentTreeOutput; - - /* Indentation on output requires that we previously set - * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2 - * spaces per level of indentation of intermediate elements, - * but no leading indentation before the starting element. - * Thankfully, libxml maps what looks like globals into - * thread-local uses, so we are thread-safe. */ - xmlIndentTreeOutput =3D 1; - xmlbuf =3D virXMLBufferCreate(); - - if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata, - virBufferGetIndent(buf) / 2, 1) < 0) { - xmlIndentTreeOutput =3D oldIndentTreeOutput; - return -1; - } - virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); - xmlIndentTreeOutput =3D oldIndentTreeOutput; - } + if (virXMLFormatMetadata(buf, def->metadata) < 0) + return -1; =20 if (def->forward.type !=3D VIR_NETWORK_FORWARD_NONE) { const char *dev =3D NULL; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e1aef5267e..37515f80ec 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3555,6 +3555,7 @@ virXMLCheckIllegalChars; virXMLExtractNamespaceXML; virXMLFormatElement; virXMLFormatElementEmpty; +virXMLFormatMetadata; virXMLNewNode; virXMLNodeContentString; virXMLNodeNameEqual; diff --git a/src/util/virxml.c b/src/util/virxml.c index 8dcece704a..062a5402f6 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1707,6 +1707,49 @@ virXMLFormatElement(virBuffer *buf, } =20 =20 +/** + * virXMLFormatMetadata: + * @buf: the parent buffer where the element will be placed + * @metadata: pointer to metadata node + * + * Helper to format metadata element. If @metadata is NULL then + * this function is a NOP. + * + * Returns: 0 on success, + * -1 otherwise. + */ +int +virXMLFormatMetadata(virBuffer *buf, + xmlNodePtr metadata) +{ + g_autoptr(xmlBuffer) xmlbuf =3D NULL; + int oldIndentTreeOutput =3D xmlIndentTreeOutput; + + if (!metadata) + return 0; + + /* Indentation on output requires that we previously set + * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2 + * spaces per level of indentation of intermediate elements, + * but no leading indentation before the starting element. + * Thankfully, libxml maps what looks like globals into + * thread-local uses, so we are thread-safe. */ + xmlIndentTreeOutput =3D 1; + xmlbuf =3D virXMLBufferCreate(); + + if (xmlNodeDump(xmlbuf, metadata->doc, metadata, + virBufferGetIndent(buf) / 2, 1) < 0) { + xmlIndentTreeOutput =3D oldIndentTreeOutput; + return -1; + } + + virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf)); + xmlIndentTreeOutput =3D oldIndentTreeOutput; + + return 0; +} + + void virXPathContextNodeRestore(virXPathContextNodeSave *save) { diff --git a/src/util/virxml.h b/src/util/virxml.h index ed02abd2e9..0bb0d1c118 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -333,6 +333,9 @@ virXMLFormatElementEmpty(virBuffer *buf, virBuffer *attrBuf, virBuffer *childBuf); =20 +int +virXMLFormatMetadata(virBuffer *buf, + xmlNodePtr metadata); =20 struct _virXPathContextNodeSave { xmlXPathContextPtr ctxt; --=20 2.26.3