X-Git-Url: https://git.draconx.ca/gitweb/upkg.git/blobdiff_plain/4b340ebadaa33a8f8c342136b93d8a1568dafd21..0438ce1525f7c1e722140c046fccb0f686b4cab2:/src/upkg.c diff --git a/src/upkg.c b/src/upkg.c index 62f9936..a8c7c7e 100644 --- a/src/upkg.c +++ b/src/upkg.c @@ -25,9 +25,10 @@ #include #include "upkg.h" -#include "module.h" -#include "uobject.h" -#include "exportable.h" +#include +#include +#include +#include enum { MODE_INFO, @@ -38,9 +39,10 @@ enum { int verbose = 0; static const char *progname = "upkg"; -static const char *sopts = "ivVH"; +static const char *sopts = "ixvVH"; static const struct option lopts[] = { { "info", 0, NULL, 'i' }, + { "export", 0, NULL, 'x' }, { "verbose", 0, NULL, 'v' }, { "version", 0, NULL, 'V' }, { "help", 0, NULL, 'H' }, @@ -110,17 +112,15 @@ void print_guid(unsigned char guid[static 16]) void print_upkg_exports(struct upkg *pkg) { for (unsigned i = 0; i < pkg->export_count; i++) { - const char *name, *package, *group, *class; + const char *name, *package, *class; name = upkg_export_name(pkg, i); - class = upkg_export_class(pkg, i, &package, &group); + class = upkg_export_class(pkg, i, &package); if (!name || !class) continue; - printf("%u - %s%s%s (%s.%s)\n", i+1, - group ? group : "", group ? "." : "", name, - package, class); + printf("%u - %s (%s.%s)\n", i+1, name, package, class); } } @@ -152,6 +152,71 @@ int package_info(struct upkg *pkg) return EXIT_SUCCESS; } +static int export(struct upkg *pkg, GObject *obj, unsigned idx) +{ + char name[256]; + FILE *of; + + if (u_object_deserialize(obj, pkg, idx) != 0) { + return -1; + } + + if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) { + return -1; + } + + u_object_export_name(obj, name, sizeof name); + + printf("exporting %s to %s\n", upkg_export_name(pkg, idx), name); + of = fopen(name, "wb"); + if (!of) { + perror(name); + return -1; + } + + u_object_export(obj, of); + + if (fclose(of) != 0) { + perror(name); + return -1; + } + + if (U_OBJECT_IS_LOADABLE(obj)) { + u_object_unload(obj); + } + + return 0; +} + +int package_export(struct upkg *pkg) +{ + const char *class, *package; + GObject *obj; + GType type; + int rc = EXIT_SUCCESS; + + for (unsigned i = 0; i < pkg->export_count; i++) { + class = upkg_export_class(pkg, i, &package); + if (!class) { + fprintf(stderr, "error getting class information.\n"); + return EXIT_FAILURE; + } + + type = u_object_module_get_class(package, class); + if (!type) continue; + + obj = g_object_new(type, NULL); + if (U_OBJECT_IS_EXPORTABLE(obj)) { + if (export(pkg, obj, i) != 0) { + rc = EXIT_FAILURE; + } + } + g_object_unref(obj); + } + + return rc; +} + int main(int argc, char **argv) { struct upkg *pkg; @@ -165,6 +230,9 @@ int main(int argc, char **argv) case 'i': mode = MODE_INFO; break; + case 'x': + mode = MODE_EXPORT; + break; case 'v': verbose++; break; @@ -185,7 +253,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - if (module_init() != 0) + if (u_object_module_init() != 0) return EXIT_FAILURE; pkg = upkg_fopen(argv[optind]); @@ -198,9 +266,12 @@ int main(int argc, char **argv) case MODE_INFO: rc = package_info(pkg); break; + case MODE_EXPORT: + rc = package_export(pkg); + break; } upkg_close(pkg); - module_exit(); + u_object_module_exit(); return rc; }