/**
 * backend_get_requires:
 */
static void
backend_get_requires (PkBackend *backend, PkBitfield filters, gchar **package_ids, gboolean recursive)
{
	guint i;
	guint len;
	const gchar *package_id;
	PkPackageId *pi;

	slapt_pkg_info_t *pkg;

	slapt_pkg_list_t *installed;
	slapt_pkg_list_t *available;

	slapt_pkg_list_t *requires;

	PkInfoEnum state;
	const char *summary;

	installed = slapt_get_installed_pkgs();
	available = slapt_get_available_pkgs();

	pk_backend_set_status (backend, PK_STATUS_ENUM_QUERY);

	len = g_strv_length (package_ids);
	for (i=0; i<len; i++) {
	    package_id = package_ids[i];
	    pi = pk_package_id_new_from_string (package_id);
	    if (pi == NULL) {
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_ID_INVALID, "invalid package id");
		pk_backend_finished (backend);
		return;
	    }
	    pkg = _get_pkg_from_id(pi, available, installed);
	    pk_package_id_free (pi);
	    if (pkg == NULL) {
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_NOT_FOUND, "package not found");
		continue;
	    }

	    requires = slapt_is_required_by(_config, available, pkg);

	    for (i = 0; i < requires->pkg_count; i++) {
		pkg = requires->pkgs[i];

		state = pkg->installed ? PK_INFO_ENUM_INSTALLED : PK_INFO_ENUM_AVAILABLE;
		package_id = _get_string_from_pkg(pkg);
		summary = _get_pkg_summary(pkg);
		pk_backend_package (backend, state, package_id, summary);
		g_free((gpointer) summary);
		g_free((gpointer) package_id);
	    }

	    slapt_free_pkg_list(requires);
	}

	slapt_free_pkg_list(available);
	slapt_free_pkg_list(installed);

	pk_backend_finished (backend);
}
static slapt_pkg_info_t* _get_pkg_from_string(const gchar *package_id,
                            slapt_pkg_list_t *avail_pkgs,
                            slapt_pkg_list_t *installed_pkgs)
{
	PkPackageId *pi;
	slapt_pkg_info_t *pkg;

	pi = pk_package_id_new_from_string (package_id);
	if (pi == NULL)
	    return NULL;
	pkg = _get_pkg_from_id(pi, avail_pkgs, installed_pkgs);
	pk_package_id_free (pi);
	return pkg;
}
Example #3
0
zypp::sat::Solvable
zypp_get_package_by_id (const gchar *package_id)
{
	PkPackageId *pi;
	pi = pk_package_id_new_from_string (package_id);
	if (pi == NULL) {
		// TODO: Do we need to do something more for this error?
		return zypp::sat::Solvable::noSolvable;
	}

	std::vector<zypp::sat::Solvable> *v = zypp_get_packages_by_name (pi->name, zypp::ResKind::package, TRUE);
	std::vector<zypp::sat::Solvable> *v2 = zypp_get_packages_by_name (pi->name, zypp::ResKind::patch, TRUE);

	v->insert (v->end (), v2->begin (), v2->end ());
	
	if (v == NULL)
		return zypp::sat::Solvable::noSolvable;

	zypp::sat::Solvable package;

	for (std::vector<zypp::sat::Solvable>::iterator it = v->begin ();
			it != v->end (); it++) {
		gchar *version = g_strdup (it->edition ().c_str ());
		gchar *arch = g_strdup (it->arch ().c_str ());
		if (strcmp (pi->version, version) == 0 && strcmp (pi->arch, arch) == 0) {
			package = *it;
			break;
		}
		g_free (version);
		g_free (arch);
	}

	delete (v);
	delete (v2);
	return package;
}
/**
 * backend_remove_packages:
 */
static void
backend_remove_packages (PkBackend *backend, gchar **package_ids, gboolean allow_deps, gboolean autoremove)
{
	guint i;
	guint len;
	const gchar *package_id;
	PkPackageId *pi;
	int ret;

	slapt_pkg_list_t *installed;
	slapt_pkg_list_t *available;
	slapt_transaction_t *transaction;
	slapt_pkg_info_t *pkg;

	/* FIXME: support only_trusted */

	pk_backend_set_status (backend, PK_STATUS_ENUM_REMOVE);
	pk_backend_set_percentage (backend, 0);

	installed = slapt_get_installed_pkgs();
	available = slapt_get_available_pkgs();

	transaction = slapt_init_transaction();

	len = g_strv_length (package_ids);
	for (i=0; i<len; i++) {
	    package_id = package_ids[i];
	    pi = pk_package_id_new_from_string (package_id);
	    if (pi == NULL) {
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_ID_INVALID, "invalid package id");
		pk_backend_finished (backend);
		return;
	    }
	    pkg = _get_pkg_from_id(pi, installed, NULL);
	    pk_package_id_free (pi);
	    if (pkg == NULL) {
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_NOT_FOUND, "package not found");
		continue;
	    }

	    if (!pkg->installed) {
		char *pkgname = slapt_stringify_pkg(pkg);
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_NOT_INSTALLED, "package %s not installed", pkgname);
		free(pkgname);
		continue;
	    }

	    slapt_add_remove_to_transaction(transaction, pkg);
	}

	ret = slapt_handle_transaction(_config, transaction);
	if (ret != 0) {
	    pk_backend_error_code (backend, PK_ERROR_ENUM_TRANSACTION_ERROR, "remove failed");
	}

	slapt_free_transaction(transaction);

	slapt_free_pkg_list(available);
	slapt_free_pkg_list(installed);

	pk_backend_set_percentage (backend, 100);
	pk_backend_finished (backend);
}
/**
 * backend_get_update_detail:
 */
static void
backend_get_update_detail (PkBackend *backend, gchar **package_ids)
{
	guint i;
	guint len;
	const gchar *package_id;
	const gchar *old_package_id;
	PkPackageId *pi;

	slapt_pkg_list_t *installed;
	slapt_pkg_list_t *available;
	const gchar *search;
	slapt_pkg_list_t *results;
	slapt_pkg_info_t *pkg;
	slapt_pkg_info_t *oldpkg;
	const gchar *title;
	char *changelog;
	const gchar *issued;

	pk_backend_set_status (backend, PK_STATUS_ENUM_QUERY);
	pk_backend_set_percentage (backend, 0);

	installed = slapt_get_installed_pkgs();
	available = slapt_get_available_pkgs();

	len = g_strv_length (package_ids);
	for (i=0; i<len; i++) {
	    package_id = package_ids[i];
	    pi = pk_package_id_new_from_string (package_id);
	    if (pi == NULL) {
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_ID_INVALID, "invalid package id");
		pk_backend_finished (backend);
		return;
	    }
	    pkg = _get_pkg_from_id(pi, available, installed);
	    pk_package_id_free (pi);
	    if (pkg == NULL) {
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_NOT_FOUND, "package not found");
		continue;
	    }
	    package_id = _get_string_from_pkg(pkg);

	    search = g_strdup_printf("^%s$", pkg->name);
	    results = slapt_search_pkg_list(installed, search);
	    g_free((gpointer) search);
	    if (results->pkg_count > 0) {
		oldpkg = results->pkgs[0];
		old_package_id = _get_string_from_pkg(oldpkg);
	    } else {
		oldpkg = NULL;
		old_package_id = "";
	    }

	    changelog = slapt_get_pkg_changelog(pkg);
	    title = ""; /* first line of changelog */

	    issued = NULL;

	    pk_backend_update_detail (backend, package_id, old_package_id,
	        "", "", "", NULL, PK_RESTART_ENUM_NONE,
	        title, changelog, PK_UPDATE_STATE_ENUM_UNKNOWN, issued, NULL);

	    g_free((gpointer) issued);
	    if (changelog != NULL)
	        free(changelog);
	}

	slapt_free_pkg_list(available);
	slapt_free_pkg_list(installed);

	pk_backend_set_percentage (backend, 100);
	pk_backend_finished (backend);
}
/**
 * backend_get_details:
 */
static void
backend_get_details (PkBackend *backend, gchar **package_ids)
{
	guint i;
	guint len;
	const gchar *package_id;
	PkPackageId *pi;
	const gchar *license = "";
	const gchar *homepage = "";
	const gchar *description;
	PkGroupEnum group;

	slapt_pkg_list_t *installed;
	slapt_pkg_list_t *available;
	slapt_pkg_info_t *pkg;
	const char *category;
	struct category_map *catgroup;

	pk_backend_set_status (backend, PK_STATUS_ENUM_QUERY);
	pk_backend_set_percentage (backend, 0);

	installed = slapt_get_installed_pkgs();
	available = slapt_get_available_pkgs();

	len = g_strv_length (package_ids);
	for (i=0; i<len; i++) {
	    package_id = package_ids[i];
	    pi = pk_package_id_new_from_string (package_id);
	    if (pi == NULL) {
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_ID_INVALID, "invalid package id");
		pk_backend_finished (backend);
		return;
	    }
	    pkg = _get_pkg_from_id(pi, available, installed);
	    pk_package_id_free (pi);
	    if (pkg == NULL) {
		pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_NOT_FOUND, "package not found");
		continue;
	    }

	    category = _get_pkg_category(pkg);
	    group = PK_GROUP_ENUM_UNKNOWN;
	    for (catgroup = CATGROUP; catgroup->category != NULL; catgroup++) {
		if (strcmp(catgroup->category, category) == 0) {
		    group = catgroup->group;
		    break;
		}
	    }

	    description = g_strstrip((gchar*) _get_pkg_description(pkg));

	    pk_backend_details (backend, package_id,
		license, group, description, homepage, pkg->size_c * 1024);

	    g_free((gpointer) description);
	}

	slapt_free_pkg_list(available);
	slapt_free_pkg_list(installed);

	pk_backend_set_percentage (backend, 100);
	pk_backend_finished (backend);
}
Example #7
0
void
pk_package_id_test (EggTest *test)
{
	gboolean ret;
	gchar *text;
	const gchar *temp;
	PkPackageId *id;
	PkPackageId *id2;
	gchar **array;

	if (!egg_test_start (test, "PkPackageId"))
		return;

	/************************************************************
	 ****************          id           ******************
	 ************************************************************/

	egg_test_title (test, "id build valid");
	text = pk_package_id_build ("moo", "0.0.1", "i386", "fedora");
	if (g_strcmp0 (text, "moo;0.0.1;i386;fedora") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, NULL);
	g_free (text);

	egg_test_title (test, "id build partial");
	text = pk_package_id_build ("moo", NULL, NULL, NULL);
	if (g_strcmp0 (text, "moo;;;") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "got '%s', expected '%s'", text, "moo;;;");
	g_free (text);

	egg_test_title (test, "pid equal pass (same)");
	ret = pk_package_id_equal_strings ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora");
	egg_test_assert (test, ret);

	/************************************************************/
	egg_test_title (test, "pid equal pass (different)");
	ret = pk_package_id_equal_strings ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;data");
	egg_test_assert (test, ret);

	/************************************************************/
	egg_test_title (test, "get an id object");
	id = pk_package_id_new ();
	egg_test_assert (test, id != NULL);

	/************************************************************/
	egg_test_title (test, "test id freeing early");
	ret = pk_package_id_free (id);
	egg_test_assert (test, ret);

	/************************************************************/
	egg_test_title (test, "parse incorrect package_id from string (empty)");
	temp = "";
	id = pk_package_id_new_from_string (temp);
	if (id == NULL)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "passed an invalid string '%s'", temp);

	/************************************************************/
	egg_test_title (test, "parse incorrect package_id from string (not enough)");
	temp = "moo;0.0.1;i386";
	id = pk_package_id_new_from_string (temp);
	if (id == NULL)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "passed an invalid string '%s'", temp);

	/************************************************************/
	egg_test_title (test, "parse package_id from string");
	id = pk_package_id_new_from_string ("moo;0.0.1;i386;fedora");
	if (id != NULL &&
	    g_strcmp0 (id->name, "moo") == 0 &&
	    g_strcmp0 (id->arch, "i386") == 0 &&
	    g_strcmp0 (id->data, "fedora") == 0 &&
	    g_strcmp0 (id->version, "0.0.1") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, NULL);

	/************************************************************/
	egg_test_title (test, "test copying");
	id2 = pk_package_id_copy (id);
	if (id2 != NULL)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, NULL);

	/************************************************************/
	egg_test_title (test, "test id building with valid data");
	text = pk_package_id_to_string (id2);
	if (g_strcmp0 (text, "moo;0.0.1;i386;fedora") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "package_id is '%s'", text);
	g_free (text);
	pk_package_id_free (id);
	pk_package_id_free (id2);

	/************************************************************/
	egg_test_title (test, "test id building with partial data");
	id = pk_package_id_new_from_string ("moo;;;");
	text = pk_package_id_to_string (id);
	if (g_strcmp0 (text, "moo;;;") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "package_id is '%s', should be '%s'", text, "moo;;;");
	g_free (text);
	pk_package_id_free (id);

	/************************************************************/
	egg_test_title (test, "parse short package_id from string");
	id = pk_package_id_new_from_string ("moo;0.0.1;;");
	if (id != NULL &&
	    (g_strcmp0 (id->name, "moo") == 0) &&
	    (g_strcmp0 (id->version, "0.0.1") == 0) &&
	    id->data == NULL &&
	    id->arch == NULL)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, NULL);
	pk_package_id_free (id);

	egg_test_title (test, "id equal pass (same)");
	ret = pk_strcmp_sections ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora", 4, 3);
	egg_test_assert (test, ret);

	egg_test_title (test, "id equal pass (parts==match)");
	ret = pk_strcmp_sections ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora", 4, 4);
	egg_test_assert (test, ret);

	egg_test_title (test, "id equal pass (different)");
	ret = pk_strcmp_sections ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;data", 4, 3);
	egg_test_assert (test, ret);

	egg_test_title (test, "id equal fail1");
	ret = pk_strcmp_sections ("moo;0.0.1;i386;fedora", "moo;0.0.2;x64;fedora", 4, 3);
	egg_test_assert (test, !ret);

	egg_test_title (test, "id equal fail2");
	ret = pk_strcmp_sections ("moo;0.0.1;i386;fedora", "gnome;0.0.2;i386;fedora", 4, 3);
	egg_test_assert (test, !ret);

	egg_test_title (test, "id equal fail3");
	ret = pk_strcmp_sections ("moo;0.0.1;i386;fedora", "moo;0.0.3;i386;fedora", 4, 3);
	egg_test_assert (test, !ret);

	egg_test_title (test, "id equal fail (match too high)");
	ret = pk_strcmp_sections ("moo;0.0.1;i386;fedora", "moo;0.0.3;i386;fedora", 4, 5);
	egg_test_assert (test, !ret);

	/************************************************************
	 ****************          splitting         ****************
	 ************************************************************/
	egg_test_title (test, "test pass 1");
	array = pk_strsplit ("foo", 1);
	if (array != NULL &&
	    g_strcmp0 (array[0], "foo") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "got %s", array[0]);
	g_strfreev (array);

	/************************************************************/
	egg_test_title (test, "test pass 2");
	array = pk_strsplit ("foo;moo", 2);
	if (array != NULL &&
	    g_strcmp0 (array[0], "foo") == 0 &&
	    g_strcmp0 (array[1], "moo") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "got %s, %s", array[0], array[1]);
	g_strfreev (array);

	/************************************************************/
	egg_test_title (test, "test pass 3");
	array = pk_strsplit ("foo;moo;bar", 3);
	if (array != NULL &&
	    g_strcmp0 (array[0], "foo") == 0 &&
	    g_strcmp0 (array[1], "moo") == 0 &&
	    g_strcmp0 (array[2], "bar") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "got %s, %s, %s, %s", array[0], array[1], array[2], array[3]);
	g_strfreev (array);

	/************************************************************/
	egg_test_title (test, "test on real packageid");
	array = pk_strsplit ("kde-i18n-csb;4:3.5.8~pre20071001-0ubuntu1;all;", 4);
	if (array != NULL &&
	    g_strcmp0 (array[0], "kde-i18n-csb") == 0 &&
	    g_strcmp0 (array[1], "4:3.5.8~pre20071001-0ubuntu1") == 0 &&
	    g_strcmp0 (array[2], "all") == 0 &&
	    g_strcmp0 (array[3], "") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "got %s, %s, %s, %s", array[0], array[1], array[2], array[3]);
	g_strfreev (array);

	/************************************************************/
	egg_test_title (test, "test on short packageid");
	array = pk_strsplit ("kde-i18n-csb;4:3.5.8~pre20071001-0ubuntu1;;", 4);
	if (array != NULL &&
	    g_strcmp0 (array[0], "kde-i18n-csb") == 0 &&
	    g_strcmp0 (array[1], "4:3.5.8~pre20071001-0ubuntu1") == 0 &&
	    g_strcmp0 (array[2], "") == 0 &&
	    g_strcmp0 (array[3], "") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "got %s, %s, %s, %s", array[0], array[1], array[2], array[3]);
	g_strfreev (array);

	/************************************************************/
	egg_test_title (test, "test fail under");
	array = pk_strsplit ("foo;moo", 1);
	egg_test_assert (test, array == NULL);

	/************************************************************/
	egg_test_title (test, "test fail over");
	array = pk_strsplit ("foo;moo", 3);
	egg_test_assert (test, array == NULL);

	/************************************************************/
	egg_test_title (test, "test fail missing first");
	array = pk_strsplit (";moo", 2);
	egg_test_assert (test, array == NULL);

	egg_test_end (test);
}