From nobody Mon Feb 9 16:13:09 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=1618566136; cv=none; d=zohomail.com; s=zohoarc; b=P0iPytmDy5ba23cdnFZkvQkBKoGiOK1ENUENGAxGbhTIt8egnZGuf6yWyffP2S8snaLARcpB/BjrDUyqnBExCA6YfHpj92DtJLXLFa/BVJbE6rtULXeJnR5+JYDPMz7SRNPS+dyX1lDd7pC2B635cK4gN/xFijczlHgAr86inlo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618566136; 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=+JdjSjuZbH8YwXZ+w2Vk6jiNOX2X4Qmodv1NB9spK7w=; b=V8HDTrbLqfG5MlKsi+a2Ga9qAuna/r7udsCbDrEvgPMzEafBir1nc25Ohcc7Bgrr8JrOZEo9nfM5Oe6friY/vxLRg3EmFz8iZ8afG2W8O2KKSQ44YJFdB55z60Hkm0XCbjcJ/wP7iC5qZxLeMB2OZHNK5QjFsxYpVB9rNQ6LAaM= 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 1618566136023273.8228038026391; Fri, 16 Apr 2021 02:42:16 -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-577-2kYp2fK8NnK69FFdS_wJCg-1; Fri, 16 Apr 2021 05:42:11 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5C29519251A3; Fri, 16 Apr 2021 09:42:05 +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 9D0E719744; Fri, 16 Apr 2021 09:42:04 +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 65A004ED86; Fri, 16 Apr 2021 09:42:02 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 13G9g1Gp006189 for ; Fri, 16 Apr 2021 05:42:01 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2BEEF6E505; Fri, 16 Apr 2021 09:42:01 +0000 (UTC) Received: from work.redhat.com (ovpn-113-12.ams2.redhat.com [10.36.113.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id 49F9F6A046; Fri, 16 Apr 2021 09:42:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618566133; 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=+JdjSjuZbH8YwXZ+w2Vk6jiNOX2X4Qmodv1NB9spK7w=; b=cqABzv647BWitx/5H6cpTMeJMRE2pDD9/duYPbNQI18KAhezc+y1+P116Cw1enUUC4Jue4 HgWDTXJwFMeq05lf+VhBjCUeXEWn6PcP3zqW+NLYEh4gfxPUsknJcgqdlxrBjB5Xyh/Rhw s9QdSdS3xRspKJKSc+oV3p0097sjTGE= X-MC-Unique: 2kYp2fK8NnK69FFdS_wJCg-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH v6 3/8] virxml: Add virXMLPropInt Date: Fri, 16 Apr 2021 11:41:47 +0200 Message-Id: <20210416094152.202334-4-twiederh@redhat.com> In-Reply-To: <20210416094152.202334-1-twiederh@redhat.com> References: <20210416094152.202334-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Peter Krempa , 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.84 on 10.5.11.23 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 Reviewed-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/virxml.c | 53 ++++++++++++++++++++++++++++++++++++++++ src/util/virxml.h | 8 ++++++ 3 files changed, 62 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ba70e8b4ca..11e7b0c741 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3548,6 +3548,7 @@ virXMLNodeSanitizeNamespaces; virXMLNodeToString; virXMLParseHelper; virXMLPickShellSafeComment; +virXMLPropInt; virXMLPropString; virXMLPropStringLimit; virXMLPropTristateBool; diff --git a/src/util/virxml.c b/src/util/virxml.c index 1b1c3878e6..6da4a2b1f6 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -646,6 +646,59 @@ virXMLPropTristateSwitch(xmlNodePtr node, } =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 val; + + if (!(tmp =3D virXMLPropString(node, name))) { + if (!(flags & VIR_XML_PROP_REQUIRED)) + return 0; + + virReportError(VIR_ERR_XML_ERROR, + _("Missing required attribute '%s' in element '%s'"= ), + name, node->name); + return -1; + } + + if (virStrToLong_i(tmp, NULL, base, &val) < 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) && (val =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; + } + + *result =3D val; + return 1; +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index 4c9497ba17..10c2404f5f 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 absent */ + VIR_XML_PROP_NONZERO =3D 1 << 1, /* Attribute may not be zero */ } virXMLPropFlags; =20 int virXPathBoolean(const char *xpath, @@ -96,6 +97,13 @@ int virXMLPropTristateSwitch(xmlNodePtr node, virTristateSwitch *result) ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); =20 +int virXMLPropInt(xmlNodePtr node, + const char *name, + int base, + virXMLPropFlags flags, + int *result) + ATTRIBUTE_NONNULL(0) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4); + /* Internal function; prefer the macros below. */ xmlDocPtr virXMLParseHelper(int domcode, const char *filename, --=20 2.26.2