From nobody Mon Feb 9 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 --- 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