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