qom: add container_get

This is QOM "mkdir -p".  It is useful when referring to
container objects such as "/machine".

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Paolo Bonzini 2012-03-27 18:38:45 +02:00 committed by Anthony Liguori
parent cefc898806
commit a612b2a663
4 changed files with 66 additions and 22 deletions

View file

@ -180,9 +180,7 @@ static Object *qdev_get_peripheral(void)
static Object *dev;
if (dev == NULL) {
dev = object_new("container");
object_property_add_child(object_get_root(), "peripheral",
OBJECT(dev), NULL);
dev = container_get("/peripheral");
}
return dev;
@ -193,9 +191,7 @@ static Object *qdev_get_peripheral_anon(void)
static Object *dev;
if (dev == NULL) {
dev = object_new("container");
object_property_add_child(object_get_root(), "peripheral-anon",
OBJECT(dev), NULL);
dev = container_get("/peripheral-anon");
}
return dev;

View file

@ -836,6 +836,18 @@ Object *object_resolve_path(const char *path, bool *ambiguous);
Object *object_resolve_path_type(const char *path, const char *typename,
bool *ambiguous);
/**
* object_resolve_path_component:
* @parent: the object in which to resolve the path
* @part: the component to resolve.
*
* This is similar to object_resolve_path with an absolute path, but it
* only resolves one element (@part) and takes the others from @parent.
*
* Returns: The resolved object or NULL on path lookup failure.
*/
Object *object_resolve_path_component(Object *parent, gchar *part);
/**
* object_property_add_child:
* @obj: the object to add a property to
@ -891,4 +903,16 @@ void object_property_add_str(Object *obj, const char *name,
void (*set)(Object *, const char *, struct Error **),
struct Error **errp);
/**
* container_get:
* @path: path to the container
*
* Return a container object whose path is @path. Create more containers
* along the path if necessary.
*
* Returns: the container object.
*/
Object *container_get(const char *path);
#endif

View file

@ -12,6 +12,7 @@
#include "qemu/object.h"
#include "module.h"
#include <assert.h>
static TypeInfo container_info = {
.name = "container",
@ -24,4 +25,26 @@ static void container_register_types(void)
type_register_static(&container_info);
}
Object *container_get(const char *path)
{
Object *obj, *child;
gchar **parts;
int i;
parts = g_strsplit(path, "/", 0);
assert(parts != NULL && parts[0] != NULL && !parts[0][0]);
obj = object_get_root();
for (i = 1; parts[i] != NULL; i++, obj = child) {
child = object_resolve_path_component(obj, parts[i]);
if (!child) {
child = object_new("container");
object_property_add_child(obj, parts[i], child, NULL);
}
}
return obj;
}
type_init(container_register_types)

View file

@ -1022,12 +1022,27 @@ gchar *object_get_canonical_path(Object *obj)
return newpath;
}
Object *object_resolve_path_component(Object *parent, gchar *part)
{
ObjectProperty *prop = object_property_find(parent, part);
if (prop == NULL) {
return NULL;
}
if (strstart(prop->type, "link<", NULL)) {
return *(Object **)prop->opaque;
} else if (strstart(prop->type, "child<", NULL)) {
return prop->opaque;
} else {
return NULL;
}
}
static Object *object_resolve_abs_path(Object *parent,
gchar **parts,
const char *typename,
int index)
{
ObjectProperty *prop;
Object *child;
if (parts[index] == NULL) {
@ -1038,21 +1053,7 @@ static Object *object_resolve_abs_path(Object *parent,
return object_resolve_abs_path(parent, parts, typename, index + 1);
}
prop = object_property_find(parent, parts[index]);
if (prop == NULL) {
return NULL;
}
child = NULL;
if (strstart(prop->type, "link<", NULL)) {
Object **pchild = prop->opaque;
if (*pchild) {
child = *pchild;
}
} else if (strstart(prop->type, "child<", NULL)) {
child = prop->opaque;
}
child = object_resolve_path_component(parent, parts[index]);
if (!child) {
return NULL;
}