From nobody Sun Feb 8 12:37:42 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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.129.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=1655393835; cv=none; d=zohomail.com; s=zohoarc; b=kHfeYoeJ07QsUduPsxtXmb2DfbsITNxmYeXOzZREFMtH3w9iwC9DAYId2/fhCUx+oM4tVVb2rQyhli+s6AVnbMPObaD0VkRJNzj+h4t9tEbeiM/sfhrh0HQgrfMjKbGrzeB+U7uS1Fli9O4ue10GGyXMJAvT9TmSFGXNKFyi8Ys= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655393835; h=Content-Type:Content-Transfer-Encoding:Cc: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=f6BphaxXhVWengZi4NZOzEenNVjz3vKvfyOmU0gCF48=; b=Z8+XZ4TWJrTz4nBJNxByIKFpnrcMl+DEE6xZn1+y5r4EsQoTnx05Y6l+ad+1YGdoM4wB5bEX+aaSE8pOInRlFlvd1K1D01G3RPKaZILMriZpS0Exio8kaWWfIhUBWXtxePbfsRWN4bcvEhwxbQS6MA7vbuRRWanH3BaguIz6pVo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1655393835733555.0924470964837; Thu, 16 Jun 2022 08:37:15 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-19-bnSCpY42MciSj5gqe5sSaw-1; Thu, 16 Jun 2022 11:37:05 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 09C3F8339A2; Thu, 16 Jun 2022 15:37:01 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 977231415106; Thu, 16 Jun 2022 15:36:57 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 68974194705F; Thu, 16 Jun 2022 15:36:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 79240194706C for ; Thu, 16 Jun 2022 15:36:56 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 6BAE52166B29; Thu, 16 Jun 2022 15:36:56 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.33.36.111]) by smtp.corp.redhat.com (Postfix) with ESMTP id B77D72166B26; Thu, 16 Jun 2022 15:36:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655393834; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=f6BphaxXhVWengZi4NZOzEenNVjz3vKvfyOmU0gCF48=; b=NlUpjx01yG4VL5gDiokp8rgHHNlOynrpoVkFfTq7O1bpDhKlQ7Appuoizook4bh88eVwKo u/7rqgGGK70H2eA18xNd6D5+lnCExuh9IYlLlUfYPtY33XPm2eItumKc8tmIrJ+9MJ7q+I 11XwOzOHxj5VPDR9RPHC/gmM249Xerg= X-MC-Unique: bnSCpY42MciSj5gqe5sSaw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 1/2] tools: add helper method for printing an XML document Date: Thu, 16 Jun 2022 16:36:52 +0100 Message-Id: <20220616153653.1046960-2-berrange@redhat.com> In-Reply-To: <20220616153653.1046960-1-berrange@redhat.com> References: <20220616153653.1046960-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stephen Finucane Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1655393837632100001 The trivial case of fully printing an XML document is boring, but this helper does more by allowing an XPath expression to be given. It will then print just the subset of nodes which match the expression. It either print each match as a standalone XML doc or can put them into one new XML doc wrapped woith ... Signed-off-by: Daniel P. Berrang=C3=A9 --- tools/virsh-util.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ tools/virsh-util.h | 7 ++++++ 2 files changed, 66 insertions(+) diff --git a/tools/virsh-util.c b/tools/virsh-util.c index dc6ed7a86d..8b06091be8 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -437,3 +437,62 @@ virshDomainBlockJobToString(int type) const char *str =3D virshDomainBlockJobTypeToString(type); return str ? _(str) : _("Unknown job"); } + +bool +virshDumpXML(vshControl *ctl, + const char *xml, + const char *url, + const char *xpath, + bool wrap) +{ + g_autoptr(xmlDoc) doc =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + g_autofree xmlNodePtr *nodes =3D NULL; + int nnodes =3D 0; + size_t i; + int oldblanks; + + if (xpath =3D=3D NULL) { + vshPrint(ctl, "%s", xml); + return true; + } + + oldblanks =3D xmlKeepBlanksDefault(0); + doc =3D virXMLParseString(xml, url); + xmlKeepBlanksDefault(oldblanks); + if (!doc) + return false; + + if (!(ctxt =3D virXMLXPathContextNew(doc))) + return false; + + if ((nnodes =3D virXPathNodeSet(xpath, ctxt, &nodes)) < 0) { + return false; + } + + if (wrap) { + g_autoptr(xmlDoc) newdoc =3D xmlNewDoc((xmlChar *)"1.0"); + xmlNodePtr newroot =3D xmlNewNode(NULL, (xmlChar *)"nodes"); + g_autofree char *xmlbit =3D NULL; + + xmlDocSetRootElement(newdoc, newroot); + + for (i =3D 0; i < nnodes; i++) { + g_autoptr(xmlNode) copy =3D xmlDocCopyNode(nodes[i], newdoc, 1= ); + if (!xmlAddChild(newroot, copy)) + return false; + + copy =3D NULL; + } + + xmlbit =3D virXMLNodeToString(doc, newroot); + vshPrint(ctl, "%s\n", xmlbit); + } else { + for (i =3D 0; i < nnodes; i++) { + g_autofree char *xmlbit =3D virXMLNodeToString(doc, nodes[i]); + vshPrint(ctl, "%s\n", xmlbit); + } + } + + return true; +} diff --git a/tools/virsh-util.h b/tools/virsh-util.h index 4d4fe6c01e..0f81a2771b 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -156,3 +156,10 @@ VIR_ENUM_DECL(virshDomainBlockJob); =20 const char * virshDomainBlockJobToString(int type); + +bool +virshDumpXML(vshControl *ctl, + const char *xml, + const char *url, + const char *xpath, + bool wrap); --=20 2.36.1