File: dnf-backend.c

package info (click to toggle)
packagekit 1.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,824 kB
  • sloc: ansic: 56,491; cpp: 15,652; xml: 5,532; python: 4,932; sh: 316; perl: 60; makefile: 56
file content (280 lines) | stat: -rw-r--r-- 8,721 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * 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
 */

#include "config.h"

#include <stdlib.h>
#include <glib.h>
#include <glib/gstdio.h>

#include <libdnf/libdnf.h>

#include "dnf-backend.h"

void
dnf_emit_package (PkBackendJob *job, PkInfoEnum info, DnfPackage *pkg)
{
	PkInfoEnum update_severity;

	update_severity = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (pkg), PK_DNF_UPDATE_SEVERITY_KEY));

	/* detect */
	if (info == PK_INFO_ENUM_UNKNOWN)
		info = (PkInfoEnum) dnf_package_get_info (pkg);
	if (info == PK_INFO_ENUM_UNKNOWN)
		info = (PkInfoEnum) dnf_package_installed (pkg) ? PK_INFO_ENUM_INSTALLED : PK_INFO_ENUM_AVAILABLE;
	pk_backend_job_package_full (job,
				     info,
				     dnf_package_get_package_id (pkg),
				     dnf_package_get_summary (pkg),
				     update_severity);
}

void
dnf_emit_package_list (PkBackendJob *job,
		       PkInfoEnum info,
		       GPtrArray *pkglist)
{
	g_autoptr(GPtrArray) pk_packages = g_ptr_array_new_full (pkglist->len, (GDestroyNotify) g_object_unref);

	for (guint i = 0; i < pkglist->len; i++) {
		DnfPackage *dnf_package;
		g_autoptr(PkPackage) pk_package = NULL;
		PkInfoEnum package_info;
		PkInfoEnum update_severity;
		const gchar *package_id;
		g_autoptr(GError) local_error = NULL;

		dnf_package = g_ptr_array_index (pkglist, i);
		package_id = dnf_package_get_package_id (dnf_package);
		update_severity = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (dnf_package), PK_DNF_UPDATE_SEVERITY_KEY));

		package_info = info;
		if (package_info == PK_INFO_ENUM_UNKNOWN)
			package_info = (PkInfoEnum) dnf_package_get_info (dnf_package);
		if (package_info == PK_INFO_ENUM_UNKNOWN)
			package_info = (PkInfoEnum) dnf_package_installed (dnf_package) ? PK_INFO_ENUM_INSTALLED : PK_INFO_ENUM_AVAILABLE;

		/* check we are valid */
		pk_package = pk_package_new ();
		if (!pk_package_set_id (pk_package, package_id, &local_error)) {
			g_warning ("package_id %s invalid and cannot be processed: %s",
				   package_id, local_error->message);
			continue;
		}

		pk_package_set_info (pk_package, package_info);
		pk_package_set_update_severity (pk_package, update_severity);
		pk_package_set_summary (pk_package, dnf_package_get_summary (dnf_package));

		g_ptr_array_add (pk_packages, g_steal_pointer (&pk_package));
	}

	if (pk_packages->len > 0)
		pk_backend_job_packages (job, pk_packages);
}

void
dnf_emit_package_array (PkBackendJob *job,
			 PkInfoEnum info,
			 GPtrArray *array)
{
	dnf_emit_package_list (job, info, array);
}

void
dnf_emit_package_list_filter (PkBackendJob *job,
			      PkBitfield filters,
			      GPtrArray *pkglist)
{
	DnfPackage *found;
	DnfPackage *pkg;
	guint i;
	g_autoptr(GHashTable) hash_cost = NULL;
	g_autoptr(GHashTable) hash_installed = NULL;
	g_autoptr(GPtrArray) filtered_list = g_ptr_array_new_with_free_func (NULL);

	/* if a package exists in multiple repos, show the one with the lowest
	 * cost of downloading */
	hash_cost = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	for (i = 0; i < pkglist->len; i++) {
		pkg = g_ptr_array_index (pkglist, i);
		if (dnf_package_installed (pkg))
			continue;

		/* if the NEVRA does not already exist in the array, just add */
		found = g_hash_table_lookup (hash_cost,
					     dnf_package_get_nevra (pkg));
		if (found == NULL) {
			g_hash_table_insert (hash_cost,
					     g_strdup (dnf_package_get_nevra (pkg)),
					     (gpointer) pkg);
			continue;
		}

		/* a lower cost package */
		if (dnf_package_get_cost (pkg) < dnf_package_get_cost (found)) {
			dnf_package_set_info (found, (DnfPackageInfo) PK_INFO_ENUM_BLOCKED);
			g_hash_table_replace (hash_cost,
					      g_strdup (dnf_package_get_nevra (pkg)),
					      (gpointer) pkg);
		} else {
			dnf_package_set_info (pkg, (DnfPackageInfo) PK_INFO_ENUM_BLOCKED);
		}
	}

	/* add all the installed packages to a hash */
	hash_installed = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	for (i = 0; i < pkglist->len; i++) {
		pkg = g_ptr_array_index (pkglist, i);
		if (!dnf_package_installed (pkg))
			continue;
		g_hash_table_insert (hash_installed,
				     g_strdup (dnf_package_get_nevra (pkg)),
				     (gpointer) pkg);
	}

	/* anything remote in metadata-only mode needs to be unavailable */
	for (i = 0; i < pkglist->len; i++) {
		DnfRepo *repo;
		pkg = g_ptr_array_index (pkglist, i);
		if (dnf_package_installed (pkg))
			continue;
		repo = dnf_package_get_repo (pkg);
		if (repo == NULL)
			continue;
		if (dnf_repo_get_enabled (repo) != DNF_REPO_ENABLED_METADATA)
			continue;
		dnf_package_set_info (pkg, (DnfPackageInfo) PK_INFO_ENUM_UNAVAILABLE);
	}

	for (i = 0; i < pkglist->len; i++) {
		pkg = g_ptr_array_index (pkglist, i);

		/* blocked */
		if ((PkInfoEnum) dnf_package_get_info (pkg) == PK_INFO_ENUM_BLOCKED)
			continue;

		/* GUI */
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_GUI) && !dnf_package_is_gui (pkg))
			continue;
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_GUI) && dnf_package_is_gui (pkg))
			continue;

		/* DEVELOPMENT */
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_DEVELOPMENT) && !dnf_package_is_devel (pkg))
			continue;
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_DEVELOPMENT) && dnf_package_is_devel (pkg))
			continue;

		/* DOWNLOADED */
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_DOWNLOADED) && !dnf_package_is_downloaded (pkg))
			continue;
		if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_DOWNLOADED) && dnf_package_is_downloaded (pkg))
			continue;

		/* if this package is available and the very same NEVRA is
		 * installed, skip this package */
		if (!dnf_package_installed (pkg)) {
			found = g_hash_table_lookup (hash_installed,
						     dnf_package_get_nevra (pkg));
			if (found != NULL)
				continue;
		}

		g_ptr_array_add (filtered_list, pkg);
	}

	dnf_emit_package_list (job, PK_INFO_ENUM_UNKNOWN, filtered_list);
}

PkInfoEnum
dnf_advisory_kind_to_info_enum (DnfAdvisoryKind kind)
{
	PkInfoEnum info_enum = PK_INFO_ENUM_UNKNOWN;
	switch (kind) {
	case DNF_ADVISORY_KIND_SECURITY:
		info_enum = PK_INFO_ENUM_SECURITY;
		break;
	case DNF_ADVISORY_KIND_BUGFIX:
		info_enum = PK_INFO_ENUM_BUGFIX;
		break;
	case DNF_ADVISORY_KIND_UNKNOWN:
		info_enum = PK_INFO_ENUM_NORMAL;
		break;
	case DNF_ADVISORY_KIND_ENHANCEMENT:
		info_enum = PK_INFO_ENUM_ENHANCEMENT;
		break;
	default:
		g_warning ("Failed to find DnfAdvisoryKind enum %i", kind);
		break;
	}
	return info_enum;
}

PkInfoEnum
dnf_update_severity_to_enum (const gchar *severity)
{
	if (severity == NULL || severity[0] == '\0')
		return PK_INFO_ENUM_UNKNOWN;
	if (g_ascii_strcasecmp (severity, "None") == 0)
		return PK_INFO_ENUM_UNKNOWN;
	if (g_ascii_strcasecmp (severity, "Low") == 0)
		return PK_INFO_ENUM_LOW;
	if (g_ascii_strcasecmp (severity, "Moderate") == 0)
		return PK_INFO_ENUM_NORMAL;
	if (g_ascii_strcasecmp (severity, "Important") == 0)
		return PK_INFO_ENUM_IMPORTANT;
	if (g_ascii_strcasecmp (severity, "Critical") == 0)
		return PK_INFO_ENUM_CRITICAL;

	g_warning ("Failed to map update severity '%s', returning Unknown", severity);

	return PK_INFO_ENUM_UNKNOWN;
}

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;
}