Fix Sparc/Linux host breakage by df70204db53e3611af986f434e74a882bce190ca
[qemu] / qemu-img.c
index 947e71b..070fe2e 100644 (file)
 #include <windows.h>
 #endif
 
+typedef struct img_cmd_t {
+    const char *name;
+    int (*handler)(int argc, char **argv);
+} img_cmd_t;
+
 /* Default to cache=writeback as data integrity is not important for qemu-tcg. */
 #define BRDV_O_FLAGS BDRV_O_CACHE_WB
 
@@ -58,12 +63,11 @@ static void help(void)
            "QEMU disk image utility\n"
            "\n"
            "Command syntax:\n"
-           "  check [-f fmt] filename\n"
-           "  create [-e] [-6] [-F fmt] [-b base_image] [-f fmt] filename [size]\n"
-           "  commit [-f fmt] filename\n"
-           "  convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
-           "  info [-f fmt] filename\n"
-           "  snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename\n"
+#define DEF(option, callback, arg_string)        \
+           "  " arg_string "\n"
+#include "qemu-img-cmds.h"
+#undef DEF
+#undef GEN_DOCS
            "\n"
            "Command parameters:\n"
            "  'filename' is a disk image filename\n"
@@ -79,9 +83,10 @@ static void help(void)
            "    supported any 'k' or 'K' is ignored\n"
            "  'output_filename' is the destination disk image filename\n"
            "  'output_fmt' is the destination format\n"
+           "  'options' is a comma separated list of format specific options in a\n"
+           "    name=value format. Use -o ? for an overview of the options supported by the\n"
+           "    used format\n"
            "  '-c' indicates that target image must be compressed (qcow format only)\n"
-           "  '-e' indicates that the target image must be encrypted (qcow format only)\n"
-           "  '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
            "  '-h' with or without a command shows this help and lists the supported formats\n"
            "\n"
            "Parameters to snapshot subcommand:\n"
@@ -742,14 +747,20 @@ static int img_convert(int argc, char **argv)
                 n = bs_offset + bs_sectors - sector_num;
 
             if (strcmp(drv->format_name, "host_device")) {
-                if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
-                                       n, &n1)) {
-                    sector_num += n1;
-                    continue;
+                /* If the output image is being created as a copy on write image,
+                   assume that sectors which are unallocated in the input image
+                   are present in both the output's and input's base images (no
+                   need to copy them). */
+                if (out_baseimg) {
+                    if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
+                                           n, &n1)) {
+                        sector_num += n1;
+                        continue;
+                    }
+                    /* The next 'n1' sectors are allocated in the input image. Copy
+                       only those as they may be followed by unallocated sectors. */
+                    n = n1;
                 }
-                /* The next 'n1' sectors are allocated in the input image. Copy
-                   only those as they may be followed by unallocated sectors. */
-                n = n1;
             } else {
                 n1 = n;
             }
@@ -918,7 +929,7 @@ static int img_info(int argc, char **argv)
 #define SNAPSHOT_APPLY  3
 #define SNAPSHOT_DELETE 4
 
-static void img_snapshot(int argc, char **argv)
+static int img_snapshot(int argc, char **argv)
 {
     BlockDriverState *bs;
     QEMUSnapshotInfo sn;
@@ -935,18 +946,18 @@ static void img_snapshot(int argc, char **argv)
         switch(c) {
         case 'h':
             help();
-            return;
+            return 0;
         case 'l':
             if (action) {
                 help();
-                return;
+                return 0;
             }
             action = SNAPSHOT_LIST;
             break;
         case 'a':
             if (action) {
                 help();
-                return;
+                return 0;
             }
             action = SNAPSHOT_APPLY;
             snapshot_name = optarg;
@@ -954,7 +965,7 @@ static void img_snapshot(int argc, char **argv)
         case 'c':
             if (action) {
                 help();
-                return;
+                return 0;
             }
             action = SNAPSHOT_CREATE;
             snapshot_name = optarg;
@@ -962,7 +973,7 @@ static void img_snapshot(int argc, char **argv)
         case 'd':
             if (action) {
                 help();
-                return;
+                return 0;
             }
             action = SNAPSHOT_DELETE;
             snapshot_name = optarg;
@@ -1020,31 +1031,38 @@ static void img_snapshot(int argc, char **argv)
 
     /* Cleanup */
     bdrv_delete(bs);
+
+    return 0;
 }
 
+static const img_cmd_t img_cmds[] = {
+#define DEF(option, callback, arg_string)        \
+    { option, callback },
+#include "qemu-img-cmds.h"
+#undef DEF
+#undef GEN_DOCS
+    { NULL, NULL, },
+};
+
 int main(int argc, char **argv)
 {
-    const char *cmd;
+    const img_cmd_t *cmd;
+    const char *cmdname;
 
     bdrv_init();
     if (argc < 2)
         help();
-    cmd = argv[1];
+    cmdname = argv[1];
     argc--; argv++;
-    if (!strcmp(cmd, "create")) {
-        img_create(argc, argv);
-    } else if (!strcmp(cmd, "check")) {
-        img_check(argc, argv);
-    } else if (!strcmp(cmd, "commit")) {
-        img_commit(argc, argv);
-    } else if (!strcmp(cmd, "convert")) {
-        img_convert(argc, argv);
-    } else if (!strcmp(cmd, "info")) {
-        img_info(argc, argv);
-    } else if (!strcmp(cmd, "snapshot")) {
-        img_snapshot(argc, argv);
-    } else {
-        help();
+
+    /* find the command */
+    for(cmd = img_cmds; cmd->name != NULL; cmd++) {
+        if (!strcmp(cmdname, cmd->name)) {
+            return cmd->handler(argc, argv);
+        }
     }
+
+    /* not found */
+    help();
     return 0;
 }