From e46160ae6f3ea48d4de4e09dfe6fb3ed2ae943b5 Mon Sep 17 00:00:00 2001 From: Peter Schiffer Date: Fri, 18 Apr 2014 16:48:02 +0200 Subject: Software-dbus: implemented LMI_InstalledSoftwareIdentityProvider --- .../LMI_InstalledSoftwareIdentityProvider.c | 330 +++++++++++++++++---- .../LMI_MemberOfSoftwareCollectionProvider.c | 2 +- src/software-dbus/LMI_Software.h | 3 + src/software-dbus/LMI_SoftwareIdentityProvider.c | 2 +- src/software-dbus/sw-utils.c | 5 +- src/software-dbus/sw-utils.h | 4 +- 6 files changed, 291 insertions(+), 55 deletions(-) diff --git a/src/software-dbus/LMI_InstalledSoftwareIdentityProvider.c b/src/software-dbus/LMI_InstalledSoftwareIdentityProvider.c index 6f7a25d..849e8f6 100644 --- a/src/software-dbus/LMI_InstalledSoftwareIdentityProvider.c +++ b/src/software-dbus/LMI_InstalledSoftwareIdentityProvider.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 + */ + #include #include "LMI_InstalledSoftwareIdentity.h" +#include "LMI_Software.h" static const CMPIBroker* _cb; -static void LMI_InstalledSoftwareIdentityInitialize() +static void LMI_InstalledSoftwareIdentityInitialize(const CMPIContext *ctx) { + lmi_init(provider_name, _cb, ctx, provider_config_defaults); } static CMPIStatus LMI_InstalledSoftwareIdentityCleanup( @@ -15,14 +37,72 @@ static CMPIStatus LMI_InstalledSoftwareIdentityCleanup( CMReturn(CMPI_RC_OK); } +static CMPIStatus enum_instances(const CMPIResult *cr, const char *ns, + const short names) +{ + 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); + + get_pk_packages(pk_bitfield_value(PK_FILTER_ENUM_INSTALLED), &array, + error_msg, ERROR_MSG_LEN); + if (!array) { + goto done; + } + + 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, ns); + LMI_SoftwareIdentityRef_Set_InstanceID(&si, instance_id); + + LMI_InstalledSoftwareIdentity w; + LMI_InstalledSoftwareIdentity_Init(&w, _cb, ns); + LMI_InstalledSoftwareIdentity_SetObjectPath_System(&w, + lmi_get_computer_system()); + LMI_InstalledSoftwareIdentity_Set_InstalledSoftware(&w, &si); + + if (names) { + KReturnObjectPath(cr, w); + } else { + KReturnInstance(cr, w); + } + } + +done: + if (array) { + g_ptr_array_unref(array); + } + + if (*error_msg) { + KReturn2(_cb, ERR_FAILED, "%s", error_msg); + } + + CMReturn(CMPI_RC_OK); +} + static CMPIStatus LMI_InstalledSoftwareIdentityEnumInstanceNames( CMPIInstanceMI* mi, const CMPIContext* cc, const CMPIResult* cr, const CMPIObjectPath* cop) { - return KDefaultEnumerateInstanceNames( - _cb, mi, cc, cr, cop); + return enum_instances(cr, KNameSpace(cop), 1); } static CMPIStatus LMI_InstalledSoftwareIdentityEnumInstances( @@ -32,7 +112,7 @@ static CMPIStatus LMI_InstalledSoftwareIdentityEnumInstances( const CMPIObjectPath* cop, const char** properties) { - CMReturn(CMPI_RC_OK); + return enum_instances(cr, KNameSpace(cop), 0); } static CMPIStatus LMI_InstalledSoftwareIdentityGetInstance( @@ -42,8 +122,30 @@ static CMPIStatus LMI_InstalledSoftwareIdentityGetInstance( const CMPIObjectPath* cop, const char** properties) { - return KDefaultGetInstance( - _cb, mi, cc, cr, cop, properties); + PkPackage *pk_pkg = NULL; + SwPackage sw_pkg; + + init_sw_package(&sw_pkg); + + LMI_InstalledSoftwareIdentity w; + LMI_InstalledSoftwareIdentity_InitFromObjectPath(&w, _cb, cop); + + if (create_sw_package_from_elem_name(get_str_property_from_op(w.InstalledSoftware.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_bitfield_value(PK_FILTER_ENUM_INSTALLED), + &pk_pkg); + free_sw_package(&sw_pkg); + if (!pk_pkg) { + CMReturn(CMPI_RC_ERR_NOT_FOUND); + } + g_object_unref(pk_pkg); + + KReturnInstance(cr, w); + CMReturn(CMPI_RC_OK); } static CMPIStatus LMI_InstalledSoftwareIdentityCreateInstance( @@ -95,6 +197,100 @@ static CMPIStatus LMI_InstalledSoftwareIdentityAssociationCleanup( CMReturn(CMPI_RC_OK); } +static CMPIStatus associators( + 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; + char error_msg[ERROR_MSG_LEN] = ""; + const char *computer_system_name; + + computer_system_name = get_str_property_from_op(lmi_get_computer_system(), + "CreationClassName"); + + if (!cm_class_is_a(_cb, KNameSpace(cop), assocClass, + ORGID "_" INST_SW_IDENTITY_CLASS_NAME)) { + goto done; + } + + if (CMClassPathIsA(_cb, cop, computer_system_name, &st)) { + /* got PG_ComputerSystem - System */ + if (cm_class_is_a(_cb, KNameSpace(cop), resultClass, + ORGID "_" SW_IDENTITY_CLASS_NAME) != 0) { + goto done; + } + if (role && strcmp(role, SYSTEM_ATTR) != 0) { + goto done; + } + if (resultRole && strcmp(resultRole, INST_SW_ATTR) != 0) { + goto done; + } + + if (names) { + enum_sw_identity_instance_names( + pk_bitfield_value(PK_FILTER_ENUM_INSTALLED), _cb, + KNameSpace(cop), cr, error_msg, ERROR_MSG_LEN); + } else { + enum_sw_identity_instances( + pk_bitfield_value(PK_FILTER_ENUM_INSTALLED), _cb, + KNameSpace(cop), cr, error_msg, ERROR_MSG_LEN); + } + } else if (CMClassPathIsA(_cb, cop, ORGID "_" SW_IDENTITY_CLASS_NAME, &st)) { + /* got SoftwareIdentity - InstalledSoftware */ + PkPackage *pk_pkg = NULL; + SwPackage sw_pkg; + + init_sw_package(&sw_pkg); + + if (cm_class_is_a(_cb, KNameSpace(cop), resultClass, + computer_system_name) != 0) { + goto done; + } + if (role && strcmp(role, INST_SW_ATTR) != 0) { + goto done; + } + if (resultRole && strcmp(resultRole, SYSTEM_ATTR) != 0) { + goto done; + } + + /* Is this SwIdentity installed? */ + 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; + } + get_pk_pkg_from_sw_pkg(&sw_pkg, pk_bitfield_value(PK_FILTER_ENUM_INSTALLED), + &pk_pkg); + free_sw_package(&sw_pkg); + if (!pk_pkg) { + goto done; + } + g_object_unref(pk_pkg); + + if (names) { + CMReturnObjectPath(cr, lmi_get_computer_system()); + } else { + CMPIObjectPath *o = lmi_get_computer_system(); + CMPIInstance *ci = _cb->bft->getInstance(_cb, cc, o, properties, &st); + CMReturnInstance(cr, ci); + } + } + +done: + if (*error_msg) { + KReturn2(_cb, ERR_FAILED, "%s", error_msg); + } + + CMReturn(CMPI_RC_OK); +} + static CMPIStatus LMI_InstalledSoftwareIdentityAssociators( CMPIAssociationMI* mi, const CMPIContext* cc, @@ -106,18 +302,8 @@ static CMPIStatus LMI_InstalledSoftwareIdentityAssociators( const char* resultRole, const char** properties) { - return KDefaultAssociators( - _cb, - mi, - cc, - cr, - cop, - LMI_InstalledSoftwareIdentity_ClassName, - assocClass, - resultClass, - role, - resultRole, - properties); + return associators(cc, cr, cop, assocClass, resultClass, role, + resultRole, properties, 0); } static CMPIStatus LMI_InstalledSoftwareIdentityAssociatorNames( @@ -130,17 +316,78 @@ static CMPIStatus LMI_InstalledSoftwareIdentityAssociatorNames( const char* role, const char* resultRole) { - return KDefaultAssociatorNames( - _cb, - mi, - cc, - cr, - cop, - LMI_InstalledSoftwareIdentity_ClassName, - assocClass, - resultClass, - role, - resultRole); + return associators(cc, cr, cop, assocClass, resultClass, role, + resultRole, NULL, 1); +} + +static CMPIStatus references( + const CMPIResult* cr, + const CMPIObjectPath* cop, + const char* assocClass, + const char* role, + const short names) +{ + CMPIStatus st; + const char *computer_system_name; + + computer_system_name = get_str_property_from_op(lmi_get_computer_system(), + "CreationClassName"); + + if (!cm_class_is_a(_cb, KNameSpace(cop), assocClass, + ORGID "_" INST_SW_IDENTITY_CLASS_NAME)) { + goto done; + } + + if (CMClassPathIsA(_cb, cop, computer_system_name, &st)) { + /* got PG_ComputerSystem - System */ + if (role && strcmp(role, SYSTEM_ATTR) != 0) { + goto done; + } + + return enum_instances(cr, KNameSpace(cop), names); + } else if (CMClassPathIsA(_cb, cop, ORGID "_" SW_IDENTITY_CLASS_NAME, &st)) { + /* got SoftwareIdentity - InstalledSoftware */ + PkPackage *pk_pkg = NULL; + SwPackage sw_pkg; + + init_sw_package(&sw_pkg); + + if (role && strcmp(role, INST_SW_ATTR) != 0) { + goto done; + } + + /* Is this SwIdentity installed? */ + 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; + } + get_pk_pkg_from_sw_pkg(&sw_pkg, pk_bitfield_value(PK_FILTER_ENUM_INSTALLED), + &pk_pkg); + free_sw_package(&sw_pkg); + if (!pk_pkg) { + goto done; + } + g_object_unref(pk_pkg); + + LMI_SoftwareIdentityRef si; + LMI_SoftwareIdentityRef_InitFromObjectPath(&si, _cb, cop); + + LMI_InstalledSoftwareIdentity w; + LMI_InstalledSoftwareIdentity_Init(&w, _cb, KNameSpace(cop)); + LMI_InstalledSoftwareIdentity_SetObjectPath_System(&w, + lmi_get_computer_system()); + LMI_InstalledSoftwareIdentity_Set_InstalledSoftware(&w, &si); + + if (names) { + KReturnObjectPath(cr, w); + } else { + KReturnInstance(cr, w); + } + } + +done: + CMReturn(CMPI_RC_OK); } static CMPIStatus LMI_InstalledSoftwareIdentityReferences( @@ -152,16 +399,7 @@ static CMPIStatus LMI_InstalledSoftwareIdentityReferences( const char* role, const char** properties) { - return KDefaultReferences( - _cb, - mi, - cc, - cr, - cop, - LMI_InstalledSoftwareIdentity_ClassName, - assocClass, - role, - properties); + return references(cr, cop, assocClass, role, 0); } static CMPIStatus LMI_InstalledSoftwareIdentityReferenceNames( @@ -172,28 +410,20 @@ static CMPIStatus LMI_InstalledSoftwareIdentityReferenceNames( const char* assocClass, const char* role) { - return KDefaultReferenceNames( - _cb, - mi, - cc, - cr, - cop, - LMI_InstalledSoftwareIdentity_ClassName, - assocClass, - role); + return references(cr, cop, assocClass, role, 1); } CMInstanceMIStub( LMI_InstalledSoftwareIdentity, LMI_InstalledSoftwareIdentity, _cb, - LMI_InstalledSoftwareIdentityInitialize()) + LMI_InstalledSoftwareIdentityInitialize(ctx)) CMAssociationMIStub( LMI_InstalledSoftwareIdentity, LMI_InstalledSoftwareIdentity, _cb, - LMI_InstalledSoftwareIdentityInitialize()) + LMI_InstalledSoftwareIdentityInitialize(ctx)) KONKRET_REGISTRATION( "root/cimv2", diff --git a/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c b/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c index db4e06a..ae36daa 100644 --- a/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c +++ b/src/software-dbus/LMI_MemberOfSoftwareCollectionProvider.c @@ -150,7 +150,7 @@ static CMPIStatus LMI_MemberOfSoftwareCollectionGetInstance( CMReturn(CMPI_RC_ERR_NOT_FOUND); } - get_pk_pkg_from_sw_pkg(&sw_pkg, &pk_pkg); + get_pk_pkg_from_sw_pkg(&sw_pkg, 0, &pk_pkg); free_sw_package(&sw_pkg); if (!pk_pkg) { CMReturn(CMPI_RC_ERR_NOT_FOUND); diff --git a/src/software-dbus/LMI_Software.h b/src/software-dbus/LMI_Software.h index be17297..d059f45 100644 --- a/src/software-dbus/LMI_Software.h +++ b/src/software-dbus/LMI_Software.h @@ -26,8 +26,11 @@ #define SYSTEM_SW_COLLECTION_CLASS_NAME "SystemSoftwareCollection" #define SW_IDENTITY_RESOURCE_CLASS_NAME "SoftwareIdentityResource" #define MEM_SW_COLL_CLASS_NAME "MemberOfSoftwareCollection" +#define INST_SW_IDENTITY_CLASS_NAME "InstalledSoftwareIdentity" #define COLLECTION_ATTR "Collection" #define MEMBER_ATTR "Member" +#define SYSTEM_ATTR "System" +#define INST_SW_ATTR "InstalledSoftware" #endif /* LMI_SOFTWARE_H_ */ diff --git a/src/software-dbus/LMI_SoftwareIdentityProvider.c b/src/software-dbus/LMI_SoftwareIdentityProvider.c index 6d2f065..be22497 100644 --- a/src/software-dbus/LMI_SoftwareIdentityProvider.c +++ b/src/software-dbus/LMI_SoftwareIdentityProvider.c @@ -86,7 +86,7 @@ static CMPIStatus LMI_SoftwareIdentityGetInstance( goto done; } - get_pk_pkg_from_sw_pkg(&sw_pkg, &pk_pkg); + get_pk_pkg_from_sw_pkg(&sw_pkg, 0, &pk_pkg); if (!pk_pkg) { goto done; } diff --git a/src/software-dbus/sw-utils.c b/src/software-dbus/sw-utils.c index 7101e00..d06bf03 100644 --- a/src/software-dbus/sw-utils.c +++ b/src/software-dbus/sw-utils.c @@ -209,7 +209,8 @@ void sw_pkg_get_element_name(const SwPackage *pkg, char *elem_name, * Functions related to single PkPackage ******************************************************************************/ -void get_pk_pkg_from_sw_pkg(const SwPackage *sw_pkg, PkPackage **pk_pkg) +void get_pk_pkg_from_sw_pkg(const SwPackage *sw_pkg, PkBitfield filters, + PkPackage **pk_pkg) { PkTask *task = NULL; PkPackage *item = NULL; @@ -225,7 +226,7 @@ void get_pk_pkg_from_sw_pkg(const SwPackage *sw_pkg, PkPackage **pk_pkg) values = g_new0(gchar*, 2); values[0] = g_strdup(sw_pkg->name); - results = pk_task_search_names_sync(task, 0, values, NULL, NULL, NULL, &gerror); + results = pk_task_search_names_sync(task, filters, values, NULL, NULL, NULL, &gerror); if (check_and_create_error_msg(results, gerror, "Resolving package failed", error_msg, ERROR_MSG_LEN)) { warn(error_msg); diff --git a/src/software-dbus/sw-utils.h b/src/software-dbus/sw-utils.h index b43a068..1cb7593 100644 --- a/src/software-dbus/sw-utils.h +++ b/src/software-dbus/sw-utils.h @@ -112,9 +112,11 @@ void sw_pkg_get_element_name(const SwPackage *pkg, char *elem_name, /* * Get PkPackage according to the SwPackage. * @param sw_pkg SwPackage + * @param filters * @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); +void get_pk_pkg_from_sw_pkg(const SwPackage *sw_pkg, PkBitfield filters, + PkPackage **pk_pkg); /* * Get PkDetails from PkPackage. -- cgit