- u_object_export_name(music, name, sizeof name);
- FILE *fp = fopen(name, "wb");
- if (!fp) return EXIT_FAILURE;
- u_object_export(music, fp);
- fclose(fp);
- printf("Wrote %s\n", name);
+ FILE *of;
+ int rc = -1;
+
+ if (u_object_deserialize(obj, f) != 0) {
+ goto out;
+ }
+
+ if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) {
+ goto out;
+ }
+
+ 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);
+ goto out;
+ }
+
+ rc = u_object_export(obj, of);
+
+ if (fclose(of) != 0) {
+ perror(name);
+ rc = -1;
+ goto out;
+ }
+
+ if (U_OBJECT_IS_LOADABLE(obj)) {
+ u_object_unload(obj);
+ }
+
+out:
+ upkg_export_close(f);
+ return rc;
+}
+
+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 = 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;
+ unsigned mode = MODE_INFO;
+ int opt, rc = EXIT_FAILURE;
+
+ if (argc > 0) progname = argv[0];
+
+ while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
+ switch (opt) {
+ case 'i':
+ mode = MODE_INFO;
+ break;
+ case 'x':
+ mode = MODE_EXPORT;
+ break;
+ case 'v':
+ verbose++;
+ break;
+ case 'V':
+ print_version();
+ return EXIT_SUCCESS;
+ case 'H':
+ print_help();
+ return EXIT_SUCCESS;
+ default:
+ print_usage(stderr);
+ return EXIT_FAILURE;
+ }
+ }
+
+ if (argv[optind] == NULL) {
+ print_usage(stderr);
+ return EXIT_FAILURE;
+ }
+
+ if (module_init() != 0)
+ return EXIT_FAILURE;
+
+ pkg = upkg_fopen(argv[optind]);
+ if (!pkg) {
+ fprintf(stderr, "failed to open package!\n");
+ return EXIT_FAILURE;
+ }
+
+ switch (mode) {
+ case MODE_INFO:
+ rc = package_info(pkg);
+ break;
+ case MODE_EXPORT:
+ rc = package_export(pkg);
+ break;
+ }