2014-09-30 13:59:30 +02: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 18:49:55 +01:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 16:35:06 +02:00
|
|
|
#include "qemu/ctype.h"
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/id.h"
|
2014-09-30 13:59:30 +02: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-13 01:36:49 +02:00
|
|
|
|
|
|
|
#define ID_SPECIAL_CHAR '#'
|
|
|
|
|
2015-11-25 22:03:37 +01:00
|
|
|
static const char *const id_subsys_str[ID_MAX] = {
|
2015-10-13 01:36:49 +02:00
|
|
|
[ID_QDEV] = "qdev",
|
|
|
|
[ID_BLOCK] = "block",
|
2019-10-21 16:31:31 +02:00
|
|
|
[ID_CHR] = "chr",
|
2015-10-13 01:36:49 +02: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 22:03:37 +01:00
|
|
|
assert(id < ARRAY_SIZE(id_subsys_str));
|
2015-10-13 01:36:49 +02: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);
|
|
|
|
}
|