block: bdrv_get_full_backing_filename_from_...'s ret. val.

Make bdrv_get_full_backing_filename_from_filename() return an allocated
string instead of placing the result in a caller-provided buffer.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20190201192935.18394-11-mreitz@redhat.com
Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Max Reitz 2019-02-01 20:29:14 +01:00
parent 009b03aaa2
commit 645ae7d88e
4 changed files with 49 additions and 33 deletions

53
block.c
View File

@ -312,20 +312,29 @@ fail:
return -EACCES; return -EACCES;
} }
void bdrv_get_full_backing_filename_from_filename(const char *backed, /*
const char *backing, * If @backing is empty, this function returns NULL without setting
char *dest, size_t sz, * @errp. In all other cases, NULL will only be returned with @errp
Error **errp) * set.
*
* Therefore, a return value of NULL without @errp set means that
* there is no backing file; if @errp is set, there is one but its
* absolute filename cannot be generated.
*/
char *bdrv_get_full_backing_filename_from_filename(const char *backed,
const char *backing,
Error **errp)
{ {
if (backing[0] == '\0' || path_has_protocol(backing) || if (backing[0] == '\0') {
path_is_absolute(backing)) return NULL;
{ } else if (path_has_protocol(backing) || path_is_absolute(backing)) {
pstrcpy(dest, sz, backing); return g_strdup(backing);
} else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) { } else if (backed[0] == '\0' || strstart(backed, "json:", NULL)) {
error_setg(errp, "Cannot use relative backing file names for '%s'", error_setg(errp, "Cannot use relative backing file names for '%s'",
backed); backed);
return NULL;
} else { } else {
path_combine_deprecated(dest, sz, backed, backing); return path_combine(backed, backing);
} }
} }
@ -333,12 +342,24 @@ void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz,
Error **errp) Error **errp)
{ {
char *backed; char *backed;
char *full_name;
Error *local_error = NULL;
bdrv_refresh_filename(bs); bdrv_refresh_filename(bs);
backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename; backed = bs->exact_filename[0] ? bs->exact_filename : bs->filename;
bdrv_get_full_backing_filename_from_filename(backed, bs->backing_file,
dest, sz, errp); full_name = bdrv_get_full_backing_filename_from_filename(backed,
bs->backing_file,
&local_error);
if (full_name) {
pstrcpy(dest, sz, full_name);
g_free(full_name);
} else if (local_error) {
error_propagate(errp, local_error);
} else if (sz > 0) {
*dest = '\0';
}
} }
void bdrv_register(BlockDriver *bdrv) void bdrv_register(BlockDriver *bdrv)
@ -5179,17 +5200,17 @@ void bdrv_img_create(const char *filename, const char *fmt,
size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, img_size); size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, img_size);
if (backing_file && !(flags & BDRV_O_NO_BACKING)) { if (backing_file && !(flags & BDRV_O_NO_BACKING)) {
BlockDriverState *bs; BlockDriverState *bs;
char *full_backing = g_new0(char, PATH_MAX); char *full_backing;
int back_flags; int back_flags;
QDict *backing_options = NULL; QDict *backing_options = NULL;
bdrv_get_full_backing_filename_from_filename(filename, backing_file, full_backing =
full_backing, PATH_MAX, bdrv_get_full_backing_filename_from_filename(filename, backing_file,
&local_err); &local_err);
if (local_err) { if (local_err) {
g_free(full_backing);
goto out; goto out;
} }
assert(full_backing);
/* backing files always opened read-only */ /* backing files always opened read-only */
back_flags = flags; back_flags = flags;

View File

@ -2078,16 +2078,16 @@ static int coroutine_fn vmdk_co_do_create(int64_t size,
if (backing_file) { if (backing_file) {
BlockBackend *backing; BlockBackend *backing;
char *full_backing = g_new0(char, PATH_MAX); char *full_backing =
bdrv_get_full_backing_filename_from_filename(blk_bs(blk)->filename, backing_file, bdrv_get_full_backing_filename_from_filename(blk_bs(blk)->filename,
full_backing, PATH_MAX, backing_file,
&local_err); &local_err);
if (local_err) { if (local_err) {
g_free(full_backing);
error_propagate(errp, local_err); error_propagate(errp, local_err);
ret = -ENOENT; ret = -ENOENT;
goto exit; goto exit;
} }
assert(full_backing);
backing = blk_new_open(full_backing, NULL, NULL, backing = blk_new_open(full_backing, NULL, NULL,
BDRV_O_NO_BACKING, errp); BDRV_O_NO_BACKING, errp);

View File

@ -489,10 +489,9 @@ void bdrv_get_backing_filename(BlockDriverState *bs,
char *filename, int filename_size); char *filename, int filename_size);
void bdrv_get_full_backing_filename(BlockDriverState *bs, void bdrv_get_full_backing_filename(BlockDriverState *bs,
char *dest, size_t sz, Error **errp); char *dest, size_t sz, Error **errp);
void bdrv_get_full_backing_filename_from_filename(const char *backed, char *bdrv_get_full_backing_filename_from_filename(const char *backed,
const char *backing, const char *backing,
char *dest, size_t sz, Error **errp);
Error **errp);
int path_has_protocol(const char *path); int path_has_protocol(const char *path);
int path_is_absolute(const char *path); int path_is_absolute(const char *path);

View File

@ -3343,18 +3343,14 @@ static int img_rebase(int argc, char **argv)
bdrv_refresh_filename(bs); bdrv_refresh_filename(bs);
overlay_filename = bs->exact_filename[0] ? bs->exact_filename overlay_filename = bs->exact_filename[0] ? bs->exact_filename
: bs->filename; : bs->filename;
out_real_path = g_malloc(PATH_MAX); out_real_path =
bdrv_get_full_backing_filename_from_filename(overlay_filename,
bdrv_get_full_backing_filename_from_filename(overlay_filename, out_baseimg,
out_baseimg, &local_err);
out_real_path,
PATH_MAX,
&local_err);
if (local_err) { if (local_err) {
error_reportf_err(local_err, error_reportf_err(local_err,
"Could not resolve backing filename: "); "Could not resolve backing filename: ");
ret = -1; ret = -1;
g_free(out_real_path);
goto out; goto out;
} }