From nobody Tue Feb 10 04:02:55 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; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1619076711; cv=none; d=zohomail.com; s=zohoarc; b=A4S71iQ+1+atUIBIauDfZQlCYNVpjYccg0jNt96N3xfRYMFWHiGIyO9Un60GkElTiFgCVTmsTR6oRKDWOXtC8xTqYr+ILnzCC+VwESDcuinFRPQ5f4WIQvP3M/YhVKJT02H41RpWPOTa0NrN+YGUNtmUmC/93ByOwoHj+m9eGd0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1619076711; 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=RABx1kKGQnEvIhPoW+KWFBNccFMOF9+gl0HgSJozvbg=; b=WKxPdmHmBjVg6vfNRIi22Bguw9hfVvM2C8FtAou0acz8FljRQ8CygDcUSPOqDDguk7H1psho8o53unNATudxWTj5xrjL2kZgVCp4jXVd7EM8X9inAZ+n/1DtNtwAm21ARg92cxm5hjdv0BVXxQyxO91eYt8XynZ9xTRWSXKSczA= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com 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 1619076711340710.8911293402367; Thu, 22 Apr 2021 00:31:51 -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-585-kXEZC-XwP6iqn7VinO0W8g-1; Thu, 22 Apr 2021 03:31:48 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C4310107ACE6; Thu, 22 Apr 2021 07:31:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 407935C260; Thu, 22 Apr 2021 07:31:37 +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 342101806D1A; Thu, 22 Apr 2021 07:31:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 13M7VTUY023774 for ; Thu, 22 Apr 2021 03:31:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id 13CDAF8965; Thu, 22 Apr 2021 07:31:29 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast02.extmail.prod.ext.rdu2.redhat.com [10.11.55.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0F2A2F896A for ; Thu, 22 Apr 2021 07:31:26 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 384B3A2881D for ; Thu, 22 Apr 2021 07:31:26 +0000 (UTC) Received: from m97136.mail.qiye.163.com (m97136.mail.qiye.163.com [220.181.97.136]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-597-wZi6OTg8NCG9Le7IZj0PNQ-1; Thu, 22 Apr 2021 03:31:22 -0400 Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp2 (Coremail) with SMTP id iOCowAA3HLYJJYFgMwTJAQ--.5876S10; Thu, 22 Apr 2021 15:26:06 +0800 (CST) X-MC-Unique: kXEZC-XwP6iqn7VinO0W8g-1 X-MC-Unique: wZi6OTg8NCG9Le7IZj0PNQ-1 From: Shi Lei To: libvir-list@redhat.com Subject: [RFCv3 08/25] util: Add helper aliases and functions for 'bool' and 'time_t' to cooperate with xmlgen Date: Thu, 22 Apr 2021 15:25:16 +0800 Message-Id: <20210422072533.312211-9-shi_lei@massclouds.com> In-Reply-To: <20210422072533.312211-1-shi_lei@massclouds.com> References: <20210422072533.312211-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: iOCowAA3HLYJJYFgMwTJAQ--.5876S10 X-Coremail-Antispam: 1Uf129KBjvJXoWxKw4fZw15WFWxGrW5tryxZrb_yoW7ZF1xpa yxGa1fAryqq3Z3G397Ka4kGr13Zr4Fqr43Xwsxuw1kGF4Iyr1jyrsxJF98ua15GFZFgr43 tr9Yqry5C3ZxJw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0JbMD7hUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiegV8T1qri0QjWQAAsI X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Cc: Shi Lei 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.16 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 Content-Type: text/plain; charset="utf-8" Add three aliases virBoolYesNo, virBoolOnOff and virBoolTrueFalse for type 'bool'. The tool xmlgen depends on them to determine their ture values in XML. Also, add corresponding functions to parse them. Add virStrToTime and virTimeFormatBuf to convert 'time_t' and 'string'. Add virStrToLong_u8p to convert 'string' to 'uint_8'. Signed-off-by: Shi Lei --- src/internal.h | 8 +++ src/libvirt_private.syms | 5 ++ src/util/virstring.c | 102 +++++++++++++++++++++++++++++++++++++++ src/util/virstring.h | 15 ++++++ 4 files changed, 130 insertions(+) diff --git a/src/internal.h b/src/internal.h index 0a03dfc4..3342934f 100644 --- a/src/internal.h +++ b/src/internal.h @@ -529,3 +529,11 @@ enum { # define fprintf(fh, ...) g_fprintf(fh, __VA_ARGS__) =20 #endif /* VIR_NO_GLIB_STDIO */ + +/* These are aliases for bool. + * The xmlgen depends on them to determine their true values + * in XML. + */ +typedef bool virBoolYesNo; /* True values: 'yes', 'no' */ +typedef bool virBoolOnOff; /* True values: 'on', 'off' */ +typedef bool virBoolTrueFalse; /* True values: 'true', 'false' */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fad0ff71..e78491dc 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3275,6 +3275,9 @@ virStringStripIPv6Brackets; virStringStripSuffix; virStringToUpper; virStringTrimOptionalNewline; +virStrToBoolOnOff; +virStrToBoolTrueFalse; +virStrToBoolYesNo; virStrToDouble; virStrToLong_i; virStrToLong_l; @@ -3285,6 +3288,8 @@ virStrToLong_ul; virStrToLong_ull; virStrToLong_ullp; virStrToLong_ulp; +virStrToTime; +virTimeFormatBuf; virTrimSpaces; =20 =20 diff --git a/src/util/virstring.c b/src/util/virstring.c index a2c07e5c..ac6c0aae 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -233,6 +233,21 @@ virStrToLong_ulp(char const *s, char **end_ptr, int ba= se, return 0; } =20 +/* Just like virStrToLong_uip, above, but produce an "uint8_t" value. + * This version rejects any negative signs. */ +int +virStrToLong_u8p(char const *s, char **end_ptr, int base, uint8_t *result) +{ + unsigned int val; + int ret; + ret =3D virStrToLong_uip(s, end_ptr, base, &val); + if (ret < 0 || val > 0xff) + return -1; + + *result =3D (uint8_t) val; + return 0; +} + /* Just like virStrToLong_i, above, but produce a "long long" value. */ int virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result) @@ -1077,3 +1092,90 @@ int virStringParseYesNo(const char *str, bool *resul= t) =20 return 0; } + + +int +virStrToBoolYesNo(const char *str, bool *result) +{ + if (STREQ(str, "yes")) + *result =3D true; + else if (STREQ(str, "no")) + *result =3D false; + else + return -1; + + return 0; +} + + +int +virStrToBoolOnOff(const char *str, bool *result) +{ + if (STREQ(str, "on")) + *result =3D true; + else if (STREQ(str, "off")) + *result =3D false; + else + return -1; + + return 0; +} + + +int +virStrToBoolTrueFalse(const char *str, bool *result) +{ + if (STREQ(str, "true")) + *result =3D true; + else if (STREQ(str, "false")) + *result =3D false; + else + return -1; + + return 0; +} + + +int virStrToTime(const char *str, time_t *result) +{ + g_autoptr(GDateTime) then =3D NULL; + g_autoptr(GTimeZone) tz =3D g_time_zone_new_utc(); + char *tmp; + int year, mon, mday, hour, min, sec; + + /* Expect: YYYY-MM-DDTHH:MM:SS (%d-%d-%dT%d:%d:%d) eg 2010-11-28T14:2= 9:01 */ + if (/* year */ + virStrToLong_i(str, &tmp, 10, &year) < 0 || *tmp !=3D '-' || + /* month */ + virStrToLong_i(tmp+1, &tmp, 10, &mon) < 0 || *tmp !=3D '-' || + /* day */ + virStrToLong_i(tmp+1, &tmp, 10, &mday) < 0 || *tmp !=3D 'T' || + /* hour */ + virStrToLong_i(tmp+1, &tmp, 10, &hour) < 0 || *tmp !=3D ':' || + /* minute */ + virStrToLong_i(tmp+1, &tmp, 10, &min) < 0 || *tmp !=3D ':' || + /* second */ + virStrToLong_i(tmp+1, &tmp, 10, &sec) < 0 || *tmp !=3D '\0') { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("invalid time '%s', expect YYYY-MM-DDTHH:MM:SS"), + str); + return -1; + } + + then =3D g_date_time_new(tz, year, mon, mday, hour, min, sec); + *result =3D (time_t)g_date_time_to_unix(then); + return 0; +} + + +int +virTimeFormatBuf(virBuffer *buf, const char *layout, const time_t time) +{ + g_autoptr(GDateTime) then =3D NULL; + g_autofree char *thenstr =3D NULL; + + then =3D g_date_time_new_from_unix_utc(time); + thenstr =3D g_date_time_format(then, "%Y-%m-%dT%H:%M:%S"); + virBufferAsprintf(buf, layout, thenstr); + return 0; +} diff --git a/src/util/virstring.h b/src/util/virstring.h index 7cc1d8c5..e16da824 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -19,6 +19,7 @@ #pragma once =20 #include "internal.h" +#include "virbuffer.h" =20 #define VIR_INT64_STR_BUFLEN 21 =20 @@ -59,6 +60,11 @@ int virStrToLong_ulp(char const *s, int base, unsigned long *result) G_GNUC_WARN_UNUSED_RESULT; +int virStrToLong_u8p(char const *s, + char **end_ptr, + int base, + uint8_t *result) + G_GNUC_WARN_UNUSED_RESULT; int virStrToLong_ll(char const *s, char **end_ptr, int base, @@ -141,3 +147,12 @@ int virStringParsePort(const char *str, int virStringParseYesNo(const char *str, bool *result) G_GNUC_WARN_UNUSED_RESULT; + +int virStrToBoolYesNo(const char *str, bool *result); +int virStrToBoolOnOff(const char *str, bool *result); +int virStrToBoolTrueFalse(const char *str, bool *result); + +int virStrToTime(const char *str, time_t *result) + G_GNUC_WARN_UNUSED_RESULT; + +int virTimeFormatBuf(virBuffer *buf, const char *layout, const time_t time= ); --=20 2.25.1