Re: [PATCH 12/23] virfile: Add helpers for reading simple values

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 24.03.2017 20:00, Martin Kletzander wrote:
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 <mkletzan@xxxxxxxxxx>
---
 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 = NULL;
+    char *endp = 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;
+}

I'd expect some comment for these. Esp. on retvals.

Michal

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux