2014-09-30 11:59:30 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 17:49:55 +00:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 14:35:06 +00:00
|
|
|
#include "qemu/ctype.h"
|
2016-03-20 17:16:19 +00:00
|
|
|
#include "qemu/id.h"
|
2014-09-30 11:59:30 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-10-12 23:36:49 +00:00
|
|
|
|
|
|
|
#define ID_SPECIAL_CHAR '#'
|
|
|
|
|
2015-11-25 21:03:37 +00:00
|
|
|
static const char *const id_subsys_str[ID_MAX] = {
|
2015-10-12 23:36:49 +00:00
|
|
|
[ID_QDEV] = "qdev",
|
|
|
|
[ID_BLOCK] = "block",
|
2019-10-21 14:31:31 +00:00
|
|
|
[ID_CHR] = "chr",
|
2015-10-12 23:36:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generates an ID of the form PREFIX SUBSYSTEM NUMBER
|
|
|
|
* where:
|
|
|
|
*
|
|
|
|
* - PREFIX is the reserved character '#'
|
|
|
|
* - SUBSYSTEM identifies the subsystem creating the ID
|
|
|
|
* - NUMBER is a decimal number unique within SUBSYSTEM.
|
|
|
|
*
|
|
|
|
* Example: "#block146"
|
|
|
|
*
|
|
|
|
* Note that these IDs do not satisfy id_wellformed().
|
|
|
|
*
|
|
|
|
* The caller is responsible for freeing the returned string with g_free()
|
|
|
|
*/
|
|
|
|
char *id_generate(IdSubSystems id)
|
|
|
|
{
|
|
|
|
static uint64_t id_counters[ID_MAX];
|
|
|
|
uint32_t rnd;
|
|
|
|
|
2015-11-25 21:03:37 +00:00
|
|
|
assert(id < ARRAY_SIZE(id_subsys_str));
|
2015-10-12 23:36:49 +00:00
|
|
|
assert(id_subsys_str[id]);
|
|
|
|
|
|
|
|
rnd = g_random_int_range(0, 100);
|
|
|
|
|
|
|
|
return g_strdup_printf("%c%s%" PRIu64 "%02" PRId32, ID_SPECIAL_CHAR,
|
|
|
|
id_subsys_str[id],
|
|
|
|
id_counters[id]++,
|
|
|
|
rnd);
|
|
|
|
}
|