diff options
-rw-r--r-- | src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c | 413 | ||||
-rw-r--r-- | src/software-dbus/LMI_Software.h | 10 | ||||
-rw-r--r-- | src/software-dbus/LMI_SoftwareIdentityProvider.c | 58 | ||||
-rw-r--r-- | src/software-dbus/sw-utils.c | 274 | ||||
-rw-r--r-- | src/software-dbus/sw-utils.h | 85 |
5 files changed, 750 insertions, 90 deletions
diff --git a/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c b/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c index dcae791..bbd04bc 100644 --- a/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c +++ b/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c @@ -1,10 +1,32 @@ +/* + * Copyright (C) 2013-2014 Red Hat, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: Peter Schiffer <pschiffe@redhat.com> + */ + #include <konkret/konkret.h> #include "LMI_MemberOfSoftwareCollection.h" +#include "LMI_Software.h" static const CMPIBroker* _cb; -static void LMI_MemberOfSoftwareCollectionInitialize() +static void LMI_MemberOfSoftwareCollectionInitialize(const CMPIContext *ctx) { + lmi_init(provider_name, _cb, ctx, provider_config_defaults); } static CMPIStatus LMI_MemberOfSoftwareCollectionCleanup( @@ -32,6 +54,75 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionEnumInstances( const CMPIObjectPath* cop, const char** properties) { + 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] = ""; + + create_instance_id(SYSTEM_SW_COLLECTION_CLASS_NAME, NULL, instance_id, + INSTANCE_ID_LEN); + + LMI_SystemSoftwareCollectionRef ssc; + LMI_SystemSoftwareCollectionRef_Init(&ssc, _cb, KNameSpace(cop)); + LMI_SystemSoftwareCollectionRef_Set_InstanceID(&ssc, instance_id); + + 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 si; + LMI_SoftwareIdentityRef_Init(&si, _cb, KNameSpace(cop)); + LMI_SoftwareIdentityRef_Set_InstanceID(&si, instance_id); + + LMI_MemberOfSoftwareCollection w; + LMI_MemberOfSoftwareCollection_Init(&w, _cb, KNameSpace(cop)); + LMI_MemberOfSoftwareCollection_Set_Collection(&w, &ssc); + LMI_MemberOfSoftwareCollection_Set_Member(&w, &si); + KReturnInstance(cr, w); + } + +done: + g_clear_error(&gerror); + + if (task) { + g_object_unref(task); + } + if (results) { + g_object_unref(results); + } + if (array) { + g_ptr_array_unref(array); + } + + if (*error_msg) { + KReturn2(_cb, ERR_FAILED, "%s", error_msg); + } + CMReturn(CMPI_RC_OK); } @@ -42,8 +133,26 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionGetInstance( const CMPIObjectPath* cop, const char** properties) { - return KDefaultGetInstance( - _cb, mi, cc, cr, cop, properties); + char instance_id[INSTANCE_ID_LEN] = ""; + + create_instance_id(SYSTEM_SW_COLLECTION_CLASS_NAME, NULL, instance_id, + INSTANCE_ID_LEN); + + LMI_MemberOfSoftwareCollection w; + LMI_MemberOfSoftwareCollection_InitFromObjectPath(&w, _cb, cop); + + if (strcmp(get_str_property_from_op(w.Collection.value, "InstanceID"), + instance_id) != 0) { + 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 ":"))) { + CMReturn(CMPI_RC_ERR_NOT_FOUND); + } + + KReturnInstance(cr, w); + CMReturn(CMPI_RC_OK); } static CMPIStatus LMI_MemberOfSoftwareCollectionCreateInstance( @@ -95,6 +204,132 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionAssociationCleanup( CMReturn(CMPI_RC_OK); } +static CMPIStatus associators( + CMPIAssociationMI* mi, + const CMPIContext* cc, + const CMPIResult* cr, + const CMPIObjectPath* cop, + const char* assocClass, + const char* resultClass, + const char* role, + const char* resultRole, + const char** properties, + const short names) +{ + CMPIStatus st; + CMPIInstance *ci; + CMPIObjectPath *o; + 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] = ""; + + if (!cm_class_is_a(_cb, KNameSpace(cop), assocClass, + ORGID "_" MEM_SW_COLL_CLASS_NAME)) { + goto done; + } + + if (CMClassPathIsA(_cb, cop, ORGID "_" SYSTEM_SW_COLLECTION_CLASS_NAME, &st)) { + /* got SystemSoftwareCollection - Collection; + * where listing only associators names is supported */ + if (!names) { + CMReturn(CMPI_RC_ERR_NOT_SUPPORTED); + } + + if (cm_class_is_a(_cb, KNameSpace(cop), resultClass, + ORGID "_" SW_IDENTITY_CLASS_NAME) != 0) { + goto done; + } + if (role && strcmp(role, COLLECTION_ATTR) != 0) { + goto done; + } + if (resultRole && strcmp(resultRole, MEMBER_ATTR) != 0) { + goto done; + } + + 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 si; + LMI_SoftwareIdentityRef_Init(&si, _cb, KNameSpace(cop)); + LMI_SoftwareIdentityRef_Set_InstanceID(&si, instance_id); + o = LMI_SoftwareIdentityRef_ToObjectPath(&si, &st); + CMReturnObjectPath(cr, o); + } + } else if (CMClassPathIsA(_cb, cop, ORGID "_" SW_IDENTITY_CLASS_NAME, &st)) { + /* got SoftwareIdentity - Member */ + if (cm_class_is_a(_cb, KNameSpace(cop), resultClass, + ORGID "_" SYSTEM_SW_COLLECTION_CLASS_NAME) != 0) { + goto done; + } + if (role && strcmp(role, MEMBER_ATTR) != 0) { + goto done; + } + if (resultRole && strcmp(resultRole, COLLECTION_ATTR) != 0) { + goto done; + } + + create_instance_id(SYSTEM_SW_COLLECTION_CLASS_NAME, NULL, instance_id, + INSTANCE_ID_LEN); + + LMI_SystemSoftwareCollectionRef ssc; + LMI_SystemSoftwareCollectionRef_Init(&ssc, _cb, KNameSpace(cop)); + LMI_SystemSoftwareCollectionRef_Set_InstanceID(&ssc, instance_id); + o = LMI_SystemSoftwareCollectionRef_ToObjectPath(&ssc, &st); + if (names) { + CMReturnObjectPath(cr, o); + } else { + ci = _cb->bft->getInstance(_cb, cc, o, properties, &st); + CMReturnInstance(cr, ci); + } + } + +done: + g_clear_error(&gerror); + + if (task) { + g_object_unref(task); + } + if (results) { + g_object_unref(results); + } + if (array) { + g_ptr_array_unref(array); + } + + if (*error_msg) { + KReturn2(_cb, ERR_FAILED, "%s", error_msg); + } + + CMReturn(CMPI_RC_OK); +} + static CMPIStatus LMI_MemberOfSoftwareCollectionAssociators( CMPIAssociationMI* mi, const CMPIContext* cc, @@ -106,18 +341,8 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionAssociators( const char* resultRole, const char** properties) { - return KDefaultAssociators( - _cb, - mi, - cc, - cr, - cop, - LMI_MemberOfSoftwareCollection_ClassName, - assocClass, - resultClass, - role, - resultRole, - properties); + return associators(mi, cc, cr, cop, assocClass, resultClass, role, + resultRole, properties, 0); } static CMPIStatus LMI_MemberOfSoftwareCollectionAssociatorNames( @@ -130,17 +355,128 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionAssociatorNames( const char* role, const char* resultRole) { - return KDefaultAssociatorNames( - _cb, - mi, - cc, - cr, - cop, - LMI_MemberOfSoftwareCollection_ClassName, - assocClass, - resultClass, - role, - resultRole); + return associators(mi, cc, cr, cop, assocClass, resultClass, role, + resultRole, NULL, 1); +} + +static CMPIStatus references( + CMPIAssociationMI* mi, + const CMPIContext* cc, + const CMPIResult* cr, + const CMPIObjectPath* cop, + const char* assocClass, + const char* role, + const char** properties, + const short names) +{ + CMPIStatus st; + CMPIObjectPath *o; + 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] = ""; + + if (!cm_class_is_a(_cb, KNameSpace(cop), assocClass, + ORGID "_" MEM_SW_COLL_CLASS_NAME)) { + goto done; + } + + create_instance_id(SYSTEM_SW_COLLECTION_CLASS_NAME, NULL, instance_id, + INSTANCE_ID_LEN); + + LMI_SystemSoftwareCollectionRef ssc; + LMI_SystemSoftwareCollectionRef_Init(&ssc, _cb, KNameSpace(cop)); + LMI_SystemSoftwareCollectionRef_Set_InstanceID(&ssc, instance_id); + + if (CMClassPathIsA(_cb, cop, ORGID "_" SYSTEM_SW_COLLECTION_CLASS_NAME, &st)) { + /* got SystemSoftwareCollection - Collection */ + if (role && strcmp(role, COLLECTION_ATTR) != 0) { + goto done; + } + + 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 si; + LMI_SoftwareIdentityRef_Init(&si, _cb, KNameSpace(cop)); + LMI_SoftwareIdentityRef_Set_InstanceID(&si, instance_id); + + LMI_MemberOfSoftwareCollection w; + LMI_MemberOfSoftwareCollection_Init(&w, _cb, KNameSpace(cop)); + LMI_MemberOfSoftwareCollection_Set_Collection(&w, &ssc); + LMI_MemberOfSoftwareCollection_Set_Member(&w, &si); + if (names) { + o = LMI_MemberOfSoftwareCollection_ToObjectPath(&w, &st); + CMReturnObjectPath(cr, o); + } else { + KReturnInstance(cr, w); + } + } + } else if (CMClassPathIsA(_cb, cop, ORGID "_" SW_IDENTITY_CLASS_NAME, &st)) { + /* got SoftwareIdentity - Member */ + if (role && strcmp(role, MEMBER_ATTR) != 0) { + goto done; + } + + LMI_SoftwareIdentityRef si; + LMI_SoftwareIdentityRef_InitFromObjectPath(&si, _cb, cop); + + LMI_MemberOfSoftwareCollection w; + LMI_MemberOfSoftwareCollection_Init(&w, _cb, KNameSpace(cop)); + LMI_MemberOfSoftwareCollection_Set_Collection(&w, &ssc); + LMI_MemberOfSoftwareCollection_Set_Member(&w, &si); + if (names) { + o = LMI_MemberOfSoftwareCollection_ToObjectPath(&w, &st); + CMReturnObjectPath(cr, o); + } else { + KReturnInstance(cr, w); + } + } + +done: + g_clear_error(&gerror); + + if (task) { + g_object_unref(task); + } + if (results) { + g_object_unref(results); + } + if (array) { + g_ptr_array_unref(array); + } + + if (*error_msg) { + KReturn2(_cb, ERR_FAILED, "%s", error_msg); + } + + CMReturn(CMPI_RC_OK); } static CMPIStatus LMI_MemberOfSoftwareCollectionReferences( @@ -152,16 +488,7 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionReferences( const char* role, const char** properties) { - return KDefaultReferences( - _cb, - mi, - cc, - cr, - cop, - LMI_MemberOfSoftwareCollection_ClassName, - assocClass, - role, - properties); + return references(mi, cc, cr, cop, assocClass, role, properties, 0); } static CMPIStatus LMI_MemberOfSoftwareCollectionReferenceNames( @@ -172,28 +499,20 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionReferenceNames( const char* assocClass, const char* role) { - return KDefaultReferenceNames( - _cb, - mi, - cc, - cr, - cop, - LMI_MemberOfSoftwareCollection_ClassName, - assocClass, - role); + return references(mi, cc, cr, cop, assocClass, role, NULL, 1); } CMInstanceMIStub( LMI_MemberOfSoftwareCollection, LMI_MemberOfSoftwareCollection, _cb, - LMI_MemberOfSoftwareCollectionInitialize()) + LMI_MemberOfSoftwareCollectionInitialize(ctx)) CMAssociationMIStub( LMI_MemberOfSoftwareCollection, LMI_MemberOfSoftwareCollection, _cb, - LMI_MemberOfSoftwareCollectionInitialize()) + LMI_MemberOfSoftwareCollectionInitialize(ctx)) KONKRET_REGISTRATION( "root/cimv2", diff --git a/src/software-dbus/LMI_Software.h b/src/software-dbus/LMI_Software.h index 6e434f9..be17297 100644 --- a/src/software-dbus/LMI_Software.h +++ b/src/software-dbus/LMI_Software.h @@ -23,13 +23,11 @@ #include "sw-utils.h" -#define VER_STR_LEN 256 -#define ELEM_NAME_LEN 256 -#define ERROR_MSG_LEN 512 -#define INSTANCE_ID_LEN 282 - -#define SW_IDENTITY_CLASS_NAME "SoftwareIdentity" #define SYSTEM_SW_COLLECTION_CLASS_NAME "SystemSoftwareCollection" #define SW_IDENTITY_RESOURCE_CLASS_NAME "SoftwareIdentityResource" +#define MEM_SW_COLL_CLASS_NAME "MemberOfSoftwareCollection" + +#define COLLECTION_ATTR "Collection" +#define MEMBER_ATTR "Member" #endif /* LMI_SOFTWARE_H_ */ diff --git a/src/software-dbus/LMI_SoftwareIdentityProvider.c b/src/software-dbus/LMI_SoftwareIdentityProvider.c index 09fbd93..c0052d1 100644 --- a/src/software-dbus/LMI_SoftwareIdentityProvider.c +++ b/src/software-dbus/LMI_SoftwareIdentityProvider.c @@ -63,6 +63,7 @@ static CMPIStatus LMI_SoftwareIdentityEnumInstanceNames( } 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) { @@ -119,29 +120,26 @@ static CMPIStatus LMI_SoftwareIdentityGetInstance( const CMPIObjectPath* cop, 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; - GPtrArray *array = NULL; gchar **values = NULL; SwPackage sw_pkg; unsigned i, j; - short found = 0; - char error_msg[ERROR_MSG_LEN] = "", elem_name[ELEM_NAME_LEN] = "", - ver_str[VER_STR_LEN] = ""; + short found = 0, found2 = 0; + char error_msg[ERROR_MSG_LEN] = ""; init_sw_package(&sw_pkg); - LMI_SoftwareIdentity w; - LMI_SoftwareIdentity_InitFromObjectPath(&w, _cb, cop); - if (create_sw_package_from_elem_name( - w.InstanceID.chars + strlen( + 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", - w.InstanceID.chars); + get_str_property_from_op(cop, "InstanceID")); goto done; } @@ -167,52 +165,35 @@ static CMPIStatus LMI_SoftwareIdentityGetInstance( break; } } - if (!found) { goto done; } - sw_pkg_get_element_name(&sw_pkg, elem_name, ELEM_NAME_LEN); - sw_pkg_get_version_str(&sw_pkg, ver_str, VER_STR_LEN); - - LMI_SoftwareIdentity_Init_Classifications(&w, 1); - LMI_SoftwareIdentity_Set_Classifications(&w, 0, 0); - LMI_SoftwareIdentity_Init_TargetTypes(&w, 2); - LMI_SoftwareIdentity_Set_TargetTypes(&w, 0, "rpm"); - LMI_SoftwareIdentity_Set_TargetTypes(&w, 1, "yum"); - LMI_SoftwareIdentity_Set_IsEntity(&w, 1); - - LMI_SoftwareIdentity_Set_Architecture(&w, sw_pkg.arch); - LMI_SoftwareIdentity_Set_ElementName(&w, elem_name); - LMI_SoftwareIdentity_Set_Epoch(&w, atoi(sw_pkg.epoch)); - LMI_SoftwareIdentity_Set_Name(&w, sw_pkg.name); - LMI_SoftwareIdentity_Set_Release(&w, sw_pkg.release); - LMI_SoftwareIdentity_Set_Version(&w, sw_pkg.version); - LMI_SoftwareIdentity_Set_VersionString(&w, ver_str); - LMI_SoftwareIdentity_Set_Caption(&w, pk_package_get_summary(pk_pkg)); - 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); + 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 { - g_ptr_array_unref(array); - array = pk_results_get_details_array(results2); - for (j = 0; j < array->len; j++) { - pk_det = g_ptr_array_index(array, j); + 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) { - LMI_SoftwareIdentity_Set_Description(&w, - pk_details_get_description(pk_det)); + found2 = 1; break; } } } + if (!found2) { + pk_det = NULL; + } + + create_instance_from_pkgkit_data(pk_pkg, pk_det, &sw_pkg, _cb, + KNameSpace(cop), &w); KReturnInstance(cr, w); @@ -234,6 +215,9 @@ done: if (array) { g_ptr_array_unref(array); } + if (array2) { + g_ptr_array_unref(array2); + } if (*error_msg) { KReturn2(_cb, ERR_FAILED, "%s", error_msg); diff --git a/src/software-dbus/sw-utils.c b/src/software-dbus/sw-utils.c index 3cfa175..156c5ae 100644 --- a/src/software-dbus/sw-utils.c +++ b/src/software-dbus/sw-utils.c @@ -201,6 +201,245 @@ 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) +{ + PkTask *task = NULL; + PkPackage *pk_pkg = 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[1] = NULL; + + results = pk_task_resolve_sync(task, 0, values, NULL, NULL, NULL, &gerror); + if (check_and_create_error_msg(results, gerror, "Resolving package failed", + error_msg, ERROR_MSG_LEN)) { + warn(error_msg); + 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) { + ret = 1; + break; + } + } + +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 (array) { + g_ptr_array_unref(array); + } + + return ret; +} + +void create_instance_from_pkgkit_data(PkPackage *pk_pkg, PkDetails *pk_det, + SwPackage *sw_pkg, const CMPIBroker *cb, const char *ns, + LMI_SoftwareIdentity *w) +{ + const gchar *summary, *desc = NULL; + char elem_name[ELEM_NAME_LEN] = "", ver_str[VER_STR_LEN] = "", + instance_id[INSTANCE_ID_LEN] = ""; + + summary = pk_package_get_summary(pk_pkg); + if (pk_det) { + desc = pk_details_get_description(pk_det); + } + + sw_pkg_get_element_name(sw_pkg, elem_name, ELEM_NAME_LEN); + sw_pkg_get_version_str(sw_pkg, ver_str, VER_STR_LEN); + + create_instance_id(SW_IDENTITY_CLASS_NAME, elem_name, instance_id, + INSTANCE_ID_LEN); + + LMI_SoftwareIdentity_Init(w, cb, ns); + LMI_SoftwareIdentity_Set_InstanceID(w, instance_id); + + LMI_SoftwareIdentity_Init_Classifications(w, 1); + LMI_SoftwareIdentity_Set_Classifications(w, 0, 0); + LMI_SoftwareIdentity_Init_TargetTypes(w, 2); + LMI_SoftwareIdentity_Set_TargetTypes(w, 0, "rpm"); + LMI_SoftwareIdentity_Set_TargetTypes(w, 1, "yum"); + LMI_SoftwareIdentity_Set_IsEntity(w, 1); + + LMI_SoftwareIdentity_Set_Architecture(w, sw_pkg->arch); + LMI_SoftwareIdentity_Set_ElementName(w, elem_name); + LMI_SoftwareIdentity_Set_Epoch(w, atoi(sw_pkg->epoch)); + LMI_SoftwareIdentity_Set_Name(w, sw_pkg->name); + LMI_SoftwareIdentity_Set_Release(w, sw_pkg->release); + LMI_SoftwareIdentity_Set_Version(w, sw_pkg->version); + LMI_SoftwareIdentity_Set_VersionString(w, ver_str); + + if (summary) { + LMI_SoftwareIdentity_Set_Caption(w, summary); + } + if (desc) { + LMI_SoftwareIdentity_Set_Description(w, desc); + } + + return; +} + +void enum_sw_identity_instances(PkBitfield filters, const CMPIBroker *cb, + const char *ns, const CMPIResult* cr, char *error_msg, + const unsigned error_msg_len) +{ + LMI_SoftwareIdentity w; + PkTask *task = NULL; + PkPackage *pk_pkg = NULL; + PkDetails *pk_det = NULL; + PkResults *results = NULL, *results2 = NULL; + GError *gerror = NULL; + GPtrArray *array = NULL, *array2 = NULL; + gchar **values = NULL; + SwPackage sw_pkg; + int cmpres; + unsigned i, j, a2i; + + init_sw_package(&sw_pkg); + + task = pk_task_new(); + + results = pk_task_get_packages_sync(task, filters, 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 / PK_DETAILS_LIMIT + 1; i++) { + values = g_new0(gchar*, PK_DETAILS_LIMIT + 1); + values[PK_DETAILS_LIMIT] = NULL; + + for (j = i * PK_DETAILS_LIMIT; + j < (i + 1) * PK_DETAILS_LIMIT && j < array->len; j++) { + values[j - i * PK_DETAILS_LIMIT] = g_strdup(pk_package_get_id( + g_ptr_array_index(array, j))); + } + + 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); + g_ptr_array_sort(array2, (GCompareFunc) pk_det_cmp); + } + + g_strfreev(values); + values = NULL; + + a2i = 0; + for (j = i * PK_DETAILS_LIMIT; + j < (i + 1) * PK_DETAILS_LIMIT && j < array->len; j++) { + pk_pkg = g_ptr_array_index(array, j); + if (create_sw_package_from_pk_pkg(pk_pkg, &sw_pkg) != 0) { + continue; + } + + cmpres = -1; + if (array2) { + while (cmpres < 0 && a2i < array2->len) { + pk_det = g_ptr_array_index(array2, a2i); + cmpres = strcmp(pk_details_get_package_id(pk_det), + pk_package_get_id(pk_pkg)); + if (cmpres < 0) { + /* this should not happen - + * we have spare unmatched package details */ + a2i++; + } else if (cmpres > 0) { + /* no matching package details for current pk_pkg */ + pk_det = NULL; + } else { + /* found a match */ + a2i++; + } + } + } + + create_instance_from_pkgkit_data(pk_pkg, pk_det, &sw_pkg, cb, ns, &w); + KReturnInstance(cr, w); + + free_sw_package(&sw_pkg); + } + + if (results2) { + g_object_unref(results2); + results2 = NULL; + } + if (array2) { + g_ptr_array_unref(array2); + array2 = NULL; + } + } + +done: + g_clear_error(&gerror); + + if (task) { + g_object_unref(task); + } + if (results) { + g_object_unref(results); + } + if (array) { + g_ptr_array_unref(array); + } + + 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)); +} + short check_and_create_error_msg(PkResults *results, GError *gerror, const char *custom_msg, char *error_msg, const unsigned error_msg_len) { @@ -248,3 +487,38 @@ void create_instance_id(const char *class_name, const char *id, class_name); } } + +const char *get_str_property_from_op(const CMPIObjectPath *o, const char *prop) +{ + CMPIData d; + d = CMGetKey(o, prop, NULL); + return KChars(d.value.string); +} + +short cm_class_is_a(const CMPIBroker *cb, const char *ns, const char *cm_class, + const char *type) +{ + CMPIStatus st; + CMPIObjectPath *o = NULL; + short ret = 0; + + o = CMNewObjectPath(cb, ns, cm_class, &st); + if (!o) { + goto done; + } else if (st.rc != CMPI_RC_OK) { + goto done; + } + + if (type && !CMClassPathIsA(cb, o, type, &st)) { + goto done; + } + + ret = 1; + +done: + if (o) { + CMRelease(o); + } + + return ret; +} diff --git a/src/software-dbus/sw-utils.h b/src/software-dbus/sw-utils.h index 0735259..f479824 100644 --- a/src/software-dbus/sw-utils.h +++ b/src/software-dbus/sw-utils.h @@ -23,13 +23,25 @@ #define I_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE +#define VER_STR_LEN 256 +#define ELEM_NAME_LEN 256 +#define INSTANCE_ID_LEN 282 +#define ERROR_MSG_LEN 512 + +#define PK_DETAILS_LIMIT 4999 + +#define SW_IDENTITY_CLASS_NAME "SoftwareIdentity" + #include <stdio.h> #include <stdlib.h> #include <string.h> #include <packagekit-glib2/packagekit.h> +#include <konkret/konkret.h> #include "globals.h" +#include "LMI_SoftwareIdentity.h" + const char *provider_name; const ConfigEntry *provider_config_defaults; @@ -92,6 +104,40 @@ void sw_pkg_get_element_name(const SwPackage *pkg, char *elem_name, const unsigned elem_name_len); /* + * Check with PackageKit whether the element name is valid. + * @param elem_name element name + * @return 1 if element name is valid, 0 otherwise + */ +short is_valid_sw_pkg_elem_name(const char *elem_name); + +/* + * Create LMI_SoftwareIdentity instance from data from PackageKit. + * @param pk_pkg PkPackage + * @param pk_det PkDetials; can be NULL + * @param sw_pkg SwPackage + * @param cb CMPI Broker + * @param ns CMPI namespace + * @param w LMI_SoftwareIdentity + */ +void create_instance_from_pkgkit_data(PkPackage *pk_pkg, PkDetails *pk_det, + SwPackage *sw_pkg, const CMPIBroker *cb, const char *ns, + LMI_SoftwareIdentity *w); + +/* + * Enumerate SoftwareIdentity instances. This function calls KReturnInstance(), + * so instances are returned directly from within the function. + * @param filters PackageKit filters + * @param cb CMPI Broker + * @param ns Namespace + * @param cr CMPI Result + * @param error_msg error message, if filled, problem occured + * @param error_msg_len error message length + */ +void enum_sw_identity_instances(PkBitfield filters, const CMPIBroker *cb, + const char *ns, const CMPIResult* cr, char *error_msg, + const unsigned error_msg_len); + +/* * Analyze PkResults and construct error message if error occurred. * @param results from package kit call * @param custom_msg message preceding error message from package kit @@ -104,6 +150,26 @@ short check_and_create_error_msg(PkResults *results, GError *gerror, const char *custom_msg, char *error_msg, const unsigned error_msg_len); /* + * Compare two PkPackage packages according to the package ID. + * @param a + * @param b + * @return negative integer if the a comes before the b, + * 0 if they are equal, + * or a positive integer if the a comes after the b + */ +gint pk_pkg_cmp(gpointer a, gpointer b); + +/* + * Compare two PkDetails according to the package ID. + * @param a + * @param b + * @return negative integer if the a comes before the b, + * 0 if they are equal, + * or a positive integer if the a comes after the b + */ +gint pk_det_cmp(gpointer a, gpointer b); + +/* * Create standard instance ID based on class name and ID. * @param class_name * @param id, may by null @@ -113,4 +179,23 @@ short check_and_create_error_msg(PkResults *results, GError *gerror, void create_instance_id(const char *class_name, const char *id, char *instance_id, const unsigned instance_id_len); +/* + * Get string property from Object Path. + * @param o Object Path + * @param prop property + * @return string value of property + */ +const char *get_str_property_from_op(const CMPIObjectPath *o, const char *prop); + +/* + * Check whether cm_class is type of type. Takes inheritance into account. + * @param cb CMPI Broker + * @param ns namespace + * @param cm_class + * @param type; can be NULL, in which case the comparison will succeed + * @return 1 if cm_class is type of type, 0 otherwise + */ +short cm_class_is_a(const CMPIBroker *cb, const char *ns, const char *cm_class, + const char *type); + #endif /* SW_UTILS_H_ */ |