From nobody Thu Oct 30 08:11:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1526088847712318.5895897906639; Fri, 11 May 2018 18:34:07 -0700 (PDT) Received: from localhost ([::1]:33852 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHJQA-0000ba-J0 for importer@patchew.org; Fri, 11 May 2018 21:34:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40344) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHJIE-0006SU-Ay for qemu-devel@nongnu.org; Fri, 11 May 2018 21:25:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fHJIB-0006Ye-Ms for qemu-devel@nongnu.org; Fri, 11 May 2018 21:25:50 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33394 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fHJI7-0006TL-PK; Fri, 11 May 2018 21:25:43 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6D8D67D65F; Sat, 12 May 2018 01:25:43 +0000 (UTC) Received: from probe.bos.redhat.com (dhcp-17-231.bos.redhat.com [10.18.17.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id 07455215CDA7; Sat, 12 May 2018 01:25:42 +0000 (UTC) From: John Snow To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Fri, 11 May 2018 21:25:35 -0400 Message-Id: <20180512012537.22478-11-jsnow@redhat.com> In-Reply-To: <20180512012537.22478-1-jsnow@redhat.com> References: <20180512012537.22478-1-jsnow@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Sat, 12 May 2018 01:25:43 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Sat, 12 May 2018 01:25:43 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jsnow@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [RFC PATCH 10/12] qemu-img: split off common chunk of map command X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Vladimir Sementsov-Ogievskiy , Markus Armbruster , Max Reitz , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It will be re-used for a bitmap listing command. Signed-off-by: John Snow --- qemu-img.c | 192 +++++++++++++++++++++++++++++++++++----------------------= ---- 1 file changed, 110 insertions(+), 82 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index ea62d2d61e..e31e38f674 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -69,8 +69,8 @@ enum { }; =20 typedef enum OutputFormat { - OFORMAT_JSON, OFORMAT_HUMAN, + OFORMAT_JSON, } OutputFormat; =20 /* Default to cache=3Dwriteback as data integrity is not important for qem= u-img */ @@ -2728,96 +2728,122 @@ static inline bool entry_mergeable(const MapEntry = *curr, const MapEntry *next) return true; } =20 +typedef struct CommonOpts { + OutputFormat output_format; + const char *filename; + const char *fmt; + bool force_share; + bool image_opts; +} CommonOpts; + +static int parse_opts_common(CommonOpts *opts, int argc, char **argv) +{ + int c; + + for (;;) { + int option_index =3D 0; + static const struct option long_options[] =3D { + {"help", no_argument, 0, 'h'}, + {"format", required_argument, 0, 'f'}, + {"output", required_argument, 0, OPTION_OUTPUT}, + {"object", required_argument, 0, OPTION_OBJECT}, + {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, + {"force-share", no_argument, 0, 'U'}, + {0, 0, 0, 0} + }; + c =3D getopt_long(argc, argv, ":f:hU", + long_options, &option_index); + if (c =3D=3D -1) { + break; + } + switch (c) { + case ':': + missing_argument(argv[optind - 1]); + break; + case '?': + unrecognized_option(argv[optind - 1]); + break; + case 'h': + help(); + break; + case 'f': + opts->fmt =3D optarg; + break; + case 'U': + opts->force_share =3D true; + break; + case OPTION_OUTPUT: + if (optarg && !strcmp(optarg, "json")) { + opts->output_format =3D OFORMAT_JSON; + } else if (optarg && !strcmp(optarg, "human")) { + opts->output_format =3D OFORMAT_HUMAN; + } else { + error_report("--output must be used with human or json as = argument."); + return -EINVAL; + } + break; + case OPTION_OBJECT: { + QemuOpts *opts; + opts =3D qemu_opts_parse_noisily(&qemu_object_opts, + optarg, true); + if (!opts) { + return -EINVAL; + } + } break; + case OPTION_IMAGE_OPTS: + opts->image_opts =3D true; + break; + } + } + if (qemu_opts_foreach(&qemu_object_opts, + user_creatable_add_opts_foreach, + NULL, NULL)) { + return -EINVAL; + } + + return 0; +} + +static void parse_positional(int argc, char *argv[], + bool opt, const char **str, const char *name)= { + if (!opt && optind >=3D argc) { + error_exit("Expecting '%s' positional parameter", name); + } else if (optind < argc) { + *str =3D argv[optind++]; + } +} + +static void parse_unexpected(int argc, char *argv[]) { + if (optind !=3D argc) { + error_exit("Unexpected argument '%s'\n", argv[optind]); + } +} + static int img_map(int argc, char **argv) { - int c; - OutputFormat output_format =3D OFORMAT_HUMAN; BlockBackend *blk; BlockDriverState *bs; - const char *filename, *fmt, *output; int64_t length; + int ret =3D 0; MapEntry curr =3D { .length =3D 0 }, next; - int ret =3D 0; - bool image_opts =3D false; - bool force_share =3D false; + CommonOpts *opts; =20 - fmt =3D NULL; - output =3D NULL; - for (;;) { - int option_index =3D 0; - static const struct option long_options[] =3D { - {"help", no_argument, 0, 'h'}, - {"format", required_argument, 0, 'f'}, - {"output", required_argument, 0, OPTION_OUTPUT}, - {"object", required_argument, 0, OPTION_OBJECT}, - {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, - {"force-share", no_argument, 0, 'U'}, - {0, 0, 0, 0} - }; - c =3D getopt_long(argc, argv, ":f:hU", - long_options, &option_index); - if (c =3D=3D -1) { - break; - } - switch (c) { - case ':': - missing_argument(argv[optind - 1]); - break; - case '?': - unrecognized_option(argv[optind - 1]); - break; - case 'h': - help(); - break; - case 'f': - fmt =3D optarg; - break; - case 'U': - force_share =3D true; - break; - case OPTION_OUTPUT: - output =3D optarg; - break; - case OPTION_OBJECT: { - QemuOpts *opts; - opts =3D qemu_opts_parse_noisily(&qemu_object_opts, - optarg, true); - if (!opts) { - return 1; - } - } break; - case OPTION_IMAGE_OPTS: - image_opts =3D true; - break; - } - } - if (optind !=3D argc - 1) { - error_exit("Expecting one image file name"); - } - filename =3D argv[optind]; - - if (output && !strcmp(output, "json")) { - output_format =3D OFORMAT_JSON; - } else if (output && !strcmp(output, "human")) { - output_format =3D OFORMAT_HUMAN; - } else if (output) { - error_report("--output must be used with human or json as argument= ."); - return 1; - } - - if (qemu_opts_foreach(&qemu_object_opts, - user_creatable_add_opts_foreach, - NULL, NULL)) { - return 1; + opts =3D g_new0(CommonOpts, 1); + if (parse_opts_common(opts, argc, argv)) { + return EXIT_FAILURE; } + parse_positional(argc, argv, 0, &opts->filename, "filename"); + parse_unexpected(argc, argv); =20 - blk =3D img_open(image_opts, filename, fmt, 0, false, false, force_sha= re); + blk =3D img_open(opts->image_opts, opts->filename, opts->fmt, 0, + false, false, opts->force_share); if (!blk) { - return 1; + ret =3D -1; + goto out1; } bs =3D blk_bs(blk); =20 - if (output_format =3D=3D OFORMAT_HUMAN) { + if (opts->output_format =3D=3D OFORMAT_HUMAN) { printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "Fi= le"); } =20 @@ -2832,7 +2858,7 @@ static int img_map(int argc, char **argv) =20 if (ret < 0) { error_report("Could not read file metadata: %s", strerror(-ret= )); - goto out; + goto out2; } =20 if (entry_mergeable(&curr, &next)) { @@ -2841,15 +2867,17 @@ static int img_map(int argc, char **argv) } =20 if (curr.length > 0) { - dump_map_entry(output_format, &curr, &next); + dump_map_entry(opts->output_format, &curr, &next); } curr =3D next; } =20 - dump_map_entry(output_format, &curr, NULL); + dump_map_entry(opts->output_format, &curr, NULL); =20 -out: +out2: blk_unref(blk); +out1: + g_free(opts); return ret < 0; } =20 --=20 2.14.3