* 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
*
* 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;
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