Commit f5bebbbb authored by Markus Armbruster's avatar Markus Armbruster Committed by Stefan Hajnoczi
Browse files

util: Emancipate id_wellformed() from QemuOpts



IDs have long spread beyond QemuOpts: not everything with an ID
necessarily goes through QemuOpts.  Commit 9aebf3b8 is about such a
case: block layer names are meant to be well-formed IDs, but some of
them don't go through QemuOpts, and thus weren't checked.  The commit
fixed that the straightforward way: rename the internal QemuOpts
helper id_wellformed() to qemu_opts_id_wellformed() and give it
external linkage.

Instead of using it directly in block.c, the commit adds wrapper
bdrv_is_valid_name(), probably to hide the connection to QemuOpts.

Go one logical step further: emancipate IDs from QemuOpts.  Rename the
function back to id_wellformed(), and put it in another file.  While
there, clean up its value to bool.  Peel off the bdrv_is_valid_name()
wrapper.

[Replaced stray return 0 with return false to match bool returns used
elsewhere in id_wellformed().
--Stefan]

Signed-off-by: default avatarMarkus Armbruster <armbru@redhat.com>
Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
parent d93162e1
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -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;
    }
+3 −0
Original line number Diff line number Diff line
@@ -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);
+0 −1
Original line number Diff line number Diff line
@@ -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);
+1 −0
Original line number Diff line number Diff line
@@ -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

util/id.c

0 → 100644
+28 −0
Original line number Diff line number Diff line
/*
 * Dealing with identifiers
 *
 * Copyright (C) 2014 Red Hat, Inc.
 *
 * Authors:
 *  Markus Armbruster <armbru@redhat.com>,
 *
 * 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;
}
Loading