From patchwork Mon Mar 11 11:23:15 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 226530 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5D5A72C02B3 for ; Mon, 11 Mar 2013 22:29:22 +1100 (EST) Received: from localhost ([::1]:39689 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UF0uu-0003hS-Ev for incoming@patchwork.ozlabs.org; Mon, 11 Mar 2013 07:29:20 -0400 Received: from eggs.gnu.org ([208.118.235.92]:52947) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UF0rd-0006zL-53 for qemu-devel@nongnu.org; Mon, 11 Mar 2013 07:26:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UF0rY-0001cj-Sg for qemu-devel@nongnu.org; Mon, 11 Mar 2013 07:25:57 -0400 Received: from e28smtp03.in.ibm.com ([122.248.162.3]:49076) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UF0rY-0001bK-82 for qemu-devel@nongnu.org; Mon, 11 Mar 2013 07:25:52 -0400 Received: from /spool/local by e28smtp03.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 11 Mar 2013 16:52:52 +0530 Received: from d28dlp02.in.ibm.com (9.184.220.127) by e28smtp03.in.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 11 Mar 2013 16:52:51 +0530 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by d28dlp02.in.ibm.com (Postfix) with ESMTP id 298023940053 for ; Mon, 11 Mar 2013 16:55:49 +0530 (IST) Received: from d28av02.in.ibm.com (d28av02.in.ibm.com [9.184.220.64]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r2BBPiNq34210000 for ; Mon, 11 Mar 2013 16:55:44 +0530 Received: from d28av02.in.ibm.com (loopback [127.0.0.1]) by d28av02.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r2BBPlKl026706 for ; Mon, 11 Mar 2013 22:25:48 +1100 Received: from RH63Wenchao (wenchaox.cn.ibm.com [9.115.122.175] (may be forged)) by d28av02.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r2BBNRVH015564; Mon, 11 Mar 2013 22:25:45 +1100 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Mon, 11 Mar 2013 19:23:15 +0800 Message-Id: <1363000996-13221-14-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1363000996-13221-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1363000996-13221-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13031111-3864-0000-0000-000007308C86 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.3 Cc: kwolf@redhat.com, stefanha@gmail.com, armbru@redhat.com, lcapitulino@redhat.com, pbonzini@redhat.com, Wenchao Xia Subject: [Qemu-devel] [PATCH V9 13/14] block: dump to buffer for bdrv_image_info_dump() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This allow hmp use this function, just like qemu-img. It also returns a pointer now to make it easy to use. Signed-off-by: Wenchao Xia --- block/qapi.c | 67 +++++++++++++++++++++++++++++++++++-------------- include/block/qapi.h | 2 +- qemu-img.c | 6 +++- 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/block/qapi.c b/block/qapi.c index 1eb3de1..e6a6ec6 100644 --- a/block/qapi.c +++ b/block/qapi.c @@ -285,9 +285,30 @@ DeviceImageInfoList *qmp_query_images(bool has_device, return NULL; } -void bdrv_image_info_dump(ImageInfo *info) +GCC_FMT_ATTR(3, 4) +static void snprintf_tail(char **p_buf, int *p_size, const char *fmt, ...) +{ + int l; + if (*p_size < 1) { + return; + } + + va_list ap; + va_start(ap, fmt); + l = vsnprintf(*p_buf, *p_size, fmt, ap); + va_end(ap); + if (l > 0) { + *p_buf += l; + *p_size -= l; + } +} + +/* Use buf instead of asprintf to reduce memory fragility. */ +char *bdrv_image_info_dump(char *buf, int buf_size, ImageInfo *info) { char size_buf[128], dsize_buf[128]; + char *buf1 = buf; + if (!info->has_actual_size) { snprintf(dsize_buf, sizeof(dsize_buf), "unavailable"); } else { @@ -295,43 +316,49 @@ void bdrv_image_info_dump(ImageInfo *info) info->actual_size); } get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size); - printf("image: %s\n" - "file format: %s\n" - "virtual size: %s (%" PRId64 " bytes)\n" - "disk size: %s\n", - info->filename, info->format, size_buf, - info->virtual_size, - dsize_buf); + snprintf_tail(&buf1, &buf_size, + "image: %s\n" + "file format: %s\n" + "virtual size: %s (%" PRId64 " bytes)\n" + "disk size: %s\n", + info->filename, info->format, size_buf, + info->virtual_size, + dsize_buf); if (info->has_encrypted && info->encrypted) { - printf("encrypted: yes\n"); + snprintf_tail(&buf1, &buf_size, "encrypted: yes\n"); } if (info->has_cluster_size) { - printf("cluster_size: %" PRId64 "\n", info->cluster_size); + snprintf_tail(&buf1, &buf_size, "cluster_size: %" PRId64 "\n", + info->cluster_size); } if (info->has_dirty_flag && info->dirty_flag) { - printf("cleanly shut down: no\n"); + snprintf_tail(&buf1, &buf_size, "cleanly shut down: no\n"); } if (info->has_backing_filename) { - printf("backing file: %s", info->backing_filename); + snprintf_tail(&buf1, &buf_size, "backing file: %s", + info->backing_filename); if (info->has_full_backing_filename) { - printf(" (actual path: %s)", info->full_backing_filename); + snprintf_tail(&buf1, &buf_size, " (actual path: %s)", + info->full_backing_filename); } - putchar('\n'); + snprintf_tail(&buf1, &buf_size, "\n"); if (info->has_backing_filename_format) { - printf("backing file format: %s\n", info->backing_filename_format); + snprintf_tail(&buf1, &buf_size, "backing file format: %s\n", + info->backing_filename_format); } } if (info->has_snapshots) { SnapshotInfoList *elem; - char buf[256]; + char buf_sn[256]; - printf("Snapshot list:\n"); - printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); + snprintf_tail(&buf1, &buf_size, "Snapshot list:\n"); + snprintf_tail(&buf1, &buf_size, "%s\n", + bdrv_snapshot_dump(buf_sn, sizeof(buf_sn), NULL)); /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but * we convert to the block layer's native QEMUSnapshotInfo for now. @@ -347,7 +374,9 @@ void bdrv_image_info_dump(ImageInfo *info) pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); pstrcpy(sn.name, sizeof(sn.name), elem->value->name); - printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn)); + snprintf_tail(&buf1, &buf_size, "%s\n", + bdrv_snapshot_dump(buf_sn, sizeof(buf_sn), &sn)); } } + return buf; } diff --git a/include/block/qapi.h b/include/block/qapi.h index cc55bda..7962871 100644 --- a/include/block/qapi.h +++ b/include/block/qapi.h @@ -24,5 +24,5 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs, int bdrv_query_image_info(BlockDriverState *bs, ImageInfo **p_info, Error **errp); -void bdrv_image_info_dump(ImageInfo *info); +char *bdrv_image_info_dump(char *buf, int buf_size, ImageInfo *info); #endif diff --git a/qemu-img.c b/qemu-img.c index e1d6bac..6db2b38 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1606,10 +1606,12 @@ static void dump_json_image_info(ImageInfo *info) QDECREF(str); } +#define IMAGE_INFO_BUF_SIZE (2048) static void dump_human_image_info_list(ImageInfoList *list) { ImageInfoList *elem; bool delim = false; + char *buf = g_malloc0(IMAGE_INFO_BUF_SIZE); for (elem = list; elem; elem = elem->next) { if (delim) { @@ -1617,8 +1619,10 @@ static void dump_human_image_info_list(ImageInfoList *list) } delim = true; - bdrv_image_info_dump(elem->value); + printf("%s", + bdrv_image_info_dump(buf, IMAGE_INFO_BUF_SIZE, elem->value)); } + g_free(buf); } static gboolean str_equal_func(gconstpointer a, gconstpointer b)