int bdrv_file_open(BlockDriverState **pbs, const char *filename,

                   const char *reference, QDict *options, int flags,

                   Error **errp)

{

    BlockDriverState *bs = NULL;

    BlockDriver *drv;

    const char *drvname;

    bool allow_protocol_prefix = false;

    Error *local_err = NULL;

    int ret;



    /* NULL means an empty set of options */

    if (options == NULL) {

        options = qdict_new();

    }



    if (reference) {

        if (filename || qdict_size(options)) {

            error_setg(errp, "Cannot reference an existing block device with "

                       "additional options or a new filename");

            return -EINVAL;

        }

        QDECREF(options);



        bs = bdrv_find(reference);

        if (!bs) {

            error_setg(errp, "Cannot find block device '%s'", reference);

            return -ENODEV;

        }

        bdrv_ref(bs);

        *pbs = bs;

        return 0;

    }



    bs = bdrv_new("");

    bs->options = options;

    options = qdict_clone_shallow(options);



    /* Fetch the file name from the options QDict if necessary */

    if (!filename) {

        filename = qdict_get_try_str(options, "filename");

    } else if (filename && !qdict_haskey(options, "filename")) {

        qdict_put(options, "filename", qstring_from_str(filename));

        allow_protocol_prefix = true;

    } else {

        error_setg(errp, "Can't specify 'file' and 'filename' options at the "

                   "same time");

        ret = -EINVAL;

        goto fail;

    }



    /* Find the right block driver */

    drvname = qdict_get_try_str(options, "driver");

    if (drvname) {

        drv = bdrv_find_format(drvname);

        if (!drv) {

            error_setg(errp, "Unknown driver '%s'", drvname);

        }

        qdict_del(options, "driver");

    } else if (filename) {

        drv = bdrv_find_protocol(filename, allow_protocol_prefix);

        if (!drv) {

            error_setg(errp, "Unknown protocol");

        }

    } else {

        error_setg(errp, "Must specify either driver or file");

        drv = NULL;

    }



    if (!drv) {

        /* errp has been set already */

        ret = -ENOENT;

        goto fail;

    }



    /* Parse the filename and open it */

    if (drv->bdrv_parse_filename && filename) {

        drv->bdrv_parse_filename(filename, options, &local_err);

        if (error_is_set(&local_err)) {

            error_propagate(errp, local_err);

            ret = -EINVAL;

            goto fail;

        }

        qdict_del(options, "filename");

    } else if (drv->bdrv_needs_filename && !filename) {

        error_setg(errp, "The '%s' block driver requires a file name",

                   drv->format_name);

        ret = -EINVAL;

        goto fail;

    }



    if (!drv->bdrv_file_open) {

        ret = bdrv_open(bs, filename, options, flags, drv, &local_err);

        options = NULL;

    } else {

        ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err);

    }

    if (ret < 0) {

        error_propagate(errp, local_err);

        goto fail;

    }



    /* Check if any unknown options were used */

    if (options && (qdict_size(options) != 0)) {

        const QDictEntry *entry = qdict_first(options);

        error_setg(errp, "Block protocol '%s' doesn't support the option '%s'",

                   drv->format_name, entry->key);

        ret = -EINVAL;

        goto fail;

    }

    QDECREF(options);



    bs->growable = 1;

    *pbs = bs;

    return 0;



fail:

    QDECREF(options);

    if (!bs->drv) {

        QDECREF(bs->options);

    }

    bdrv_unref(bs);

    return ret;

}

int bdrv_file_open(BlockDriverState **pbs, const char *filename,

                   const char *reference, QDict *options, int flags,

                   Error **errp)

{

    BlockDriverState *bs = NULL;

    BlockDriver *drv;

    const char *drvname;

    bool allow_protocol_prefix = false;

    Error *local_err = NULL;

    int ret;



    

    if (options == NULL) {

        options = qdict_new();

    }



    if (reference) {

        if (filename || qdict_size(options)) {

            error_setg(errp, "Cannot reference an existing block device with "

                       "additional options or a new filename");

            return -EINVAL;

        }

        QDECREF(options);



        bs = bdrv_find(reference);

        if (!bs) {

            error_setg(errp, "Cannot find block device '%s'", reference);

            return -ENODEV;

        }

        bdrv_ref(bs);

        *pbs = bs;

        return 0;

    }



    bs = bdrv_new("");

    bs->options = options;

    options = qdict_clone_shallow(options);



    

    if (!filename) {

        filename = qdict_get_try_str(options, "filename");

    } else if (filename && !qdict_haskey(options, "filename")) {

        qdict_put(options, "filename", qstring_from_str(filename));

        allow_protocol_prefix = true;

    } else {

        error_setg(errp, "Can't specify 'file' and 'filename' options at the "

                   "same time");

        ret = -EINVAL;

        goto fail;

    }



    

    drvname = qdict_get_try_str(options, "driver");

    if (drvname) {

        drv = bdrv_find_format(drvname);

        if (!drv) {

            error_setg(errp, "Unknown driver '%s'", drvname);

        }

        qdict_del(options, "driver");

    } else if (filename) {

        drv = bdrv_find_protocol(filename, allow_protocol_prefix);

        if (!drv) {

            error_setg(errp, "Unknown protocol");

        }

    } else {

        error_setg(errp, "Must specify either driver or file");

        drv = NULL;

    }



    if (!drv) {

        

        ret = -ENOENT;

        goto fail;

    }



    

    if (drv->bdrv_parse_filename && filename) {

        drv->bdrv_parse_filename(filename, options, &local_err);

        if (error_is_set(&local_err)) {

            error_propagate(errp, local_err);

            ret = -EINVAL;

            goto fail;

        }

        qdict_del(options, "filename");

    } else if (drv->bdrv_needs_filename && !filename) {

        error_setg(errp, "The '%s' block driver requires a file name",

                   drv->format_name);

        ret = -EINVAL;

        goto fail;

    }



    if (!drv->bdrv_file_open) {

        ret = bdrv_open(bs, filename, options, flags, drv, &local_err);

        options = NULL;

    } else {

        ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err);

    }

    if (ret < 0) {

        error_propagate(errp, local_err);

        goto fail;

    }



    

    if (options && (qdict_size(options) != 0)) {

        const QDictEntry *entry = qdict_first(options);

        error_setg(errp, "Block protocol '%s' doesn't support the option '%s'",

                   drv->format_name, entry->key);

        ret = -EINVAL;

        goto fail;

    }

    QDECREF(options);



    bs->growable = 1;

    *pbs = bs;

    return 0;



fail:

    QDECREF(options);

    if (!bs->drv) {

        QDECREF(bs->options);

    }

    bdrv_unref(bs);

    return ret;

}

    } else if (drv->bdrv_needs_filename && !filename) {         error_setg(errp, "The '%s' block driver requires a file name",                    drv->format_name);         ret = -EINVAL;         goto fail;
int FUNC_0(BlockDriverState **VAR_0, const char *VAR_1,

                   const char *VAR_2, QDict *VAR_3, int VAR_4,

                   Error **VAR_5)

{

    BlockDriverState *bs = NULL;

    BlockDriver *drv;

    const char *VAR_6;

    bool allow_protocol_prefix = false;

    Error *local_err = NULL;

    int VAR_7;



    

    if (VAR_3 == NULL) {

        VAR_3 = qdict_new();

    }



    if (VAR_2) {

        if (VAR_1 || qdict_size(VAR_3)) {

            error_setg(VAR_5, "Cannot VAR_2 an existing block device with "

                       "additional VAR_3 or a new VAR_1");

            return -EINVAL;

        }

        QDECREF(VAR_3);



        bs = bdrv_find(VAR_2);

        if (!bs) {

            error_setg(VAR_5, "Cannot find block device '%s'", VAR_2);

            return -ENODEV;

        }

        bdrv_ref(bs);

        *VAR_0 = bs;

        return 0;

    }



    bs = bdrv_new("");

    bs->VAR_3 = VAR_3;

    VAR_3 = qdict_clone_shallow(VAR_3);



    

    if (!VAR_1) {

        VAR_1 = qdict_get_try_str(VAR_3, "VAR_1");

    } else if (VAR_1 && !qdict_haskey(VAR_3, "VAR_1")) {

        qdict_put(VAR_3, "VAR_1", qstring_from_str(VAR_1));

        allow_protocol_prefix = true;

    } else {

        error_setg(VAR_5, "Can't specify 'file' and 'VAR_1' VAR_3 at the "

                   "same time");

        VAR_7 = -EINVAL;

        goto fail;

    }



    

    VAR_6 = qdict_get_try_str(VAR_3, "driver");

    if (VAR_6) {

        drv = bdrv_find_format(VAR_6);

        if (!drv) {

            error_setg(VAR_5, "Unknown driver '%s'", VAR_6);

        }

        qdict_del(VAR_3, "driver");

    } else if (VAR_1) {

        drv = bdrv_find_protocol(VAR_1, allow_protocol_prefix);

        if (!drv) {

            error_setg(VAR_5, "Unknown protocol");

        }

    } else {

        error_setg(VAR_5, "Must specify either driver or file");

        drv = NULL;

    }



    if (!drv) {

        

        VAR_7 = -ENOENT;

        goto fail;

    }



    

    if (drv->bdrv_parse_filename && VAR_1) {

        drv->bdrv_parse_filename(VAR_1, VAR_3, &local_err);

        if (error_is_set(&local_err)) {

            error_propagate(VAR_5, local_err);

            VAR_7 = -EINVAL;

            goto fail;

        }

        qdict_del(VAR_3, "VAR_1");

    } else if (drv->bdrv_needs_filename && !VAR_1) {

        error_setg(VAR_5, "The '%s' block driver requires a file name",

                   drv->format_name);

        VAR_7 = -EINVAL;

        goto fail;

    }



    if (!drv->FUNC_0) {

        VAR_7 = bdrv_open(bs, VAR_1, VAR_3, VAR_4, drv, &local_err);

        VAR_3 = NULL;

    } else {

        VAR_7 = bdrv_open_common(bs, NULL, VAR_3, VAR_4, drv, &local_err);

    }

    if (VAR_7 < 0) {

        error_propagate(VAR_5, local_err);

        goto fail;

    }



    

    if (VAR_3 && (qdict_size(VAR_3) != 0)) {

        const QDictEntry *VAR_8 = qdict_first(VAR_3);

        error_setg(VAR_5, "Block protocol '%s' doesn't support the option '%s'",

                   drv->format_name, VAR_8->key);

        VAR_7 = -EINVAL;

        goto fail;

    }

    QDECREF(VAR_3);



    bs->growable = 1;

    *VAR_0 = bs;

    return 0;



fail:

    QDECREF(VAR_3);

    if (!bs->drv) {

        QDECREF(bs->VAR_3);

    }

    bdrv_unref(bs);

    return VAR_7;

}

int FUNC_0(BlockDriverState **VAR_0, const char *VAR_1,
const char *VAR_2, QDict *VAR_3, int VAR_4,
Error **VAR_5)
{ BlockDriverState *bs = NULL; BlockDriver *drv; const char *VAR_6; bool allow_protocol_prefix = false; Error *local_err = NULL; int VAR_7; if (VAR_3 == NULL) { VAR_3 = qdict_new(); } if (VAR_2) { if (VAR_1 || qdict_size(VAR_3)) { error_setg(VAR_5, "Cannot VAR_2 an existing block device with "
"additional VAR_3 or a new VAR_1"); return -EINVAL; } QDECREF(VAR_3); bs = bdrv_find(VAR_2); if (!bs) { error_setg(VAR_5, "Cannot find block device '%s'", VAR_2); return -ENODEV; } bdrv_ref(bs); *VAR_0 = bs; return 0; } bs = bdrv_new(""); bs->VAR_3 = VAR_3; VAR_3 = qdict_clone_shallow(VAR_3); if (!VAR_1) { VAR_1 = qdict_get_try_str(VAR_3, "VAR_1"); } else if (VAR_1 && !qdict_haskey(VAR_3, "VAR_1")) { qdict_put(VAR_3, "VAR_1", qstring_from_str(VAR_1)); allow_protocol_prefix = true; } else { error_setg(VAR_5, "Can't specify 'file' and 'VAR_1' VAR_3 at the "
"same time"); VAR_7 = -EINVAL; goto fail; } VAR_6 = qdict_get_try_str(VAR_3, "driver"); if (VAR_6) { drv = bdrv_find_format(VAR_6); if (!drv) { error_setg(VAR_5, "Unknown driver '%s'", VAR_6); } qdict_del(VAR_3, "driver"); } else if (VAR_1) { drv = bdrv_find_protocol(VAR_1, allow_protocol_prefix); if (!drv) { error_setg(VAR_5, "Unknown protocol"); } } else { error_setg(VAR_5, "Must specify either driver or file"); drv = NULL; } if (!drv) { VAR_7 = -ENOENT; goto fail; } if (drv->bdrv_parse_filename && VAR_1) { drv->bdrv_parse_filename(VAR_1, VAR_3, &local_err); if (error_is_set(&local_err)) { error_propagate(VAR_5, local_err); VAR_7 = -EINVAL; goto fail; } qdict_del(VAR_3, "VAR_1"); } else if (drv->bdrv_needs_filename && !VAR_1) { error_setg(VAR_5, "The '%s' block driver requires a file name",
drv->format_name); VAR_7 = -EINVAL; goto fail; } if (!drv->FUNC_0) { VAR_7 = bdrv_open(bs, VAR_1, VAR_3, VAR_4, drv, &local_err); VAR_3 = NULL; } else { VAR_7 = bdrv_open_common(bs, NULL, VAR_3, VAR_4, drv, &local_err); } if (VAR_7 < 0) { error_propagate(VAR_5, local_err); goto fail; } if (VAR_3 && (qdict_size(VAR_3) != 0)) { const QDictEntry *VAR_8 = qdict_first(VAR_3); error_setg(VAR_5, "Block protocol '%s' doesn't support the option '%s'",
drv->format_name, VAR_8->key); VAR_7 = -EINVAL; goto fail; } QDECREF(VAR_3); bs->growable = 1; *VAR_0 = bs; return 0; fail:
QDECREF(VAR_3); if (!bs->drv) { QDECREF(bs->VAR_3); } bdrv_unref(bs); return VAR_7; }