mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-10 14:57:42 +00:00
util: Don't output too many zeros from virBitmapToString
Truncate the output so that it is only as big as is needed to fit all the bits, not all the units from the map. This will be needed in the future in order to properly format bitmaps for kernel's sysfs files. Signed-off-by: Martin Kletzander <mkletzan@redhat.com> Reviewed-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
parent
5d893ed6ad
commit
fb10602875
@ -1510,7 +1510,7 @@ int virQEMUCapsParseHelpStr(const char *qemu,
|
|||||||
qemuCaps, check_yajl) < 0)
|
qemuCaps, check_yajl) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
strflags = virBitmapToString(qemuCaps->flags, true);
|
strflags = virBitmapToString(qemuCaps->flags, true, false);
|
||||||
VIR_DEBUG("Version %u.%u.%u, cooked version %u, flags %s",
|
VIR_DEBUG("Version %u.%u.%u, cooked version %u, flags %s",
|
||||||
major, minor, micro, *version, NULLSTR(strflags));
|
major, minor, micro, *version, NULLSTR(strflags));
|
||||||
VIR_FREE(strflags);
|
VIR_FREE(strflags);
|
||||||
@ -2377,7 +2377,7 @@ virQEMUCapsClear(virQEMUCapsPtr qemuCaps,
|
|||||||
|
|
||||||
char *virQEMUCapsFlagsString(virQEMUCapsPtr qemuCaps)
|
char *virQEMUCapsFlagsString(virQEMUCapsPtr qemuCaps)
|
||||||
{
|
{
|
||||||
return virBitmapToString(qemuCaps->flags, true);
|
return virBitmapToString(qemuCaps->flags, true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -313,6 +313,7 @@ int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
|
|||||||
* virBitmapToString:
|
* virBitmapToString:
|
||||||
* @bitmap: Pointer to bitmap
|
* @bitmap: Pointer to bitmap
|
||||||
* @prefix: Whether to prepend "0x"
|
* @prefix: Whether to prepend "0x"
|
||||||
|
* @trim: Whether to output only the minimum required characters
|
||||||
*
|
*
|
||||||
* Convert @bitmap to printable string.
|
* Convert @bitmap to printable string.
|
||||||
*
|
*
|
||||||
@ -320,10 +321,14 @@ int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
|
|||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
virBitmapToString(virBitmapPtr bitmap,
|
virBitmapToString(virBitmapPtr bitmap,
|
||||||
bool prefix)
|
bool prefix,
|
||||||
|
bool trim)
|
||||||
{
|
{
|
||||||
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
size_t sz;
|
size_t sz;
|
||||||
|
size_t len;
|
||||||
|
size_t diff;
|
||||||
|
char *ret = NULL;
|
||||||
|
|
||||||
if (prefix)
|
if (prefix)
|
||||||
virBufferAddLit(&buf, "0x");
|
virBufferAddLit(&buf, "0x");
|
||||||
@ -337,7 +342,28 @@ virBitmapToString(virBitmapPtr bitmap,
|
|||||||
}
|
}
|
||||||
|
|
||||||
virBufferCheckError(&buf);
|
virBufferCheckError(&buf);
|
||||||
return virBufferContentAndReset(&buf);
|
ret = virBufferContentAndReset(&buf);
|
||||||
|
if (!ret)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (!trim)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
if (bitmap->max_bit != bitmap->map_len * VIR_BITMAP_BITS_PER_UNIT) {
|
||||||
|
char *tmp = ret;
|
||||||
|
|
||||||
|
if (prefix)
|
||||||
|
tmp += 2;
|
||||||
|
|
||||||
|
len = strlen(tmp);
|
||||||
|
sz = VIR_DIV_UP(bitmap->max_bit, 4);
|
||||||
|
diff = len - sz;
|
||||||
|
|
||||||
|
if (diff)
|
||||||
|
memmove(tmp, tmp + diff, sz + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,7 +80,7 @@ bool virBitmapIsBitSet(virBitmapPtr bitmap, size_t b)
|
|||||||
int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
|
int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
char *virBitmapToString(virBitmapPtr bitmap, bool prefix)
|
char *virBitmapToString(virBitmapPtr bitmap, bool prefix, bool trim)
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
char *virBitmapFormat(virBitmapPtr bitmap);
|
char *virBitmapFormat(virBitmapPtr bitmap);
|
||||||
|
Loading…
Reference in New Issue
Block a user