render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
404507
From 86761c187660d73daed649c6be28502aa5e01a14 Mon Sep 17 00:00:00 2001
404507
Message-Id: <86761c187660d73daed649c6be28502aa5e01a14@dist-git>
404507
From: Martin Kletzander <mkletzan@redhat.com>
404507
Date: Wed, 31 Jan 2018 16:32:13 +0100
404507
Subject: [PATCH] util: Don't output too many zeros from virBitmapToString
404507
404507
https://bugzilla.redhat.com/show_bug.cgi?id=1289368
404507
404507
Truncate the output so that it is only as big as is needed to fit all
404507
the bits, not all the units from the map.  This will be needed in the
404507
future in order to properly format bitmaps for kernel's sysfs files.
404507
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
Reviewed-by: John Ferlan <jferlan@redhat.com>
404507
(cherry picked from commit fb106028752d8bd31f48a7c50357b7437b87d7c1)
404507
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
404507
---
404507
 src/qemu/qemu_capabilities.c |  4 ++--
404507
 src/util/virbitmap.c         | 30 ++++++++++++++++++++++++++++--
404507
 src/util/virbitmap.h         |  2 +-
404507
 3 files changed, 31 insertions(+), 5 deletions(-)
404507
404507
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
404507
index 5bf188f427..dc546ba702 100644
404507
--- a/src/qemu/qemu_capabilities.c
404507
+++ b/src/qemu/qemu_capabilities.c
404507
@@ -1525,7 +1525,7 @@ int virQEMUCapsParseHelpStr(const char *qemu,
404507
                                    qemuCaps, check_yajl) < 0)
404507
         goto cleanup;
404507
 
404507
-    strflags = virBitmapToString(qemuCaps->flags, true);
404507
+    strflags = virBitmapToString(qemuCaps->flags, true, false);
404507
     VIR_DEBUG("Version %u.%u.%u, cooked version %u, flags %s",
404507
               major, minor, micro, *version, NULLSTR(strflags));
404507
     VIR_FREE(strflags);
404507
@@ -2407,7 +2407,7 @@ virQEMUCapsClear(virQEMUCapsPtr qemuCaps,
404507
 
404507
 char *virQEMUCapsFlagsString(virQEMUCapsPtr qemuCaps)
404507
 {
404507
-    return virBitmapToString(qemuCaps->flags, true);
404507
+    return virBitmapToString(qemuCaps->flags, true, false);
404507
 }
404507
 
404507
 
404507
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
404507
index f5700a6424..b1c1236fd3 100644
404507
--- a/src/util/virbitmap.c
404507
+++ b/src/util/virbitmap.c
404507
@@ -313,6 +313,7 @@ int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
404507
  * virBitmapToString:
404507
  * @bitmap: Pointer to bitmap
404507
  * @prefix: Whether to prepend "0x"
404507
+ * @trim: Whether to output only the minimum required characters
404507
  *
404507
  * Convert @bitmap to printable string.
404507
  *
404507
@@ -320,10 +321,14 @@ int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
404507
  */
404507
 char *
404507
 virBitmapToString(virBitmapPtr bitmap,
404507
-                  bool prefix)
404507
+                  bool prefix,
404507
+                  bool trim)
404507
 {
404507
     virBuffer buf = VIR_BUFFER_INITIALIZER;
404507
     size_t sz;
404507
+    size_t len;
404507
+    size_t diff;
404507
+    char *ret = NULL;
404507
 
404507
     if (prefix)
404507
         virBufferAddLit(&buf, "0x");
404507
@@ -337,7 +342,28 @@ virBitmapToString(virBitmapPtr bitmap,
404507
     }
404507
 
404507
     virBufferCheckError(&buf;;
404507
-    return virBufferContentAndReset(&buf;;
404507
+    ret = virBufferContentAndReset(&buf;;
404507
+    if (!ret)
404507
+        return NULL;
404507
+
404507
+    if (!trim)
404507
+        return ret;
404507
+
404507
+    if (bitmap->max_bit != bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT) {
404507
+        char *tmp = ret;
404507
+
404507
+        if (prefix)
404507
+            tmp += 2;
404507
+
404507
+        len = strlen(tmp);
404507
+        sz = VIR_DIV_UP(bitmap->max_bit, 4);
404507
+        diff = len - sz;
404507
+
404507
+        if (diff)
404507
+            memmove(tmp, tmp + diff, sz + 1);
404507
+    }
404507
+
404507
+    return ret;
404507
 }
404507
 
404507
 /**
404507
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
404507
index 720b389cfe..02acb7519d 100644
404507
--- a/src/util/virbitmap.h
404507
+++ b/src/util/virbitmap.h
404507
@@ -80,7 +80,7 @@ bool virBitmapIsBitSet(virBitmapPtr bitmap, size_t b)
404507
 int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
404507
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
404507
 
404507
-char *virBitmapToString(virBitmapPtr bitmap, bool prefix)
404507
+char *virBitmapToString(virBitmapPtr bitmap, bool prefix, bool trim)
404507
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
404507
 
404507
 char *virBitmapFormat(virBitmapPtr bitmap);
404507
-- 
404507
2.16.1
404507