From nobody Sun Feb 8 00:56:15 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=1669990631; cv=none; d=zohomail.com; s=zohoarc; b=gJiv5dqQ7ftL7do4+R2005EDFItiP3dhXv/Yrl13DCbCNYnV4BvWFJbfPLwJiYcqLDBSTaPoW5O4nEbSrhhRh7ZSLQWeofzNxqf1lqKMGQrJSfatVydC5k/JmOsf/3rgW5xcZHHQSKYZ7cc5d+ZIIH8ZRml/Ab0HFzywbz26tk8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990631; 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=b9wJnoYtCpcKylLcTtEl+18YlR5f73TSe6JmKQ7WYX8=; b=ezZRie8HikbdjMetfdNnxYIm5XxKunmbJhCVmYOxpHi6otuebZaw9JGkngrpSMKnxtbtcfLl9EP7oI1XR8hEyGS/MSq7UfUkvjdq/xSVx666vNy+b1I3OiTphImUta6tPlZ9b1plM0TmM3ytYOA4XCrFJEoDfEtTcvh+Mt3a5RE= 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 1669990631086604.3942714489307; Fri, 2 Dec 2022 06:17:11 -0800 (PST) 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-55-xkyXRFdzMWeDGEhOxPEHew-1; Fri, 02 Dec 2022 09:17:07 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 431B48027F5; Fri, 2 Dec 2022 14:17:03 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 27E4563F45; Fri, 2 Dec 2022 14:17:03 +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 E91F61946A48; Fri, 2 Dec 2022 14:17:01 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 1D9351946A47 for ; Fri, 2 Dec 2022 14:17:01 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 02E8540E9789; Fri, 2 Dec 2022 14:17:01 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id D372840E9785 for ; Fri, 2 Dec 2022 14:16:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990630; 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=b9wJnoYtCpcKylLcTtEl+18YlR5f73TSe6JmKQ7WYX8=; b=ZXX+NcjHliDH9sFzgJ+mdWKBflqpPMKDFRrSqRjaoV3b3JL+++PFpo8hBWzZhC2/sa6h59 mhrcZefaBfDjKE2ylKlUbTZgdsKP71ZrltT3g2xgrSv2OYlQgJN209dwGyRXWpKt17YDNQ y108KFOm46DQ1NnSpfg0Uy5vZdZgQyY= X-MC-Unique: xkyXRFdzMWeDGEhOxPEHew-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 01/13] virsh: Add --print-xml option for 'detach-interface' Date: Fri, 2 Dec 2022 15:16:45 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990632499100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- docs/manpages/virsh.rst | 5 ++++- tools/virsh-domain.c | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 1e8a43bc55..09fc1f67ad 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -5090,7 +5090,7 @@ detach-interface :: detach-interface domain type [--mac mac] - [[[--live] [--config] | [--current]] | [--persistent]] + [[[--live] [--config] | [--current]] | [--persistent]] [--print-xml] Detach a network interface from a domain. *type* can be either *network* to indicate a physical network device or @@ -5112,6 +5112,9 @@ an offline domain, and like *--live* *--config* for a= running domain. Note that older versions of virsh used *--config* as an alias for *--persistent*. +If *--print-xml* is specified, then the XML used to detach the interface +is printed instead. + Please see documentation for ``detach-device`` for known quirks. diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 2d22547cc6..9574a6eab6 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -12454,6 +12454,10 @@ static const vshCmdOptDef opts_detach_interface[] = =3D { VIRSH_COMMON_OPT_DOMAIN_CONFIG, VIRSH_COMMON_OPT_DOMAIN_LIVE, VIRSH_COMMON_OPT_DOMAIN_CURRENT, + {.name =3D "print-xml", + .type =3D VSH_OT_BOOL, + .help =3D N_("print XML document rather than detach the interface") + }, {.name =3D NULL} }; @@ -12464,7 +12468,8 @@ virshDomainDetachInterface(char *doc, vshControl *ctl, bool current, const char *type, - const char *mac) + const char *mac, + bool printxml) { g_autoptr(xmlDoc) xml =3D NULL; g_autoptr(xmlXPathObject) obj =3D NULL; @@ -12533,6 +12538,11 @@ virshDomainDetachInterface(char *doc, return false; } + if (printxml) { + vshPrint(ctl, "%s", detach_xml); + return true; + } + if (flags !=3D 0 || current) return virDomainDetachDeviceFlags(dom, detach_xml, flags) =3D=3D 0; return virDomainDetachDevice(dom, detach_xml) =3D=3D 0; @@ -12552,6 +12562,7 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *c= md) bool config =3D vshCommandOptBool(cmd, "config"); bool live =3D vshCommandOptBool(cmd, "live"); bool persistent =3D vshCommandOptBool(cmd, "persistent"); + bool printxml =3D vshCommandOptBool(cmd, "print-xml"); VSH_EXCLUSIVE_OPTIONS_VAR(persistent, current); @@ -12574,7 +12585,8 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *c= md) goto cleanup; if (!(ret =3D virshDomainDetachInterface(doc_config, flags | VIR_DOMAIN_AFFECT_C= ONFIG, - dom, ctl, current, type, ma= c))) + dom, ctl, current, type, ma= c, + printxml))) goto cleanup; } @@ -12590,9 +12602,12 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *= cmd) goto cleanup; ret =3D virshDomainDetachInterface(doc_live, flags, - dom, ctl, current, type, mac); + dom, ctl, current, type, mac, pri= ntxml); } + if (printxml) + return true; + cleanup: if (!ret) { vshError(ctl, "%s", _("Failed to detach interface")); --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990643; cv=none; d=zohomail.com; s=zohoarc; b=AK5DCMcQucTijvdJPMZ0WgOFDt+0pWnTBHQQypZZlOJ67sJuIhjJRene/HXB5Vs6M3lHUPRRlA2Jb23ATPZDoIaRUfXuRrkgfN24hL0wobYAoGc6atGXxQzmQa3ibmfzB+ucvP/bjQP/7AAF8OAtvPITRgyAy+dglb5AqIyFa7Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990643; 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=iSDsaFSU/WzbWLcbmJRLsvL1xT12Ug3N7df3DEeZPlQ=; b=eGEi8RbMmowDGwQ8dLInF0JViYeYiaKknEWcIoDpo6Ty7lJmY79aQQOLjl9Efa4ONGBTKw89Ekk3eU8AUNk8qJ4olhaxufCTuPl65p4nC6x7X/HyrktkmnBgj6UtcaFdX1tkXmMNBFzmZbukYXdAReu7nQkdOOGvVDKU8CN/GYQ= 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 1669990643576410.134740759362; Fri, 2 Dec 2022 06:17:23 -0800 (PST) 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-381-_4crIsvNMgGBBI0zam2aFA-1; Fri, 02 Dec 2022 09:17:09 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C8ECE801231; Fri, 2 Dec 2022 14:17:04 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id B4A7A2166BC8; Fri, 2 Dec 2022 14:17:04 +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 8D49D194658F; Fri, 2 Dec 2022 14:17:04 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id C0D1C194658F for ; Fri, 2 Dec 2022 14:17:02 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id B251840E9787; Fri, 2 Dec 2022 14:17:02 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8BFBB40E9785 for ; Fri, 2 Dec 2022 14:17:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990642; 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=iSDsaFSU/WzbWLcbmJRLsvL1xT12Ug3N7df3DEeZPlQ=; b=ZlpMrSN5KFe3DLriNjO4U2XyvDpnwC4aKxoiI7G5thudda2Ql5SMqk9rGqqwAhXmgk2nRu KEwvLQAhRBOedEbIe3+l2xnAmhp6ymwknFXy6KeIrrA2jNMoRtXoDkYE/J4lqgzTlAgnwZ Gwjan6VEJGXdMn7/+TNoZOmbBes/ego= X-MC-Unique: _4crIsvNMgGBBI0zam2aFA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 02/13] virshDomainDetachInterface: Use virXPathNodeSet instead of xmlXpathEval Date: Fri, 2 Dec 2022 15:16:46 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990644501100001 Content-Type: text/plain; charset="utf-8" Refactor the XPath lookup to use the internal helper. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain.c | 52 ++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 9574a6eab6..87de3a708a 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -12472,12 +12472,12 @@ virshDomainDetachInterface(char *doc, bool printxml) { g_autoptr(xmlDoc) xml =3D NULL; - g_autoptr(xmlXPathObject) obj =3D NULL; g_autoptr(xmlXPathContext) ctxt =3D NULL; - xmlNodePtr cur =3D NULL, matchNode =3D NULL; g_autofree char *detach_xml =3D NULL; - char buf[64]; - int diff_mac =3D -1; + g_autofree char *xpath =3D g_strdup_printf("/domain/devices/interface[= @type=3D'%s']", type); + g_autofree xmlNodePtr *nodes =3D NULL; + ssize_t nnodes; + xmlNodePtr matchNode =3D NULL; size_t i; if (!(xml =3D virXMLParseStringCtxt(doc, _("(domain_definition)"), &ct= xt))) { @@ -12485,34 +12485,20 @@ virshDomainDetachInterface(char *doc, return false; } - g_snprintf(buf, sizeof(buf), "/domain/devices/interface[@type=3D'%s']"= , type); - obj =3D xmlXPathEval(BAD_CAST buf, ctxt); - if (obj =3D=3D NULL || obj->type !=3D XPATH_NODESET || - obj->nodesetval =3D=3D NULL || obj->nodesetval->nodeNr =3D=3D 0) { + if ((nnodes =3D virXPathNodeSet(xpath, ctxt, &nodes)) <=3D 0) { vshError(ctl, _("No interface found whose type is %s"), type); return false; } - if (!mac && obj->nodesetval->nodeNr > 1) { - vshError(ctl, _("Domain has %d interfaces. Please specify which on= e " - "to detach using --mac"), obj->nodesetval->nodeNr); - return false; - } + if (mac) { + for (i =3D 0; i < nnodes; i++) { + g_autofree char *tmp_mac =3D NULL; - if (!mac) { - matchNode =3D obj->nodesetval->nodeTab[0]; - goto hit; - } + ctxt->node =3D nodes[i]; - /* multiple possibilities, so search for matching mac */ - for (i =3D 0; i < obj->nodesetval->nodeNr; i++) { - cur =3D obj->nodesetval->nodeTab[i]->children; - while (cur !=3D NULL) { - if (cur->type =3D=3D XML_ELEMENT_NODE && - virXMLNodeNameEqual(cur, "mac")) { - g_autofree char *tmp_mac =3D virXMLPropString(cur, "addres= s"); - diff_mac =3D virMacAddrCompare(tmp_mac, mac); - if (!diff_mac) { + if ((tmp_mac =3D virXPathString("string(./mac/@address)", ctxt= ))) { + + if (virMacAddrCompare(tmp_mac, mac) =3D=3D 0) { if (matchNode) { /* this is the 2nd match, so it's ambiguous */ vshError(ctl, _("Domain has multiple interfaces ma= tching " @@ -12521,18 +12507,26 @@ virshDomainDetachInterface(char *doc, mac); return false; } - matchNode =3D obj->nodesetval->nodeTab[i]; + + matchNode =3D nodes[i]; } } - cur =3D cur->next; } + } else { + if (nnodes > 1) { + vshError(ctl, _("Domain has %zd interfaces. Please specify whi= ch one to detach using --mac"), + nnodes); + return false; + } + + matchNode =3D nodes[0]; } + if (!matchNode) { vshError(ctl, _("No interface with MAC address %s was found"), mac= ); return false; } - hit: if (!(detach_xml =3D virXMLNodeToString(xml, matchNode))) { vshSaveLibvirtError(); return false; --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990633; cv=none; d=zohomail.com; s=zohoarc; b=VFzWZ3dt3UoBCeQ5QqTeZfZPxXOdSwJac0n5Wxh0/o1mSuZSushPA75qStJ7Aw4HZ0FQSCvBwQlQBW15evaCqr4XSCCSqjk/MdEdDvT+l/BOn20VFGBV5X2WHaL6aWOGicJe0zOBMXOf7bQUR4K7qUCE0k+K8MHVaxHpDqOpqHc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990633; 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=dY3Os3R5dXGDzn4j+tU5EyUxkrAtXohmYxyRrRjsXMQ=; b=mH3MsnY4HC6vxu3l9q0CzkxY72cUugEkPY7YySsuCP2ISvQA/YvMDOZ5fpioju0YGuGG/5HwSphvJkAMGK7hOyC7rueZCCvhJiq3PiZRaB/TLcf4dEPXK6uMBVFh0tfDLBgYazZoOk69s033EJujpeLVe4yjp4HzaAbjZ/VvjZM= 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) 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 1669990633412223.22759297835; Fri, 2 Dec 2022 06:17:13 -0800 (PST) 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-438-9HeB91WeNFSiYM3NIMbBrA-1; Fri, 02 Dec 2022 09:17:09 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5BABC86C15A; Fri, 2 Dec 2022 14:17:05 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 495E163A55; Fri, 2 Dec 2022 14:17:05 +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 3902619465A0; Fri, 2 Dec 2022 14:17:05 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 293A9194658F for ; Fri, 2 Dec 2022 14:17:04 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 0DF3A40E9787; Fri, 2 Dec 2022 14:17:04 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4F35540E9785 for ; Fri, 2 Dec 2022 14:17:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990632; 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=dY3Os3R5dXGDzn4j+tU5EyUxkrAtXohmYxyRrRjsXMQ=; b=Df9fuyUFKdlWTps59X4spXWF0WNuSbmE9bSmAH8LbFoI94KE0TGqc055tBKGN43sY1sstN PSMnCXrtEIWfeicEZfZxgm08jAX7SmiNlBJkAj3XqwWB/rao07eXu3puGyQ60m0MyUfLBZ MAy/7ufN5B4AqgOi7nuMSUX+iMr8M8U= X-MC-Unique: 9HeB91WeNFSiYM3NIMbBrA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 03/13] virsh: Add --print-xml option for 'domif-setlink' Date: Fri, 2 Dec 2022 15:16:47 +0100 Message-Id: <766f2f405a95a2420fe56d93cf61d2ddf38b594f.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990634478100003 Content-Type: text/plain; charset="utf-8" Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- docs/manpages/virsh.rst | 5 ++++- tools/virsh-domain.c | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 09fc1f67ad..14ed9cd5a0 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -1890,7 +1890,7 @@ domif-setlink :: - domif-setlink domain interface-device state [--config] + domif-setlink domain interface-device state [--config] [--print-xml] Modify link state of the domain's virtual interface. Possible values for state are "up" and "down". If *--config* is specified, only the persistent @@ -1898,6 +1898,9 @@ configuration of the domain is modified, for compatib= ility purposes, *--persistent* is alias of *--config*. *interface-device* can be the interface's target name or the MAC address. +If *--print-xml* is specified, then the XML used to update the interface is +printed instead. + domifaddr --------- diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 87de3a708a..9baab2672a 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -3120,6 +3120,10 @@ static const vshCmdOptDef opts_domif_setlink[] =3D { .help =3D "config" }, VIRSH_COMMON_OPT_DOMAIN_CONFIG, + {.name =3D "print-xml", + .type =3D VSH_OT_BOOL, + .help =3D N_("print XML document rather than set the interface link s= tate") + }, {.name =3D NULL} }; @@ -3238,6 +3242,11 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd) return false; } + if (vshCommandOptBool(cmd, "print-xml")) { + vshPrint(ctl, "%s", xml_buf); + return true; + } + if (virDomainUpdateDeviceFlags(dom, xml_buf, flags) < 0) { vshError(ctl, _("Failed to update interface link state")); return false; --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990638; cv=none; d=zohomail.com; s=zohoarc; b=Nu04IyFgT5BHHrPx9J/IsdLCjVTjApkOr/EwLBawCZiB0TMkylJV+sp92/LCxeMkrTQFXJgN6UWW/NviGHuoksSG00w1OOp6ciPyhixhG9zvKwcTSIbhhlNNibpAplaTeGhqXEi5WkPtkCx6cX4rbphlZPRejxpz27c/ddNAAo4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990638; 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=sKUnexCWjA6X/JlBxArR5sLEQPkDfqmpZqr6e6MkRQY=; b=BYd4Tm5T08vKgtwyG0CFCTkzT+s0XKgZ5ho7oQaO5kBAty52C1k2F6tdJLLBFujTWZOvmXyiwyA3R9Njxr3i00lDa9mNXS+WvVA/vXRQv5BOfjte3Q5x+lbPnTSwPE8loNnfCFO/S4b3o+4bW8VsVSnoOv1gTBm1/lha1YJockE= 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) 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 16699906380791013.5302490603273; Fri, 2 Dec 2022 06:17:18 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-472-FNj6wiUWNVOb09C4D1k8ZQ-1; Fri, 02 Dec 2022 09:17:11 -0500 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A2CF52A59569; Fri, 2 Dec 2022 14:17:06 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 90D76483EFB; Fri, 2 Dec 2022 14:17:06 +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 822EE194658F; Fri, 2 Dec 2022 14:17:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 5F02819465A0 for ; Fri, 2 Dec 2022 14:17:05 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 50D4A40E9789; Fri, 2 Dec 2022 14:17:05 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7C2C940E9785 for ; Fri, 2 Dec 2022 14:17:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990637; 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=sKUnexCWjA6X/JlBxArR5sLEQPkDfqmpZqr6e6MkRQY=; b=ixY13/WPGb1bBz2vRGSkb5UIejOZXVCDjjABHEbfxy0D9MMjLUXr+P97xuUwx2YcQnXdQX TB0YTB7gWVZZptluiQnQhpelIlfcI3MOIKCwb9/E0B5PuTaS0bDoEVLZhlmaNDQjYODBal K/tF8AwWmEGt3i6EhbHFtDxyA6YqpqM= X-MC-Unique: FNj6wiUWNVOb09C4D1k8ZQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 04/13] virsh: cmdDomIfSetLink: Use virXPathNodeSet instead of xmlXpathEval Date: Fri, 2 Dec 2022 15:16:48 +0100 Message-Id: <70869ce75732e204a581a7089127ae495437b60a.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990638555100001 Content-Type: text/plain; charset="utf-8" Refactor the XPath lookup to use the internal helper. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain.c | 93 ++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 55 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 9baab2672a..2fb1751f25 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -3133,18 +3133,17 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd) g_autoptr(virshDomain) dom =3D NULL; const char *iface; const char *state; - virMacAddr macaddr; - const char *element; - const char *attr; - bool config; unsigned int flags =3D 0; unsigned int xmlflags =3D 0; size_t i; g_autoptr(xmlDoc) xml =3D NULL; g_autoptr(xmlXPathContext) ctxt =3D NULL; - g_autoptr(xmlXPathObject) obj =3D NULL; - xmlNodePtr cur =3D NULL; g_autofree char *xml_buf =3D NULL; + g_autofree xmlNodePtr *nodes =3D NULL; + ssize_t nnodes; + xmlNodePtr ifaceNode =3D NULL; + xmlNodePtr linkNode =3D NULL; + xmlAttrPtr stateAttr; if (!(dom =3D virshCommandOptDomain(ctl, cmd, NULL))) return false; @@ -3153,14 +3152,12 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd) vshCommandOptStringReq(ctl, cmd, "state", &state) < 0) return false; - config =3D vshCommandOptBool(cmd, "config"); - if (STRNEQ(state, "up") && STRNEQ(state, "down")) { vshError(ctl, _("invalid link state '%s'"), state); return false; } - if (config) { + if (vshCommandOptBool(cmd, "config")) { flags =3D VIR_DOMAIN_AFFECT_CONFIG; xmlflags |=3D VIR_DOMAIN_XML_INACTIVE; } else { @@ -3173,70 +3170,56 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd) if (virshDomainGetXMLFromDom(ctl, dom, xmlflags, &xml, &ctxt) < 0) return false; - obj =3D xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt); - if (obj =3D=3D NULL || obj->type !=3D XPATH_NODESET || - obj->nodesetval =3D=3D NULL || obj->nodesetval->nodeNr =3D=3D 0) { + if ((nnodes =3D virXPathNodeSet("/domain/devices/interface", ctxt, &no= des)) <=3D 0) { vshError(ctl, _("Failed to extract interface information or no int= erfaces found")); return false; } - if (virMacAddrParse(iface, &macaddr) =3D=3D 0) { - element =3D "mac"; - attr =3D "address"; - } else { - element =3D "target"; - attr =3D "dev"; - } + for (i =3D 0; i < nnodes; i++) { + g_autofree char *macaddr =3D NULL; + g_autofree char *target =3D NULL; - /* find interface with matching mac addr */ - for (i =3D 0; i < obj->nodesetval->nodeNr; i++) { - cur =3D obj->nodesetval->nodeTab[i]->children; + ctxt->node =3D nodes[i]; - while (cur) { - if (cur->type =3D=3D XML_ELEMENT_NODE && - virXMLNodeNameEqual(cur, element)) { - g_autofree char *value =3D virXMLPropString(cur, attr); + if ((macaddr =3D virXPathString("string(./mac/@address)", ctxt)) && + STRCASEEQ(macaddr, iface)) { + ifaceNode =3D nodes[i]; + break; + } - if (STRCASEEQ(value, iface)) - goto hit; - } - cur =3D cur->next; + if ((target =3D virXPathString("string(./target/@dev)", ctxt)) && + STRCASEEQ(target, iface)) { + ifaceNode =3D nodes[i]; + break; } } - vshError(ctl, _("interface (%s: %s) not found"), element, iface); - return false; - - hit: - /* find and modify/add link state node */ - /* try to find element */ - cur =3D obj->nodesetval->nodeTab[i]->children; + if (!ifaceNode) { + vshError(ctl, _("interface '%s' not found"), iface); + return false; + } - while (cur) { - if (cur->type =3D=3D XML_ELEMENT_NODE && - virXMLNodeNameEqual(cur, "link")) { - /* found, just modify the property */ - xmlSetProp(cur, BAD_CAST "state", BAD_CAST state); + ctxt->node =3D ifaceNode; - break; + /* try to find element or create new one */ + if (!(linkNode =3D virXPathNode("./link", ctxt))) { + if (!(linkNode =3D xmlNewChild(ifaceNode, NULL, BAD_CAST "link", N= ULL))) { + vshError(ctl, _("failed to create XML node")); + return false; } - cur =3D cur->next; } - if (!cur) { - /* element not found, add one */ - cur =3D xmlNewChild(obj->nodesetval->nodeTab[i], - NULL, - BAD_CAST "link", - NULL); - if (!cur) - return false; + if (xmlHasProp(linkNode, BAD_CAST "link")) + stateAttr =3D xmlSetProp(linkNode, BAD_CAST "state", BAD_CAST stat= e); + else + stateAttr =3D xmlNewProp(linkNode, BAD_CAST "state", BAD_CAST stat= e); - if (xmlNewProp(cur, BAD_CAST "state", BAD_CAST state) =3D=3D NULL) - return false; + if (!stateAttr) { + vshError(ctl, _("Failed to create or modify the state XML attribut= e")); + return false; } - if (!(xml_buf =3D virXMLNodeToString(xml, obj->nodesetval->nodeTab[i])= )) { + if (!(xml_buf =3D virXMLNodeToString(xml, ifaceNode))) { vshSaveLibvirtError(); vshError(ctl, _("Failed to create XML")); return false; --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990693; cv=none; d=zohomail.com; s=zohoarc; b=JFvvtpEsvuFI9IfhPgBJ254e5MFTmHlEd/WXMSDUXJoQxB+NeXRP4X1qXB9T0U4fbPSHKueWTd4m5xQ0XR29rhYyM69lOOdhodg0rIviUsbvQtzHAyFeZds0FnB7r33AZBz+O3mqTAjA4wfkknsL/gvDvDQifDPm4+14b5LLu6U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990693; 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=584QpiRxRz8fhlkbDH0r5h+RTFBS56uRtdex7PU2fNQ=; b=dbrV4TwW/DwYU8NQsX3wcBMKIsPzx0j/jMmrmry+NEEi6cLdGWqfqTCxiILFAXTCissZ7soLKG5fiALM9Q2Anr7Peq/iWwQwWdLE0FxFeFpOV40c3fKN2pJo6mo0T39E53WsQHQhr6++XjSlgvXBsGMBQXJOQKQv8UZrqN1MSCs= 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 1669990693213201.86610679561534; Fri, 2 Dec 2022 06:18:13 -0800 (PST) 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-633-sTb2XJyiMua2MHJ549fHIg-1; Fri, 02 Dec 2022 09:17:13 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1165985A588; Fri, 2 Dec 2022 14:17:08 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0CE92166B2A; Fri, 2 Dec 2022 14:17:07 +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 E6A43194658F; Fri, 2 Dec 2022 14:17:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 9E24F19465B8 for ; Fri, 2 Dec 2022 14:17:06 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 744D540E9785; Fri, 2 Dec 2022 14:17:06 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id AFBF740E9789 for ; Fri, 2 Dec 2022 14:17:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990692; 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=584QpiRxRz8fhlkbDH0r5h+RTFBS56uRtdex7PU2fNQ=; b=OiwIYPNWAXgfCF0JKniS2kMHUT78ToI9VoqxJnClBra0gW646VI2AXIsP6cihDjVK60pmB sW2/3G2CJbRIw4ZvgnfuN/0AvACY69WsQ+rk2HMmJMH/3zgQd5cE8sGZORQwTLXO6uDCBk 0k0MrzxK7B4fzPj6MdxwtWSQMUg3DfY= X-MC-Unique: sTb2XJyiMua2MHJ549fHIg-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 05/13] virsh: Refactor cleanup in 'cmdVolClone' Date: Fri, 2 Dec 2022 15:16:49 +0100 Message-Id: <1baa339f38efd0283eb9ae5c8f2691502b2dd0bd.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990694869100003 Content-Type: text/plain; charset="utf-8" Automatically free 'newxml' and remove the 'cleanup' label and 'ret' variable. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-volume.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index 4f23481180..b7a1ddb9e5 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -570,12 +570,11 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd) g_autoptr(virshStorageVol) newvol =3D NULL; const char *name =3D NULL; g_autofree char *origxml =3D NULL; - xmlChar *newxml =3D NULL; - bool ret =3D false; + g_autofree xmlChar *newxml =3D NULL; unsigned int flags =3D 0; if (!(origvol =3D virshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) - goto cleanup; + return false; if (vshCommandOptBool(cmd, "prealloc-metadata")) flags |=3D VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA; @@ -586,38 +585,30 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd) origpool =3D virStoragePoolLookupByVolume(origvol); if (!origpool) { vshError(ctl, "%s", _("failed to get parent pool")); - goto cleanup; + return false; } if (vshCommandOptStringReq(ctl, cmd, "newname", &name) < 0) - goto cleanup; + return false; - origxml =3D virStorageVolGetXMLDesc(origvol, 0); - if (!origxml) - goto cleanup; + if (!(origxml =3D virStorageVolGetXMLDesc(origvol, 0))) + return false; - newxml =3D virshMakeCloneXML(origxml, name); - if (!newxml) { + if (!(newxml =3D virshMakeCloneXML(origxml, name))) { vshError(ctl, "%s", _("Failed to allocate XML buffer")); - goto cleanup; + return false; } - newvol =3D virStorageVolCreateXMLFrom(origpool, (char *) newxml, origv= ol, flags); - - if (newvol !=3D NULL) { - vshPrintExtra(ctl, _("Vol %s cloned from %s\n"), - virStorageVolGetName(newvol), virStorageVolGetName(o= rigvol)); - } else { + if (!(newvol =3D virStorageVolCreateXMLFrom(origpool, (char *) newxml, + origvol, flags))) { vshError(ctl, _("Failed to clone vol from %s"), virStorageVolGetName(origvol)); - goto cleanup; + return false; } - ret =3D true; - - cleanup: - xmlFree(newxml); - return ret; + vshPrintExtra(ctl, _("Vol %s cloned from %s\n"), + virStorageVolGetName(newvol), virStorageVolGetName(origv= ol)); + return true; } /* --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990646; cv=none; d=zohomail.com; s=zohoarc; b=EBH0b9E1suGBwDnH1DB0YmXZ76djEm0P9iF/2zcz7H1TDQ2M/Zr0FX3zUR2uNhTe+LmNSyF39P0kNfV6t9Hiq9moDBAg7t8mmnbOg8Y8IsJIRqZ6yeoAR/+V2vvD7+mUNVcfiCZS24aN7agyXS3LbrkZjPzl0nmIZ3frN0ZCIO0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990646; 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=F5RntNoV4lWTuP2Hy4R360SG5KoqylP+8Q/+V1F44wk=; b=Kzzs8xqwsLwDqdtv2Ldbt3Miyu9/bMTO4u84DClzphggDGRZkEKpWKT2zlLmEkqnXxBt7fh+vVmCVojyj3f3m0pMB+nurxTs7ZEQsiqSW0y7qFVirOtNKzUBDKKXLDJs1LVnCUfQuBsO+65N2OPx4HctDPEn+xTM5/mdAbBnw6k= 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) 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 1669990646344502.0364830113624; Fri, 2 Dec 2022 06:17:26 -0800 (PST) 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-644-OYHf1hMGMaG3w_VAOrY4wA-1; Fri, 02 Dec 2022 09:17:14 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E858F185A7A3; Fri, 2 Dec 2022 14:17:08 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id D5541111DCE0; Fri, 2 Dec 2022 14:17:08 +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 CA68419465A8; Fri, 2 Dec 2022 14:17:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id D06951946587 for ; Fri, 2 Dec 2022 14:17:07 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id C726A40E9787; Fri, 2 Dec 2022 14:17:07 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 276C640E9785 for ; Fri, 2 Dec 2022 14:17:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990645; 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=F5RntNoV4lWTuP2Hy4R360SG5KoqylP+8Q/+V1F44wk=; b=brttaf/lV1MoW4KSFVgGYiy14i3HvvD5KecdQqCAViEnWmcLz/23DXsLE9S2unis2NzN/4 7kAicudYWYQj7WqYIhK0jyOi2c0VuiLtq3o4koXV9b37JLc6cjDqCOTMm68y2xg9m46K8a 0ME/bUJJ8cWpRH9pWKgjyO0Ju0Akors= X-MC-Unique: OYHf1hMGMaG3w_VAOrY4wA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 06/13] virsh: Add --print-xml flag for 'vol-clone' command Date: Fri, 2 Dec 2022 15:16:50 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990648556100005 Content-Type: text/plain; charset="utf-8" Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- docs/manpages/virsh.rst | 5 ++++- tools/virsh-volume.c | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 14ed9cd5a0..c85bc8151d 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -6719,7 +6719,7 @@ vol-clone :: vol-clone vol-name-or-key-or-path name - [--pool pool-or-uuid] [--prealloc-metadata] [--reflink] + [--pool pool-or-uuid] [--prealloc-metadata] [--reflink] [--print-xml] Clone an existing volume within the parent pool. Less powerful, but easier to type, version of ``vol-create-from``. @@ -6743,6 +6743,9 @@ When *--reflink* is specified, perform a COW lightwei= ght copy, where the data blocks are copied only when modified. If this is not possible, the copy fails. +If *--print-xml* is specified, then the XML used to clone the volume is +printed instead. + vol-delete ---------- diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index b7a1ddb9e5..adfea570be 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -559,6 +559,10 @@ static const vshCmdOptDef opts_vol_clone[] =3D { .type =3D VSH_OT_BOOL, .help =3D N_("use btrfs COW lightweight copy") }, + {.name =3D "print-xml", + .type =3D VSH_OT_BOOL, + .help =3D N_("print XML document rather than clone the volume") + }, {.name =3D NULL} }; @@ -599,6 +603,11 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd) return false; } + if (vshCommandOptBool(cmd, "print-xml")) { + vshPrint(ctl, "%s", newxml); + return true; + } + if (!(newvol =3D virStorageVolCreateXMLFrom(origpool, (char *) newxml, origvol, flags))) { vshError(ctl, _("Failed to clone vol from %s"), --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990744; cv=none; d=zohomail.com; s=zohoarc; b=Xarfv0L2LgeYqQxOyW3Ygloi6dt1LPcRVzV7m8whAnjeb1RPZkhzfXjCMntOqw3DZm5Y77FkH8IshUljZYYOVaFjaylZ8qM4hX+7FLdxNYj/G4qgeCVYDKL+iyDtAptTIa9NiO2TsXiLmGZzSL5lN07WaHAwAjX6SPD1m6ouplE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990744; 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=9kpkuNAcDWj7oMNlCuCKjPcRXodIpq8A/NrTsBHDaPA=; b=jsjlf7xbIM8oikx7GtWEWqhCbfawD0X2lTRsIMeDoIEI/pltDsMALYRER4V7SH54pQe20nn7JQrq87qMSsbOdZmjbYNU+smsSgo86ay2RCccplSQ1xtgnuGEZqBkI6vbsBPTjcsdtwCpgFoj9OZ0PBuaiH/efKNED6ww66mJJwQ= 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) 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 16699907447981022.0903351876763; Fri, 2 Dec 2022 06:19:04 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-267-FtxemdbXORWfmIjVsEQxYQ-1; Fri, 02 Dec 2022 09:17:16 -0500 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3E1563810D3C; Fri, 2 Dec 2022 14:17:11 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2C059403165; Fri, 2 Dec 2022 14:17:11 +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 1A85319465A8; Fri, 2 Dec 2022 14:17:11 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 236BC19465B8 for ; Fri, 2 Dec 2022 14:17:09 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 13C6A40E9788; Fri, 2 Dec 2022 14:17:09 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29CEB40E9785 for ; Fri, 2 Dec 2022 14:17:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990743; 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=9kpkuNAcDWj7oMNlCuCKjPcRXodIpq8A/NrTsBHDaPA=; b=ZyOyyegkemcy+LJdljtr3NrGpnSGkZYgQAaC4XnxzeawnX2hP55/TJKaLfFTdIEYb4ZzU2 32TTTZYhVdWq0a3zLyHjQtl2ZZUrtgBGy/Iye9opaDK8X4IKPBtt7BFo7dZVRZoaqTldU2 Z4LksSygdUuVbwcQ0mZv0oJRl4f6yaw= X-MC-Unique: FtxemdbXORWfmIjVsEQxYQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 07/13] virsh: virshMakeCloneXML: Use virXPathNode instead of xmlXPathEval Date: Fri, 2 Dec 2022 15:16:51 +0100 Message-Id: <160ce46c1730ad3492da60a0ada7b971e9d9c867.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990745292100001 Content-Type: text/plain; charset="utf-8" Refactor the code to use the XPath helpers instead of open-coding them. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-volume.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index adfea570be..2a7809140e 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -505,28 +505,22 @@ cmdVolCreateFrom(vshControl *ctl, const vshCmd *cmd) return true; } -static xmlChar * +static char * virshMakeCloneXML(const char *origxml, const char *newname) { g_autoptr(xmlDoc) doc =3D NULL; g_autoptr(xmlXPathContext) ctxt =3D NULL; - g_autoptr(xmlXPathObject) obj =3D NULL; - xmlChar *newxml =3D NULL; - int size; + xmlNodePtr node; - doc =3D virXMLParseStringCtxt(origxml, _("(volume_definition)"), &ctxt= ); - if (!doc) + if (!(doc =3D virXMLParseStringCtxt(origxml, _("(volume_definition)"),= &ctxt))) return NULL; - obj =3D xmlXPathEval(BAD_CAST "/volume/name", ctxt); - if (obj =3D=3D NULL || obj->nodesetval =3D=3D NULL || - obj->nodesetval->nodeTab =3D=3D NULL) + if (!(node =3D virXPathNode("/volume/name", ctxt))) return NULL; - xmlNodeSetContent(obj->nodesetval->nodeTab[0], (const xmlChar *)newnam= e); - xmlDocDumpMemory(doc, &newxml, &size); + xmlNodeSetContent(node, (const xmlChar *)newname); - return newxml; + return virXMLNodeToString(doc, doc->children); } /* @@ -574,7 +568,7 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd) g_autoptr(virshStorageVol) newvol =3D NULL; const char *name =3D NULL; g_autofree char *origxml =3D NULL; - g_autofree xmlChar *newxml =3D NULL; + g_autofree char *newxml =3D NULL; unsigned int flags =3D 0; if (!(origvol =3D virshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) @@ -608,8 +602,7 @@ cmdVolClone(vshControl *ctl, const vshCmd *cmd) return true; } - if (!(newvol =3D virStorageVolCreateXMLFrom(origpool, (char *) newxml, - origvol, flags))) { + if (!(newvol =3D virStorageVolCreateXMLFrom(origpool, newxml, origvol,= flags))) { vshError(ctl, _("Failed to clone vol from %s"), virStorageVolGetName(origvol)); return false; --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990688; cv=none; d=zohomail.com; s=zohoarc; b=RfIfEqbJzjix8kpXqK/2os0S9vFh+hlsDxf2ebVEUhQ7VRFyAhr7PhrYoIhiklxfjdVevrf/cbeGAJK/E4g4h4RllG97J7nKRFPZ6h/PdNLlcSdvhz4xeG9zlLtfjrZNfMqCdmM99jYliEKtHwOQBKrsonBJW6CLm/7df8wnB5M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990688; 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=fXD7gaAP0w7samOXhF7fZppVO6Tq+JR5rZLJ/SDDudU=; b=YFTc7SeTSVHSpVlaTxc+ZlKkQFqwUjXc52wnuKg2yBYKbs2FoyNRpuLjEhDXEL/Yt4luwFBb4q/wFo+oxNSIBQV5k6vK/Tls4GjigC4+8ZlFR5oUEhb8cSQjjtBOyxVPMeP7vXOKP80r8r9fEliM3BQzU7SESsZqmku7YQ29BSg= 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) 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 1669990688115180.41259942738418; Fri, 2 Dec 2022 06:18:08 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-351-WwDCVLZpP8qXdNB_xk2PHA-1; Fri, 02 Dec 2022 09:17:20 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8C6341C0759D; Fri, 2 Dec 2022 14:17:17 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 79D4B20290A5; Fri, 2 Dec 2022 14:17:17 +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 4DCF919465A8; Fri, 2 Dec 2022 14:17:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 2518B1946587 for ; Fri, 2 Dec 2022 14:17:16 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 146EB40E9785; Fri, 2 Dec 2022 14:17:16 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6FA4540E9789 for ; Fri, 2 Dec 2022 14:17:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990687; 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=fXD7gaAP0w7samOXhF7fZppVO6Tq+JR5rZLJ/SDDudU=; b=JHCYwpY04MzXwvV9vdn+KZJtV7i/7or7t0gEzbHW+egq2brAxi0m9OHrLoMa6lEWJ3Q3sL yO7C1X26s9HXnRC1sw+s0Edqb1+Pjz6RvoCO2xG2pMtJs33mWQ1eoaf2oYp3zqaAL+Z2Ec n6GRnJrem7XsV9VJJzVcmR5fJgvMzcg= X-MC-Unique: WwDCVLZpP8qXdNB_xk2PHA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 08/13] virsh: cmdDetachDisk: Refactor cleanup Date: Fri, 2 Dec 2022 15:16:52 +0100 Message-Id: <33259e3aa961dab4c4d880d92ba93e5c7ed6bfde.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990688842100003 Content-Type: text/plain; charset="utf-8" Use automatic pointer freeing for the 'disk_node' variable and remove the 'cleanup' label and 'functionReturn' variable. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 2fb1751f25..56490124a0 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -12894,8 +12894,7 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd) const char *target =3D NULL; g_autofree char *doc =3D NULL; int ret; - bool functionReturn =3D false; - xmlNodePtr disk_node =3D NULL; + g_autoptr(xmlNode) disk_node =3D NULL; bool current =3D vshCommandOptBool(cmd, "current"); bool config =3D vshCommandOptBool(cmd, "config"); bool live =3D vshCommandOptBool(cmd, "live"); @@ -12916,7 +12915,7 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd) return false; if (vshCommandOptStringReq(ctl, cmd, "target", &target) < 0) - goto cleanup; + return false; if (flags =3D=3D VIR_DOMAIN_AFFECT_CONFIG) doc =3D virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE); @@ -12924,24 +12923,23 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd) doc =3D virDomainGetXMLDesc(dom, 0); if (!doc) - goto cleanup; + return false; if (persistent && virDomainIsActive(dom) =3D=3D 1) flags |=3D VIR_DOMAIN_AFFECT_LIVE; if (!(disk_node =3D virshFindDisk(doc, target, VIRSH_FIND_DISK_NORMAL)= )) - goto cleanup; + return false; if (!(disk_xml =3D virXMLNodeToString(NULL, disk_node))) { vshSaveLibvirtError(); - goto cleanup; + return false; } if (vshCommandOptBool(cmd, "print-xml")) { vshPrint(ctl, "%s", disk_xml); - functionReturn =3D true; - goto cleanup; + return true; } if (flags !=3D 0 || current) @@ -12951,15 +12949,11 @@ cmdDetachDisk(vshControl *ctl, const vshCmd *cmd) if (ret !=3D 0) { vshError(ctl, "%s", _("Failed to detach disk")); - goto cleanup; + return false; } vshPrintExtra(ctl, "%s", _("Disk detached successfully\n")); - functionReturn =3D true; - - cleanup: - xmlFreeNode(disk_node); - return functionReturn; + return true; } /* --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990645; cv=none; d=zohomail.com; s=zohoarc; b=AQznmAg2b4VpYM9jQF9aGPoeDhW938pwnayjURTwyfMYVraPgb6F8u0L9mRQ2FNt/+LL8kuKzDSQTJcF7gAstjaJwa8uq5n/Td9sCzdAlR5uRwKw9MsTgDeltBWjVuOrokVKfQ7mchYfX1DU0ptm0olZH1gM2bbvqbcSMSqvOOM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990645; 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=CNkSBmZ0lFchQM/cU4d2Mzy4nDRSX3qjSvh1UIGSiMc=; b=nXsaAlexzMy1Uo2LPY1M6JeMdissO6jbc8tbZVddO3r9Jp3JKoIrCAhr2exwAgbGQEwl/7Xuz8gPsTJR+p5HWDLzC0n891kgyNgSu0U0Avif7FfFzuygPr2nQtWfvDE5vn/Zz4r3TPkIYxfBLQCyi/7s6YTZSoJcsHuNJVPg4os= 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) 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 166999064554191.86447130636088; Fri, 2 Dec 2022 06:17:25 -0800 (PST) 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-632-iu3YhVh4MGSFXtjm4ZUC_A-1; Fri, 02 Dec 2022 09:17:21 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4C767811E84; Fri, 2 Dec 2022 14:17:19 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 39FF340AE335; Fri, 2 Dec 2022 14:17:19 +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 14F3B1946A41; Fri, 2 Dec 2022 14:17:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 438D819465A0 for ; Fri, 2 Dec 2022 14:17:17 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 22FBD40E9787; Fri, 2 Dec 2022 14:17:17 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 72BE640E9785 for ; Fri, 2 Dec 2022 14:17:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990644; 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=CNkSBmZ0lFchQM/cU4d2Mzy4nDRSX3qjSvh1UIGSiMc=; b=VFJbvONVd3KQXk8qJdmKxz5OrIOgcWj+Zi0OGcR7DhuU4nwQ4tV0+Z7hhCywVP7mNOPo2I IU701Lux2MbTo98JRcivO6mhvVF7iao2mbtOXOa8//tnp8zCZ7prpPaor5KYbGBc8RMIkP 7XbCA8RgrY+1sctMT8OcBJrjZOJBWqM= X-MC-Unique: iu3YhVh4MGSFXtjm4ZUC_A-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 09/13] virsh: cmdChangeMedia: Refactor cleanup Date: Fri, 2 Dec 2022 15:16:53 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990646533100003 Content-Type: text/plain; charset="utf-8" Use automatic pointer freeing for the 'disk_node' variable and remove the 'cleanup' label and 'ret' variable. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 56490124a0..c4cf7aa95e 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -13089,9 +13089,8 @@ cmdChangeMedia(vshControl *ctl, const vshCmd *cmd) const char *source =3D NULL; const char *path =3D NULL; g_autofree char *doc =3D NULL; - xmlNodePtr disk_node =3D NULL; + g_autoptr(xmlNode) disk_node =3D NULL; g_autofree char *disk_xml =3D NULL; - bool ret =3D false; virshUpdateDiskXMLType update_type; const char *action =3D NULL; const char *success_msg =3D NULL; @@ -13152,38 +13151,34 @@ cmdChangeMedia(vshControl *ctl, const vshCmd *cmd) return false; if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0) - goto cleanup; + return false; if (flags & VIR_DOMAIN_AFFECT_CONFIG) doc =3D virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE); else doc =3D virDomainGetXMLDesc(dom, 0); if (!doc) - goto cleanup; + return false; if (!(disk_node =3D virshFindDisk(doc, path, VIRSH_FIND_DISK_CHANGEABL= E))) - goto cleanup; + return false; if (!(disk_xml =3D virshUpdateDiskXML(disk_node, source, block, path, update_type))) - goto cleanup; + return false; if (vshCommandOptBool(cmd, "print-xml")) { vshPrint(ctl, "%s", disk_xml); - } else { - if (virDomainUpdateDeviceFlags(dom, disk_xml, flags) !=3D 0) { - vshError(ctl, _("Failed to complete action %s on media"), acti= on); - goto cleanup; - } - - vshPrint(ctl, "%s", success_msg); + return true; } - ret =3D true; + if (virDomainUpdateDeviceFlags(dom, disk_xml, flags) !=3D 0) { + vshError(ctl, _("Failed to complete action %s on media"), action); + return false; + } - cleanup: - xmlFreeNode(disk_node); - return ret; + vshPrint(ctl, "%s", success_msg); + return true; } static const vshCmdInfo info_domfstrim[] =3D { --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990652; cv=none; d=zohomail.com; s=zohoarc; b=D9M/Du+PH285ybqLjjWbcMI58VV/mgivqWmS5tHoBxylX45QT3PUG31t2bO+37ujWfclvq48XUaASIz2zupA/V5kpOCiY5sIf5aNdLSmAO6rruFsv+i3cyzIOYk3m4EONmxqPOp9aLSLx98DgwvyI54sTAZP94sRBltqBqI0s6g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990652; 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=CMn5CupqJXMKdyjB441T/I+n2fhY3YtUCbAd/0+Q+gw=; b=DSSLMJHlejnnfbd2effVfg0dzc457B4Pm9EXI7hhe4nDC5gjhKg/nthVclkDeFqKTda9Zxv+sgfid8yJsk+ZLMv00NJU+jII9V37/X6hm3Tjwni2HC4eIZdsBzYCcNO13/Bs/KnzUBtz445h5KkkgCFn/GPfZHVGIQ6G3hUieLk= 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 166999065238239.18776103637981; Fri, 2 Dec 2022 06:17:32 -0800 (PST) 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-563-3R2l15zWOg-dKGeqbDztiA-1; Fri, 02 Dec 2022 09:17:23 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D2528894E96; Fri, 2 Dec 2022 14:17:19 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id B8FB040AE336; Fri, 2 Dec 2022 14:17:19 +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 8BF1B1946A48; Fri, 2 Dec 2022 14:17:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 4488319465B8 for ; Fri, 2 Dec 2022 14:17:18 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 2AA0340E9785; Fri, 2 Dec 2022 14:17:18 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8B97C40E9788 for ; Fri, 2 Dec 2022 14:17:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990650; 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=CMn5CupqJXMKdyjB441T/I+n2fhY3YtUCbAd/0+Q+gw=; b=Ed+h/xC/Yl1ncSQPoOzeVnA5Ro2HvD563PhSJzesas0YNsgMrY3xuZUSfYxyozLeDBc6wr 1lQ1qZ7ZoOrA3TIxOfLRcoDG7Plnlvad11pVLAlySgDrJQaNRybxXGJ5w3IzBTDEbgpbmc H1eDZAb35+sAMu8sMUaHChBI8gqqRN0= X-MC-Unique: 3R2l15zWOg-dKGeqbDztiA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 10/13] virshFindDisk: Use virXPathNodeSet instead of xmlXPathEval Date: Fri, 2 Dec 2022 15:16:54 +0100 Message-Id: <872387161399500d28d6a9f8cb3b85468e3b7d67.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990654561100002 Content-Type: text/plain; charset="utf-8" Don't open-code the XPath lookup. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index c4cf7aa95e..1beaacd5e9 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -12639,8 +12639,9 @@ virshFindDisk(const char *doc, int type) { g_autoptr(xmlDoc) xml =3D NULL; - g_autoptr(xmlXPathObject) obj =3D NULL; g_autoptr(xmlXPathContext) ctxt =3D NULL; + g_autofree xmlNodePtr *nodes =3D NULL; + ssize_t nnodes; xmlNodePtr cur =3D NULL; size_t i; @@ -12650,21 +12651,17 @@ virshFindDisk(const char *doc, return NULL; } - obj =3D xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt); - if (obj =3D=3D NULL || - obj->type !=3D XPATH_NODESET || - obj->nodesetval =3D=3D NULL || - obj->nodesetval->nodeNr =3D=3D 0) { + if ((nnodes =3D virXPathNodeSet("/domain/devices/disk", ctxt, &nodes))= <=3D 0) { vshError(NULL, "%s", _("Failed to get disk information")); return NULL; } /* search disk using @path */ - for (i =3D 0; i < obj->nodesetval->nodeNr; i++) { + for (i =3D 0; i < nnodes; i++) { bool is_supported =3D true; if (type =3D=3D VIRSH_FIND_DISK_CHANGEABLE) { - xmlNodePtr n =3D obj->nodesetval->nodeTab[i]; + xmlNodePtr n =3D nodes[i]; is_supported =3D false; /* Check if the disk is CDROM or floppy disk */ @@ -12680,7 +12677,7 @@ virshFindDisk(const char *doc, continue; } - cur =3D obj->nodesetval->nodeTab[i]->children; + cur =3D nodes[i]->children; while (cur !=3D NULL) { if (cur->type =3D=3D XML_ELEMENT_NODE) { g_autofree char *tmp =3D NULL; @@ -12696,7 +12693,7 @@ virshFindDisk(const char *doc, } if (STREQ_NULLABLE(tmp, path)) { - xmlNodePtr ret =3D xmlCopyNode(obj->nodesetval->nodeTa= b[i], 1); + xmlNodePtr ret =3D xmlCopyNode(nodes[i], 1); /* drop backing store since they are not needed here */ virshDiskDropBackingStore(ret); return ret; --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990688; cv=none; d=zohomail.com; s=zohoarc; b=X7Ctc1jTCJSmQhdSyLkETL4OUYD+zZk2BtSxWTnWm/V9W/t0cMK0fXQo7MIfnQp9hnsCmXTD1p7GLAEIMBO1W/AZ/qFc5hHhIAz2ZUIPKYV9dlPXVNfWq56Id4imKfXuh7BLtgZX22KvbZOhIaoAns5YTo0nvMQs2BVGnFWokwE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990688; 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=vUOns0kOYd4WamHZ9XPxAaTg3dvrrkAFuKHl7Z3xKbo=; b=B5bgrcqAP26hE1uUWGZbE/DfUPR10xFL+p3fSk6s4EMAYymaQqbEZZ9CM8vRGfemJZXTESDxS9tr8riynuOKvh6wgRp1rAsUKJCIn+UMB4awDCwPCgOPlC8mmYyZMo0ttXfZP0CfCB1K7zkQXjb1XwhcDj1Jqrw+OvHpAlzdg0Y= 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) 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 1669990688271721.8001270510675; Fri, 2 Dec 2022 06:18:08 -0800 (PST) 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-630-GUll2wIQPiuMzu_eX5nUhg-1; Fri, 02 Dec 2022 09:17:24 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4A68E894E82; Fri, 2 Dec 2022 14:17:21 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 35EA320290A6; Fri, 2 Dec 2022 14:17:21 +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 1BA091946A63; Fri, 2 Dec 2022 14:17:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 553E01946A43 for ; Fri, 2 Dec 2022 14:17:19 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 362B640E9787; Fri, 2 Dec 2022 14:17:19 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 951E740E9785 for ; Fri, 2 Dec 2022 14:17:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990686; 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=vUOns0kOYd4WamHZ9XPxAaTg3dvrrkAFuKHl7Z3xKbo=; b=IIX4Kl+NP+I7oxK+tFyfXscOQx6eJTTWw7mlPwj1QnJNHQDtLsDE7Fa0wMXgPjRIE8viHJ ByauWzrkRhZijVDTZzjDZLnyoEP6uJDuTneCXAyXZSHrDR+IJddt492foribxIFNM47M58 5k4I7XOqtmlnpQ7rf7MLLsZRVuOPHTM= X-MC-Unique: GUll2wIQPiuMzu_eX5nUhg-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 11/13] virshFindDisk: Sanitize removable media check Date: Fri, 2 Dec 2022 15:16:55 +0100 Message-Id: <1b379033d33d5cf33812bbe74763c6b3b9655de6.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990688822100001 Content-Type: text/plain; charset="utf-8" The XPath lookup guarantees that the top level element is always 'disk' so there's no need to check that it actually is. We can also remove the two unnecessary temporary variables. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 1beaacd5e9..8bd058a33a 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -12658,22 +12658,13 @@ virshFindDisk(const char *doc, /* search disk using @path */ for (i =3D 0; i < nnodes; i++) { - bool is_supported =3D true; - if (type =3D=3D VIRSH_FIND_DISK_CHANGEABLE) { - xmlNodePtr n =3D nodes[i]; - is_supported =3D false; + g_autofree char *device =3D virXMLPropString(nodes[i], "device= "); /* Check if the disk is CDROM or floppy disk */ - if (virXMLNodeNameEqual(n, "disk")) { - g_autofree char *device_value =3D virXMLPropString(n, "dev= ice"); - - if (STREQ(device_value, "cdrom") || - STREQ(device_value, "floppy")) - is_supported =3D true; - } - - if (!is_supported) + if (device && + STRNEQ(device, "cdrom") && + STRNEQ(device, "floppy")) continue; } --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990691; cv=none; d=zohomail.com; s=zohoarc; b=R8qbo23H/BmYyMi7r+zZ4cqLOty3mZV+43gaIPypAGVZhhryitZbAaJnt7Q1W4SgqwqWMqC+/WmS+zW8wJynJqHcgx9Fq2XE0UEuEtHrbBAdWP13+Aa+eCJd0rtJOEmoug8TNQQr1l8jFMPtOhn+Gs8B52SjrkwR2MbjE5mrELg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990691; 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=FhZhkZwpRpnXwY6abi6ZuZnPI+vdZVx+sNRHZWdpTVc=; b=jK+KiJ4h0VhB4r8wl/rDjkotH7oGVYcg99aOcJdZAat9CBHJ532qr08otH2kkbFdWuLbWTsTuHbSSlPE3F714G8irGfngjjBeInEJowVyR8WVeA2t8FM3V/cPXY9b7uHpuEFIi+/O9fKpYWrgDQ4j0d1c2rGENoqVHFGIu2hMOo= 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 1669990691542429.1747347343179; Fri, 2 Dec 2022 06:18:11 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-92-kuqWdUv3MG6YVo7RgGMzDQ-1; Fri, 02 Dec 2022 09:17:29 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1D7D72A59568; Fri, 2 Dec 2022 14:17:27 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A77640E9785; Fri, 2 Dec 2022 14:17:27 +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 EE5E4194658F; Fri, 2 Dec 2022 14:17:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 4BDC519465A0 for ; Fri, 2 Dec 2022 14:17:25 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 3257F40E9785; Fri, 2 Dec 2022 14:17:20 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9BAFF40E9787 for ; Fri, 2 Dec 2022 14:17:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990690; 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=FhZhkZwpRpnXwY6abi6ZuZnPI+vdZVx+sNRHZWdpTVc=; b=hMg3OFeG0Hp9yNruxgdPC0I39joMMcajRCBzPDiKAo2+0WiLwC7YslPDxxhTuJyEmYA/Ty u5HRNAK0HarmE6kYA21Zt+eTXFYdAJ0Ric1P5MJwO467AgxGIwdgMa/dYoEYPFBRNI33rk IVQ1o39HFXfLhAJSlF1QeK/o4V0EiCI= X-MC-Unique: kuqWdUv3MG6YVo7RgGMzDQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 12/13] util: xml: Introduce virXMLNodeGetSubelement Date: Fri, 2 Dec 2022 15:16:56 +0100 Message-Id: <0ca37d33f0cbdba7496bdd0d2b92bac5f59dd836.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990692853100001 Content-Type: text/plain; charset="utf-8" Introduce a simple helper fetching a sub-element node by name. This is meant as a simple replacement for either open-coded versions of this or use of XPath for this trivial lookup. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/libvirt_private.syms | 1 + src/util/virxml.c | 23 +++++++++++++++++++++++ src/util/virxml.h | 5 +++++ 3 files changed, 29 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ebd7bc61a8..209ec14544 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3671,6 +3671,7 @@ virXMLFormatElementInternal; virXMLFormatMetadata; virXMLNewNode; virXMLNodeContentString; +virXMLNodeGetSubelement; virXMLNodeNameEqual; virXMLNodeSanitizeNamespaces; virXMLNodeToString; diff --git a/src/util/virxml.c b/src/util/virxml.c index 870ba194b0..7d9d4bb234 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -849,6 +849,29 @@ virXPathBoolean(const char *xpath, } +/** + * virXMLNodeGetSubelement: + * @node: node to get subelement of + * @name: name of subelement to fetch + * + * Find and return a sub-element node of @node named @name. + */ +xmlNodePtr +virXMLNodeGetSubelement(xmlNodePtr node, + const char *name) +{ + xmlNodePtr n; + + for (n =3D node->children; n; n =3D n->next) { + if (n->type =3D=3D XML_ELEMENT_NODE && + virXMLNodeNameEqual(n, name)) + return n; + } + + return NULL; +} + + /** * virXPathNode: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index f19cbe59ae..d5b998263c 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -72,6 +72,11 @@ int virXPathLongLong(const char *xpath, xmlXPathContextPtr ctxt, long long *value); + +xmlNodePtr +virXMLNodeGetSubelement(xmlNodePtr node, + const char *name); + xmlNodePtr virXPathNode(const char *xpath, xmlXPathContextPtr ctxt); --=20 2.38.1 From nobody Sun Feb 8 00:56:15 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=1669990652; cv=none; d=zohomail.com; s=zohoarc; b=KdSvrSGcXqujzBJiUCPWbn36sHyzp9GCc7OvsELMnfuAZwcI+htoa9GMddanuQrmWQ33yrqKGCC68+L3qyTyhBeqwuLLKpiF4FTWN2v45/Jy0JnVp0BtxaO3/KXiBLnkEk4Lo62jm28jNOx/D/Y6IqFuBOkQqgiXKt7U51AtzZs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990652; 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=3kpkE9ytUQGrYt0H5odc/PdM1KWS9K5VoDnKqB0zLSg=; b=KfiNfkycCBCG/YEgW5Itocu722KOhQ6Nd7nspNoxBVrVjWL97oBiZtTbcXrFdyttkSV+urYL0HSOAoyi3/3xVw37hrCHES+TBWqybn9UFBxtlPiCLNRucnTNlCRG69FkNfJFdR0F5BAzu0AceeiVswYc8y7DPzSicK8YaLfBFak= 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) 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 1669990652440259.36902550827995; Fri, 2 Dec 2022 06:17:32 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-450-wGU9ZFgdNWC0x5jIjWUTXA-1; Fri, 02 Dec 2022 09:17:26 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 589F22A59578; Fri, 2 Dec 2022 14:17:23 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 442F9C16922; Fri, 2 Dec 2022 14:17:23 +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 644491946A41; Fri, 2 Dec 2022 14:17:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 7AB7B19465B8 for ; Fri, 2 Dec 2022 14:17:21 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 7018C40E9788; Fri, 2 Dec 2022 14:17:21 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1F0740E9787 for ; Fri, 2 Dec 2022 14:17:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990651; 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=3kpkE9ytUQGrYt0H5odc/PdM1KWS9K5VoDnKqB0zLSg=; b=BAkycxLkLkfNltZEGOXFofasCTro8Hyn5pbMSG0TrAoaVZjaP6iyKikOFE6LtXCnL1j1Wc U9Z7FqcKWIFix3bewnpSme3CYkXlyGzysdgnMYr9bbL+OFvPEz+j6zltljVqyHV0BobveW jEDPJvddtKwswVUMihz2Qhrf5F+1/Mg= X-MC-Unique: wGU9ZFgdNWC0x5jIjWUTXA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 13/13] virshFindDisk: Sanitize use of 'tmp' variable Date: Fri, 2 Dec 2022 15:16:57 +0100 Message-Id: <1656aaa12bfe50101e234bb364ef7c54d4e507c6.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990654559100001 Content-Type: text/plain; charset="utf-8" The return value of virXMLPropString was assigned into 'tmp' multiple times and to prevent static analyzers moaning about a potential leak a short-circuited if+logic or was used. Replace the code by having a helper variable for each possibility and also replace the for-loop to iterate elements. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain.c | 48 +++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 8bd058a33a..4d8690d9db 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -12642,7 +12642,6 @@ virshFindDisk(const char *doc, g_autoptr(xmlXPathContext) ctxt =3D NULL; g_autofree xmlNodePtr *nodes =3D NULL; ssize_t nnodes; - xmlNodePtr cur =3D NULL; size_t i; xml =3D virXMLParseStringCtxt(doc, _("(domain_definition)"), &ctxt); @@ -12658,6 +12657,13 @@ virshFindDisk(const char *doc, /* search disk using @path */ for (i =3D 0; i < nnodes; i++) { + xmlNodePtr sourceNode; + g_autofree char *sourceFile =3D NULL; + g_autofree char *sourceDev =3D NULL; + g_autofree char *sourceDir =3D NULL; + g_autofree char *sourceName =3D NULL; + g_autofree char *targetDev =3D NULL; + if (type =3D=3D VIRSH_FIND_DISK_CHANGEABLE) { g_autofree char *device =3D virXMLPropString(nodes[i], "device= "); @@ -12668,29 +12674,25 @@ virshFindDisk(const char *doc, continue; } - cur =3D nodes[i]->children; - while (cur !=3D NULL) { - if (cur->type =3D=3D XML_ELEMENT_NODE) { - g_autofree char *tmp =3D NULL; - - if (virXMLNodeNameEqual(cur, "source")) { - if ((tmp =3D virXMLPropString(cur, "file")) || - (tmp =3D virXMLPropString(cur, "dev")) || - (tmp =3D virXMLPropString(cur, "dir")) || - (tmp =3D virXMLPropString(cur, "name"))) { - } - } else if (virXMLNodeNameEqual(cur, "target")) { - tmp =3D virXMLPropString(cur, "dev"); - } + if ((sourceNode =3D virXMLNodeGetSubelement(nodes[i], "source"))) { + sourceFile =3D virXMLPropString(sourceNode, "file"); + sourceDev =3D virXMLPropString(sourceNode, "dev"); + sourceDir =3D virXMLPropString(sourceNode, "dir"); + sourceName =3D virXMLPropString(sourceNode, "name"); + } - if (STREQ_NULLABLE(tmp, path)) { - xmlNodePtr ret =3D xmlCopyNode(nodes[i], 1); - /* drop backing store since they are not needed here */ - virshDiskDropBackingStore(ret); - return ret; - } - } - cur =3D cur->next; + ctxt->node =3D nodes[i]; + targetDev =3D virXPathString("string(./target/@dev)", ctxt); + + if (STREQ_NULLABLE(targetDev, path) || + STREQ_NULLABLE(sourceFile, path) || + STREQ_NULLABLE(sourceDev, path) || + STREQ_NULLABLE(sourceDir, path) || + STREQ_NULLABLE(sourceName, path)) { + xmlNodePtr ret =3D xmlCopyNode(nodes[i], 1); + /* drop backing store since they are not needed here */ + virshDiskDropBackingStore(ret); + return ret; } } --=20 2.38.1