From nobody Mon Feb 9 22:02:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.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 216.205.24.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=1617880829; cv=none; d=zohomail.com; s=zohoarc; b=YtWtw2W++E178kec50xzia37iq3vjQHKSDYwIQiyii3oy4j1HWFVgzs5UHj0nFchXLLIFdgwAPhW1YiiZ4TnZgmhoE9gbSs/iqgHiAxBsQo7Bq5Qt55xXZMCBDIMLwLlK/XHalFi+IJQg7xUFnUa9gEJumFVskB0Z1ATRq03xI4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617880829; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=B6DdiEaRGUOCq1xHmvXWmb3YZqbaDsQJ2UcbVkOjNc4=; b=Jau7+T12MmhhDZWiex9ewgouzGiJHEtP/4pu6/khSC/cERByMZ5YKIpHKfK68+NJNO3HWe7UEIjVBxmfBtDaU1fvccRAxNRgLk7zlRpE7QOLUFLII1gk/jaM5wpholAlSRiWEodOI0t+PaUXbDlK0oOPp8wxTR+I+iGLQ+HxSB8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1617880829519237.13967523253143; Thu, 8 Apr 2021 04:20:29 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-505-Din8CGCENAul_o4HLwe_Qg-1; Thu, 08 Apr 2021 07:20:24 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6DB17189C447; Thu, 8 Apr 2021 11:20:18 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 45C97690F2; Thu, 8 Apr 2021 11:20:18 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0153855348; Thu, 8 Apr 2021 11:20:18 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 138BKBqt009420 for ; Thu, 8 Apr 2021 07:20:11 -0400 Received: by smtp.corp.redhat.com (Postfix) id A73FB5C1C4; Thu, 8 Apr 2021 11:20:11 +0000 (UTC) Received: from work.redhat.com (ovpn-112-196.ams2.redhat.com [10.36.112.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 43A224DA32; Thu, 8 Apr 2021 11:20:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617880826; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=B6DdiEaRGUOCq1xHmvXWmb3YZqbaDsQJ2UcbVkOjNc4=; b=ae6mUIRqqcHS51Kxz8SkG7BLLCYKtAU92k10hK/KX6w05fCUZ1nJlBx0yg6/1kDW/Ga6RM VTeXrc2WdQ310Vu0pWTPPvZKT3ME75mcEWbydra1q3Z7mVycAlI/KLL2EQypIPCxgHLOFr 7qc5c9waMiworMlnmbL723rmSnQkJYI= X-MC-Unique: Din8CGCENAul_o4HLwe_Qg-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH v5 3/8] virxml: Add virXMLPropInt Date: Thu, 8 Apr 2021 13:19:53 +0200 Message-Id: <20210408111958.32678-4-twiederh@redhat.com> In-Reply-To: <20210408111958.32678-1-twiederh@redhat.com> References: <20210408111958.32678-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Cc: Tim Wiederhake X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Convenience function to return the value of an integer XML attribute. Signed-off-by: Tim Wiederhake --- src/libvirt_private.syms | 1 + src/util/virxml.c | 57 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 6 +++++ 3 files changed, 64 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 776387e6b3..bdb63349ee 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3544,6 +3544,7 @@ virXMLNodeSanitizeNamespaces; virXMLNodeToString; virXMLParseHelper; virXMLPickShellSafeComment; +virXMLPropInt; virXMLPropString; virXMLPropStringLimit; virXMLPropTristateBool; diff --git a/src/util/virxml.c b/src/util/virxml.c index f62c5c39c4..bd6b75150e 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -656,6 +656,63 @@ virXMLPropTristateSwitch(xmlNodePtr node, const char* = name, } =20 =20 +/** + * virXMLPropInt: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @base: Number base, see strtol + * @flags: Bitwise or of virXMLPropFlags + * @result: The returned value + * + * Convenience function to return value of an integer attribute. + * + * Returns 1 in case of success in which case @result is set, + * or 0 if the attribute is not present, + * or -1 and reports an error on failure. + */ +int +virXMLPropInt(xmlNodePtr node, const char *name, int base, + virXMLPropFlags flags, int *result) +{ + g_autofree char *tmp =3D NULL; + int ret; + + if (!node || !name || !result) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid argument to %s"), + __FUNCTION__); + return -1; + } + + if (!(tmp =3D virXMLPropString(node, name))) { + if ((flags & VIR_XML_PROP_REQUIRED) !=3D VIR_XML_PROP_REQUIRED) + return 0; + + virReportError(VIR_ERR_XML_ERROR, + _("Missing required attribute '%s' in element '%s'"= ), + name, node->name); + return -1; + } + + ret =3D virStrToLong_i(tmp, NULL, base, result); + + if (ret < 0) { + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in element '%s'= : '%s'. Expected integer value"), + name, node->name, tmp); + return -1; + } + + if ((flags & VIR_XML_PROP_NONZERO) && (*result =3D=3D 0)) { + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value for attribute '%s' in element '%s'= : Zero is not permitted"), + name, node->name); + } + + return 1; +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index a5ecfbb01a..aba863ae11 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -36,6 +36,7 @@ xmlXPathContextPtr virXMLXPathContextNew(xmlDocPtr xml) typedef enum { VIR_XML_PROP_OPTIONAL =3D 0, /* Attribute may be absent */ VIR_XML_PROP_REQUIRED =3D 1 << 0, /* Attribute may not be abse= nt */ + VIR_XML_PROP_NONZERO =3D 1 << 1, /* Attribute may not be zero= */ } virXMLPropFlags; =20 int virXPathBoolean(const char *xpath, @@ -91,6 +92,11 @@ int virXMLPropTristateSwitch(xmlNodePtr node, const char *name, virXMLPropFlags flags, virTristateSwitch *result); +int virXMLPropInt(xmlNodePtr node, + const char *name, + int base, + virXMLPropFlags flags, + int *result); =20 /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, --=20 2.26.2