]> git.draconx.ca Git - upkg.git/blobdiff - src/uobject/uobject.c
libuobject: Store hierarchical object names for UObjects.
[upkg.git] / src / uobject / uobject.c
index fa93ec0f420bb6af18b9845dcd83ae811a21ec2c..47782922c5876ee84f93b1783c62ecaf0c02a9ce 100644 (file)
@@ -311,10 +311,47 @@ static int deserialize(UObject *uo)
        return 0;
 }
 
+/*
+ * Get the full hierarchical object name for an export, used for diagnostics.
+ * Currently, this does not include the package name (which is actually
+ * required for this to be a "full" name) as it's not yet passed to the
+ * deserialize method.
+ *
+ * Returns a buffer allocated by malloc on success, or NULL on failure.
+ */
+static char *get_obj_fullname(const struct upkg_export *e)
+{
+       size_t total_len = 0, len;
+       char *fullname;
+
+       for (const struct upkg_export *c = e; c; c = c->parent) {
+               len = strlen(c->name) + 1;
+               if (total_len > SIZE_MAX - len)
+                       return NULL;
+               total_len += len;
+       }
+
+       fullname = malloc(total_len);
+       if (!fullname)
+               return NULL;
+
+       for (const struct upkg_export *c = e; c; c = c->parent) {
+               len = strlen(c->name);
+               assert(total_len > len);
+
+               total_len -= len + 1;
+               memcpy(fullname + total_len, c->name, len);
+               fullname[total_len + len] = c == e ? '\0' : '.';
+       }
+
+       return fullname;
+}
+
 int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
 {
        g_return_val_if_fail(IS_U_OBJECT(obj), -1);
        UObject *uo = U_OBJECT(obj);
+       const struct upkg_export *e;
        struct upkg_file *f;
        int rc;
 
@@ -327,6 +364,13 @@ int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx)
        uo->pkg      = pkg;
        uo->pkg_file = f;
 
+       e = upkg_get_export(pkg, idx);
+       if (!e)
+               return -1;
+       uo->pkg_name = get_obj_fullname(e);
+       if (!uo->pkg_name)
+               return -1;
+
        rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo);
        if (rc != 0) {
                upkg_export_close(f);
@@ -371,6 +415,8 @@ static void u_object_finalize(GObject *o)
                upkg_export_close(uo->pkg_file);
        }
 
+       free(uo->pkg_name);
+
        G_OBJECT_CLASS(u_object_parent_class)->finalize(o);
 }
 
@@ -427,12 +473,8 @@ void u_vlog_full(GObject *o, GLogLevelFlags level, const char *fmt, va_list ap)
        UObject *uo = U_OBJECT(o);
        char *new_fmt = NULL;
 
-       if (uo->pkg_file) {
-               /* XXX: Currently, there's no way to get the full object name
-                * here because the object path information is discarded
-                * after opening it.  In principle, there's no reason why
-                * we couldn't keep it around to improve log messages. */
-               new_fmt = prepend_fmt(uo->pkg_file->name, fmt);
+       if (uo->pkg_name) {
+               new_fmt = prepend_fmt(uo->pkg_name, fmt);
                if (!new_fmt) {
                        g_log(G_OBJECT_TYPE_NAME(o), level, "%s",
                              uo->pkg_file->name);