qom: support orphan objects in object_get_canonical_path
PATCH 1/1 qom: support orphan objects in object_get_canonical_path
Mostly a rewrite, in order to keep the loop simple.
--- qom/object.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)
qom/object.c +16 -13
@@ -1669,25 +1669,28 @@ gchar *object_get_canonical_path(Object *obj)Object *root =object_get_root ();char *newpath, *path =NULL ; -while (obj != root) { +if (obj == root) { +return g_strdup ("/" ); + } + +do {char *component =object_get_canonical_path_component (obj); -if (path) { - newpath =g_strdup_printf ("%s/%s" , component, path); -g_free (component); +if (!component) { +/* A canonical path must be complete, so discard what was +* collected so far. +*/ g_free (path); - path = newpath; - }else { - path = component; +return NULL ; } - obj = obj->parent; - } - - newpath =g_strdup_printf ("/%s" , path ? path :"" ); -g_free (path); + newpath =g_strdup_printf ("/%s%s" , component, path ? path :"" ); +g_free (path); +g_free (component); + path = newpath; + }while ((obj = obj->parent) != root); -return newpath; +return path; }Object *object_resolve_path_component (Object *parent,const gchar *part)
Signature · 1 line
2.17.0
Quoted 1 line · Paolo Bonzini
Mostly a rewrite, in order to keep the loop simple.
Thus easier to review using "git difftool -y -x sdiff" (or whichever tool you prefer, such meld). "git format-patch" doesn't provide a way to generate side-by-side diffs applicable.
Quoted 24 lines
Signed-off-by Paolo Bonzini <[email protected]>--- qom/object.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)qom/object.c +5
-3@@ -1669,25 +1669,28 @@ gchar *object_get_canonical_path(Object *obj)Object *root =object_get_root ();char *newpath, *path =NULL ; -while (obj != root) { +if (obj == root) { +return g_strdup ("/" ); + } + + do { char *component = object_get_canonical_path_component(obj); -if (path) { - newpath =g_strdup_printf ("%s/%s" , component, path);
qom/object.c gchar *object_get_canonical_path(Object *obj) · line 1684
Quoted 10 lines
qom/object.c +5
-4-g_free (component); +if (!component) { +/* A canonical path must be complete, so discard what was +* collected so far. +*/ g_free (path); - path = newpath; - }else { - path = component; +return NULL ;
This function now matches his documentation!
qom/object.c gchar *object_get_canonical_path(Object *obj) · line 1690
Quoted 11 lines
qom/object.c +4
-5} - obj = obj->parent; - } - - newpath =g_strdup_printf ("/%s" , path ? path :"" ); -g_free (path); + newpath =g_strdup_printf ("/%s%s" , component, path ? path :"" ); +g_free (path); +g_free (component); + path = newpath;
Easier to read in 2 lines:
obj = obj->parent;
} while (obj != root);qom/object.c gchar *object_get_canonical_path(Object *obj) · line 1696
Quoted 8 lines
qom/object.c +2
-1+ }while ((obj = obj->parent) != root); -return newpath; +return path; }Object *object_resolve_path_component (Object *parent,const gchar *part)
Quoted 20 lines · Paolo Bonzini
Mostly a rewrite, in order to keep the loop simple.
Signed-off-by Paolo Bonzini <[email protected]>--- qom/object.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-)qom/object.c +4
-1@@ -1669,25 +1669,28 @@ gchar *object_get_canonical_path(Object *obj)Object *root =object_get_root ();char *newpath, *path =NULL ; -while (obj != root) { +if (obj == root) { +return g_strdup ("/" ); + } +
qom/object.c gchar *object_get_canonical_path(Object *obj) · line 1682
Quoted 10 lines
qom/object.c +5
-3+ do { char *component = object_get_canonical_path_component(obj); -if (path) { - newpath =g_strdup_printf ("%s/%s" , component, path); -g_free (component); + if (!component) { + /* A canonical path must be complete, so discard what was + * collected so far. + */
Well, this is correct indeed for the normal case when the result is used for internal business but for my task (show the owner of an MR or at least give a clue what to grep for) it will discard a partial path.
I guess I could print a typename if object_get_canonical_path() returns NULL, I'll repost v4.
Quoted 24 lines
qom/object.c +7
-9g_free (path); - path = newpath; - }else { - path = component; +return NULL ; } - obj = obj->parent; - } - - newpath =g_strdup_printf ("/%s" , path ? path :"" ); -g_free (path); + newpath =g_strdup_printf ("/%s%s" , component, path ? path :"" ); +g_free (path); +g_free (component); + path = newpath; + }while ((obj = obj->parent) != root); -return newpath; +return path; }Object *object_resolve_path_component (Object *parent,const gchar *part)
Signature · 1 line
Alexey
Quoted 6 lines · Alexey Kardashevskiy
Well, this is correct indeed for the normal case when the result is used for internal business but for my task (show the owner of an MR or at least give a clue what to grep for) it will discard a partial path.
I guess I could print a typename if
object_get_canonical_path()returns NULL, I'll repost v4.
Very good!
Paolo