qemu.img.c: Use error_report() instead of own error() implementation

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Jes Sorensen 2010-12-16 14:31:53 +01:00 committed by Kevin Wolf
parent 4f70f249ca
commit 15654a6d7c

View file

@ -41,16 +41,6 @@ typedef struct img_cmd_t {
/* Default to cache=writeback as data integrity is not important for qemu-tcg. */ /* Default to cache=writeback as data integrity is not important for qemu-tcg. */
#define BDRV_O_FLAGS BDRV_O_CACHE_WB #define BDRV_O_FLAGS BDRV_O_CACHE_WB
static void GCC_FMT_ATTR(1, 2) error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "qemu-img: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
static void format_print(void *opaque, const char *name) static void format_print(void *opaque, const char *name)
{ {
printf(" %s", name); printf(" %s", name);
@ -197,13 +187,13 @@ static int print_block_option_help(const char *filename, const char *fmt)
/* Find driver and parse its options */ /* Find driver and parse its options */
drv = bdrv_find_format(fmt); drv = bdrv_find_format(fmt);
if (!drv) { if (!drv) {
error("Unknown file format '%s'", fmt); error_report("Unknown file format '%s'", fmt);
return 1; return 1;
} }
proto_drv = bdrv_find_protocol(filename); proto_drv = bdrv_find_protocol(filename);
if (!proto_drv) { if (!proto_drv) {
error("Unknown protocol '%s'", filename); error_report("Unknown protocol '%s'", filename);
return 1; return 1;
} }
@ -226,30 +216,30 @@ static BlockDriverState *bdrv_new_open(const char *filename,
bs = bdrv_new(""); bs = bdrv_new("");
if (!bs) { if (!bs) {
error("Not enough memory"); error_report("Not enough memory");
goto fail; goto fail;
} }
if (fmt) { if (fmt) {
drv = bdrv_find_format(fmt); drv = bdrv_find_format(fmt);
if (!drv) { if (!drv) {
error("Unknown file format '%s'", fmt); error_report("Unknown file format '%s'", fmt);
goto fail; goto fail;
} }
} else { } else {
drv = NULL; drv = NULL;
} }
if (bdrv_open(bs, filename, flags, drv) < 0) { if (bdrv_open(bs, filename, flags, drv) < 0) {
error("Could not open '%s'", filename); error_report("Could not open '%s'", filename);
goto fail; goto fail;
} }
if (bdrv_is_encrypted(bs)) { if (bdrv_is_encrypted(bs)) {
printf("Disk image '%s' is encrypted.\n", filename); printf("Disk image '%s' is encrypted.\n", filename);
if (read_password(password, sizeof(password)) < 0) { if (read_password(password, sizeof(password)) < 0) {
error("No password given"); error_report("No password given");
goto fail; goto fail;
} }
if (bdrv_set_key(bs, password) < 0) { if (bdrv_set_key(bs, password) < 0) {
error("invalid password"); error_report("invalid password");
goto fail; goto fail;
} }
} }
@ -267,13 +257,15 @@ static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
{ {
if (base_filename) { if (base_filename) {
if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) { if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
error("Backing file not supported for file format '%s'", fmt); error_report("Backing file not supported for file format '%s'",
fmt);
return -1; return -1;
} }
} }
if (base_fmt) { if (base_fmt) {
if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) { if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
error("Backing file format not supported for file format '%s'", fmt); error_report("Backing file format not supported for file "
"format '%s'", fmt);
return -1; return -1;
} }
} }
@ -310,11 +302,11 @@ static int img_create(int argc, char **argv)
fmt = optarg; fmt = optarg;
break; break;
case 'e': case 'e':
error("qemu-img: option -e is deprecated, please use \'-o " error_report("qemu-img: option -e is deprecated, please use \'-o "
"encryption\' instead!"); "encryption\' instead!");
return 1; return 1;
case '6': case '6':
error("qemu-img: option -6 is deprecated, please use \'-o " error_report("qemu-img: option -6 is deprecated, please use \'-o "
"compat6\' instead!"); "compat6\' instead!");
return 1; return 1;
case 'o': case 'o':
@ -334,9 +326,9 @@ static int img_create(int argc, char **argv)
ssize_t sval; ssize_t sval;
sval = strtosz_suffix(argv[optind++], NULL, STRTOSZ_DEFSUFFIX_B); sval = strtosz_suffix(argv[optind++], NULL, STRTOSZ_DEFSUFFIX_B);
if (sval < 0) { if (sval < 0) {
error("Invalid image size specified! You may use k, M, G or " error_report("Invalid image size specified! You may use k, M, G or "
"T suffixes for "); "T suffixes for ");
error("kilobytes, megabytes, gigabytes and terabytes."); error_report("kilobytes, megabytes, gigabytes and terabytes.");
ret = -1; ret = -1;
goto out; goto out;
} }
@ -400,7 +392,7 @@ static int img_check(int argc, char **argv)
ret = bdrv_check(bs, &result); ret = bdrv_check(bs, &result);
if (ret == -ENOTSUP) { if (ret == -ENOTSUP) {
error("This image format does not support checks"); error_report("This image format does not support checks");
bdrv_delete(bs); bdrv_delete(bs);
return 1; return 1;
} }
@ -482,16 +474,16 @@ static int img_commit(int argc, char **argv)
printf("Image committed.\n"); printf("Image committed.\n");
break; break;
case -ENOENT: case -ENOENT:
error("No disk inserted"); error_report("No disk inserted");
break; break;
case -EACCES: case -EACCES:
error("Image is read-only"); error_report("Image is read-only");
break; break;
case -ENOTSUP: case -ENOTSUP:
error("Image is already committed"); error_report("Image is already committed");
break; break;
default: default:
error("Error while committing image"); error_report("Error while committing image");
break; break;
} }
@ -614,11 +606,11 @@ static int img_convert(int argc, char **argv)
compress = 1; compress = 1;
break; break;
case 'e': case 'e':
error("qemu-img: option -e is deprecated, please use \'-o " error_report("qemu-img: option -e is deprecated, please use \'-o "
"encryption\' instead!"); "encryption\' instead!");
return 1; return 1;
case '6': case '6':
error("qemu-img: option -6 is deprecated, please use \'-o " error_report("qemu-img: option -6 is deprecated, please use \'-o "
"compat6\' instead!"); "compat6\' instead!");
return 1; return 1;
case 'o': case 'o':
@ -643,7 +635,8 @@ static int img_convert(int argc, char **argv)
} }
if (bs_n > 1 && out_baseimg) { if (bs_n > 1 && out_baseimg) {
error("-B makes no sense when concatenating multiple input images"); error_report("-B makes no sense when concatenating multiple input "
"images");
ret = -1; ret = -1;
goto out; goto out;
} }
@ -654,7 +647,7 @@ static int img_convert(int argc, char **argv)
for (bs_i = 0; bs_i < bs_n; bs_i++) { for (bs_i = 0; bs_i < bs_n; bs_i++) {
bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS); bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS);
if (!bs[bs_i]) { if (!bs[bs_i]) {
error("Could not open '%s'", argv[optind + bs_i]); error_report("Could not open '%s'", argv[optind + bs_i]);
ret = -1; ret = -1;
goto out; goto out;
} }
@ -664,12 +657,12 @@ static int img_convert(int argc, char **argv)
if (snapshot_name != NULL) { if (snapshot_name != NULL) {
if (bs_n > 1) { if (bs_n > 1) {
error("No support for concatenating multiple snapshot\n"); error_report("No support for concatenating multiple snapshot\n");
ret = -1; ret = -1;
goto out; goto out;
} }
if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) { if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) {
error("Failed to load snapshot\n"); error_report("Failed to load snapshot\n");
ret = -1; ret = -1;
goto out; goto out;
} }
@ -678,14 +671,14 @@ static int img_convert(int argc, char **argv)
/* Find driver and parse its options */ /* Find driver and parse its options */
drv = bdrv_find_format(out_fmt); drv = bdrv_find_format(out_fmt);
if (!drv) { if (!drv) {
error("Unknown file format '%s'", out_fmt); error_report("Unknown file format '%s'", out_fmt);
ret = -1; ret = -1;
goto out; goto out;
} }
proto_drv = bdrv_find_protocol(out_filename); proto_drv = bdrv_find_protocol(out_filename);
if (!proto_drv) { if (!proto_drv) {
error("Unknown protocol '%s'", out_filename); error_report("Unknown protocol '%s'", out_filename);
ret = -1; ret = -1;
goto out; goto out;
} }
@ -698,7 +691,7 @@ static int img_convert(int argc, char **argv)
if (options) { if (options) {
param = parse_option_parameters(options, create_options, param); param = parse_option_parameters(options, create_options, param);
if (param == NULL) { if (param == NULL) {
error("Invalid options for file format '%s'.", out_fmt); error_report("Invalid options for file format '%s'.", out_fmt);
ret = -1; ret = -1;
goto out; goto out;
} }
@ -724,13 +717,14 @@ static int img_convert(int argc, char **argv)
get_option_parameter(param, BLOCK_OPT_ENCRYPT); get_option_parameter(param, BLOCK_OPT_ENCRYPT);
if (!drv->bdrv_write_compressed) { if (!drv->bdrv_write_compressed) {
error("Compression not supported for this file format"); error_report("Compression not supported for this file format");
ret = -1; ret = -1;
goto out; goto out;
} }
if (encryption && encryption->value.n) { if (encryption && encryption->value.n) {
error("Compression and encryption not supported at the same time"); error_report("Compression and encryption not supported at "
"the same time");
ret = -1; ret = -1;
goto out; goto out;
} }
@ -740,11 +734,14 @@ static int img_convert(int argc, char **argv)
ret = bdrv_create(drv, out_filename, param); ret = bdrv_create(drv, out_filename, param);
if (ret < 0) { if (ret < 0) {
if (ret == -ENOTSUP) { if (ret == -ENOTSUP) {
error("Formatting not supported for file format '%s'", out_fmt); error_report("Formatting not supported for file format '%s'",
out_fmt);
} else if (ret == -EFBIG) { } else if (ret == -EFBIG) {
error("The image size is too large for file format '%s'", out_fmt); error_report("The image size is too large for file format '%s'",
out_fmt);
} else { } else {
error("%s: error while converting %s: %s", out_filename, out_fmt, strerror(-ret)); error_report("%s: error while converting %s: %s",
out_filename, out_fmt, strerror(-ret));
} }
goto out; goto out;
} }
@ -764,12 +761,12 @@ static int img_convert(int argc, char **argv)
if (compress) { if (compress) {
ret = bdrv_get_info(out_bs, &bdi); ret = bdrv_get_info(out_bs, &bdi);
if (ret < 0) { if (ret < 0) {
error("could not get block driver info"); error_report("could not get block driver info");
goto out; goto out;
} }
cluster_size = bdi.cluster_size; cluster_size = bdi.cluster_size;
if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) { if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
error("invalid cluster size"); error_report("invalid cluster size");
ret = -1; ret = -1;
goto out; goto out;
} }
@ -810,7 +807,7 @@ static int img_convert(int argc, char **argv)
ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow); ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
if (ret < 0) { if (ret < 0) {
error("error while reading"); error_report("error while reading");
goto out; goto out;
} }
@ -828,7 +825,7 @@ static int img_convert(int argc, char **argv)
ret = bdrv_write_compressed(out_bs, sector_num, buf, ret = bdrv_write_compressed(out_bs, sector_num, buf,
cluster_sectors); cluster_sectors);
if (ret != 0) { if (ret != 0) {
error("error while compressing sector %" PRId64, error_report("error while compressing sector %" PRId64,
sector_num); sector_num);
goto out; goto out;
} }
@ -887,7 +884,7 @@ static int img_convert(int argc, char **argv)
ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n); ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
if (ret < 0) { if (ret < 0) {
error("error while reading"); error_report("error while reading");
goto out; goto out;
} }
/* NOTE: at the same time we convert, we do not write zero /* NOTE: at the same time we convert, we do not write zero
@ -906,7 +903,7 @@ static int img_convert(int argc, char **argv)
is_allocated_sectors(buf1, n, &n1)) { is_allocated_sectors(buf1, n, &n1)) {
ret = bdrv_write(out_bs, sector_num, buf1, n1); ret = bdrv_write(out_bs, sector_num, buf1, n1);
if (ret < 0) { if (ret < 0) {
error("error while writing"); error_report("error while writing");
goto out; goto out;
} }
} }
@ -1148,7 +1145,7 @@ static int img_snapshot(int argc, char **argv)
ret = bdrv_snapshot_create(bs, &sn); ret = bdrv_snapshot_create(bs, &sn);
if (ret) { if (ret) {
error("Could not create snapshot '%s': %d (%s)", error_report("Could not create snapshot '%s': %d (%s)",
snapshot_name, ret, strerror(-ret)); snapshot_name, ret, strerror(-ret));
} }
break; break;
@ -1156,7 +1153,7 @@ static int img_snapshot(int argc, char **argv)
case SNAPSHOT_APPLY: case SNAPSHOT_APPLY:
ret = bdrv_snapshot_goto(bs, snapshot_name); ret = bdrv_snapshot_goto(bs, snapshot_name);
if (ret) { if (ret) {
error("Could not apply snapshot '%s': %d (%s)", error_report("Could not apply snapshot '%s': %d (%s)",
snapshot_name, ret, strerror(-ret)); snapshot_name, ret, strerror(-ret));
} }
break; break;
@ -1164,7 +1161,7 @@ static int img_snapshot(int argc, char **argv)
case SNAPSHOT_DELETE: case SNAPSHOT_DELETE:
ret = bdrv_snapshot_delete(bs, snapshot_name); ret = bdrv_snapshot_delete(bs, snapshot_name);
if (ret) { if (ret) {
error("Could not delete snapshot '%s': %d (%s)", error_report("Could not delete snapshot '%s': %d (%s)",
snapshot_name, ret, strerror(-ret)); snapshot_name, ret, strerror(-ret));
} }
break; break;
@ -1241,7 +1238,7 @@ static int img_rebase(int argc, char **argv)
if (!unsafe && bs->backing_format[0] != '\0') { if (!unsafe && bs->backing_format[0] != '\0') {
old_backing_drv = bdrv_find_format(bs->backing_format); old_backing_drv = bdrv_find_format(bs->backing_format);
if (old_backing_drv == NULL) { if (old_backing_drv == NULL) {
error("Invalid format name: '%s'", bs->backing_format); error_report("Invalid format name: '%s'", bs->backing_format);
ret = -1; ret = -1;
goto out; goto out;
} }
@ -1250,7 +1247,7 @@ static int img_rebase(int argc, char **argv)
if (out_basefmt != NULL) { if (out_basefmt != NULL) {
new_backing_drv = bdrv_find_format(out_basefmt); new_backing_drv = bdrv_find_format(out_basefmt);
if (new_backing_drv == NULL) { if (new_backing_drv == NULL) {
error("Invalid format name: '%s'", out_basefmt); error_report("Invalid format name: '%s'", out_basefmt);
ret = -1; ret = -1;
goto out; goto out;
} }
@ -1269,7 +1266,7 @@ static int img_rebase(int argc, char **argv)
ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS, ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS,
old_backing_drv); old_backing_drv);
if (ret) { if (ret) {
error("Could not open old backing file '%s'", backing_name); error_report("Could not open old backing file '%s'", backing_name);
goto out; goto out;
} }
@ -1277,7 +1274,7 @@ static int img_rebase(int argc, char **argv)
ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS, ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS,
new_backing_drv); new_backing_drv);
if (ret) { if (ret) {
error("Could not open new backing file '%s'", out_baseimg); error_report("Could not open new backing file '%s'", out_baseimg);
goto out; goto out;
} }
} }
@ -1321,12 +1318,12 @@ static int img_rebase(int argc, char **argv)
/* Read old and new backing file */ /* Read old and new backing file */
ret = bdrv_read(bs_old_backing, sector, buf_old, n); ret = bdrv_read(bs_old_backing, sector, buf_old, n);
if (ret < 0) { if (ret < 0) {
error("error while reading from old backing file"); error_report("error while reading from old backing file");
goto out; goto out;
} }
ret = bdrv_read(bs_new_backing, sector, buf_new, n); ret = bdrv_read(bs_new_backing, sector, buf_new, n);
if (ret < 0) { if (ret < 0) {
error("error while reading from new backing file"); error_report("error while reading from new backing file");
goto out; goto out;
} }
@ -1342,7 +1339,7 @@ static int img_rebase(int argc, char **argv)
ret = bdrv_write(bs, sector + written, ret = bdrv_write(bs, sector + written,
buf_old + written * 512, pnum); buf_old + written * 512, pnum);
if (ret < 0) { if (ret < 0) {
error("Error while writing to COW image: %s", error_report("Error while writing to COW image: %s",
strerror(-ret)); strerror(-ret));
goto out; goto out;
} }
@ -1363,10 +1360,10 @@ static int img_rebase(int argc, char **argv)
*/ */
ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
if (ret == -ENOSPC) { if (ret == -ENOSPC) {
error("Could not change the backing file to '%s': No space left in " error_report("Could not change the backing file to '%s': No "
"the file header", out_baseimg); "space left in the file header", out_baseimg);
} else if (ret < 0) { } else if (ret < 0) {
error("Could not change the backing file to '%s': %s", error_report("Could not change the backing file to '%s': %s",
out_baseimg, strerror(-ret)); out_baseimg, strerror(-ret));
} }
@ -1465,7 +1462,7 @@ static int img_resize(int argc, char **argv)
total_size = n; total_size = n;
} }
if (total_size <= 0) { if (total_size <= 0) {
error("New image size must be positive"); error_report("New image size must be positive");
ret = -1; ret = -1;
goto out; goto out;
} }
@ -1476,13 +1473,13 @@ static int img_resize(int argc, char **argv)
printf("Image resized.\n"); printf("Image resized.\n");
break; break;
case -ENOTSUP: case -ENOTSUP:
error("This image format does not support resize"); error_report("This image format does not support resize");
break; break;
case -EACCES: case -EACCES:
error("Image is read-only"); error_report("Image is read-only");
break; break;
default: default:
error("Error resizing image (%d)", -ret); error_report("Error resizing image (%d)", -ret);
break; break;
} }
out: out: