static int convert_do_copy(ImgConvertState *s)

{

    uint8_t *buf = NULL;

    int64_t sector_num, allocated_done;

    int ret;

    int n;



    /* Check whether we have zero initialisation or can get it efficiently */

    s->has_zero_init = s->min_sparse && !s->target_has_backing

                     ? bdrv_has_zero_init(blk_bs(s->target))

                     : false;



    if (!s->has_zero_init && !s->target_has_backing &&

        bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))

    {

        ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);

        if (ret == 0) {

            s->has_zero_init = true;

        }

    }



    /* Allocate buffer for copied data. For compressed images, only one cluster

     * can be copied at a time. */

    if (s->compressed) {

        if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {

            error_report("invalid cluster size");

            ret = -EINVAL;

            goto fail;

        }

        s->buf_sectors = s->cluster_sectors;

    }

    buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);



    /* Calculate allocated sectors for progress */

    s->allocated_sectors = 0;

    sector_num = 0;

    while (sector_num < s->total_sectors) {

        n = convert_iteration_sectors(s, sector_num);

        if (n < 0) {

            ret = n;

            goto fail;

        }

        if (s->status == BLK_DATA) {

            s->allocated_sectors += n;

        }

        sector_num += n;

    }



    /* Do the copy */

    s->src_cur = 0;

    s->src_cur_offset = 0;

    s->sector_next_status = 0;



    sector_num = 0;

    allocated_done = 0;



    while (sector_num < s->total_sectors) {

        n = convert_iteration_sectors(s, sector_num);

        if (n < 0) {

            ret = n;

            goto fail;

        }

        if (s->status == BLK_DATA) {

            allocated_done += n;

            qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,

                                0);

        }



        ret = convert_read(s, sector_num, n, buf);

        if (ret < 0) {

            error_report("error while reading sector %" PRId64

                         ": %s", sector_num, strerror(-ret));

            goto fail;

        }



        ret = convert_write(s, sector_num, n, buf);

        if (ret < 0) {

            error_report("error while writing sector %" PRId64

                         ": %s", sector_num, strerror(-ret));

            goto fail;

        }



        sector_num += n;

    }



    if (s->compressed) {

        /* signal EOF to align */

        ret = blk_write_compressed(s->target, 0, NULL, 0);

        if (ret < 0) {

            goto fail;

        }

    }



    ret = 0;

fail:

    qemu_vfree(buf);

    return ret;

}

static int convert_do_copy(ImgConvertState *s)

{

    uint8_t *buf = NULL;

    int64_t sector_num, allocated_done;

    int ret;

    int n;



    

    s->has_zero_init = s->min_sparse && !s->target_has_backing

                     ? bdrv_has_zero_init(blk_bs(s->target))

                     : false;



    if (!s->has_zero_init && !s->target_has_backing &&

        bdrv_can_write_zeroes_with_unmap(blk_bs(s->target)))

    {

        ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP);

        if (ret == 0) {

            s->has_zero_init = true;

        }

    }



    



    if (s->compressed) {

        if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) {

            error_report("invalid cluster size");

            ret = -EINVAL;

            goto fail;

        }

        s->buf_sectors = s->cluster_sectors;

    }

    buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE);



    

    s->allocated_sectors = 0;

    sector_num = 0;

    while (sector_num < s->total_sectors) {

        n = convert_iteration_sectors(s, sector_num);

        if (n < 0) {

            ret = n;

            goto fail;

        }

        if (s->status == BLK_DATA) {

            s->allocated_sectors += n;

        }

        sector_num += n;

    }



    

    s->src_cur = 0;

    s->src_cur_offset = 0;

    s->sector_next_status = 0;



    sector_num = 0;

    allocated_done = 0;



    while (sector_num < s->total_sectors) {

        n = convert_iteration_sectors(s, sector_num);

        if (n < 0) {

            ret = n;

            goto fail;

        }

        if (s->status == BLK_DATA) {

            allocated_done += n;

            qemu_progress_print(100.0 * allocated_done / s->allocated_sectors,

                                0);

        }



        ret = convert_read(s, sector_num, n, buf);

        if (ret < 0) {

            error_report("error while reading sector %" PRId64

                         ": %s", sector_num, strerror(-ret));

            goto fail;

        }



        ret = convert_write(s, sector_num, n, buf);

        if (ret < 0) {

            error_report("error while writing sector %" PRId64

                         ": %s", sector_num, strerror(-ret));

            goto fail;

        }



        sector_num += n;

    }



    if (s->compressed) {

        

        ret = blk_write_compressed(s->target, 0, NULL, 0);

        if (ret < 0) {

            goto fail;

        }

    }



    ret = 0;

fail:

    qemu_vfree(buf);

    return ret;

}

static int FUNC_0(ImgConvertState *VAR_0)

{

    uint8_t *buf = NULL;

    int64_t sector_num, allocated_done;

    int VAR_1;

    int VAR_2;



    

    VAR_0->has_zero_init = VAR_0->min_sparse && !VAR_0->target_has_backing

                     ? bdrv_has_zero_init(blk_bs(VAR_0->target))

                     : false;



    if (!VAR_0->has_zero_init && !VAR_0->target_has_backing &&

        bdrv_can_write_zeroes_with_unmap(blk_bs(VAR_0->target)))

    {

        VAR_1 = bdrv_make_zero(blk_bs(VAR_0->target), BDRV_REQ_MAY_UNMAP);

        if (VAR_1 == 0) {

            VAR_0->has_zero_init = true;

        }

    }



    



    if (VAR_0->compressed) {

        if (VAR_0->cluster_sectors <= 0 || VAR_0->cluster_sectors > VAR_0->buf_sectors) {

            error_report("invalid cluster size");

            VAR_1 = -EINVAL;

            goto fail;

        }

        VAR_0->buf_sectors = VAR_0->cluster_sectors;

    }

    buf = blk_blockalign(VAR_0->target, VAR_0->buf_sectors * BDRV_SECTOR_SIZE);



    

    VAR_0->allocated_sectors = 0;

    sector_num = 0;

    while (sector_num < VAR_0->total_sectors) {

        VAR_2 = convert_iteration_sectors(VAR_0, sector_num);

        if (VAR_2 < 0) {

            VAR_1 = VAR_2;

            goto fail;

        }

        if (VAR_0->status == BLK_DATA) {

            VAR_0->allocated_sectors += VAR_2;

        }

        sector_num += VAR_2;

    }



    

    VAR_0->src_cur = 0;

    VAR_0->src_cur_offset = 0;

    VAR_0->sector_next_status = 0;



    sector_num = 0;

    allocated_done = 0;



    while (sector_num < VAR_0->total_sectors) {

        VAR_2 = convert_iteration_sectors(VAR_0, sector_num);

        if (VAR_2 < 0) {

            VAR_1 = VAR_2;

            goto fail;

        }

        if (VAR_0->status == BLK_DATA) {

            allocated_done += VAR_2;

            qemu_progress_print(100.0 * allocated_done / VAR_0->allocated_sectors,

                                0);

        }



        VAR_1 = convert_read(VAR_0, sector_num, VAR_2, buf);

        if (VAR_1 < 0) {

            error_report("error while reading sector %" PRId64

                         ": %VAR_0", sector_num, strerror(-VAR_1));

            goto fail;

        }



        VAR_1 = convert_write(VAR_0, sector_num, VAR_2, buf);

        if (VAR_1 < 0) {

            error_report("error while writing sector %" PRId64

                         ": %VAR_0", sector_num, strerror(-VAR_1));

            goto fail;

        }



        sector_num += VAR_2;

    }



    if (VAR_0->compressed) {

        

        VAR_1 = blk_write_compressed(VAR_0->target, 0, NULL, 0);

        if (VAR_1 < 0) {

            goto fail;

        }

    }



    VAR_1 = 0;

fail:

    qemu_vfree(buf);

    return VAR_1;

}

static int FUNC_0(ImgConvertState *VAR_0)
{ uint8_t *buf = NULL; int64_t sector_num, allocated_done; int VAR_1; int VAR_2; VAR_0->has_zero_init = VAR_0->min_sparse && !VAR_0->target_has_backing
? bdrv_has_zero_init(blk_bs(VAR_0->target))
: false; if (!VAR_0->has_zero_init && !VAR_0->target_has_backing &&
bdrv_can_write_zeroes_with_unmap(blk_bs(VAR_0->target)))
{ VAR_1 = bdrv_make_zero(blk_bs(VAR_0->target), BDRV_REQ_MAY_UNMAP); if (VAR_1 == 0) { VAR_0->has_zero_init = true; } } if (VAR_0->compressed) { if (VAR_0->cluster_sectors <= 0 || VAR_0->cluster_sectors > VAR_0->buf_sectors) { error_report("invalid cluster size"); VAR_1 = -EINVAL; goto fail; } VAR_0->buf_sectors = VAR_0->cluster_sectors; } buf = blk_blockalign(VAR_0->target, VAR_0->buf_sectors * BDRV_SECTOR_SIZE); VAR_0->allocated_sectors = 0; sector_num = 0; while (sector_num < VAR_0->total_sectors) { VAR_2 = convert_iteration_sectors(VAR_0, sector_num); if (VAR_2 < 0) { VAR_1 = VAR_2; goto fail; } if (VAR_0->status == BLK_DATA) { VAR_0->allocated_sectors += VAR_2; } sector_num += VAR_2; } VAR_0->src_cur = 0; VAR_0->src_cur_offset = 0; VAR_0->sector_next_status = 0; sector_num = 0; allocated_done = 0; while (sector_num < VAR_0->total_sectors) { VAR_2 = convert_iteration_sectors(VAR_0, sector_num); if (VAR_2 < 0) { VAR_1 = VAR_2; goto fail; } if (VAR_0->status == BLK_DATA) { allocated_done += VAR_2; qemu_progress_print(100.0 * allocated_done / VAR_0->allocated_sectors,
0); } VAR_1 = convert_read(VAR_0, sector_num, VAR_2, buf); if (VAR_1 < 0) { error_report("error while reading sector %" PRId64
": %VAR_0", sector_num, strerror(-VAR_1)); goto fail; } VAR_1 = convert_write(VAR_0, sector_num, VAR_2, buf); if (VAR_1 < 0) { error_report("error while writing sector %" PRId64
": %VAR_0", sector_num, strerror(-VAR_1)); goto fail; } sector_num += VAR_2; } if (VAR_0->compressed) { VAR_1 = blk_write_compressed(VAR_0->target, 0, NULL, 0); if (VAR_1 < 0) { goto fail; } } VAR_1 = 0; fail:
qemu_vfree(buf); return VAR_1; }