]> git.draconx.ca Git - upkg.git/blobdiff - src/uobject/package.c
package: Get rid of separate mechanisms for opening packages.
[upkg.git] / src / uobject / package.c
index bd9713500392cb504ac5d3d19f250f75e37051ef..af4ebb3f929cd82c7f6441f9bc8f8d78029fcd06 100644 (file)
@@ -25,6 +25,7 @@
 #include <ltdl.h>
 
 #include <uobject/package.h>
+#include <uobject/vfs.h>
 #include "upkg.h"
 
 #define U_PKG_GET_PRIV(o) \
@@ -82,74 +83,31 @@ static void dl_print_errors(const char *prefix)
        }
 }
 
-/*
- * Function for use with lt_dlforeachfile.  The user data must point to
- * a UPkg with the GTypeModule name field initialized.  If a suitable file
- * is found and could be successfully opened for reading, the pkg class member
- * will be filled in.
- */
-static int find_package_by_name(const char *filename, void *data)
-{
-       GTypeModule *m = G_TYPE_MODULE(data);
-       UPkg *upkg = U_PKG(data);
-
-       const char *base;
-       size_t len;
-       char *buf;
-
-       base = strrchr(filename, '/');
-       if (base) {
-               base++;
-       } else {
-               base = filename;
-       }
-
-       if (str_cmp_lower(base, m->name) != 0)
-               return 0;
-
-       len = strlen(filename);
-       buf = malloc(len + sizeof **u_pkg_exts);
-       if (!buf)
-               return 0;
-       strcpy(buf, filename);
-
-       for (unsigned i = 0; i < sizeof u_pkg_exts / sizeof *u_pkg_exts; i++) {
-               strcpy(buf+len, u_pkg_exts[i]);
-               upkg->pkg = upkg_fopen(buf);
-               if (!upkg->pkg) {
-                       free(buf);
-                       return 1;
-               }
-       }
-
-       free(buf);
-       return 0;
-}
-
 static gboolean u_pkg_load(GTypeModule *m)
 {
        struct upkg_priv *priv = U_PKG_GET_PRIV(m);
        int (*init_func)(GTypeModule *);
+       const char *file;
 
-       if (m->name) {
-               priv->native = lt_dlopenext(m->name);
-               if (!priv->native) {
-                       dl_print_errors(m->name);
+       file = u_pkg_vfs_lookup(m->name);
+       if (file) {
+               U_PKG(m)->pkg = upkg_fopen(file);
+               if (!U_PKG(m)->pkg) {
                        return FALSE;
                }
+       }
 
+       priv->native = lt_dlopenext(m->name);
+       if (priv->native) {
                init_func = lt_dlsym(priv->native, "init");
                if (!init_func || init_func(m) != 0) {
                        dl_print_errors(__func__);
                        lt_dlclose(priv->native);
+                       upkg_close(U_PKG(m)->pkg);
                        return FALSE;
                }
        }
 
-       if (!U_PKG(m)->pkg) {
-               lt_dlforeachfile(u_pkg_get_search_path(), find_package_by_name, m);
-       }
-
        return TRUE;
 }
 
@@ -168,6 +126,8 @@ static void u_pkg_unload(GTypeModule *m)
                if (lt_dlclose(priv->native) != 0) {
                        dl_print_errors(__func__);
                }
+
+               priv->native = NULL;
        }
 
        if (upkg->pkg) {
@@ -183,11 +143,7 @@ static void u_pkg_init(UPkg *pkg)
 
 static void u_pkg_finalize(GObject *o)
 {
-       UPkg *upkg = U_PKG(o);
-
-       if (upkg->pkg) {
-               upkg_close(upkg->pkg);
-       }
+       u_pkg_unload(G_TYPE_MODULE(o));
 }
 
 static void u_pkg_class_init(UPkgClass *class)
@@ -264,7 +220,7 @@ int u_pkg_add_search_dir(const char *path)
        return 0;
 }
 
-GTypeModule *u_pkg_new_by_name(const char *name)
+GTypeModule *u_pkg_open(const char *name)
 {
        g_return_val_if_fail(name != NULL, NULL);
 
@@ -282,21 +238,3 @@ GTypeModule *u_pkg_new_by_name(const char *name)
        mod->name = str_cpy_lower(pkgname, name);
        return mod;
 }
-
-GTypeModule *u_pkg_new_by_file(const char *filename)
-{
-       struct upkg *pkg = upkg_fopen(filename);
-       if (!pkg) {
-               return NULL;
-       }
-
-       GTypeModule *mod = g_object_new(U_PKG_TYPE, NULL);
-       if (!mod) {
-               upkg_close(pkg);
-               return NULL;
-       }
-
-       mod->name = NULL;
-       U_PKG(mod)->pkg = pkg;
-       return mod;
-}