Beispiel #1
0
/**
 * dnf_get_filter_for_ids:
 */
PkBitfield
dnf_get_filter_for_ids (gchar **package_ids)
{
	gboolean available = FALSE;
	gboolean installed = FALSE;
	guint i;
	PkBitfield filters = 0;

	for (i = 0; package_ids[i] != NULL && (!installed || !available); i++) {
		g_auto(GStrv) split = pk_package_id_split (package_ids[i]);
		if (g_strcmp0 (split[PK_PACKAGE_ID_DATA], "installed") == 0)
			installed = TRUE;
		else
			available = TRUE;
	}

	/* a mixture */
	if (installed && available)
		return pk_bitfield_value (PK_FILTER_ENUM_NONE);

	/* we can restrict what's loaded into the sack */
	if (!installed)
		filters = pk_bitfield_value (PK_FILTER_ENUM_NOT_INSTALLED);
	if (!available)
		filters = pk_bitfield_value (PK_FILTER_ENUM_INSTALLED);
	return filters;
}
/**
 * gs_plugin_refresh:
 */
gboolean
gs_plugin_refresh (GsPlugin *plugin,
		   guint cache_age,
		   GsPluginRefreshFlags flags,
		   GCancellable *cancellable,
		   GError **error)
{
	PkBitfield filter;
	PkBitfield transaction_flags;
	ProgressData data;
	g_auto(GStrv) package_ids = NULL;
	g_autoptr(PkPackageSack) sack = NULL;
	g_autoptr(PkResults) results2 = NULL;
	g_autoptr(PkResults) results = NULL;

	/* not us */
	if ((flags & GS_PLUGIN_REFRESH_FLAGS_UPDATES) == 0)
		return TRUE;

	/* cache age of 0 is user-initiated */
	pk_client_set_background (PK_CLIENT (plugin->priv->task), cache_age > 0);

	data.plugin = plugin;
	data.ptask = NULL;

	/* update UI as this might take some time */
	gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);

	/* do sync call */
	filter = pk_bitfield_value (PK_FILTER_ENUM_NONE);
	pk_client_set_cache_age (PK_CLIENT (plugin->priv->task), cache_age);
	results = pk_client_get_updates (PK_CLIENT (plugin->priv->task),
					 filter,
					 cancellable,
					 gs_plugin_packagekit_progress_cb, &data,
					 error);
	if (results == NULL)
		return FALSE;

	/* download all the updates */
	sack = pk_results_get_package_sack (results);
	if (pk_package_sack_get_size (sack) == 0)
		return TRUE;
	package_ids = pk_package_sack_get_ids (sack);
	transaction_flags = pk_bitfield_value (PK_TRANSACTION_FLAG_ENUM_ONLY_DOWNLOAD);
	results2 = pk_client_update_packages (PK_CLIENT (plugin->priv->task),
					      transaction_flags,
					      package_ids,
					      cancellable,
					      gs_plugin_packagekit_progress_cb, &data,
					      error);
	return results2 != NULL;
}
Beispiel #3
0
/**
 * pk_filter_bitfield_from_text:
 * @filters: the enumerated constant value, e.g. "available;~gui"
 *
 * Converts text representation to its enumerated type bitfield, or 0 for invalid
 *
 * Return value: The enumerated type values
 **/
PkBitfield
pk_filter_bitfield_from_text (const gchar *filters)
{
	PkBitfield filters_enum = 0;
	gchar **split;
	guint length;
	guint i;
	PkFilterEnum filter;

	split = g_strsplit (filters, ";", 0);
	if (split == NULL) {
		egg_warning ("unable to split");
		goto out;
	}

	length = g_strv_length (split);
	for (i=0; i<length; i++) {
		filter = pk_filter_enum_from_text (split[i]);
		if (filter == PK_FILTER_ENUM_UNKNOWN) {
			filters_enum = 0;
			break;
		}
		filters_enum += pk_bitfield_value (filter);
	}
out:
	g_strfreev (split);
	return filters_enum;
}
Beispiel #4
0
/**
 * pk_group_bitfield_from_text:
 * @groups: the enumerated constant value, e.g. "available;~gui"
 *
 * Converts text representation to its enumerated type bitfield
 *
 * Return value: The enumerated type values, or 0 for invalid
 **/
PkBitfield
pk_group_bitfield_from_text (const gchar *groups)
{
	PkBitfield groups_enum = 0;
	gchar **split;
	guint length;
	guint i;
	PkGroupEnum group;

	split = g_strsplit (groups, ";", 0);
	if (split == NULL) {
		egg_warning ("unable to split");
		goto out;
	}

	length = g_strv_length (split);
	for (i=0; i<length; i++) {
		group = pk_group_enum_from_text (split[i]);
		if (group == PK_GROUP_ENUM_UNKNOWN) {
			groups_enum = 0;
			break;
		}
		groups_enum += pk_bitfield_value (group);
	}
out:
	g_strfreev (split);
	return groups_enum;
}
Beispiel #5
0
/**
 * pk_package_sack_resolve_async:
 * @sack: a valid #PkPackageSack instance
 * @cancellable: a #GCancellable or %NULL
 * @progress_callback: (scope call): the function to run when the progress changes
 * @progress_user_data: data to pass to @progress_callback
 * @callback: the function to run on completion
 * @user_data: the data to pass to @callback
 *
 * Merges in details about packages using resolve.
 *
 * Since: 0.5.2
 **/
void
pk_package_sack_resolve_async (PkPackageSack *sack, GCancellable *cancellable,
				     PkProgressCallback progress_callback, gpointer progress_user_data,
				     GAsyncReadyCallback callback, gpointer user_data)
{
	GSimpleAsyncResult *res;
	PkPackageSackState *state;
	gchar **package_ids;

	g_return_if_fail (PK_IS_PACKAGE_SACK (sack));
	g_return_if_fail (callback != NULL);

	res = g_simple_async_result_new (G_OBJECT (sack), callback, user_data, pk_package_sack_resolve_async);

	/* save state */
	state = g_slice_new0 (PkPackageSackState);
	state->res = g_object_ref (res);
	state->sack = g_object_ref (sack);
	if (cancellable != NULL) {
		state->cancellable = g_object_ref (cancellable);
	}
	state->ret = FALSE;

	/* start resolve async */
	package_ids = pk_package_sack_get_package_ids (sack);
	pk_client_resolve_async (sack->priv->client, pk_bitfield_value (PK_FILTER_ENUM_INSTALLED), package_ids,
				 cancellable, progress_callback, progress_user_data,
				 (GAsyncReadyCallback) pk_package_sack_resolve_cb, state);
	g_strfreev (package_ids);
	g_object_unref (res);
}
Beispiel #6
0
/**
 * pk_role_bitfield_from_text:
 * @roles: the enumerated constant value, e.g. "available;~gui"
 *
 * Converts text representation to its enumerated type bitfield
 *
 * Return value: The enumerated type values, or 0 for invalid
 **/
PkBitfield
pk_role_bitfield_from_text (const gchar *roles)
{
	PkBitfield roles_enum = 0;
	gchar **split;
	guint length;
	guint i;
	PkRoleEnum role;

	split = g_strsplit (roles, ";", 0);
	if (split == NULL) {
		egg_warning ("unable to split");
		goto out;
	}

	length = g_strv_length (split);
	for (i=0; i<length; i++) {
		role = pk_role_enum_from_text (split[i]);
		if (role == PK_ROLE_ENUM_UNKNOWN) {
			roles_enum = 0;
			break;
		}
		roles_enum += pk_bitfield_value (role);
	}
out:
	g_strfreev (split);
	return roles_enum;
}
Beispiel #7
0
/**
 * pk_transaction_flag_bitfield_to_string:
 * @transaction_flags: The enumerated type values
 *
 * Converts a enumerated type bitfield to its text representation
 *
 * Return value: the enumerated constant value, e.g. "only-trusted;simulate"
 *
 * Since: 0.8.1
 **/
gchar *
pk_transaction_flag_bitfield_to_string (PkBitfield transaction_flags)
{
	GString *string;
	guint i;

	/* shortcut */
	if (transaction_flags == 0)
		return g_strdup (pk_transaction_flag_enum_to_string (PK_TRANSACTION_FLAG_ENUM_NONE));

	string = g_string_new ("");
	for (i = 0; i < PK_TRANSACTION_FLAG_ENUM_LAST; i++) {
		if ((transaction_flags & pk_bitfield_value (i)) == 0)
			continue;
		g_string_append_printf (string, "%s;", pk_transaction_flag_enum_to_string (i));
	}
	/* do we have a 'none' transaction_flag? \n */
	if (string->len == 0) {
		g_warning ("not valid!");
		g_string_append (string, pk_transaction_flag_enum_to_string (PK_TRANSACTION_FLAG_ENUM_NONE));
	} else {
		/* remove last \n */
		g_string_set_size (string, string->len - 1);
	}
	return g_string_free (string, FALSE);
}
Beispiel #8
0
/**
 * pk_filter_bitfield_to_string:
 * @filters: The enumerated type values
 *
 * Converts a enumerated type bitfield to its text representation
 *
 * Return value: the enumerated constant value, e.g. "available;~gui"
 *
 * Since: 0.5.2
 **/
gchar *
pk_filter_bitfield_to_string (PkBitfield filters)
{
	GString *string;
	guint i;

	/* shortcut */
	if (filters == 0)
		return g_strdup (pk_filter_enum_to_string (PK_FILTER_ENUM_NONE));

	string = g_string_new ("");
	for (i = 0; i < PK_FILTER_ENUM_LAST; i++) {
		if ((filters & pk_bitfield_value (i)) == 0)
			continue;
		g_string_append_printf (string, "%s;", pk_filter_enum_to_string (i));
	}
	/* do we have a 'none' filter? \n */
	if (string->len == 0) {
		g_warning ("not valid!");
		g_string_append (string, pk_filter_enum_to_string (PK_FILTER_ENUM_NONE));
	} else {
		/* remove last \n */
		g_string_set_size (string, string->len - 1);
	}
	return g_string_free (string, FALSE);
}
/**
 * pk_plugin_get_installed_package_for_file:
 **/
static PkPackage *
pk_plugin_get_installed_package_for_file (PkPlugin *plugin,
					  const gchar *filename,
					  GError **error)
{
	PkPackage *package = NULL;
	gchar **filenames;

	/* use PK to find the correct package */
	if (plugin->priv->list->len > 0)
		g_ptr_array_set_size (plugin->priv->list, 0);
	pk_backend_reset_job (plugin->backend, plugin->job);
	pk_backend_job_set_vfunc (plugin->job,
				  PK_BACKEND_SIGNAL_FINISHED,
				  (PkBackendJobVFunc) pk_plugin_finished_cb,
				  plugin);
	pk_backend_job_set_vfunc (plugin->job,
				  PK_BACKEND_SIGNAL_PACKAGE,
				  (PkBackendJobVFunc) pk_plugin_package_cb,
				  plugin);
	filenames = g_strsplit (filename, "|||", -1);
	pk_backend_search_files (plugin->backend,
				 plugin->job,
				 pk_bitfield_value (PK_FILTER_ENUM_INSTALLED),
				 filenames);
	g_strfreev (filenames);

	/* wait for finished */
	g_main_loop_run (plugin->priv->loop);

	/* check that we only matched one package */
	if (plugin->priv->list->len == 0) {
		g_set_error_literal (error, 1, 0,
				     "no packages own this file");
		goto out;
	}
	if (plugin->priv->list->len > 1) {
		g_set_error (error, 1, 0,
			     "%i packages own this file",
			     plugin->priv->list->len);
		goto out;
	}

	/* get the package */
	package = g_ptr_array_index (plugin->priv->list, 0);
	if (package == NULL) {
		g_set_error_literal (error, 1, 0,
				     "package invalid");
		goto out;
	}
out:
	return package;
}
Beispiel #10
0
/**
 * pk_bitfield_from_enums:
 * @value: the first value we want to add to the bitfield
 * @...: other values to add, terminated by -1
 *
 * Create a bitfield with the suppied values set.
 *
 * Return value: a #PkBitfield, or 0 if invalid
 *
 * Since: 0.5.2
 **/
PkBitfield
pk_bitfield_from_enums (gint value, ...)
{
	va_list args;
	guint i;
	gint value_temp;
	PkBitfield values;

	/* we must query at least one thing */
	values = pk_bitfield_value (value);

	/* process the valist */
	va_start (args, value);
	for (i = 0;; i++) {
		value_temp = va_arg (args, gint);
		if (value_temp == -1)
			break;
		values += pk_bitfield_value (value_temp);
	}
	va_end (args);

	return values;
}
Beispiel #11
0
void
zypp_emit_packages_in_list (PkBackend *backend, std::vector<zypp::sat::Solvable> *v, PkBitfield filters)
{
	for (std::vector<zypp::sat::Solvable>::iterator it = v->begin ();
			it != v->end (); it++) {
		gchar *package_id = zypp_build_package_id_from_resolvable (*it);

		// iterate through the given filters
		if (filters != 0){
			gboolean print = TRUE;
			for (guint i = 0; i < PK_FILTER_ENUM_LAST; i++) {
				if ((filters & pk_bitfield_value (i)) == 0)
					continue;
				if (i == PK_FILTER_ENUM_INSTALLED && !(it->isSystem ()))
					print = FALSE;
				if (i == PK_FILTER_ENUM_NOT_INSTALLED && it->isSystem ())
					print = FALSE;;
				if (i == PK_FILTER_ENUM_ARCH) {
					if (it->arch () != zypp::ZConfig::defaultSystemArchitecture () &&
							it->arch () != zypp::Arch_noarch &&
							! system_and_package_are_x86 (*it))
						print = FALSE;
				}
				if (i == PK_FILTER_ENUM_NOT_ARCH) {
					if (it->arch () == zypp::ZConfig::defaultSystemArchitecture () ||
							system_and_package_are_x86 (*it))
						print = FALSE;
				}
				if (i == PK_FILTER_ENUM_SOURCE && !(zypp::isKind<zypp::SrcPackage>(*it))) {
					print = FALSE;
				}
				if (i == PK_FILTER_ENUM_NOT_SOURCE && zypp::isKind<zypp::SrcPackage>(*it)) {
					print = FALSE;
				}
				//const gchar * myarch = zypp::ZConfig::defaultSystemArchitecture().asString().c_str();
				//egg_debug ("my default arch is %s", myarch);
			}
			if (!print)
				continue;		
		}

		pk_backend_package (backend,
			    it->isSystem() == true ?
				PK_INFO_ENUM_INSTALLED :
				PK_INFO_ENUM_AVAILABLE,
			    package_id,
			    it->lookupStrAttribute (zypp::sat::SolvAttr::summary).c_str ());
		g_free (package_id);
	}
}
/**
 * pk_debuginfo_install_get_repo_list:
 **/
static gboolean
pk_debuginfo_install_get_repo_list (PkDebuginfoInstallPrivate *priv, GError **error)
{
	gboolean ret = FALSE;
	PkResults *results = NULL;
	guint i;
	GPtrArray *array;
	GError *error_local = NULL;
	PkRepoDetail *item;
	PkError *error_code = NULL;
	gboolean enabled;
	gchar *repo_id;

	/* get all repo details */
	results = pk_client_get_repo_list (priv->client, pk_bitfield_value (PK_FILTER_ENUM_NONE), NULL, NULL, NULL, &error_local);
	if (results == NULL) {
		*error = g_error_new (1, 0, "failed to get repo list: %s", error_local->message);
		g_error_free (error_local);
		goto out;
	}

	/* check error code */
	error_code = pk_results_get_error_code (results);
	if (error_code != NULL) {
		*error = g_error_new (1, 0, "failed to get repo list: %s, %s", pk_error_enum_to_string (pk_error_get_code (error_code)), pk_error_get_details (error_code));
		goto out;
	}

	/* get results */
	array = pk_results_get_repo_detail_array (results);
	for (i=0; i<array->len; i++) {
		item = g_ptr_array_index (array, i);
		g_object_get (item,
			      "enabled", &enabled,
			      "repo-id", &repo_id,
			      NULL);
		if (enabled)
			g_ptr_array_add (priv->enabled, repo_id);
		else
			g_ptr_array_add (priv->disabled, repo_id);
	}
	ret = TRUE;
out:
	if (error_code != NULL)
		g_object_unref (error_code);
	if (results != NULL)
		g_object_unref (results);
	return ret;
}
Beispiel #13
0
/**
 * pk_transaction_flag_bitfield_from_string:
 * @transaction_flags: the enumerated constant value, e.g. "only-trusted;simulate"
 *
 * Converts text representation to its enumerated type bitfield, or 0 for invalid
 *
 * Return value: The enumerated type values
 *
 * Since: 0.8.1
 **/
PkBitfield
pk_transaction_flag_bitfield_from_string (const gchar *transaction_flags)
{
	PkBitfield transaction_flags_enum = 0;
	guint length;
	guint i;
	PkFilterEnum transaction_flag;
	g_auto(GStrv) split = NULL;

	split = g_strsplit (transaction_flags, ";", 0);
	if (split == NULL) {
		g_warning ("unable to split");
		return 0;
	}

	length = g_strv_length (split);
	for (i = 0; i < length; i++) {
		transaction_flag = pk_transaction_flag_enum_from_string (split[i]);
		transaction_flags_enum += pk_bitfield_value (transaction_flag);
	}
	return transaction_flags_enum;
}
Beispiel #14
0
/**
 * pk_group_bitfield_to_string:
 * @groups: The enumerated type values
 *
 * Converts a enumerated type bitfield to its text representation
 *
 * Return value: the enumerated constant value, e.g. "gnome;kde"
 *
 * Since: 0.5.2
 **/
gchar *
pk_group_bitfield_to_string (PkBitfield groups)
{
	GString *string;
	guint i;

	string = g_string_new ("");
	for (i = 0; i < PK_GROUP_ENUM_LAST; i++) {
		if ((groups & pk_bitfield_value (i)) == 0)
			continue;
		g_string_append_printf (string, "%s;", pk_group_enum_to_string (i));
	}
	/* do we have a no bitfield? \n */
	if (string->len == 0) {
		g_warning ("not valid!");
		g_string_append (string, pk_group_enum_to_string (PK_GROUP_ENUM_UNKNOWN));
	} else {
		/* remove last \n */
		g_string_set_size (string, string->len - 1);
	}
	return g_string_free (string, FALSE);
}
Beispiel #15
0
/**
 * pk_role_bitfield_from_string:
 * @roles: the enumerated constant value, e.g. "search-file;update-system"
 *
 * Converts text representation to its enumerated type bitfield
 *
 * Return value: The enumerated type values, or 0 for invalid
 *
 * Since: 0.5.2
 **/
PkBitfield
pk_role_bitfield_from_string (const gchar *roles)
{
	PkBitfield roles_enum = 0;
	guint length;
	guint i;
	PkRoleEnum role;
	g_auto(GStrv) split = NULL;

	split = g_strsplit (roles, ";", 0);
	if (split == NULL) {
		g_warning ("unable to split");
		return 0;
	}

	length = g_strv_length (split);
	for (i = 0; i < length; i++) {
		role = pk_role_enum_from_string (split[i]);
		if (role != PK_ROLE_ENUM_UNKNOWN)
			roles_enum += pk_bitfield_value (role);
	}
	return roles_enum;
}
Beispiel #16
0
/**
 * pk_filter_bitfield_from_string:
 * @filters: the enumerated constant value, e.g. "available;~gui"
 *
 * Converts text representation to its enumerated type bitfield, or 0 for invalid
 *
 * Return value: The enumerated type values
 *
 * Since: 0.5.2
 **/
PkBitfield
pk_filter_bitfield_from_string (const gchar *filters)
{
	PkBitfield filters_enum = 0;
	guint length;
	guint i;
	PkFilterEnum filter;
	g_auto(GStrv) split = NULL;

	split = g_strsplit (filters, ";", 0);
	if (split == NULL) {
		g_warning ("unable to split");
		return 0;
	}

	length = g_strv_length (split);
	for (i = 0; i < length; i++) {
		filter = pk_filter_enum_from_string (split[i]);
		if (filter != PK_FILTER_ENUM_UNKNOWN)
			filters_enum += pk_bitfield_value (filter);
	}
	return filters_enum;
}
Beispiel #17
0
/**
 * pk_group_bitfield_from_string:
 * @groups: the enumerated constant value, e.g. "available;~gui"
 *
 * Converts text representation to its enumerated type bitfield
 *
 * Return value: The enumerated type values, or 0 for invalid
 *
 * Since: 0.5.2
 **/
PkBitfield
pk_group_bitfield_from_string (const gchar *groups)
{
	PkBitfield groups_enum = 0;
	guint length;
	guint i;
	PkGroupEnum group;
	g_auto(GStrv) split = NULL;

	split = g_strsplit (groups, ";", 0);
	if (split == NULL) {
		g_warning ("unable to split");
		return 0;
	}

	length = g_strv_length (split);
	for (i = 0; i < length; i++) {
		group = pk_group_enum_from_string (split[i]);
		if (group != PK_GROUP_ENUM_UNKNOWN)
			groups_enum += pk_bitfield_value (group);
	}
	return groups_enum;
}
Beispiel #18
0
void
pk_bitfield_test (gpointer user_data)
{
	EggTest *test = (EggTest *) user_data;
	gchar *text;
	PkBitfield filter;
	gint value;
	PkBitfield values;

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

	/************************************************************/
	egg_test_title (test, "check we can convert filter bitfield to text (none)");
	text = pk_filter_bitfield_to_text (pk_bitfield_value (PK_FILTER_ENUM_NONE));
	if (g_strcmp0 (text, "none") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "text was %s", text);
	g_free (text);

	/************************************************************/
	egg_test_title (test, "check we can invert a bit 1 -> 0");
	values = pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) | pk_bitfield_value (PK_FILTER_ENUM_NOT_NEWEST);
	pk_bitfield_invert (values, PK_FILTER_ENUM_NOT_DEVELOPMENT);
	if (values == pk_bitfield_value (PK_FILTER_ENUM_NOT_NEWEST))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "values were %" PK_BITFIELD_FORMAT, values);

	/************************************************************/
	egg_test_title (test, "check we can invert a bit 0 -> 1");
	values = 0;
	pk_bitfield_invert (values, PK_FILTER_ENUM_NOT_DEVELOPMENT);
	if (values == pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "values were %" PK_BITFIELD_FORMAT, values);

	/************************************************************/
	egg_test_title (test, "check we can convert filter bitfield to text (single)");
	text = pk_filter_bitfield_to_text (pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT));
	if (g_strcmp0 (text, "~devel") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "text was %s", text);
	g_free (text);

	/************************************************************/
	egg_test_title (test, "check we can convert filter bitfield to text (plural)");
	text = pk_filter_bitfield_to_text (pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) |
					   pk_bitfield_value (PK_FILTER_ENUM_GUI) |
					   pk_bitfield_value (PK_FILTER_ENUM_NEWEST));
	if (g_strcmp0 (text, "~devel;gui;newest") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "text was %s", text);
	g_free (text);

	/************************************************************/
	egg_test_title (test, "check we can convert filter text to bitfield (none)");
	filter = pk_filter_bitfield_from_text ("none");
	if (filter == pk_bitfield_value (PK_FILTER_ENUM_NONE))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "filter was %" PK_BITFIELD_FORMAT, filter);

	/************************************************************/
	egg_test_title (test, "check we can convert filter text to bitfield (single)");
	filter = pk_filter_bitfield_from_text ("~devel");
	if (filter == pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "filter was %" PK_BITFIELD_FORMAT, filter);

	/************************************************************/
	egg_test_title (test, "check we can convert filter text to bitfield (plural)");
	filter = pk_filter_bitfield_from_text ("~devel;gui;newest");
	if (filter == (pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) |
		       pk_bitfield_value (PK_FILTER_ENUM_GUI) |
		       pk_bitfield_value (PK_FILTER_ENUM_NEWEST)))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "filter was %" PK_BITFIELD_FORMAT, filter);

	/************************************************************/
	egg_test_title (test, "check we can add / remove bitfield");
	filter = pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) |
		 pk_bitfield_value (PK_FILTER_ENUM_GUI) |
		 pk_bitfield_value (PK_FILTER_ENUM_NEWEST);
	pk_bitfield_add (filter, PK_FILTER_ENUM_NOT_FREE);
	pk_bitfield_remove (filter, PK_FILTER_ENUM_NOT_DEVELOPMENT);
	text = pk_filter_bitfield_to_text (filter);
	if (g_strcmp0 (text, "gui;~free;newest") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "text was %s", text);
	g_free (text);

	/************************************************************/
	egg_test_title (test, "check we can test enum presence");
	filter = pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) |
		 pk_bitfield_value (PK_FILTER_ENUM_GUI) |
		 pk_bitfield_value (PK_FILTER_ENUM_NEWEST);
	if (pk_bitfield_contain (filter, PK_FILTER_ENUM_NOT_DEVELOPMENT))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "wrong boolean");

	/************************************************************/
	egg_test_title (test, "check we can test enum false-presence");
	if (!pk_bitfield_contain (filter, PK_FILTER_ENUM_FREE))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "wrong boolean");

	/************************************************************/
	egg_test_title (test, "check we can add / remove bitfield to nothing");
	filter = pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT);
	pk_bitfield_remove (filter, PK_FILTER_ENUM_NOT_DEVELOPMENT);
	text = pk_filter_bitfield_to_text (filter);
	if (g_strcmp0 (text, "none") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "text was %s", text);
	g_free (text);

	/************************************************************/
	egg_test_title (test, "role bitfield from enums (unknown)");
	values = pk_bitfield_from_enums (PK_ROLE_ENUM_UNKNOWN, -1);
	if (values == pk_bitfield_value (PK_ROLE_ENUM_UNKNOWN))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned bitfield %" PK_BITFIELD_FORMAT, values);

	/************************************************************/
	egg_test_title (test, "role bitfield from enums (random)");
	values = pk_bitfield_from_enums (PK_ROLE_ENUM_SEARCH_GROUP, PK_ROLE_ENUM_SEARCH_DETAILS, -1);
	if (values == (pk_bitfield_value (PK_ROLE_ENUM_SEARCH_DETAILS) |
		       pk_bitfield_value (PK_ROLE_ENUM_SEARCH_GROUP)))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned bitfield %" PK_BITFIELD_FORMAT, values);

	/************************************************************/
	egg_test_title (test, "group bitfield from enums (unknown)");
	values = pk_bitfield_from_enums (PK_GROUP_ENUM_UNKNOWN, -1);
	if (values == pk_bitfield_value (PK_GROUP_ENUM_UNKNOWN))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned bitfield %" PK_BITFIELD_FORMAT, values);

	/************************************************************/
	egg_test_title (test, "group bitfield from enums (random)");
	values = pk_bitfield_from_enums (PK_GROUP_ENUM_ACCESSIBILITY, -1);
	if (values == (pk_bitfield_value (PK_GROUP_ENUM_ACCESSIBILITY)))
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned bitfield %" PK_BITFIELD_FORMAT, values);

	/************************************************************/
	egg_test_title (test, "group bitfield to text (unknown)");
	values = pk_bitfield_from_enums (PK_GROUP_ENUM_UNKNOWN, -1);
	text = pk_group_bitfield_to_text (values);
	if (g_strcmp0 (text, "unknown") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned bitfield text %s (%" PK_BITFIELD_FORMAT ")", text, values);
	g_free (text);

	/************************************************************/
	egg_test_title (test, "group bitfield to text (first and last)");
	values = pk_bitfield_from_enums (PK_GROUP_ENUM_ACCESSIBILITY, PK_GROUP_ENUM_UNKNOWN, -1);
	text = pk_group_bitfield_to_text (values);
	if (g_strcmp0 (text, "unknown;accessibility") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned bitfield text %s (%" PK_BITFIELD_FORMAT ")", text, values);
	g_free (text);

	/************************************************************/
	egg_test_title (test, "group bitfield to text (random)");
	values = pk_bitfield_from_enums (PK_GROUP_ENUM_UNKNOWN, PK_GROUP_ENUM_REPOS, -1);
	text = pk_group_bitfield_to_text (values);
	if (g_strcmp0 (text, "unknown;repos") == 0)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned bitfield text %s (%" PK_BITFIELD_FORMAT ")", text, values);
	g_free (text);

	/************************************************************/
	egg_test_title (test, "priority check missing");
	values = pk_bitfield_value (PK_ROLE_ENUM_SEARCH_DETAILS) |
		 pk_bitfield_value (PK_ROLE_ENUM_SEARCH_GROUP);
	value = pk_bitfield_contain_priority (values, PK_ROLE_ENUM_SEARCH_FILE, -1);
	if (value == -1)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned priority %i when should be missing", value);

	/************************************************************/
	egg_test_title (test, "priority check first");
	value = pk_bitfield_contain_priority (values, PK_ROLE_ENUM_SEARCH_GROUP, -1);
	if (value == PK_ROLE_ENUM_SEARCH_GROUP)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned wrong value; %i", value);

	/************************************************************/
	egg_test_title (test, "priority check second, correct");
	value = pk_bitfield_contain_priority (values, PK_ROLE_ENUM_SEARCH_FILE, PK_ROLE_ENUM_SEARCH_GROUP, -1);
	if (value == PK_ROLE_ENUM_SEARCH_GROUP)
		egg_test_success (test, NULL);
	else
		egg_test_failed (test, "returned wrong value; %i", value);

	egg_test_end (test);
}
Beispiel #19
0
/**
 * main:
 **/
int
main (int argc, char *argv[])
{
	GError *error = NULL;
	GOptionContext *context;
	gchar *options_help;
	gboolean ret;
	gchar *filename = NULL;
	PkClient *client = NULL;
	PkControl *control = NULL;
	PkBitfield roles;
	gchar *tempdir = NULL;
	gboolean exists;
	gboolean overwrite;
	gchar **excludes = NULL;
	gchar *package_id = NULL;
	PkServicePack *pack = NULL;
	gchar *directory = NULL;
	gchar *package_list = NULL;
	gchar *package = NULL;
	gboolean updates = FALSE;
	gint retval = 1;

	const GOptionEntry options[] = {
		{ "with-package-list", 'l', 0, G_OPTION_ARG_STRING, &package_list,
			/* TRANSLATORS: we can exclude certain packages (glibc) when we know they'll exist on the target */
			_("Set the file name of dependencies to be excluded"), NULL},
		{ "output", 'o', 0, G_OPTION_ARG_STRING, &directory,
			/* TRANSLATORS: the output location */
			_("The output file or directory (the current directory is used if omitted)"), NULL},
		{ "package", 'p', 0, G_OPTION_ARG_STRING, &package,
			/* TRANSLATORS: put a list of packages in the pack */
			_("The package to be put into the service pack"), NULL},
		{ "updates", 'u', 0, G_OPTION_ARG_NONE, &updates,
			/* TRANSLATORS: put all pending updates in the pack */
			_("Put all updates available in the service pack"), NULL},
		{ NULL}
	};

	setlocale (LC_ALL, "");
	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

#if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 31)
	if (! g_thread_supported ())
		g_thread_init (NULL);
#endif

	g_type_init ();

	/* do stuff on ctrl-c */
	signal (SIGINT, pk_generate_pack_sigint_cb);

	context = g_option_context_new ("PackageKit Pack Generator");
	g_option_context_add_main_entries (context, options, NULL);
	g_option_context_add_group (context, pk_debug_get_option_group ());
	g_option_context_parse (context, &argc, &argv, NULL);
	/* Save the usage string in case command parsing fails. */
	options_help = g_option_context_get_help (context, TRUE, NULL);
	g_option_context_free (context);

	client = pk_client_new ();
	pack = pk_service_pack_new ();
	cancellable = g_cancellable_new ();
	progressbar = pk_progress_bar_new ();
	pk_progress_bar_set_size (progressbar, 25);
	pk_progress_bar_set_padding (progressbar, 20);

	/* neither options selected */
	if (package == NULL && !updates) {
		/* TRANSLATORS: This is when the user fails to supply the correct arguments */
		g_print ("%s\n", _("Neither --package or --updates option selected."));
		retval = 1;
		goto out;
	}

	/* both options selected */
	if (package != NULL && updates) {
		/* TRANSLATORS: This is when the user fails to supply just one argument */
		g_print ("%s\n", _("Both options selected."));
		retval = 1;
		goto out;
	}

	/* no argument given to --package */
	if (package != NULL && package[0] == '\0') {
		/* TRANSLATORS: This is when the user fails to supply the package name */
		g_print ("%s\n", _("A package name is required"));
		retval = 1;
		goto out;
	}

	/* no argument given to --output */
	if (directory != NULL && directory[0] == '\0') {
		/* TRANSLATORS: This is when the user fails to supply the output */
		g_print ("%s\n", _("A output directory or file name is required"));
		retval = 1;
		goto out;
	}

	/* fall back to the system copy */
	if (package_list == NULL)
		package_list = g_strdup (PK_SYSTEM_PACKAGE_LIST_FILENAME);

	/* fall back to CWD */
	if (directory == NULL)
		directory = g_get_current_dir ();

	/* are we dumb and can't do some actions */
	control = pk_control_new ();
	ret = pk_control_get_properties (control, NULL, &error);
	if (!ret) {
		/* TRANSLATORS: This is when the daemon is not-installed/broken and fails to startup */
		g_print ("%s: %s\n", _("The daemon failed to startup"), error->message);
		goto out;
	}

	/* get data */
	g_object_get (control,
		      "roles", &roles,
		      NULL);

	if (!pk_bitfield_contain (roles, PK_ROLE_ENUM_GET_DEPENDS)) {
		/* TRANSLATORS: This is when the backend doesn't have the capability to get-depends */
		g_print ("%s (GetDepends)\n", _("The package manager cannot perform this type of operation."));
		retval = 1;
		goto out;
	}
	if (!pk_bitfield_contain (roles, PK_ROLE_ENUM_DOWNLOAD_PACKAGES)) {
		/* TRANSLATORS: This is when the backend doesn't have the capability to download */
		g_print ("%s (DownloadPackage)\n", _("The package manager cannot perform this type of operation."));
		retval = 1;
		goto out;
	}

#ifndef HAVE_ARCHIVE_H
	/* TRANSLATORS: This is when the distro didn't include libarchive support into PK */
	g_print ("%s\n", _("Service packs cannot be created as PackageKit was not built with libarchive support."));
	goto out;
#endif

	/* the user can speciify a complete path */
	ret = g_file_test (directory, G_FILE_TEST_IS_DIR);
	if (ret) {
		filename = pk_generate_pack_get_filename (package, directory);
	} else {
		if (!g_str_has_suffix (directory, PK_SERVICE_PACK_FILE_EXTENSION)) {
			/* TRANSLATORS: the user specified an absolute path, but didn't get the extension correct */
			g_print ("%s .%s \n", _("If specifying a file, the service pack name must end with"), PK_SERVICE_PACK_FILE_EXTENSION);
			retval = 1;
			goto out;
		}
		filename = g_strdup (directory);
	}

	/* download packages to a temporary directory */
	tempdir = g_build_filename (g_get_tmp_dir (), "pack", NULL);

	/* check if file exists before we overwrite it */
	exists = g_file_test (filename, G_FILE_TEST_EXISTS);

	/*ask user input*/
	if (exists) {
		/* TRANSLATORS: This is when file already exists */
		overwrite = pk_console_get_prompt (_("A pack with the same name already exists, do you want to overwrite it?"), FALSE);
		if (!overwrite) {
			/* TRANSLATORS: This is when the pack was not overwritten */
			g_print ("%s\n", _("The pack was not overwritten."));
			retval = 1;
			goto out;
		}
	}

	/* get rid of temp directory if it already exists */
	g_rmdir (tempdir);

	/* make the temporary directory */
	retval = g_mkdir_with_parents (tempdir, 0777);
	if (retval != 0) {
		/* TRANSLATORS: This is when the temporary directory cannot be created, the directory name follows */
		g_print ("%s '%s'\n", _("Failed to create directory:"), tempdir);
		retval = 1;
		goto out;
	}
	pk_service_pack_set_temp_directory (pack, tempdir);

	/* get the exclude list */
	excludes = NULL;
#if 0
	ret = pk_obj_list_from_file (PK_OBJ_LIST(list), package_list);
	if (!ret) {
		/* TRANSLATORS: This is when the list of packages from the remote computer cannot be opened */
		g_print ("%s: '%s'\n", _("Failed to open package list."), package_list);
		retval = 1;
		goto out;
	}
#endif

	/* resolve package name to package_id */
	if (!updates) {
		/* TRANSLATORS: The package name is being matched up to available packages */
		g_print ("%s\n", _("Finding package name."));
		package_id = pk_console_resolve_package (client, pk_bitfield_value (PK_FILTER_ENUM_NONE), package, &error);
		if (package_id == NULL) {
			/* TRANSLATORS: This is when the package cannot be found in any software source. The detailed error follows */
			g_print (_("Failed to find package '%s': %s"), package, error->message);
			g_error_free (error);
			retval = 1;
			goto out;
		}
	}

	/* TRANSLATORS: This is telling the user we are in the process of making the pack */
	g_print ("%s\n", _("Creating service pack..."));
	if (updates)
		ret = pk_generate_pack_create_for_updates (pack, filename, excludes, &error);
	else {
		gchar **package_ids;
		package_ids = pk_package_ids_from_id (package_id);
		ret = pk_generate_pack_create_for_package_ids (pack, filename, package_ids, excludes, &error);
		g_strfreev (package_ids);
	}

	/* no more progress */
	pk_progress_bar_end (progressbar);

	if (ret) {
		/* TRANSLATORS: we succeeded in making the file */
		g_print (_("Service pack created '%s'"), filename);
		g_print ("\n");
		retval = 0;
	} else {
		/* TRANSLATORS: we failed to make te file */
		g_print (_("Failed to create '%s': %s"), filename, error->message);
		g_print ("\n");
		g_error_free (error);
	}

out:
	/* get rid of temp directory */
	g_rmdir (tempdir);

	g_object_unref (cancellable);
	if (progressbar != NULL)
		g_object_unref (progressbar);
	if (pack != NULL)
		g_object_unref (pack);
	if (client != NULL)
		g_object_unref (client);
	if (control != NULL)
		g_object_unref (control);
	g_free (tempdir);
	g_free (filename);
	g_free (package_id);
	g_free (directory);
	g_free (package_list);
	g_free (options_help);
	g_strfreev (excludes);
	return retval;
}
/**
 * pk_debuginfo_install_add_deps:
 **/
static gboolean
pk_debuginfo_install_add_deps (PkDebuginfoInstallPrivate *priv, GPtrArray *packages_search, GPtrArray *packages_results, GError **error)
{
	gboolean ret = TRUE;
	PkResults *results = NULL;
	PkPackage *item;
	gchar *package_id = NULL;
	GPtrArray *list = NULL;
	GError *error_local = NULL;
	gchar **package_ids = NULL;
	gchar *name_debuginfo;
	guint i;
	gchar **split;
	PkError *error_code = NULL;

	/* get depends for them all, not adding dup's */
	package_ids = pk_ptr_array_to_strv (packages_search);
	results = pk_client_get_depends (priv->client, pk_bitfield_value (PK_FILTER_ENUM_NONE), package_ids, TRUE, NULL, NULL, NULL, &error_local);
	if (results == NULL) {
		*error = g_error_new (1, 0, "failed to get_depends: %s", error_local->message);
		g_error_free (error_local);
		ret = FALSE;
		goto out;
	}

	/* check error code */
	error_code = pk_results_get_error_code (results);
	if (error_code != NULL) {
		*error = g_error_new (1, 0, "failed to get depends: %s, %s", pk_error_enum_to_string (pk_error_get_code (error_code)), pk_error_get_details (error_code));
		ret = FALSE;
		goto out;
	}

	/* add dependent packages */
	list = pk_results_get_package_array (results);
	for (i=0; i<list->len; i++) {
		item = g_ptr_array_index (list, i);
		split = pk_package_id_split (pk_package_get_id (item));
		/* add -debuginfo */
		name_debuginfo = pk_debuginfo_install_name_to_debuginfo (split[PK_PACKAGE_ID_NAME]);
		g_strfreev (split);

		/* resolve name */
		g_debug ("resolving: %s", name_debuginfo);
		package_id = pk_debuginfo_install_resolve_name_to_id (priv, name_debuginfo, &error_local);
		if (package_id == NULL) {
			/* TRANSLATORS: we couldn't find the package name, non-fatal */
			g_print (_("Failed to find the package %s, or already installed: %s"), name_debuginfo, error_local->message);
			g_print ("\n");
			g_error_free (error_local);
			/* don't quit, this is non-fatal */
			error = NULL;
		}

		/* add to array to install */
		if (package_id != NULL && !g_str_has_suffix (package_id, "installed")) {
			g_debug ("going to try to install (for deps): %s", package_id);
			g_ptr_array_add (packages_results, g_strdup (package_id));
		}

		g_free (package_id);
		g_free (name_debuginfo);
	}
out:
	if (error_code != NULL)
		g_object_unref (error_code);
	if (results != NULL)
		g_object_unref (results);
	if (list != NULL)
		g_ptr_array_unref (list);
	g_strfreev (package_ids);
	return ret;
}