From nobody Wed Feb 11 10:04:38 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1490382331720731.123626627818; Fri, 24 Mar 2017 12:05:31 -0700 (PDT) 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 mx1.redhat.com (Postfix) with ESMTPS id 3952BC208089; Fri, 24 Mar 2017 19:05:31 +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 0B4BC18229; Fri, 24 Mar 2017 19:05:31 +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 A09C618523D2; Fri, 24 Mar 2017 19:05:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v2OJ0Zva019147 for ; Fri, 24 Mar 2017 15:00:35 -0400 Received: by smtp.corp.redhat.com (Postfix) id 808367D569; Fri, 24 Mar 2017 19:00:35 +0000 (UTC) Received: from caroline.brq.redhat.com (dhcp129-198.brq.redhat.com [10.34.129.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 07D8719630 for ; Fri, 24 Mar 2017 19:00:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3952BC208089 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3952BC208089 From: Martin Kletzander To: libvir-list@redhat.com Date: Fri, 24 Mar 2017 20:00:00 +0100 Message-Id: <65590ba43dcd15a83e2a0a794e53813c0931f2b0.1490381160.git.mkletzan@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 12/23] virfile: Add helpers for reading simple values 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 24 Mar 2017 19:05:32 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" These helpers are doing just a read and covert the value, but they properly size the read limit, handle additional whitespace characters, and unify error reporting. Signed-off-by: Martin Kletzander --- src/libvirt_private.syms | 3 ++ src/util/virfile.c | 83 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virfile.h | 6 ++++ 3 files changed, 92 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ddeca239bb9c..27fbca589faa 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1626,6 +1626,9 @@ virFileReadBufQuiet; virFileReadHeaderFD; virFileReadLimFD; virFileReadLink; +virFileReadValueBitmap; +virFileReadValueInt; +virFileReadValueUint; virFileRelLinkPointsTo; virFileRemove; virFileRemoveLastComponent; diff --git a/src/util/virfile.c b/src/util/virfile.c index 41cdca953b28..e0e6f3c91581 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -65,6 +65,7 @@ #endif #include "configmake.h" +#include "intprops.h" #include "viralloc.h" #include "vircommand.h" #include "virerror.h" @@ -3794,3 +3795,85 @@ virFileComparePaths(const char *p1, const char *p2) VIR_FREE(res2); return ret; } + + +int +virFileReadValueInt(const char *path, int *value) +{ + char *str =3D NULL; + char *endp =3D NULL; + + if (!virFileExists(path)) + return -2; + + if (virFileReadAll(path, INT_STRLEN_BOUND(*value), &str) < 0) + return -1; + + if (virStrToLong_i(str, &endp, 10, value) < 0 || + (endp && !c_isspace(*endp))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid integer value '%s' in file '%s'"), + str, path); + return -1; + } + + VIR_FREE(str); + + return 0; +} + + +int +virFileReadValueUint(const char *path, unsigned int *value) +{ + char *str =3D NULL; + char *endp =3D NULL; + + if (!virFileExists(path)) + return -2; + + if (virFileReadAll(path, INT_STRLEN_BOUND(*value), &str) < 0) + return -1; + + if (virStrToLong_uip(str, &endp, 10, value) < 0 || + (endp && !c_isspace(*endp))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Invalid unsigned integer value '%s' in file '%s'= "), + str, path); + return -1; + } + + VIR_FREE(str); + + return 0; +} + +int +virFileReadValueBitmap(const char *path, + int maxlen, + virBitmapPtr *value) +{ + char *buf =3D NULL; + int ret =3D -1; + char *tmp =3D NULL; + + if (!virFileExists(path)) + return -2; + + if (virFileReadAll(path, maxlen, &buf) < 0) + goto cleanup; + + /* trim optinoal newline at the end */ + tmp =3D buf + strlen(buf) - 1; + if (*tmp =3D=3D '\n') + *tmp =3D '\0'; + + *value =3D virBitmapParseUnlimited(buf); + if (!*value) + goto cleanup; + + ret =3D 0; + cleanup: + VIR_FREE(buf); + return ret; +} diff --git a/src/util/virfile.h b/src/util/virfile.h index b29feeeb1d87..ba1c57c06a8e 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -30,6 +30,7 @@ # include # include "internal.h" +# include "virbitmap.h" # include "virstoragefile.h" typedef enum { @@ -334,4 +335,9 @@ int virFileCopyACLs(const char *src, const char *dst); int virFileComparePaths(const char *p1, const char *p2); + +int virFileReadValueInt(const char *path, int *value); +int virFileReadValueUint(const char *path, unsigned int *value); +int virFileReadValueBitmap(const char *path, int maxlen, virBitmapPtr *val= ue); + #endif /* __VIR_FILE_H */ --=20 2.12.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list