+static void dump_json_image_check(ImageCheck *check)
+{
+ Error *errp = NULL;
+ QString *str;
+ QmpOutputVisitor *ov = qmp_output_visitor_new();
+ QObject *obj;
+ visit_type_ImageCheck(qmp_output_get_visitor(ov),
+ &check, NULL, &errp);
+ obj = qmp_output_get_qobject(ov);
+ str = qobject_to_json_pretty(obj);
+ assert(str != NULL);
+ printf("%s\n", qstring_get_str(str));
+ qobject_decref(obj);
+ qmp_output_visitor_cleanup(ov);
+ QDECREF(str);
+}
+
+static void dump_human_image_check(ImageCheck *check)
+{
+ if (!(check->corruptions || check->leaks || check->check_errors)) {
+ printf("No errors were found on the image.\n");
+ } else {
+ if (check->corruptions) {
+ printf("\n%" PRId64 " errors were found on the image.\n"
+ "Data may be corrupted, or further writes to the image "
+ "may corrupt it.\n",
+ check->corruptions);
+ }
+
+ if (check->leaks) {
+ printf("\n%" PRId64 " leaked clusters were found on the image.\n"
+ "This means waste of disk space, but no harm to data.\n",
+ check->leaks);
+ }
+
+ if (check->check_errors) {
+ printf("\n%" PRId64 " internal errors have occurred during the check.\n",
+ check->check_errors);
+ }
+ }
+
+ if (check->total_clusters != 0 && check->allocated_clusters != 0) {
+ printf("%" PRId64 "/%" PRId64 "= %0.2f%% allocated, %0.2f%% fragmented\n",
+ check->allocated_clusters, check->total_clusters,
+ check->allocated_clusters * 100.0 / check->total_clusters,
+ check->fragmented_clusters * 100.0 / check->allocated_clusters);
+ }
+
+ if (check->image_end_offset) {
+ printf("Image end offset: %" PRId64 "\n", check->image_end_offset);
+ }
+}
+
+static int collect_image_check(BlockDriverState *bs,
+ ImageCheck *check,
+ const char *filename,
+ const char *fmt,
+ int fix)
+{
+ int ret;
+ BdrvCheckResult result;
+
+ ret = bdrv_check(bs, &result, fix);
+ if (ret < 0) {
+ return ret;
+ }
+
+ check->filename = g_strdup(filename);
+ check->format = g_strdup(bdrv_get_format_name(bs));
+ check->check_errors = result.check_errors;
+ check->corruptions = result.corruptions;
+ check->has_corruptions = result.corruptions != 0;
+ check->leaks = result.leaks;
+ check->has_leaks = result.leaks != 0;
+ check->corruptions_fixed = result.corruptions_fixed;
+ check->has_corruptions_fixed = result.corruptions != 0;
+ check->leaks_fixed = result.leaks_fixed;
+ check->has_leaks_fixed = result.leaks != 0;
+ check->image_end_offset = result.image_end_offset;
+ check->has_image_end_offset = result.image_end_offset != 0;
+ check->total_clusters = result.bfi.total_clusters;
+ check->has_total_clusters = result.bfi.total_clusters != 0;
+ check->allocated_clusters = result.bfi.allocated_clusters;
+ check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
+ check->fragmented_clusters = result.bfi.fragmented_clusters;
+ check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
+
+ return 0;
+}
+