Am 24.01.2013 03:57, schrieb Wenchao Xia: > This patch add function bdrv_query_image_info(), which will return > image info in qmp object format. The implementation code are mostly > copied from qemu-img.c, but use block layer function to get snapshot > info.
Don't copy code, reuse it. Can you move the existing qemu-img code to block.c and make qemu-img use it from there? > A check with bdrv_can_read_snapshot(), was done before collecting > snapshot info. > > Signed-off-by: Wenchao Xia <xiaw...@linux.vnet.ibm.com> > Reviewed-by: Eric Blake <ebl...@redhat.com> > --- > block.c | 73 > +++++++++++++++++++++++++++++++++++++++++++++++++ > include/block/block.h | 1 + > 2 files changed, 74 insertions(+), 0 deletions(-) > > diff --git a/block.c b/block.c > index 7cdb6c6..14bf653 100644 > --- a/block.c > +++ b/block.c > @@ -2902,6 +2902,79 @@ SnapshotInfoList > *bdrv_query_snapshot_infolist(BlockDriverState *bs, > return head; > } > > +/* collect all internal snapshot info in a image for ImageInfo */ > +static void collect_snapshots_info(BlockDriverState *bs, > + ImageInfo *info, > + Error **errp) > +{ > + SnapshotInfoList *info_list; > + > + if (!bdrv_can_read_snapshot(bs)) { > + return; > + } > + info_list = bdrv_query_snapshot_infolist(bs, NULL, NULL, errp); > + if (info_list != NULL) { > + info->has_snapshots = true; > + info->snapshots = info_list; > + } > +} > + > +static void collect_image_info(BlockDriverState *bs, > + ImageInfo *info) > +{ > + uint64_t total_sectors; > + char backing_filename[1024]; > + char backing_filename2[1024]; > + BlockDriverInfo bdi; > + const char *filename; > + > + filename = bdrv_get_filename(bs); > + bdrv_get_geometry(bs, &total_sectors); > + > + info->filename = g_strdup(filename); > + info->format = g_strdup(bdrv_get_format_name(bs)); > + info->virtual_size = total_sectors * 512; > + info->actual_size = bdrv_get_allocated_file_size(bs); > + info->has_actual_size = info->actual_size >= 0; > + if (bdrv_is_encrypted(bs)) { > + info->encrypted = true; > + info->has_encrypted = true; > + } > + if (bdrv_get_info(bs, &bdi) >= 0) { > + if (bdi.cluster_size != 0) { > + info->cluster_size = bdi.cluster_size; > + info->has_cluster_size = true; > + } > + info->dirty_flag = bdi.is_dirty; > + info->has_dirty_flag = true; > + } > + bdrv_get_backing_filename(bs, backing_filename, > sizeof(backing_filename)); No need to copy this, you can directly access bs->backing_file. Kevin