From 1906020cf2abc26cf8317a5a754dca7318802250 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Fri, 17 Jul 2009 16:10:04 -0400 Subject: [PATCH] uobject: Take a package/index tuple instead of a upkg_file for deserialize. --- src/uobject.c | 44 +++++++++++++++++++++++++++++++++++++++----- src/uobject.h | 7 +++++-- src/upkg.c | 19 +++++++------------ 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/uobject.c b/src/uobject.c index d58ba33..175a624 100644 --- a/src/uobject.c +++ b/src/uobject.c @@ -61,6 +61,8 @@ struct uobject_priv { unsigned long nbuf; }; +G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT); + static unsigned long get_real_size(unsigned long *real, unsigned size, unsigned char *buf, size_t n) { @@ -194,22 +196,54 @@ static int deserialize(UObject *o, struct upkg_file *f) return 0; } -int u_object_deserialize(GObject *obj, struct upkg_file *f) +int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx) { g_return_val_if_fail(IS_U_OBJECT(obj), -1); - return U_OBJECT_GET_CLASS(obj)->deserialize(U_OBJECT(obj), f); + UObject *uo = U_OBJECT(obj); + struct upkg_file *f; + int rc; + + g_return_val_if_fail(uo->pkg_file == NULL, -1); + f = upkg_export_open(pkg, idx); + if (!f) { + return -1; + } + + rc = U_OBJECT_GET_CLASS(obj)->deserialize(uo, f); + if (rc != 0) { + upkg_export_close(f); + } else { + uo->pkg = pkg; + uo->pkg_idx = idx; + uo->pkg_file = f; + } + + return rc; } static void u_object_init(UObject *o) { - struct uobject_priv *priv = U_OBJECT_GET_PRIV(o); + o->pkg = NULL; + o->pkg_file = NULL; + o->pkg_idx = 0; +} + +static void u_object_finalize(GObject *o) +{ + UObject *uo = U_OBJECT(o); + + if (uo->pkg_file) { + upkg_export_close(uo->pkg_file); + } + + G_OBJECT_CLASS(u_object_parent_class)->finalize(o); } static void u_object_class_init(UObjectClass *class) { g_type_class_add_private(class, sizeof (struct uobject_priv)); + GObjectClass *go = G_OBJECT_CLASS(class); class->deserialize = deserialize; + go->finalize = u_object_finalize; } - -G_DEFINE_TYPE(UObject, u_object, G_TYPE_OBJECT); diff --git a/src/uobject.h b/src/uobject.h index dc8e088..efbe28d 100644 --- a/src/uobject.h +++ b/src/uobject.h @@ -22,7 +22,6 @@ #include #include "upkg.h" -#include "avl.h" #define U_OBJECT_TYPE u_object_get_type() #define U_OBJECT(obj) \ @@ -41,6 +40,10 @@ typedef struct UObjectClass UObjectClass; struct UObject { GObject parent; + + struct upkg *pkg; + struct upkg_file *pkg_file; + unsigned long pkg_idx; }; struct UObjectClass { @@ -54,6 +57,6 @@ GType u_object_get_type(void); const GValue *u_object_get_property(UObject *obj, const char *name); void u_object_set_property(UObject *obj, const char *name, const GValue *val); -int u_object_deserialize(GObject *obj, struct upkg_file *f); +int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx); #endif diff --git a/src/upkg.c b/src/upkg.c index 4a76fa2..ef44bf6 100644 --- a/src/upkg.c +++ b/src/upkg.c @@ -154,17 +154,15 @@ int package_info(struct upkg *pkg) static int export(struct upkg *pkg, GObject *obj, unsigned idx) { - struct upkg_file *f = upkg_export_open(pkg, idx); char name[256]; FILE *of; - int rc = -1; - if (u_object_deserialize(obj, f) != 0) { - goto out; + if (u_object_deserialize(obj, pkg, idx) != 0) { + return -1; } if (U_OBJECT_IS_LOADABLE(obj) && u_object_load(obj) != 0) { - goto out; + return -1; } u_object_export_name(obj, name, sizeof name); @@ -173,24 +171,21 @@ static int export(struct upkg *pkg, GObject *obj, unsigned idx) of = fopen(name, "wb"); if (!of) { perror(name); - goto out; + return -1; } - rc = u_object_export(obj, of); + u_object_export(obj, of); if (fclose(of) != 0) { perror(name); - rc = -1; - goto out; + return -1; } if (U_OBJECT_IS_LOADABLE(obj)) { u_object_unload(obj); } -out: - upkg_export_close(f); - return rc; + return 0; } int package_export(struct upkg *pkg) -- 2.43.2