From bf8c0203b9c9b1b6c52606084448a222d19ab3ae Mon Sep 17 00:00:00 2001 From: Peter Schiffer Date: Wed, 16 Apr 2014 18:22:05 +0200 Subject: Software-dbus: refactor & cleanup --- .../LMI_MemberOfSoftwareCollectionProvider.c | 40 +++-- src/software-dbus/LMI_SoftwareIdentityProvider.c | 145 ++-------------- .../LMI_SoftwareIdentityResourceProvider.c | 8 +- src/software-dbus/sw-utils.c | 189 +++++++++++++++------ src/software-dbus/sw-utils.h | 34 +++- 5 files changed, 214 insertions(+), 202 deletions(-) (limited to 'src') diff --git a/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c b/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c index bbd04bc..ebd3da7 100644 --- a/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c +++ b/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c @@ -109,14 +109,14 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionEnumInstances( done: g_clear_error(&gerror); - if (task) { - g_object_unref(task); + if (array) { + g_ptr_array_unref(array); } if (results) { g_object_unref(results); } - if (array) { - g_ptr_array_unref(array); + if (task) { + g_object_unref(task); } if (*error_msg) { @@ -133,8 +133,12 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionGetInstance( const CMPIObjectPath* cop, const char** properties) { + PkPackage *pk_pkg = NULL; + SwPackage sw_pkg; char instance_id[INSTANCE_ID_LEN] = ""; + init_sw_package(&sw_pkg); + create_instance_id(SYSTEM_SW_COLLECTION_CLASS_NAME, NULL, instance_id, INSTANCE_ID_LEN); @@ -146,11 +150,19 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionGetInstance( CMReturn(CMPI_RC_ERR_NOT_FOUND); } - if (!is_valid_sw_pkg_elem_name(get_str_property_from_op(w.Member.value, - "InstanceID") + strlen(ORGID ":" ORGID "_" SW_IDENTITY_CLASS_NAME ":"))) { + if (create_sw_package_from_elem_name(get_str_property_from_op(w.Member.value, + "InstanceID") + strlen(ORGID ":" ORGID "_" SW_IDENTITY_CLASS_NAME ":"), + &sw_pkg) != 0) { CMReturn(CMPI_RC_ERR_NOT_FOUND); } + get_pk_pkg_from_sw_pkg(&sw_pkg, &pk_pkg); + if (!pk_pkg) { + CMReturn(CMPI_RC_ERR_NOT_FOUND); + } + g_object_unref(pk_pkg); + free_sw_package(&sw_pkg); + KReturnInstance(cr, w); CMReturn(CMPI_RC_OK); } @@ -313,14 +325,14 @@ static CMPIStatus associators( done: g_clear_error(&gerror); - if (task) { - g_object_unref(task); + if (array) { + g_ptr_array_unref(array); } if (results) { g_object_unref(results); } - if (array) { - g_ptr_array_unref(array); + if (task) { + g_object_unref(task); } if (*error_msg) { @@ -462,14 +474,14 @@ static CMPIStatus references( done: g_clear_error(&gerror); - if (task) { - g_object_unref(task); + if (array) { + g_ptr_array_unref(array); } if (results) { g_object_unref(results); } - if (array) { - g_ptr_array_unref(array); + if (task) { + g_object_unref(task); } if (*error_msg) { diff --git a/src/software-dbus/LMI_SoftwareIdentityProvider.c b/src/software-dbus/LMI_SoftwareIdentityProvider.c index 3806584..696c65e 100644 --- a/src/software-dbus/LMI_SoftwareIdentityProvider.c +++ b/src/software-dbus/LMI_SoftwareIdentityProvider.c @@ -43,58 +43,10 @@ static CMPIStatus LMI_SoftwareIdentityEnumInstanceNames( const CMPIResult* cr, const CMPIObjectPath* cop) { - PkTask *task = NULL; - PkResults *results = NULL; - GError *gerror = NULL; - GPtrArray *array = NULL; - SwPackage sw_pkg; - unsigned i; - char error_msg[ERROR_MSG_LEN] = "", elem_name[ELEM_NAME_LEN] = "", - instance_id[INSTANCE_ID_LEN] = ""; - - init_sw_package(&sw_pkg); - - task = pk_task_new(); - - results = pk_task_get_packages_sync(task, 0, NULL, NULL, NULL, &gerror); - if (check_and_create_error_msg(results, gerror, - "Getting list of packages failed", error_msg, ERROR_MSG_LEN)) { - goto done; - } - - array = pk_results_get_package_array(results); - g_ptr_array_sort(array, (GCompareFunc) pk_pkg_cmp); - for (i = 0; i < array->len; i++) { - if (create_sw_package_from_pk_pkg(g_ptr_array_index(array, i), - &sw_pkg) != 0) { - continue; - } - - sw_pkg_get_element_name(&sw_pkg, elem_name, ELEM_NAME_LEN); - - create_instance_id(SW_IDENTITY_CLASS_NAME, elem_name, instance_id, - INSTANCE_ID_LEN); - - free_sw_package(&sw_pkg); - - LMI_SoftwareIdentityRef w; - LMI_SoftwareIdentityRef_Init(&w, _cb, KNameSpace(cop)); - LMI_SoftwareIdentityRef_Set_InstanceID(&w, instance_id); - KReturnObjectPath(cr, w); - } - -done: - g_clear_error(&gerror); + char error_msg[ERROR_MSG_LEN] = ""; - if (task) { - g_object_unref(task); - } - if (results) { - g_object_unref(results); - } - if (array) { - g_ptr_array_unref(array); - } + enum_sw_identity_instances(0, _cb, KNameSpace(cop), cr, + error_msg, ERROR_MSG_LEN, 1); if (*error_msg) { KReturn2(_cb, ERR_FAILED, "%s", error_msg); @@ -121,106 +73,41 @@ static CMPIStatus LMI_SoftwareIdentityGetInstance( const char** properties) { LMI_SoftwareIdentity w; - PkTask *task = NULL; PkPackage *pk_pkg = NULL; PkDetails *pk_det = NULL; - PkResults *results = NULL, *results2 = NULL; - GPtrArray *array = NULL, *array2 = NULL; - GError *gerror = NULL; - gchar **values = NULL; SwPackage sw_pkg; - unsigned i, j; - short found = 0, found2 = 0; - char error_msg[ERROR_MSG_LEN] = ""; + short found = 0; init_sw_package(&sw_pkg); - if (create_sw_package_from_elem_name( - get_str_property_from_op(cop, "InstanceID") + strlen( - ORGID ":" ORGID "_" SW_IDENTITY_CLASS_NAME ":"), &sw_pkg) != 0) { - snprintf(error_msg, ERROR_MSG_LEN, "Failed to parse instance ID: %s", - get_str_property_from_op(cop, "InstanceID")); - goto done; - } - - task = pk_task_new(); - - values = g_new0(gchar*, 2); - values[0] = g_strdup(sw_pkg.name); - values[1] = NULL; - - results = pk_task_search_names_sync(task, 0, values, NULL, NULL, NULL, &gerror); - if (check_and_create_error_msg(results, gerror, "Resolving package failed", - error_msg, ERROR_MSG_LEN)) { + if (create_sw_package_from_elem_name(get_str_property_from_op(cop, + "InstanceID") + strlen(ORGID ":" ORGID "_" SW_IDENTITY_CLASS_NAME ":"), + &sw_pkg) != 0) { goto done; } - array = pk_results_get_package_array(results); - for (i = 0; i < array->len; i++) { - pk_pkg = g_ptr_array_index(array, i); - if (strcmp(pk_package_get_name(pk_pkg), sw_pkg.name) == 0 - && strcmp(pk_package_get_version(pk_pkg), sw_pkg.pk_version) == 0 - && strcmp(pk_package_get_arch(pk_pkg), sw_pkg.arch) == 0) { - found = 1; - break; - } - } - if (!found) { + get_pk_pkg_from_sw_pkg(&sw_pkg, &pk_pkg); + if (!pk_pkg) { goto done; } - g_free(values[0]); - values[0] = g_strdup(pk_package_get_id(pk_pkg)); - results2 = pk_task_get_details_sync(task, values, NULL, NULL, NULL, &gerror); - if (check_and_create_error_msg(results2, gerror, - "Getting package details failed", error_msg, ERROR_MSG_LEN)) { - warn(error_msg); - /* This is non-fatal problem. */ - error_msg[0] = '\0'; - } else { - array2 = pk_results_get_details_array(results2); - for (j = 0; j < array2->len; j++) { - pk_det = g_ptr_array_index(array2, j); - if (strcmp(pk_details_get_package_id(pk_det), - pk_package_get_id(pk_pkg)) == 0) { - found2 = 1; - break; - } - } - } - if (!found2) { - pk_det = NULL; - } + get_pk_det_from_pk_pkg(pk_pkg, &pk_det); create_instance_from_pkgkit_data(pk_pkg, pk_det, &sw_pkg, _cb, KNameSpace(cop), &w); KReturnInstance(cr, w); + found = 1; + done: free_sw_package(&sw_pkg); - g_strfreev(values); - g_clear_error(&gerror); - - if (task) { - g_object_unref(task); - } - if (results) { - g_object_unref(results); - } - if (results2) { - g_object_unref(results2); - } - if (array) { - g_ptr_array_unref(array); + if (pk_det) { + g_object_unref(pk_det); } - if (array2) { - g_ptr_array_unref(array2); - } - - if (*error_msg) { - KReturn2(_cb, ERR_FAILED, "%s", error_msg); + if (pk_pkg) { + g_object_unref(pk_pkg); } if (!found) { diff --git a/src/software-dbus/LMI_SoftwareIdentityResourceProvider.c b/src/software-dbus/LMI_SoftwareIdentityResourceProvider.c index c92900e..c09b71d 100644 --- a/src/software-dbus/LMI_SoftwareIdentityResourceProvider.c +++ b/src/software-dbus/LMI_SoftwareIdentityResourceProvider.c @@ -136,14 +136,14 @@ static CMPIStatus LMI_SoftwareIdentityResourceEnumInstances( done: g_clear_error(&gerror); - if (task) { - g_object_unref(task); + if (array) { + g_ptr_array_unref(array); } if (results) { g_object_unref(results); } - if (array) { - g_ptr_array_unref(array); + if (task) { + g_object_unref(task); } if (repo_id) { g_free(repo_id); diff --git a/src/software-dbus/sw-utils.c b/src/software-dbus/sw-utils.c index e2a252d..410a0fe 100644 --- a/src/software-dbus/sw-utils.c +++ b/src/software-dbus/sw-utils.c @@ -23,6 +23,10 @@ const char *provider_name = "software"; const ConfigEntry *provider_config_defaults = NULL; +/******************************************************************************* + * SwPackage & related functions + ******************************************************************************/ + void init_sw_package(SwPackage *pkg) { pkg->name = NULL; @@ -201,30 +205,25 @@ void sw_pkg_get_element_name(const SwPackage *pkg, char *elem_name, pkg->version, pkg->release, pkg->arch); } -short is_valid_sw_pkg_elem_name(const char *elem_name) +/******************************************************************************* + * Functions related to single PkPackage + ******************************************************************************/ + +void get_pk_pkg_from_sw_pkg(const SwPackage *sw_pkg, PkPackage **pk_pkg) { PkTask *task = NULL; - PkPackage *pk_pkg = NULL; + PkPackage *item = NULL; PkResults *results = NULL; GPtrArray *array = NULL; GError *gerror = NULL; gchar **values = NULL; - SwPackage sw_pkg; - short ret = 0; unsigned i; char error_msg[ERROR_MSG_LEN] = ""; - init_sw_package(&sw_pkg); - - if (create_sw_package_from_elem_name(elem_name, &sw_pkg) != 0) { - warn("Failed to parse element name: %s", elem_name); - goto done; - } - task = pk_task_new(); values = g_new0(gchar*, 2); - values[0] = g_strdup(sw_pkg.name); + values[0] = g_strdup(sw_pkg->name); values[1] = NULL; results = pk_task_search_names_sync(task, 0, values, NULL, NULL, NULL, &gerror); @@ -236,32 +235,81 @@ short is_valid_sw_pkg_elem_name(const char *elem_name) array = pk_results_get_package_array(results); for (i = 0; i < array->len; i++) { - pk_pkg = g_ptr_array_index(array, i); - if (strcmp(pk_package_get_name(pk_pkg), sw_pkg.name) == 0 - && strcmp(pk_package_get_version(pk_pkg), sw_pkg.pk_version) == 0 - && strcmp(pk_package_get_arch(pk_pkg), sw_pkg.arch) == 0) { - ret = 1; + item = g_ptr_array_index(array, i); + if (strcmp(pk_package_get_name(item), sw_pkg->name) == 0 + && strcmp(pk_package_get_version(item), sw_pkg->pk_version) == 0 + && strcmp(pk_package_get_arch(item), sw_pkg->arch) == 0) { + *pk_pkg = g_object_ref(item); break; } } done: - free_sw_package(&sw_pkg); - g_strfreev(values); g_clear_error(&gerror); - if (task) { - g_object_unref(task); + if (array) { + g_ptr_array_unref(array); } if (results) { g_object_unref(results); } + if (task) { + g_object_unref(task); + } + + return; +} + +void get_pk_det_from_pk_pkg(PkPackage *pk_pkg, PkDetails **pk_det) +{ + PkTask *task = NULL; + PkDetails *item = NULL; + PkResults *results = NULL; + GPtrArray *array = NULL; + GError *gerror = NULL; + gchar **values = NULL; + unsigned i; + char error_msg[ERROR_MSG_LEN] = ""; + + task = pk_task_new(); + + values = g_new0(gchar*, 2); + values[0] = g_strdup(pk_package_get_id(pk_pkg)); + values[1] = NULL; + + results = pk_task_get_details_sync(task, values, NULL, NULL, NULL, &gerror); + if (check_and_create_error_msg(results, gerror, + "Getting package details failed", error_msg, ERROR_MSG_LEN)) { + warn(error_msg); + goto done; + } + + array = pk_results_get_details_array(results); + for (i = 0; i < array->len; i++) { + item = g_ptr_array_index(array, i); + if (strcmp(pk_details_get_package_id(item), + pk_package_get_id(pk_pkg)) == 0) { + *pk_det = g_object_ref(item); + break; + } + } + +done: + g_strfreev(values); + g_clear_error(&gerror); + if (array) { g_ptr_array_unref(array); } + if (results) { + g_object_unref(results); + } + if (task) { + g_object_unref(task); + } - return ret; + return; } void create_instance_from_pkgkit_data(PkPackage *pk_pkg, PkDetails *pk_det, @@ -311,21 +359,20 @@ void create_instance_from_pkgkit_data(PkPackage *pk_pkg, PkDetails *pk_det, return; } +/******************************************************************************* + * Functions related to multiple PkPackages + ******************************************************************************/ + void enum_sw_identity_instances(PkBitfield filters, const CMPIBroker *cb, const char *ns, const CMPIResult* cr, char *error_msg, - const unsigned error_msg_len) + const unsigned error_msg_len, const short names) { - LMI_SoftwareIdentity w; PkTask *task = NULL; - PkPackage *pk_pkg = NULL; - PkDetails *pk_det = NULL; - PkResults *results = NULL, *results2 = NULL; + PkResults *results = NULL; GError *gerror = NULL; - GPtrArray *array = NULL, *array2 = NULL; - gchar **values = NULL; + GPtrArray *array = NULL; SwPackage sw_pkg; - int cmpres; - unsigned i, j, a2i; + unsigned i; init_sw_package(&sw_pkg); @@ -340,6 +387,40 @@ void enum_sw_identity_instances(PkBitfield filters, const CMPIBroker *cb, array = pk_results_get_package_array(results); g_ptr_array_sort(array, (GCompareFunc) pk_pkg_cmp); + if (names) { + char elem_name[ELEM_NAME_LEN] = "", instance_id[INSTANCE_ID_LEN] = ""; + + for (i = 0; i < array->len; i++) { + if (create_sw_package_from_pk_pkg(g_ptr_array_index(array, i), + &sw_pkg) != 0) { + continue; + } + + sw_pkg_get_element_name(&sw_pkg, elem_name, ELEM_NAME_LEN); + + create_instance_id(SW_IDENTITY_CLASS_NAME, elem_name, instance_id, + INSTANCE_ID_LEN); + + free_sw_package(&sw_pkg); + + LMI_SoftwareIdentityRef w; + LMI_SoftwareIdentityRef_Init(&w, cb, ns); + LMI_SoftwareIdentityRef_Set_InstanceID(&w, instance_id); + KReturnObjectPath(cr, w); + } + + goto done; + } + + LMI_SoftwareIdentity w; + PkPackage *pk_pkg = NULL; + PkDetails *pk_det = NULL; + PkResults *results2 = NULL; + GPtrArray *array2 = NULL; + gchar **values = NULL; + int cmpres; + unsigned j, a2i; + for (i = 0; i < array->len / PK_DETAILS_LIMIT + 1; i++) { values = g_new0(gchar*, PK_DETAILS_LIMIT + 1); values[PK_DETAILS_LIMIT] = NULL; @@ -411,34 +492,22 @@ void enum_sw_identity_instances(PkBitfield filters, const CMPIBroker *cb, done: g_clear_error(&gerror); - if (task) { - g_object_unref(task); + if (array) { + g_ptr_array_unref(array); } if (results) { g_object_unref(results); } - if (array) { - g_ptr_array_unref(array); + if (task) { + g_object_unref(task); } return; } -gint pk_pkg_cmp(gpointer a, gpointer b) -{ - PkPackage *al = *(PkPackage **) a; - PkPackage *bl = *(PkPackage **) b; - - return (gint) strcmp(pk_package_get_id(al), pk_package_get_id(bl)); -} - -gint pk_det_cmp(gpointer a, gpointer b) -{ - PkDetails *al = *(PkDetails **) a; - PkDetails *bl = *(PkDetails **) b; - - return (gint) strcmp(pk_details_get_package_id(al), pk_details_get_package_id(bl)); -} +/******************************************************************************* + * Functions related to PackageKit + ******************************************************************************/ short check_and_create_error_msg(PkResults *results, GError *gerror, const char *custom_msg, char *error_msg, const unsigned error_msg_len) @@ -476,6 +545,26 @@ done: return ret; } +gint pk_pkg_cmp(gpointer a, gpointer b) +{ + PkPackage *al = *(PkPackage **) a; + PkPackage *bl = *(PkPackage **) b; + + return (gint) strcmp(pk_package_get_id(al), pk_package_get_id(bl)); +} + +gint pk_det_cmp(gpointer a, gpointer b) +{ + PkDetails *al = *(PkDetails **) a; + PkDetails *bl = *(PkDetails **) b; + + return (gint) strcmp(pk_details_get_package_id(al), pk_details_get_package_id(bl)); +} + +/******************************************************************************* + * Functions related to CMPI + ******************************************************************************/ + void create_instance_id(const char *class_name, const char *id, char *instance_id, const unsigned instance_id_len) { diff --git a/src/software-dbus/sw-utils.h b/src/software-dbus/sw-utils.h index f479824..b8b7598 100644 --- a/src/software-dbus/sw-utils.h +++ b/src/software-dbus/sw-utils.h @@ -45,6 +45,9 @@ const char *provider_name; const ConfigEntry *provider_config_defaults; +/******************************************************************************* + * SwPackage & related functions + ******************************************************************************/ /* Software Package */ typedef struct _SwPackage { char *name; /* Package name w/o anything else */ @@ -103,12 +106,22 @@ void sw_pkg_get_version_str(const SwPackage *pkg, char *ver_str, void sw_pkg_get_element_name(const SwPackage *pkg, char *elem_name, const unsigned elem_name_len); +/******************************************************************************* + * Functions related to single PkPackage + ******************************************************************************/ /* - * Check with PackageKit whether the element name is valid. - * @param elem_name element name - * @return 1 if element name is valid, 0 otherwise + * Get PkPackage according to the SwPackage. + * @param sw_pkg SwPackage + * @param pk_pkg PkPackage; needs to be passed to g_object_unref() when not needed + */ +void get_pk_pkg_from_sw_pkg(const SwPackage *sw_pkg, PkPackage **pk_pkg); + +/* + * Get PkDetails from PkPackage. + * @param pk_pkg PkPackage + * @param pk_det PkDetails, needs to be passed to g_object_unref() when not needed */ -short is_valid_sw_pkg_elem_name(const char *elem_name); +void get_pk_det_from_pk_pkg(PkPackage *pk_pkg, PkDetails **pk_det); /* * Create LMI_SoftwareIdentity instance from data from PackageKit. @@ -123,6 +136,9 @@ void create_instance_from_pkgkit_data(PkPackage *pk_pkg, PkDetails *pk_det, SwPackage *sw_pkg, const CMPIBroker *cb, const char *ns, LMI_SoftwareIdentity *w); +/******************************************************************************* + * Functions related to multiple PkPackages + ******************************************************************************/ /* * Enumerate SoftwareIdentity instances. This function calls KReturnInstance(), * so instances are returned directly from within the function. @@ -132,11 +148,16 @@ void create_instance_from_pkgkit_data(PkPackage *pk_pkg, PkDetails *pk_det, * @param cr CMPI Result * @param error_msg error message, if filled, problem occured * @param error_msg_len error message length + * @param names if true, this function will enumerate only SoftwareIdentityRef + * instances */ void enum_sw_identity_instances(PkBitfield filters, const CMPIBroker *cb, const char *ns, const CMPIResult* cr, char *error_msg, - const unsigned error_msg_len); + const unsigned error_msg_len, const short names); +/******************************************************************************* + * Functions related to PackageKit + ******************************************************************************/ /* * Analyze PkResults and construct error message if error occurred. * @param results from package kit call @@ -169,6 +190,9 @@ gint pk_pkg_cmp(gpointer a, gpointer b); */ gint pk_det_cmp(gpointer a, gpointer b); +/******************************************************************************* + * Functions related to CMPI + ******************************************************************************/ /* * Create standard instance ID based on class name and ID. * @param class_name -- cgit