diff --git a/block.c b/block.c index c5a251c57ec70d5595886b17d7e970cabd3dc949..d3aebeb050b5467b0a7fdd7d377ab1a6c083eba3 100644 --- a/block.c +++ b/block.c @@ -335,18 +335,13 @@ void bdrv_register(BlockDriver *bdrv) QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); } -static bool bdrv_is_valid_name(const char *name) -{ - return qemu_opts_id_wellformed(name); -} - /* create a new block device (by default it is empty) */ BlockDriverState *bdrv_new(const char *device_name, Error **errp) { BlockDriverState *bs; int i; - if (*device_name && !bdrv_is_valid_name(device_name)) { + if (*device_name && !id_wellformed(device_name)) { error_setg(errp, "Invalid device name"); return NULL; } @@ -874,7 +869,7 @@ static void bdrv_assign_node_name(BlockDriverState *bs, } /* Check for empty string or invalid characters */ - if (!bdrv_is_valid_name(node_name)) { + if (!id_wellformed(node_name)) { error_setg(errp, "Invalid node name"); return; } diff --git a/include/qemu-common.h b/include/qemu-common.h index dcb57ab4b9ebd16bdf01c1f1598bcbec3dcbd27f..b87e9c2d8bfb362aeaf789765bd3f106d0c2cc0c 100644 --- a/include/qemu-common.h +++ b/include/qemu-common.h @@ -190,6 +190,9 @@ int64_t strtosz_suffix_unit(const char *nptr, char **end, /* used to print char* safely */ #define STR_OR_NULL(str) ((str) ? (str) : "null") +/* id.c */ +bool id_wellformed(const char *id); + /* path.c */ void init_paths(const char *prefix); const char *path(const char *pathname); diff --git a/include/qemu/option.h b/include/qemu/option.h index 945347cc8f6d1daf3a302d0c4e061faab083dd84..59bea759a2c1e5e11b8f272cd2d2230573272059 100644 --- a/include/qemu/option.h +++ b/include/qemu/option.h @@ -103,7 +103,6 @@ typedef int (*qemu_opt_loopfunc)(const char *name, const char *value, void *opaq int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, int abort_on_failure); -int qemu_opts_id_wellformed(const char *id); QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id); QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists, Error **errp); diff --git a/util/Makefile.objs b/util/Makefile.objs index cb8862ba92f84cc083625cafb9913015dc0a16da..93007e2f562089c18c6374d362e6d8157bf8cef9 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -8,6 +8,7 @@ util-obj-y += fifo8.o util-obj-y += acl.o util-obj-y += error.o qemu-error.o util-obj-$(CONFIG_POSIX) += compatfd.o +util-obj-y += id.o util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o util-obj-y += qemu-option.o qemu-progress.o util-obj-y += hexdump.o diff --git a/util/id.c b/util/id.c new file mode 100644 index 0000000000000000000000000000000000000000..09b22fb8fae04b3c13768d4dc3daad5c93c89e28 --- /dev/null +++ b/util/id.c @@ -0,0 +1,28 @@ +/* + * Dealing with identifiers + * + * Copyright (C) 2014 Red Hat, Inc. + * + * Authors: + * Markus Armbruster , + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 + * or later. See the COPYING.LIB file in the top-level directory. + */ + +#include "qemu-common.h" + +bool id_wellformed(const char *id) +{ + int i; + + if (!qemu_isalpha(id[0])) { + return false; + } + for (i = 1; id[i]; i++) { + if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) { + return false; + } + } + return true; +} diff --git a/util/qemu-option.c b/util/qemu-option.c index 0cf9960fc5461b98d8449f0bba06f7462b10ef18..5d106959caef30db0179dd93ed08e2e85d19e2c5 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -641,28 +641,13 @@ QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) return NULL; } -int qemu_opts_id_wellformed(const char *id) -{ - int i; - - if (!qemu_isalpha(id[0])) { - return 0; - } - for (i = 1; id[i]; i++) { - if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) { - return 0; - } - } - return 1; -} - QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists, Error **errp) { QemuOpts *opts = NULL; if (id) { - if (!qemu_opts_id_wellformed(id)) { + if (!id_wellformed(id)) { error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier"); #if 0 /* conversion from qerror_report() to error_set() broke this: */ error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");