#include "qapi/qobject-output-visitor.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qdict.h"
-#include "qapi/qmp/qstring.h"
#include "qemu/cutils.h"
#include "qemu/config-file.h"
#include "qemu/option.h"
exit(EXIT_SUCCESS);
}
-static QemuOptsList qemu_object_opts = {
- .name = "object",
- .implied_opt_name = "qom-type",
- .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
- .desc = {
- { }
- },
-};
-
-static bool qemu_img_object_print_help(const char *type, QemuOpts *opts)
-{
- if (user_creatable_print_help(type, opts)) {
- exit(0);
- }
- return true;
-}
-
/*
* Is @optarg safe for accumulate_options()?
* It is when multiple of them can be joined together separated by ','.
case 'u':
flags |= BDRV_O_NO_BACKING;
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- goto fail;
- }
- } break;
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
+ break;
}
}
}
optind++;
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- goto fail;
- }
-
/* Get image size, if specified */
if (optind < argc) {
int64_t sval;
static void dump_json_image_check(ImageCheck *check, bool quiet)
{
- QString *str;
+ GString *str;
QObject *obj;
Visitor *v = qobject_output_visitor_new(&obj);
visit_complete(v, &obj);
str = qobject_to_json_pretty(obj, true);
assert(str != NULL);
- qprintf(quiet, "%s\n", qstring_get_str(str));
+ qprintf(quiet, "%s\n", str->str);
qobject_unref(obj);
visit_free(v);
- qobject_unref(str);
+ g_string_free(str, true);
}
static void dump_human_image_check(ImageCheck *check, bool quiet)
case 'U':
force_share = true;
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- return 1;
- }
- } break;
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
+ break;
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
return 1;
}
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- return 1;
- }
-
ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
if (ret < 0) {
error_report("Invalid source cache option: %s", cache);
static void run_block_job(BlockJob *job, Error **errp)
{
+ uint64_t progress_current, progress_total;
AioContext *aio_context = blk_get_aio_context(job->blk);
int ret = 0;
do {
float progress = 0.0f;
aio_poll(aio_context, true);
- if (job->job.progress.total) {
- progress = (float)job->job.progress.current /
- job->job.progress.total * 100.f;
+
+ progress_get_snapshot(&job->job.progress, &progress_current,
+ &progress_total);
+ if (progress_total) {
+ progress = (float)progress_current / progress_total * 100.f;
}
qemu_progress_print(progress, 0);
} while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
return 1;
}
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- return 1;
- }
- } break;
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
+ break;
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
}
filename = argv[optind++];
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- return 1;
- }
-
flags = BDRV_O_RDWR | BDRV_O_UNMAP;
ret = bdrv_parse_cache_mode(cache, &flags, &writethrough);
if (ret < 0) {
/*
* Compares two images. Exit codes:
*
- * 0 - Images are identical
+ * 0 - Images are identical or the requested help was printed
* 1 - Images differ
* >1 - Error occurred
*/
case 'U':
force_share = true;
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- ret = 2;
- goto out4;
+ case OPTION_OBJECT:
+ {
+ Error *local_err = NULL;
+
+ if (!user_creatable_add_from_str(optarg, &local_err)) {
+ if (local_err) {
+ error_report_err(local_err);
+ exit(2);
+ } else {
+ /* Help was printed */
+ exit(EXIT_SUCCESS);
+ }
+ }
+ break;
}
- } break;
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
filename1 = argv[optind++];
filename2 = argv[optind++];
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- ret = 2;
- goto out4;
- }
-
/* Initialize before goto out */
qemu_progress_init(progress, 2.0);
blk_unref(blk1);
out3:
qemu_progress_end();
-out4:
return ret;
}
static int img_convert(int argc, char **argv)
{
- int c, bs_i, flags, src_flags = 0;
+ int c, bs_i, flags, src_flags = BDRV_O_NO_SHARE;
const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe",
*src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL,
*out_filename, *out_baseimg_param, *snapshot_name = NULL;
goto fail_getopt;
}
break;
- case OPTION_OBJECT: {
- QemuOpts *object_opts;
- object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!object_opts) {
- goto fail_getopt;
- }
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
break;
- }
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
out_fmt = "raw";
}
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- goto fail_getopt;
- }
-
if (s.compressed && s.copy_range) {
error_report("Cannot enable copy offloading when -c is used");
goto fail_getopt;
if (out_baseimg_param) {
if (!qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT)) {
- warn_report("Deprecated use of backing file without explicit "
- "backing format");
+ error_report("Use of backing file requires explicit "
+ "backing format");
+ ret = -1;
+ goto out;
}
}
static void dump_json_image_info_list(ImageInfoList *list)
{
- QString *str;
+ GString *str;
QObject *obj;
Visitor *v = qobject_output_visitor_new(&obj);
visit_complete(v, &obj);
str = qobject_to_json_pretty(obj, true);
assert(str != NULL);
- printf("%s\n", qstring_get_str(str));
+ printf("%s\n", str->str);
qobject_unref(obj);
visit_free(v);
- qobject_unref(str);
+ g_string_free(str, true);
}
static void dump_json_image_info(ImageInfo *info)
{
- QString *str;
+ GString *str;
QObject *obj;
Visitor *v = qobject_output_visitor_new(&obj);
visit_complete(v, &obj);
str = qobject_to_json_pretty(obj, true);
assert(str != NULL);
- printf("%s\n", qstring_get_str(str));
+ printf("%s\n", str->str);
qobject_unref(obj);
visit_free(v);
- qobject_unref(str);
+ g_string_free(str, true);
}
static void dump_human_image_info_list(ImageInfoList *list)
bool chain, bool force_share)
{
ImageInfoList *head = NULL;
- ImageInfoList **last = &head;
+ ImageInfoList **tail = &head;
GHashTable *filenames;
Error *err = NULL;
BlockBackend *blk;
BlockDriverState *bs;
ImageInfo *info;
- ImageInfoList *elem;
if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
error_report("Backing file '%s' creates an infinite loop.",
goto err;
}
- elem = g_new0(ImageInfoList, 1);
- elem->value = info;
- *last = elem;
- last = &elem->next;
+ QAPI_LIST_APPEND(tail, info);
blk_unref(blk);
case OPTION_BACKING_CHAIN:
chain = true;
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- return 1;
- }
- } break;
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
+ break;
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
return 1;
}
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- return 1;
- }
-
list = collect_image_info_list(image_opts, filename, fmt, chain,
force_share);
if (!list) {
break;
case OFORMAT_JSON:
printf("{ \"start\": %"PRId64", \"length\": %"PRId64","
- " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s",
- e->start, e->length, e->depth,
+ " \"depth\": %"PRId64", \"present\": %s, \"zero\": %s,"
+ " \"data\": %s", e->start, e->length, e->depth,
+ e->present ? "true" : "false",
e->zero ? "true" : "false",
e->data ? "true" : "false");
if (e->has_offset) {
.offset = map,
.has_offset = has_offset,
.depth = depth,
+ .present = !!(ret & BDRV_BLOCK_ALLOCATED),
.has_filename = filename,
.filename = filename,
};
if (curr->zero != next->zero ||
curr->data != next->data ||
curr->depth != next->depth ||
+ curr->present != next->present ||
curr->has_filename != next->has_filename ||
curr->has_offset != next->has_offset) {
return false;
return 1;
}
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- return 1;
- }
- } break;
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
+ break;
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
return 1;
}
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- return 1;
- }
-
blk = img_open(image_opts, filename, fmt, 0, false, false, force_share);
if (!blk) {
return 1;
case 'U':
force_share = true;
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- return 1;
- }
- } break;
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
+ break;
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
}
filename = argv[optind++];
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- return 1;
- }
-
/* Open the image */
blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet,
force_share);
case 'q':
quiet = true;
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- return 1;
- }
- } break;
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
+ break;
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
}
filename = argv[optind++];
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- return 1;
- }
-
qemu_progress_init(progress, 2.0);
qemu_progress_print(0, 100);
if (ret == -ENOSPC) {
error_report("Could not change the backing file to '%s': No "
"space left in the file header", out_baseimg);
+ } else if (ret == -EINVAL && out_baseimg && !out_basefmt) {
+ error_report("Could not change the backing file to '%s': backing "
+ "format must be specified", out_baseimg);
} else if (ret < 0) {
error_report("Could not change the backing file to '%s': %s",
out_baseimg, strerror(-ret));
case 'q':
quiet = true;
break;
- case OPTION_OBJECT: {
- QemuOpts *opts;
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- return 1;
- }
- } break;
+ case OPTION_OBJECT:
+ user_creatable_process_cmdline(optarg);
+ break;
case OPTION_IMAGE_OPTS:
image_opts = true;
break;
}
filename = argv[optind++];
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- return 1;
- }
-
/* Choose grow, shrink, or absolute resize mode */
switch (size[0]) {
case '+':
quiet = true;
break;
case OPTION_OBJECT:
- opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!opts) {
- ret = -1;
- goto out_no_progress;
- }
+ user_creatable_process_cmdline(optarg);
break;
case OPTION_IMAGE_OPTS:
image_opts = true;
error_exit("Must specify options (-o)");
}
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- ret = -1;
- goto out_no_progress;
- }
-
if (quiet) {
progress = false;
}
merge = true;
break;
case OPTION_OBJECT:
- opts = qemu_opts_parse_noisily(&qemu_object_opts, optarg, true);
- if (!opts) {
- goto out;
- }
+ user_creatable_process_cmdline(optarg);
break;
case OPTION_IMAGE_OPTS:
image_opts = true;
}
}
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- goto out;
- }
-
if (QSIMPLEQ_EMPTY(&actions)) {
error_report("Need at least one of --add, --remove, --clear, "
"--enable, --disable, or --merge");
force_share = true;
break;
case OPTION_OBJECT:
- if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) {
- ret = -1;
- goto out;
- }
+ user_creatable_process_cmdline(optarg);
break;
case OPTION_IMAGE_OPTS:
image_opts = true;
goto out;
}
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- ret = -1;
- goto out;
- }
-
blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
force_share);
static void dump_json_block_measure_info(BlockMeasureInfo *info)
{
- QString *str;
+ GString *str;
QObject *obj;
Visitor *v = qobject_output_visitor_new(&obj);
visit_complete(v, &obj);
str = qobject_to_json_pretty(obj, true);
assert(str != NULL);
- printf("%s\n", qstring_get_str(str));
+ printf("%s\n", str->str);
qobject_unref(obj);
visit_free(v);
- qobject_unref(str);
+ g_string_free(str, true);
}
static int img_measure(int argc, char **argv)
force_share = true;
break;
case OPTION_OBJECT:
- object_opts = qemu_opts_parse_noisily(&qemu_object_opts,
- optarg, true);
- if (!object_opts) {
- goto out;
- }
+ user_creatable_process_cmdline(optarg);
break;
case OPTION_IMAGE_OPTS:
image_opts = true;
}
}
- if (qemu_opts_foreach(&qemu_object_opts,
- user_creatable_add_opts_foreach,
- qemu_img_object_print_help, &error_fatal)) {
- goto out;
- }
-
if (argc - optind > 1) {
error_report("At most one filename argument is allowed.");
goto out;
error_exit("Not enough arguments");
}
- qemu_add_opts(&qemu_object_opts);
qemu_add_opts(&qemu_source_opts);
qemu_add_opts(&qemu_trace_opts);