From a612b2a6635fa1a3a29a8bcf41b31f1f3fae1110 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 27 Mar 2012 18:38:45 +0200 Subject: qom: add container_get This is QOM "mkdir -p". It is useful when referring to container objects such as "/machine". Reviewed-by: Anthony Liguori Signed-off-by: Paolo Bonzini Signed-off-by: Anthony Liguori --- qom/container.c | 23 +++++++++++++++++++++++ qom/object.c | 33 +++++++++++++++++---------------- 2 files changed, 40 insertions(+), 16 deletions(-) (limited to 'qom') diff --git a/qom/container.c b/qom/container.c index f10720886..67e9e8a6f 100644 --- a/qom/container.c +++ b/qom/container.c @@ -12,6 +12,7 @@ #include "qemu/object.h" #include "module.h" +#include 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) diff --git a/qom/object.c b/qom/object.c index 9cd9506eb..e721fc28f 100644 --- a/qom/object.c +++ b/qom/object.c @@ -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; } -- cgit v1.2.3-65-gdbad