From nobody Mon Feb 9 01:11:54 2026 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1713881849596986.4882395785751; Tue, 23 Apr 2024 07:17:29 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 4C0D321BC; Tue, 23 Apr 2024 10:17:28 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 851C01F8D; Tue, 23 Apr 2024 10:16:31 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 633E11E3E; Tue, 23 Apr 2024 10:16:29 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id CC2AD1DE1 for ; Tue, 23 Apr 2024 10:16:28 -0400 (EDT) Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-274-2GM0c8EBPKGsS-4pLod57A-1; Tue, 23 Apr 2024 10:16:27 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F0F301C03154 for ; Tue, 23 Apr 2024 14:16:26 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9BCE01033C63 for ; Tue, 23 Apr 2024 14:16:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 X-MC-Unique: 2GM0c8EBPKGsS-4pLod57A-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH v2 2/4] virfile: Introduce virFileReadValueBitmapAllowEmpty() Date: Tue, 23 Apr 2024 16:16:22 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Message-ID-Hash: EVOUCYL7QKWJHRAGKNM22A2UREBNVB7R X-Message-ID-Hash: EVOUCYL7QKWJHRAGKNM22A2UREBNVB7R X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8"; x-default="true" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1713881849849100001 Some sysfs files contain either string representation of a bitmap or just a newline character. An example of such file is: /sys/devices/system/cpu/isolated. Our current implementation of virFileReadValueBitmap() fails in the latter case, unfortunately. Introduce a slightly modified version that accepts empty files. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virfile.c | 81 ++++++++++++++++++++++++++++++---------- src/util/virfile.h | 2 + 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0ba6183010..2c7e4b45d3 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2360,6 +2360,7 @@ virFileReadHeaderFD; virFileReadHeaderQuiet; virFileReadLimFD; virFileReadValueBitmap; +virFileReadValueBitmapAllowEmpty; virFileReadValueInt; virFileReadValueScaledInt; virFileReadValueString; diff --git a/src/util/virfile.c b/src/util/virfile.c index deaf4555fd..c769f7d650 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -4365,26 +4365,12 @@ virFileReadValueScaledInt(unsigned long long *value= , const char *format, ...) * used for small, interface-like files, so it should not be huge (subject= ive) */ #define VIR_FILE_READ_VALUE_STRING_MAX 4096 =20 -/** - * virFileReadValueBitmap: - * @value: pointer to virBitmap * to be allocated and filled in with the v= alue - * @format, ...: file to read from - * - * Read int from @format and put it into @value. - * - * Return -2 for non-existing file, -1 on other errors and 0 if everything= went - * fine. - */ -int -virFileReadValueBitmap(virBitmap **value, const char *format, ...) +static int +virFileReadValueBitmapImpl(virBitmap **value, + const char *path, + bool allowEmpty) { g_autofree char *str =3D NULL; - g_autofree char *path =3D NULL; - va_list ap; - - va_start(ap, format); - path =3D g_strdup_vprintf(format, ap); - va_end(ap); =20 if (!virFileExists(path)) return -2; @@ -4394,13 +4380,70 @@ virFileReadValueBitmap(virBitmap **value, const cha= r *format, ...) =20 virStringTrimOptionalNewline(str); =20 - *value =3D virBitmapParseUnlimited(str); + if (allowEmpty) { + *value =3D virBitmapParseUnlimitedAllowEmpty(str); + } else { + *value =3D virBitmapParseUnlimited(str); + } + if (!*value) return -1; =20 return 0; } =20 + +/** + * virFileReadValueBitmap: + * @value: pointer to virBitmap * to be allocated and filled in with the v= alue + * @format, ...: file to read from + * + * Read int from @format and put it into @value. + * + * Returns: -2 for non-existing file, + * -1 on other errors (with error reported), + * 0 otherwise. + */ +int +virFileReadValueBitmap(virBitmap **value, const char *format, ...) +{ + g_autofree char *path =3D NULL; + va_list ap; + + va_start(ap, format); + path =3D g_strdup_vprintf(format, ap); + va_end(ap); + + return virFileReadValueBitmapImpl(value, path, false); +} + + +/** + * virFileReadValueBitmapAllowEmpty: + * @value: pointer to virBitmap * to be allocated and filled in with the v= alue + * @format, ...: file to read from + * + * Just like virFileReadValueBitmap(), except if the file is empty or cont= ains + * nothing but spaces an empty bitmap is returned instead of an error. + * + * Returns: -2 for non-existing file, + * -1 on other errors (with error reported), + * 0 otherwise. + */ +int +virFileReadValueBitmapAllowEmpty(virBitmap **value, const char *format, ..= .) +{ + g_autofree char *path =3D NULL; + va_list ap; + + va_start(ap, format); + path =3D g_strdup_vprintf(format, ap); + va_end(ap); + + return virFileReadValueBitmapImpl(value, path, true); +} + + /** * virFileReadValueString: * @value: pointer to char * to be allocated and filled in with the value diff --git a/src/util/virfile.h b/src/util/virfile.h index 56fe309bce..7df3fcb840 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -354,6 +354,8 @@ int virFileReadValueUllongQuiet(unsigned long long *val= ue, const char *format, . G_GNUC_PRINTF(2, 3); int virFileReadValueBitmap(virBitmap **value, const char *format, ...) G_GNUC_PRINTF(2, 3); +int virFileReadValueBitmapAllowEmpty(virBitmap **value, const char *format= , ...) + G_GNUC_PRINTF(2, 3); int virFileReadValueScaledInt(unsigned long long *value, const char *forma= t, ...) G_GNUC_PRINTF(2, 3); int virFileReadValueString(char **value, const char *format, ...) --=20 2.43.2 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org