]> xenbits.xensource.com Git - libvirt.git/commitdiff
virfile: Introduce virFileReadValueBitmapAllowEmpty()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 23 Apr 2024 08:41:50 +0000 (10:41 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 6 May 2024 13:29:36 +0000 (15:29 +0200)
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 <mprivozn@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h

index cc364a421faf0703785ea7e1b435a2256edfc1a3..661b560ffe33da452ef9ab0829f0b2426dfbdaf2 100644 (file)
@@ -2361,6 +2361,7 @@ virFileReadHeaderFD;
 virFileReadHeaderQuiet;
 virFileReadLimFD;
 virFileReadValueBitmap;
+virFileReadValueBitmapAllowEmpty;
 virFileReadValueInt;
 virFileReadValueScaledInt;
 virFileReadValueString;
index deaf4555fd8c56f819a46a1980ded24872c5edb3..c769f7d65031810999b925929ddf0c06e9c78f59 100644 (file)
@@ -4365,6 +4365,34 @@ virFileReadValueScaledInt(unsigned long long *value, const char *format, ...)
  * used for small, interface-like files, so it should not be huge (subjective) */
 #define VIR_FILE_READ_VALUE_STRING_MAX 4096
 
+static int
+virFileReadValueBitmapImpl(virBitmap **value,
+                           const char *path,
+                           bool allowEmpty)
+{
+    g_autofree char *str = NULL;
+
+    if (!virFileExists(path))
+        return -2;
+
+    if (virFileReadAll(path, VIR_FILE_READ_VALUE_STRING_MAX, &str) < 0)
+        return -1;
+
+    virStringTrimOptionalNewline(str);
+
+    if (allowEmpty) {
+        *value = virBitmapParseUnlimitedAllowEmpty(str);
+    } else {
+        *value = virBitmapParseUnlimited(str);
+    }
+
+    if (!*value)
+        return -1;
+
+    return 0;
+}
+
+
 /**
  * virFileReadValueBitmap:
  * @value: pointer to virBitmap * to be allocated and filled in with the value
@@ -4372,13 +4400,13 @@ virFileReadValueScaledInt(unsigned long long *value, const char *format, ...)
  *
  * 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.
+ * 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 *str = NULL;
     g_autofree char *path = NULL;
     va_list ap;
 
@@ -4386,21 +4414,36 @@ virFileReadValueBitmap(virBitmap **value, const char *format, ...)
     path = g_strdup_vprintf(format, ap);
     va_end(ap);
 
-    if (!virFileExists(path))
-        return -2;
+    return virFileReadValueBitmapImpl(value, path, false);
+}
 
-    if (virFileReadAll(path, VIR_FILE_READ_VALUE_STRING_MAX, &str) < 0)
-        return -1;
 
-    virStringTrimOptionalNewline(str);
+/**
+ * virFileReadValueBitmapAllowEmpty:
+ * @value: pointer to virBitmap * to be allocated and filled in with the value
+ * @format, ...: file to read from
+ *
+ * Just like virFileReadValueBitmap(), except if the file is empty or contains
+ * 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 = NULL;
+    va_list ap;
 
-    *value = virBitmapParseUnlimited(str);
-    if (!*value)
-        return -1;
+    va_start(ap, format);
+    path = g_strdup_vprintf(format, ap);
+    va_end(ap);
 
-    return 0;
+    return virFileReadValueBitmapImpl(value, path, true);
 }
 
+
 /**
  * virFileReadValueString:
  * @value: pointer to char * to be allocated and filled in with the value
index 56fe309bce68a86f7bc9d8793f3b7ea08ab423fa..7df3fcb8402e36d4b24e28c5818605dce7a4c261 100644 (file)
@@ -354,6 +354,8 @@ int virFileReadValueUllongQuiet(unsigned long long *value, 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 *format, ...)
  G_GNUC_PRINTF(2, 3);
 int virFileReadValueString(char **value, const char *format, ...)