static int vmdk_create(const char *filename, QEMUOptionParameter *options,

                       Error **errp)

{

    int fd, idx = 0;

    char desc[BUF_SIZE];

    int64_t total_size = 0, filesize;

    const char *adapter_type = NULL;

    const char *backing_file = NULL;

    const char *fmt = NULL;

    int flags = 0;

    int ret = 0;

    bool flat, split, compress;

    char ext_desc_lines[BUF_SIZE] = "";

    char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];

    const int64_t split_size = 0x80000000;  /* VMDK has constant split size */

    const char *desc_extent_line;

    char parent_desc_line[BUF_SIZE] = "";

    uint32_t parent_cid = 0xffffffff;

    uint32_t number_heads = 16;

    bool zeroed_grain = false;

    const char desc_template[] =

        "# Disk DescriptorFile\n"

        "version=1\n"

        "CID=%x\n"

        "parentCID=%x\n"

        "createType=\"%s\"\n"

        "%s"

        "\n"

        "# Extent description\n"

        "%s"

        "\n"

        "# The Disk Data Base\n"

        "#DDB\n"

        "\n"

        "ddb.virtualHWVersion = \"%d\"\n"

        "ddb.geometry.cylinders = \"%" PRId64 "\"\n"

        "ddb.geometry.heads = \"%d\"\n"

        "ddb.geometry.sectors = \"63\"\n"

        "ddb.adapterType = \"%s\"\n";



    if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {

        return -EINVAL;

    }

    /* Read out options */

    while (options && options->name) {

        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {

            total_size = options->value.n;

        } else if (!strcmp(options->name, BLOCK_OPT_ADAPTER_TYPE)) {

            adapter_type = options->value.s;

        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {

            backing_file = options->value.s;

        } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {

            flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;

        } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {

            fmt = options->value.s;

        } else if (!strcmp(options->name, BLOCK_OPT_ZEROED_GRAIN)) {

            zeroed_grain |= options->value.n;

        }

        options++;

    }

    if (!adapter_type) {

        adapter_type = "ide";

    } else if (strcmp(adapter_type, "ide") &&

               strcmp(adapter_type, "buslogic") &&

               strcmp(adapter_type, "lsilogic") &&

               strcmp(adapter_type, "legacyESX")) {

        error_setg(errp, "Unknown adapter type: '%s'", adapter_type);

        return -EINVAL;

    }

    if (strcmp(adapter_type, "ide") != 0) {

        /* that's the number of heads with which vmware operates when

           creating, exporting, etc. vmdk files with a non-ide adapter type */

        number_heads = 255;

    }

    if (!fmt) {

        /* Default format to monolithicSparse */

        fmt = "monolithicSparse";

    } else if (strcmp(fmt, "monolithicFlat") &&

               strcmp(fmt, "monolithicSparse") &&

               strcmp(fmt, "twoGbMaxExtentSparse") &&

               strcmp(fmt, "twoGbMaxExtentFlat") &&

               strcmp(fmt, "streamOptimized")) {

        error_setg(errp, "Unknown subformat: '%s'", fmt);

        return -EINVAL;

    }

    split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&

              strcmp(fmt, "twoGbMaxExtentSparse"));

    flat = !(strcmp(fmt, "monolithicFlat") &&

             strcmp(fmt, "twoGbMaxExtentFlat"));

    compress = !strcmp(fmt, "streamOptimized");

    if (flat) {

        desc_extent_line = "RW %lld FLAT \"%s\" 0\n";

    } else {

        desc_extent_line = "RW %lld SPARSE \"%s\"\n";

    }

    if (flat && backing_file) {

        error_setg(errp, "Flat image can't have backing file");

        return -ENOTSUP;

    }

    if (flat && zeroed_grain) {

        error_setg(errp, "Flat image can't enable zeroed grain");

        return -ENOTSUP;

    }

    if (backing_file) {

        BlockDriverState *bs = bdrv_new("");

        ret = bdrv_open(bs, backing_file, NULL, 0, NULL, errp);

        if (ret != 0) {

            bdrv_unref(bs);

            return ret;

        }

        if (strcmp(bs->drv->format_name, "vmdk")) {

            bdrv_unref(bs);

            return -EINVAL;

        }

        parent_cid = vmdk_read_cid(bs, 0);

        bdrv_unref(bs);

        snprintf(parent_desc_line, sizeof(parent_desc_line),

                "parentFileNameHint=\"%s\"", backing_file);

    }



    /* Create extents */

    filesize = total_size;

    while (filesize > 0) {

        char desc_line[BUF_SIZE];

        char ext_filename[PATH_MAX];

        char desc_filename[PATH_MAX];

        int64_t size = filesize;



        if (split && size > split_size) {

            size = split_size;

        }

        if (split) {

            snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",

                    prefix, flat ? 'f' : 's', ++idx, postfix);

        } else if (flat) {

            snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",

                    prefix, postfix);

        } else {

            snprintf(desc_filename, sizeof(desc_filename), "%s%s",

                    prefix, postfix);

        }

        snprintf(ext_filename, sizeof(ext_filename), "%s%s",

                path, desc_filename);



        if (vmdk_create_extent(ext_filename, size,

                               flat, compress, zeroed_grain)) {

            return -EINVAL;

        }

        filesize -= size;



        /* Format description line */

        snprintf(desc_line, sizeof(desc_line),

                    desc_extent_line, size / 512, desc_filename);

        pstrcat(ext_desc_lines, sizeof(ext_desc_lines), desc_line);

    }

    /* generate descriptor file */

    snprintf(desc, sizeof(desc), desc_template,

            (unsigned int)time(NULL),

            parent_cid,

            fmt,

            parent_desc_line,

            ext_desc_lines,

            (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),

            total_size / (int64_t)(63 * number_heads * 512), number_heads,

                adapter_type);

    if (split || flat) {

        fd = qemu_open(filename,

                       O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,

                       0644);

    } else {

        fd = qemu_open(filename,

                       O_WRONLY | O_BINARY | O_LARGEFILE,

                       0644);

    }

    if (fd < 0) {

        return -errno;

    }

    /* the descriptor offset = 0x200 */

    if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {

        ret = -errno;

        goto exit;

    }

    ret = qemu_write_full(fd, desc, strlen(desc));

    if (ret != strlen(desc)) {

        ret = -errno;

        goto exit;

    }

    ret = 0;

exit:

    qemu_close(fd);

    return ret;

}

static int vmdk_create(const char *filename, QEMUOptionParameter *options,

                       Error **errp)

{

    int fd, idx = 0;

    char desc[BUF_SIZE];

    int64_t total_size = 0, filesize;

    const char *adapter_type = NULL;

    const char *backing_file = NULL;

    const char *fmt = NULL;

    int flags = 0;

    int ret = 0;

    bool flat, split, compress;

    char ext_desc_lines[BUF_SIZE] = "";

    char path[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];

    const int64_t split_size = 0x80000000;  

    const char *desc_extent_line;

    char parent_desc_line[BUF_SIZE] = "";

    uint32_t parent_cid = 0xffffffff;

    uint32_t number_heads = 16;

    bool zeroed_grain = false;

    const char desc_template[] =

        "# Disk DescriptorFile\n"

        "version=1\n"

        "CID=%x\n"

        "parentCID=%x\n"

        "createType=\"%s\"\n"

        "%s"

        "\n"

        "# Extent description\n"

        "%s"

        "\n"

        "# The Disk Data Base\n"

        "#DDB\n"

        "\n"

        "ddb.virtualHWVersion = \"%d\"\n"

        "ddb.geometry.cylinders = \"%" PRId64 "\"\n"

        "ddb.geometry.heads = \"%d\"\n"

        "ddb.geometry.sectors = \"63\"\n"

        "ddb.adapterType = \"%s\"\n";



    if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {

        return -EINVAL;

    }

    

    while (options && options->name) {

        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {

            total_size = options->value.n;

        } else if (!strcmp(options->name, BLOCK_OPT_ADAPTER_TYPE)) {

            adapter_type = options->value.s;

        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {

            backing_file = options->value.s;

        } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {

            flags |= options->value.n ? BLOCK_FLAG_COMPAT6 : 0;

        } else if (!strcmp(options->name, BLOCK_OPT_SUBFMT)) {

            fmt = options->value.s;

        } else if (!strcmp(options->name, BLOCK_OPT_ZEROED_GRAIN)) {

            zeroed_grain |= options->value.n;

        }

        options++;

    }

    if (!adapter_type) {

        adapter_type = "ide";

    } else if (strcmp(adapter_type, "ide") &&

               strcmp(adapter_type, "buslogic") &&

               strcmp(adapter_type, "lsilogic") &&

               strcmp(adapter_type, "legacyESX")) {

        error_setg(errp, "Unknown adapter type: '%s'", adapter_type);

        return -EINVAL;

    }

    if (strcmp(adapter_type, "ide") != 0) {

        



        number_heads = 255;

    }

    if (!fmt) {

        

        fmt = "monolithicSparse";

    } else if (strcmp(fmt, "monolithicFlat") &&

               strcmp(fmt, "monolithicSparse") &&

               strcmp(fmt, "twoGbMaxExtentSparse") &&

               strcmp(fmt, "twoGbMaxExtentFlat") &&

               strcmp(fmt, "streamOptimized")) {

        error_setg(errp, "Unknown subformat: '%s'", fmt);

        return -EINVAL;

    }

    split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&

              strcmp(fmt, "twoGbMaxExtentSparse"));

    flat = !(strcmp(fmt, "monolithicFlat") &&

             strcmp(fmt, "twoGbMaxExtentFlat"));

    compress = !strcmp(fmt, "streamOptimized");

    if (flat) {

        desc_extent_line = "RW %lld FLAT \"%s\" 0\n";

    } else {

        desc_extent_line = "RW %lld SPARSE \"%s\"\n";

    }

    if (flat && backing_file) {

        error_setg(errp, "Flat image can't have backing file");

        return -ENOTSUP;

    }

    if (flat && zeroed_grain) {

        error_setg(errp, "Flat image can't enable zeroed grain");

        return -ENOTSUP;

    }

    if (backing_file) {

        BlockDriverState *bs = bdrv_new("");

        ret = bdrv_open(bs, backing_file, NULL, 0, NULL, errp);

        if (ret != 0) {

            bdrv_unref(bs);

            return ret;

        }

        if (strcmp(bs->drv->format_name, "vmdk")) {

            bdrv_unref(bs);

            return -EINVAL;

        }

        parent_cid = vmdk_read_cid(bs, 0);

        bdrv_unref(bs);

        snprintf(parent_desc_line, sizeof(parent_desc_line),

                "parentFileNameHint=\"%s\"", backing_file);

    }



    

    filesize = total_size;

    while (filesize > 0) {

        char desc_line[BUF_SIZE];

        char ext_filename[PATH_MAX];

        char desc_filename[PATH_MAX];

        int64_t size = filesize;



        if (split && size > split_size) {

            size = split_size;

        }

        if (split) {

            snprintf(desc_filename, sizeof(desc_filename), "%s-%c%03d%s",

                    prefix, flat ? 'f' : 's', ++idx, postfix);

        } else if (flat) {

            snprintf(desc_filename, sizeof(desc_filename), "%s-flat%s",

                    prefix, postfix);

        } else {

            snprintf(desc_filename, sizeof(desc_filename), "%s%s",

                    prefix, postfix);

        }

        snprintf(ext_filename, sizeof(ext_filename), "%s%s",

                path, desc_filename);



        if (vmdk_create_extent(ext_filename, size,

                               flat, compress, zeroed_grain)) {

            return -EINVAL;

        }

        filesize -= size;



        

        snprintf(desc_line, sizeof(desc_line),

                    desc_extent_line, size / 512, desc_filename);

        pstrcat(ext_desc_lines, sizeof(ext_desc_lines), desc_line);

    }

    

    snprintf(desc, sizeof(desc), desc_template,

            (unsigned int)time(NULL),

            parent_cid,

            fmt,

            parent_desc_line,

            ext_desc_lines,

            (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),

            total_size / (int64_t)(63 * number_heads * 512), number_heads,

                adapter_type);

    if (split || flat) {

        fd = qemu_open(filename,

                       O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,

                       0644);

    } else {

        fd = qemu_open(filename,

                       O_WRONLY | O_BINARY | O_LARGEFILE,

                       0644);

    }

    if (fd < 0) {

        return -errno;

    }

    

    if (!split && !flat && 0x200 != lseek(fd, 0x200, SEEK_SET)) {

        ret = -errno;

        goto exit;

    }

    ret = qemu_write_full(fd, desc, strlen(desc));

    if (ret != strlen(desc)) {

        ret = -errno;

        goto exit;

    }

    ret = 0;

exit:

    qemu_close(fd);

    return ret;

}

static int FUNC_0(const char *VAR_0, QEMUOptionParameter *VAR_1,

                       Error **VAR_2)

{

    int VAR_3, VAR_4 = 0;

    char VAR_5[BUF_SIZE];

    int64_t total_size = 0, filesize;

    const char *VAR_6 = NULL;

    const char *VAR_7 = NULL;

    const char *VAR_8 = NULL;

    int VAR_9 = 0;

    int VAR_10 = 0;

    bool flat, split, compress;

    char VAR_11[BUF_SIZE] = "";

    char VAR_12[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX];

    const int64_t VAR_13 = 0x80000000;  

    const char *VAR_14;

    char VAR_15[BUF_SIZE] = "";

    uint32_t parent_cid = 0xffffffff;

    uint32_t number_heads = 16;

    bool zeroed_grain = false;

    const char VAR_16[] =

        "# Disk DescriptorFile\n"

        "version=1\n"

        "CID=%x\n"

        "parentCID=%x\n"

        "createType=\"%s\"\n"

        "%s"

        "\n"

        "# Extent description\n"

        "%s"

        "\n"

        "# The Disk Data Base\n"

        "#DDB\n"

        "\n"

        "ddb.virtualHWVersion = \"%d\"\n"

        "ddb.geometry.cylinders = \"%" PRId64 "\"\n"

        "ddb.geometry.heads = \"%d\"\n"

        "ddb.geometry.sectors = \"63\"\n"

        "ddb.adapterType = \"%s\"\n";



    if (filename_decompose(VAR_0, VAR_12, prefix, postfix, PATH_MAX, VAR_2)) {

        return -EINVAL;

    }

    

    while (VAR_1 && VAR_1->name) {

        if (!strcmp(VAR_1->name, BLOCK_OPT_SIZE)) {

            total_size = VAR_1->value.n;

        } else if (!strcmp(VAR_1->name, BLOCK_OPT_ADAPTER_TYPE)) {

            VAR_6 = VAR_1->value.s;

        } else if (!strcmp(VAR_1->name, BLOCK_OPT_BACKING_FILE)) {

            VAR_7 = VAR_1->value.s;

        } else if (!strcmp(VAR_1->name, BLOCK_OPT_COMPAT6)) {

            VAR_9 |= VAR_1->value.n ? BLOCK_FLAG_COMPAT6 : 0;

        } else if (!strcmp(VAR_1->name, BLOCK_OPT_SUBFMT)) {

            VAR_8 = VAR_1->value.s;

        } else if (!strcmp(VAR_1->name, BLOCK_OPT_ZEROED_GRAIN)) {

            zeroed_grain |= VAR_1->value.n;

        }

        VAR_1++;

    }

    if (!VAR_6) {

        VAR_6 = "ide";

    } else if (strcmp(VAR_6, "ide") &&

               strcmp(VAR_6, "buslogic") &&

               strcmp(VAR_6, "lsilogic") &&

               strcmp(VAR_6, "legacyESX")) {

        error_setg(VAR_2, "Unknown adapter type: '%s'", VAR_6);

        return -EINVAL;

    }

    if (strcmp(VAR_6, "ide") != 0) {

        



        number_heads = 255;

    }

    if (!VAR_8) {

        

        VAR_8 = "monolithicSparse";

    } else if (strcmp(VAR_8, "monolithicFlat") &&

               strcmp(VAR_8, "monolithicSparse") &&

               strcmp(VAR_8, "twoGbMaxExtentSparse") &&

               strcmp(VAR_8, "twoGbMaxExtentFlat") &&

               strcmp(VAR_8, "streamOptimized")) {

        error_setg(VAR_2, "Unknown subformat: '%s'", VAR_8);

        return -EINVAL;

    }

    split = !(strcmp(VAR_8, "twoGbMaxExtentFlat") &&

              strcmp(VAR_8, "twoGbMaxExtentSparse"));

    flat = !(strcmp(VAR_8, "monolithicFlat") &&

             strcmp(VAR_8, "twoGbMaxExtentFlat"));

    compress = !strcmp(VAR_8, "streamOptimized");

    if (flat) {

        VAR_14 = "RW %lld FLAT \"%s\" 0\n";

    } else {

        VAR_14 = "RW %lld SPARSE \"%s\"\n";

    }

    if (flat && VAR_7) {

        error_setg(VAR_2, "Flat image can't have backing file");

        return -ENOTSUP;

    }

    if (flat && zeroed_grain) {

        error_setg(VAR_2, "Flat image can't enable zeroed grain");

        return -ENOTSUP;

    }

    if (VAR_7) {

        BlockDriverState *bs = bdrv_new("");

        VAR_10 = bdrv_open(bs, VAR_7, NULL, 0, NULL, VAR_2);

        if (VAR_10 != 0) {

            bdrv_unref(bs);

            return VAR_10;

        }

        if (strcmp(bs->drv->format_name, "vmdk")) {

            bdrv_unref(bs);

            return -EINVAL;

        }

        parent_cid = vmdk_read_cid(bs, 0);

        bdrv_unref(bs);

        snprintf(VAR_15, sizeof(VAR_15),

                "parentFileNameHint=\"%s\"", VAR_7);

    }



    

    filesize = total_size;

    while (filesize > 0) {

        char VAR_17[BUF_SIZE];

        char VAR_18[PATH_MAX];

        char VAR_19[PATH_MAX];

        int64_t size = filesize;



        if (split && size > VAR_13) {

            size = VAR_13;

        }

        if (split) {

            snprintf(VAR_19, sizeof(VAR_19), "%s-%c%03d%s",

                    prefix, flat ? 'f' : 's', ++VAR_4, postfix);

        } else if (flat) {

            snprintf(VAR_19, sizeof(VAR_19), "%s-flat%s",

                    prefix, postfix);

        } else {

            snprintf(VAR_19, sizeof(VAR_19), "%s%s",

                    prefix, postfix);

        }

        snprintf(VAR_18, sizeof(VAR_18), "%s%s",

                VAR_12, VAR_19);



        if (vmdk_create_extent(VAR_18, size,

                               flat, compress, zeroed_grain)) {

            return -EINVAL;

        }

        filesize -= size;



        

        snprintf(VAR_17, sizeof(VAR_17),

                    VAR_14, size / 512, VAR_19);

        pstrcat(VAR_11, sizeof(VAR_11), VAR_17);

    }

    

    snprintf(VAR_5, sizeof(VAR_5), VAR_16,

            (unsigned int)time(NULL),

            parent_cid,

            VAR_8,

            VAR_15,

            VAR_11,

            (VAR_9 & BLOCK_FLAG_COMPAT6 ? 6 : 4),

            total_size / (int64_t)(63 * number_heads * 512), number_heads,

                VAR_6);

    if (split || flat) {

        VAR_3 = qemu_open(VAR_0,

                       O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,

                       0644);

    } else {

        VAR_3 = qemu_open(VAR_0,

                       O_WRONLY | O_BINARY | O_LARGEFILE,

                       0644);

    }

    if (VAR_3 < 0) {

        return -errno;

    }

    

    if (!split && !flat && 0x200 != lseek(VAR_3, 0x200, SEEK_SET)) {

        VAR_10 = -errno;

        goto exit;

    }

    VAR_10 = qemu_write_full(VAR_3, VAR_5, strlen(VAR_5));

    if (VAR_10 != strlen(VAR_5)) {

        VAR_10 = -errno;

        goto exit;

    }

    VAR_10 = 0;

exit:

    qemu_close(VAR_3);

    return VAR_10;

}

static int FUNC_0(const char *VAR_0, QEMUOptionParameter *VAR_1,
Error **VAR_2)
{ int VAR_3, VAR_4 = 0; char VAR_5[BUF_SIZE]; int64_t total_size = 0, filesize; const char *VAR_6 = NULL; const char *VAR_7 = NULL; const char *VAR_8 = NULL; int VAR_9 = 0; int VAR_10 = 0; bool flat, split, compress; char VAR_11[BUF_SIZE] = ""; char VAR_12[PATH_MAX], prefix[PATH_MAX], postfix[PATH_MAX]; const int64_t VAR_13 = 0x80000000; const char *VAR_14; char VAR_15[BUF_SIZE] = ""; uint32_t parent_cid = 0xffffffff; uint32_t number_heads = 16; bool zeroed_grain = false; const char VAR_16[] =
"# Disk DescriptorFile\n"
"version=1\n"
"CID=%x\n"
"parentCID=%x\n"
"createType=\"%s\"\n"
"%s"
"\n"
"# Extent description\n"
"%s"
"\n"
"# The Disk Data Base\n"
"#DDB\n"
"\n"
"ddb.virtualHWVersion = \"%d\"\n"
"ddb.geometry.cylinders = \"%" PRId64 "\"\n"
"ddb.geometry.heads = \"%d\"\n"
"ddb.geometry.sectors = \"63\"\n"
"ddb.adapterType = \"%s\"\n"; if (filename_decompose(VAR_0, VAR_12, prefix, postfix, PATH_MAX, VAR_2)) { return -EINVAL; } while (VAR_1 && VAR_1->name) { if (!strcmp(VAR_1->name, BLOCK_OPT_SIZE)) { total_size = VAR_1->value.n; } else if (!strcmp(VAR_1->name, BLOCK_OPT_ADAPTER_TYPE)) { VAR_6 = VAR_1->value.s; } else if (!strcmp(VAR_1->name, BLOCK_OPT_BACKING_FILE)) { VAR_7 = VAR_1->value.s; } else if (!strcmp(VAR_1->name, BLOCK_OPT_COMPAT6)) { VAR_9 |= VAR_1->value.n ? BLOCK_FLAG_COMPAT6 : 0; } else if (!strcmp(VAR_1->name, BLOCK_OPT_SUBFMT)) { VAR_8 = VAR_1->value.s; } else if (!strcmp(VAR_1->name, BLOCK_OPT_ZEROED_GRAIN)) { zeroed_grain |= VAR_1->value.n; } VAR_1++; } if (!VAR_6) { VAR_6 = "ide"; } else if (strcmp(VAR_6, "ide") && strcmp(VAR_6, "buslogic") &&
strcmp(VAR_6, "lsilogic") &&
strcmp(VAR_6, "legacyESX")) { error_setg(VAR_2, "Unknown adapter type: '%s'", VAR_6); return -EINVAL; } if (strcmp(VAR_6, "ide") != 0) { number_heads = 255; } if (!VAR_8) { VAR_8 = "monolithicSparse"; } else if (strcmp(VAR_8, "monolithicFlat") && strcmp(VAR_8, "monolithicSparse") &&
strcmp(VAR_8, "twoGbMaxExtentSparse") &&
strcmp(VAR_8, "twoGbMaxExtentFlat") &&
strcmp(VAR_8, "streamOptimized")) { error_setg(VAR_2, "Unknown subformat: '%s'", VAR_8); return -EINVAL; } split = !(strcmp(VAR_8, "twoGbMaxExtentFlat") &&
strcmp(VAR_8, "twoGbMaxExtentSparse")); flat = !(strcmp(VAR_8, "monolithicFlat") &&
strcmp(VAR_8, "twoGbMaxExtentFlat")); compress = !strcmp(VAR_8, "streamOptimized"); if (flat) { VAR_14 = "RW %lld FLAT \"%s\" 0\n"; } else { VAR_14 = "RW %lld SPARSE \"%s\"\n"; } if (flat && VAR_7) { error_setg(VAR_2, "Flat image can't have backing file"); return -ENOTSUP; } if (flat && zeroed_grain) { error_setg(VAR_2, "Flat image can't enable zeroed grain"); return -ENOTSUP; } if (VAR_7) { BlockDriverState *bs = bdrv_new(""); VAR_10 = bdrv_open(bs, VAR_7, NULL, 0, NULL, VAR_2); if (VAR_10 != 0) { bdrv_unref(bs); return VAR_10; } if (strcmp(bs->drv->format_name, "vmdk")) { bdrv_unref(bs); return -EINVAL; } parent_cid = vmdk_read_cid(bs, 0); bdrv_unref(bs); snprintf(VAR_15, sizeof(VAR_15),
"parentFileNameHint=\"%s\"", VAR_7); } filesize = total_size; while (filesize > 0) { char VAR_17[BUF_SIZE]; char VAR_18[PATH_MAX]; char VAR_19[PATH_MAX]; int64_t size = filesize; if (split && size > VAR_13) { size = VAR_13; } if (split) { snprintf(VAR_19, sizeof(VAR_19), "%s-%c%03d%s",
prefix, flat ? 'f' : 's', ++VAR_4, postfix); } else if (flat) { snprintf(VAR_19, sizeof(VAR_19), "%s-flat%s",
prefix, postfix); } else { snprintf(VAR_19, sizeof(VAR_19), "%s%s",
prefix, postfix); } snprintf(VAR_18, sizeof(VAR_18), "%s%s",
VAR_12, VAR_19); if (vmdk_create_extent(VAR_18, size,
flat, compress, zeroed_grain)) { return -EINVAL; } filesize -= size; snprintf(VAR_17, sizeof(VAR_17),
VAR_14, size / 512, VAR_19); pstrcat(VAR_11, sizeof(VAR_11), VAR_17); } snprintf(VAR_5, sizeof(VAR_5), VAR_16,
(unsigned int)time(NULL),
parent_cid,
VAR_8,
VAR_15,
VAR_11,
(VAR_9 & BLOCK_FLAG_COMPAT6 ? 6 : 4),
total_size / (int64_t)(63 * number_heads * 512), number_heads,
VAR_6); if (split || flat) { VAR_3 = qemu_open(VAR_0,
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
0644); } else { VAR_3 = qemu_open(VAR_0,
O_WRONLY | O_BINARY | O_LARGEFILE,
0644); } if (VAR_3 < 0) { return -errno; } if (!split && !flat && 0x200 != lseek(VAR_3, 0x200, SEEK_SET)) { VAR_10 = -errno; goto exit; } VAR_10 = qemu_write_full(VAR_3, VAR_5, strlen(VAR_5)); if (VAR_10 != strlen(VAR_5)) { VAR_10 = -errno; goto exit; } VAR_10 = 0; exit:
qemu_close(VAR_3); return VAR_10; }