This function may be implemented by block drivers to derive a directory name from a BDS. Concatenating this g_free()-able string with a relative filename must result in a valid (not necessarily existing) filename, so this is a function that should generally be not implemented by format drivers, because this is protocol-specific.
If a BDS's driver does not implement this function, bdrv_dirname() will fall through to the BDS's file if it exists. If it does not, the exact_filename field will be used to generate a directory name. Signed-off-by: Max Reitz <mre...@redhat.com> --- block.c | 26 ++++++++++++++++++++++++++ include/block/block.h | 1 + include/block/block_int.h | 1 + 3 files changed, 28 insertions(+) diff --git a/block.c b/block.c index 6e89abd..23962ce 100644 --- a/block.c +++ b/block.c @@ -4028,3 +4028,29 @@ void bdrv_refresh_filename(BlockDriverState *bs) QDECREF(json); } } + +char *bdrv_dirname(BlockDriverState *bs, Error **errp) +{ + BlockDriver *drv = bs->drv; + + if (!drv) { + error_setg(errp, "Node '%s' is ejected", bs->node_name); + return NULL; + } + + if (drv->bdrv_dirname) { + return drv->bdrv_dirname(bs, errp); + } + + if (bs->file) { + return bdrv_dirname(bs->file->bs, errp); + } + + if (bs->exact_filename[0] != '\0') { + return path_combine(bs->exact_filename, ""); + } + + error_setg(errp, "Cannot generate a base directory for %s nodes", + drv->format_name); + return NULL; +} diff --git a/include/block/block.h b/include/block/block.h index 83f9f31..b0fe5dc 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -437,6 +437,7 @@ char *bdrv_get_full_backing_filename(BlockDriverState *bs, Error **errp); char *bdrv_get_full_backing_filename_from_filename(const char *backed, const char *backing, Error **errp); +char *bdrv_dirname(BlockDriverState *bs, Error **errp); int bdrv_is_snapshot(BlockDriverState *bs); int path_has_protocol(const char *path); diff --git a/include/block/block_int.h b/include/block/block_int.h index eb3665a..087d9ac 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -137,6 +137,7 @@ struct BlockDriver { int (*bdrv_make_empty)(BlockDriverState *bs); void (*bdrv_refresh_filename)(BlockDriverState *bs, QDict *options); + char *(*bdrv_dirname)(BlockDriverState *bs, Error **errp); /* aio */ BlockAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs, -- 2.8.0