]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virfile: Add helpers for reading simple values
authorMartin Kletzander <mkletzan@redhat.com>
Tue, 7 Mar 2017 09:36:54 +0000 (10:36 +0100)
committerMartin Kletzander <mkletzan@redhat.com>
Mon, 27 Mar 2017 11:13:29 +0000 (13:13 +0200)
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@redhat.com>
src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h

index 00efc296998954a117c3dc3ab4715eb2e81db179..0f5a96d12376fdc3d74b8775917a44657ecb0af5 100644 (file)
@@ -1629,6 +1629,9 @@ virFileReadBufQuiet;
 virFileReadHeaderFD;
 virFileReadLimFD;
 virFileReadLink;
+virFileReadValueBitmap;
+virFileReadValueInt;
+virFileReadValueUint;
 virFileRelLinkPointsTo;
 virFileRemove;
 virFileRemoveLastComponent;
index 41cdca953b282eea23f11a4f0d240310940f4a2a..a91c2c3495015eedfedaf823991b0142b119e860 100644 (file)
@@ -65,6 +65,7 @@
 #endif
 
 #include "configmake.h"
+#include "intprops.h"
 #include "viralloc.h"
 #include "vircommand.h"
 #include "virerror.h"
@@ -3794,3 +3795,116 @@ virFileComparePaths(const char *p1, const char *p2)
     VIR_FREE(res2);
     return ret;
 }
+
+
+/**
+ * virFileReadValueInt:
+ * @path: file to read from
+ * @value: pointer to int to be filled in with the value
+ *
+ * Read int from @path and put it into @value.
+ *
+ * Return -2 for non-existing file, -1 on other errors and 0 if everything went
+ * fine.
+ */
+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;
+}
+
+
+/**
+ * virFileReadValueUint:
+ * @path: file to read from
+ * @value: pointer to unsigned int to be filled in with the value
+ *
+ * Read int from @path and put it into @value.
+ *
+ * Return -2 for non-existing file, -1 on other errors and 0 if everything went
+ * fine.
+ */
+int
+virFileReadValueUint(const char *path, unsigned 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_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;
+}
+
+/**
+ * virFileReadValueBitmap:
+ * @path: file to read from
+ * @value: double pointer to virBitmap to be allocated and filled in with the
+ * value
+ *
+ * Read int from @path and put it into @value.
+ *
+ * Return -2 for non-existing file, -1 on other errors and 0 if everything went
+ * fine.
+ */
+int
+virFileReadValueBitmap(const char *path,
+                       int maxlen,
+                       virBitmapPtr *value)
+{
+    char *buf = NULL;
+    int ret = -1;
+    char *tmp = NULL;
+
+    if (!virFileExists(path))
+        return -2;
+
+    if (virFileReadAll(path, maxlen, &buf) < 0)
+        goto cleanup;
+
+    /* trim optinoal newline at the end */
+    tmp = buf + strlen(buf) - 1;
+    if (*tmp == '\n')
+        *tmp = '\0';
+
+    *value = virBitmapParseUnlimited(buf);
+    if (!*value)
+        goto cleanup;
+
+    ret = 0;
+ cleanup:
+    VIR_FREE(buf);
+    return ret;
+}
index b29feeeb1d87917eac914dfe2899cdf5a9db08ab..ba1c57c06a8e6344b0e8baa8ff1965c5880b8d9a 100644 (file)
@@ -30,6 +30,7 @@
 # include <dirent.h>
 
 # 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 *value);
+
 #endif /* __VIR_FILE_H */