diff options
Diffstat (limited to 'src/software-dbus/sw-utils.c')
-rw-r--r-- | src/software-dbus/sw-utils.c | 274 |
1 files changed, 274 insertions, 0 deletions
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; +} |