File: dnf5-backend-utils.cpp

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 (593 lines) | stat: -rw-r--r-- 19,489 bytes parent folder | download
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2025 Neal Gompa <neal@gompa.dev>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include "dnf5-backend-utils.hpp"
#include <packagekit-glib2/pk-common-private.h>
#include <packagekit-glib2/pk-update-detail.h>
#include <libdnf5/conf/config_parser.hpp>
#include <libdnf5/conf/const.hpp>
#include <libdnf5/logger/logger.hpp>
#include <libdnf5/rpm/arch.hpp>
#include <libdnf5/repo/package_downloader.hpp>
#include <libdnf5/base/goal.hpp>
#include <libdnf5/advisory/advisory_query.hpp>
#include <libdnf5/rpm/reldep_list.hpp>
#include <libdnf5/base/transaction.hpp>
#include <rpm/rpmlib.h>
#include <glib/gstdio.h>
#include <algorithm>
#include <set>
#include <queue>
#include <filesystem>
#include <map>
#include "dnf5-backend-vendor.hpp"

void
dnf5_setup_base (PkBackendDnf5Private *priv, gboolean refresh, gboolean force, const char *releasever)
{
	priv->base = std::make_unique<libdnf5::Base>();

	priv->base->load_config();

	auto &config = priv->base->get_config();
	if (priv->conf != NULL) {
		g_autofree gchar *destdir = g_key_file_get_string (priv->conf, "Daemon", "DestDir", NULL);
		if (destdir != NULL) {
			config.get_installroot_option().set(libdnf5::Option::Priority::COMMANDLINE, destdir);
		}

		gboolean keep_cache = g_key_file_get_boolean (priv->conf, "Daemon", "KeepCache", NULL);
		config.get_keepcache_option().set(libdnf5::Option::Priority::COMMANDLINE, keep_cache != FALSE);

		g_autofree gchar *distro_version = NULL;
		if (releasever == NULL) {
			g_autoptr(GError) error = NULL;
			distro_version = pk_get_distro_version_id (&error);
		} else {
			distro_version = g_strdup(releasever);
		}

		if (distro_version != NULL) {
			priv->base->get_vars()->set("releasever", distro_version);
			const char *root = (destdir != NULL) ? destdir : "/";
			g_autofree gchar *cache_dir = g_build_filename (root, "/var/cache/PackageKit", distro_version, "metadata", NULL);
			g_debug("Using cachedir: %s", cache_dir);
			config.get_cachedir_option().set(libdnf5::Option::Priority::COMMANDLINE, cache_dir);
		}

		auto &optional_metadata_types = config.get_optional_metadata_types_option();
		auto &optional_metadata_types_setting = optional_metadata_types.get_value();
		if (!optional_metadata_types_setting.contains(libdnf5::METADATA_TYPE_ALL)) {
			// Ensure all required repodata types are downloaded
			if(!optional_metadata_types_setting.contains(libdnf5::METADATA_TYPE_COMPS)) {
				optional_metadata_types.add_item(libdnf5::Option::Priority::RUNTIME, libdnf5::METADATA_TYPE_COMPS);
			}
			if(!optional_metadata_types_setting.contains(libdnf5::METADATA_TYPE_UPDATEINFO)) {
				optional_metadata_types.add_item(libdnf5::Option::Priority::RUNTIME, libdnf5::METADATA_TYPE_UPDATEINFO);
			}
			if(!optional_metadata_types_setting.contains(libdnf5::METADATA_TYPE_APPSTREAM)) {
				optional_metadata_types.add_item(libdnf5::Option::Priority::RUNTIME, libdnf5::METADATA_TYPE_APPSTREAM);
			}
		}
		
		// Always assume yes to avoid interactive prompts failing the transaction
		// TODO: Drop this once InstallSignature is implemented
		config.get_assumeyes_option().set(libdnf5::Option::Priority::COMMANDLINE, true);
	}

	priv->base->setup();
	
	// Ensure releasever is set AFTER setup() because setup() might run auto-detection and overwrite it.
	if (priv->conf != NULL) {
		g_autofree gchar *distro_version = NULL;
		if (releasever == NULL) {
			g_autoptr(GError) error = NULL;
			distro_version = pk_get_distro_version_id (&error);
		} else {
			distro_version = g_strdup(releasever);
		}
		if (distro_version != NULL) {
			priv->base->get_vars()->set("releasever", distro_version);
		}
	}
	
	auto repo_sack = priv->base->get_repo_sack();
	repo_sack->create_repos_from_system_configuration();
	repo_sack->get_system_repo();

	if (refresh && force) {
		libdnf5::repo::RepoQuery query(*priv->base);
		for (auto repo : query) {
			if (repo->is_enabled()) {
				g_debug("Expiring repository metadata: %s", repo->get_id().c_str());
				repo->expire();
			}
		}
	}

	g_debug("Loading repositories");
	repo_sack->load_repos();

	libdnf5::repo::RepoQuery query(*priv->base);
	query.filter_enabled(true);
	for (auto repo : query) {
		g_debug("Enabled repository: %s", repo->get_id().c_str());
	}
}

void
dnf5_refresh_cache(PkBackendDnf5Private *priv, gboolean force)
{
	dnf5_setup_base(priv, TRUE, force);
}

PkInfoEnum
dnf5_advisory_kind_to_info_enum (const std::string &type)
{
	if (type == "security")
		return PK_INFO_ENUM_SECURITY;
	if (type == "bugfix")
		return PK_INFO_ENUM_BUGFIX;
	if (type == "enhancement")
		return PK_INFO_ENUM_ENHANCEMENT;
	if (type == "newpackage")
		return PK_INFO_ENUM_NORMAL;
	return PK_INFO_ENUM_UNKNOWN;
}

PkInfoEnum
dnf5_update_severity_to_enum (const std::string &severity)
{
	if (severity == "low")
		return PK_INFO_ENUM_LOW;
	if (severity == "moderate")
		return PK_INFO_ENUM_NORMAL;
	if (severity == "important")
		return PK_INFO_ENUM_IMPORTANT;
	if (severity == "critical")
		return PK_INFO_ENUM_CRITICAL;
	return PK_INFO_ENUM_UNKNOWN;
}

bool
dnf5_force_distupgrade_on_upgrade (libdnf5::Base &base)
{
	std::vector<std::string> distroverpkg_names = { "system-release", "distribution-release" };
	std::vector<std::string> distupgrade_provides = { "system-upgrade(dsync)", "product-upgrade() = dup" };

	libdnf5::rpm::PackageQuery query(base);
	query.filter_installed();
	query.filter_name(distroverpkg_names);
	query.filter_provides(distupgrade_provides);

	return !query.empty();
}

bool
dnf5_repo_is_devel (const libdnf5::repo::Repo &repo)
{
	std::string id = repo.get_id();
	return (id.ends_with("-debuginfo") || id.ends_with("-debugsource") || id.ends_with("-devel"));
}

bool
dnf5_repo_is_source (const libdnf5::repo::Repo &repo)
{
	std::string id = repo.get_id();
	return id.ends_with("-source");
}

bool
dnf5_repo_is_supported (const libdnf5::repo::Repo &repo)
{
	return dnf5_validate_supported_repo(repo.get_id());
}

bool
dnf5_backend_pk_repo_filter (const libdnf5::repo::Repo &repo, PkBitfield filters)
{
	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_DEVELOPMENT) && !dnf5_repo_is_devel (repo))
		return false;
	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_DEVELOPMENT) && dnf5_repo_is_devel (repo))
		return false;

	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_SOURCE) && !dnf5_repo_is_source (repo))
		return false;
	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_SOURCE) && dnf5_repo_is_source (repo))
		return false;

	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_INSTALLED) && !repo.is_enabled())
		return false;
	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_INSTALLED) && repo.is_enabled())
		return false;

	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_SUPPORTED) && !dnf5_repo_is_supported (repo))
		return false;
	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_SUPPORTED) && dnf5_repo_is_supported (repo))
		return false;

	return true;
}

bool
dnf5_package_is_gui (const libdnf5::rpm::Package &pkg)
{
	for (const auto &provide : pkg.get_provides()) {
		std::string name = provide.get_name();
		if (name.starts_with("application("))
			return true;
	}
	return false;
}

bool
dnf5_package_filter (const libdnf5::rpm::Package &pkg, PkBitfield filters)
{
	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_GUI) && !dnf5_package_is_gui (pkg))
		return false;
	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_GUI) && dnf5_package_is_gui (pkg))
		return false;

	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_DOWNLOADED) && !pkg.is_available_locally())
		return false;
	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_DOWNLOADED) && pkg.is_available_locally())
		return false;

	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_DEVELOPMENT) ||
	    pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_DEVELOPMENT) ||
	    pk_bitfield_contain (filters, PK_FILTER_ENUM_SOURCE) ||
	    pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_SOURCE) ||
	    pk_bitfield_contain (filters, PK_FILTER_ENUM_SUPPORTED) ||
	    pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_SUPPORTED)) {
		auto repo_weak = pkg.get_repo();
		if (repo_weak.is_valid()) {
			if (!dnf5_backend_pk_repo_filter(*repo_weak, filters))
				return false;
		}
	}

	return true;
}

std::vector<libdnf5::rpm::Package>
dnf5_process_dependency (libdnf5::Base &base, const libdnf5::rpm::Package &pkg, PkRoleEnum role, gboolean recursive)
{
	std::vector<libdnf5::rpm::Package> results;
	std::set<std::string> visited;
	std::queue<libdnf5::rpm::Package> queue;
	queue.push(pkg);
	visited.insert(pkg.get_name() + ";" + pkg.get_evr() + ";" + pkg.get_arch());
	
	while (!queue.empty()) {
		auto curr = queue.front();
		queue.pop();
		libdnf5::rpm::ReldepList reldeps(base);
		if (role == PK_ROLE_ENUM_DEPENDS_ON) reldeps = curr.get_requires();
		else reldeps = curr.get_provides();
		
		for (const auto &reldep : reldeps) {
			std::string req = reldep.to_string();
			libdnf5::rpm::PackageQuery query(base);
			if (role == PK_ROLE_ENUM_DEPENDS_ON) query.filter_provides(req);
			else query.filter_requires(req);
			
			// Filter for latest version and supported architectures to avoid duplicates
			// for available packages
			query.filter_latest_evr();
			query.filter_arch(libdnf5::rpm::get_supported_arches());
			
			for (const auto &res : query) {
				std::string res_nevra = res.get_name() + ";" + res.get_evr() + ";" + res.get_arch();
				if (visited.find(res_nevra) == visited.end()) {
					visited.insert(res_nevra);
					results.push_back(res);
					if (recursive) queue.push(res);
				}
			}
		}
	}
	return results;
}

void
dnf5_emit_pkg (PkBackendJob *job, const libdnf5::rpm::Package &pkg, PkInfoEnum info, PkInfoEnum severity)
{
	if (info == PK_INFO_ENUM_UNKNOWN) {
		info = PK_INFO_ENUM_AVAILABLE;
		if (pkg.get_install_time() > 0) {
			info = PK_INFO_ENUM_INSTALLED;
		}
	}
	
	std::string evr = pkg.get_evr();
	std::string repo_id = pkg.get_repo_id();
	if (pkg.get_install_time() > 0) {
		repo_id = "installed";
	}
	
	std::string package_id = pkg.get_name() + ";" + evr + ";" + pkg.get_arch() + ";" + repo_id;
	if (severity != PK_INFO_ENUM_UNKNOWN) {
		pk_backend_job_package_full (job, info, package_id.c_str(), pkg.get_summary().c_str(), severity);
	} else {
		pk_backend_job_package (job, info, package_id.c_str(), pkg.get_summary().c_str());
	}
}

void
dnf5_sort_and_emit (PkBackendJob *job, std::vector<libdnf5::rpm::Package> &pkgs)
{
	std::sort(pkgs.begin(), pkgs.end(), [](const libdnf5::rpm::Package &a, const libdnf5::rpm::Package &b) {
		bool a_installed = (a.get_install_time() > 0);
		bool b_installed = (b.get_install_time() > 0);
		if (a_installed != b_installed) return a_installed; 
		if (a.get_name() != b.get_name()) return a.get_name() < b.get_name();
		if (a.get_arch() != b.get_arch()) return a.get_arch() < b.get_arch();
		return a.get_evr() < b.get_evr();
	});

	std::set<std::string> seen_nevras;
	for (auto &pkg : pkgs) {
		std::string nevra = pkg.get_name() + ";" + pkg.get_evr() + ";" + pkg.get_arch();
		if (seen_nevras.find(nevra) == seen_nevras.end()) {
			dnf5_emit_pkg(job, pkg);
			seen_nevras.insert(nevra);
		}
	}
}

void
dnf5_apply_filters (libdnf5::Base &base, libdnf5::rpm::PackageQuery &query, PkBitfield filters)
{
	gboolean installed = pk_bitfield_contain (filters, PK_FILTER_ENUM_INSTALLED);
	gboolean available = pk_bitfield_contain (filters, PK_FILTER_ENUM_NOT_INSTALLED);

	if (installed && !available) {
		query.filter_installed();
	} else if (!installed && available) {
		query.filter_available();
	}

	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_ARCH)) {
		auto vars = base.get_vars();
		if (vars.is_valid()) {
			std::string arch = vars->get_value("arch");
			if (!arch.empty()) {
				query.filter_arch({arch, "noarch"});
			} else {
				query.filter_arch(libdnf5::rpm::get_supported_arches());
			}
		}
	}

	if (pk_bitfield_contain (filters, PK_FILTER_ENUM_NEWEST)) {
		query.filter_latest_evr();
	}
}

std::vector<libdnf5::rpm::Package>
dnf5_resolve_package_ids(libdnf5::Base &base, gchar **package_ids)
{
	std::vector<libdnf5::rpm::Package> pkgs;
	if (!package_ids) return pkgs;
	
	for (int i = 0; package_ids[i] != NULL; i++) {
		// Check if this is a simple package name (no semicolons) or a full package ID
		if (strchr(package_ids[i], ';') == NULL) {
			// Simple package name - search by name and get latest available
			try {
				g_debug("Resolving simple package name: %s", package_ids[i]);
				libdnf5::rpm::PackageQuery query(base);
				query.filter_name(std::string(package_ids[i]), libdnf5::sack::QueryCmp::EQ);
				query.filter_available();
				query.filter_latest_evr();
				query.filter_arch(libdnf5::rpm::get_supported_arches());

				
				if (!query.empty()) {
					for (auto pkg : query) {
						g_debug("Found package: name=%s, evr=%s, arch=%s, repo=%s",
							pkg.get_name().c_str(), pkg.get_evr().c_str(), 
							pkg.get_arch().c_str(), pkg.get_repo_id().c_str());
						pkgs.push_back(pkg);
						break; // Take the first match
					}
				} else {
					g_debug("No available package found for name: %s", package_ids[i]);
				}
			} catch (const std::exception &e) {
				g_debug("Exception resolving package name %s: %s", package_ids[i], e.what());
			}
			continue;
		}
		
		// Full package ID - use existing logic
		g_auto(GStrv) split = pk_package_id_split(package_ids[i]);
		if (!split) continue;
		
		try {
			libdnf5::rpm::PackageQuery query(base);
			g_debug("Resolving package ID: name=%s, version=%s, arch=%s, repo=%s",
				split[PK_PACKAGE_ID_NAME], split[PK_PACKAGE_ID_VERSION],
				split[PK_PACKAGE_ID_ARCH], split[PK_PACKAGE_ID_DATA]);
			query.filter_name(split[PK_PACKAGE_ID_NAME]);
			query.filter_evr(split[PK_PACKAGE_ID_VERSION]);
			query.filter_arch(split[PK_PACKAGE_ID_ARCH]);
			
			if (g_strcmp0(split[PK_PACKAGE_ID_DATA], "installed") == 0) {
				query.filter_installed();
			} else {
				 query.filter_repo_id(split[PK_PACKAGE_ID_DATA]);
			}
			
			if (query.empty()) {
				g_debug("No exact match for ID: %s. Listing similar packages...", package_ids[i]);
				libdnf5::rpm::PackageQuery fallback(base);
				fallback.filter_name(split[PK_PACKAGE_ID_NAME]);
				for (const auto &p : fallback) {
					g_debug("Found similar package: name=%s, evr=%s, arch=%s, repo=%s",
						p.get_name().c_str(), p.get_evr().c_str(), p.get_arch().c_str(), p.get_repo_id().c_str());
				}
			}

			for (auto pkg : query) {
				pkgs.push_back(pkg);
				break;
			}
		} catch (const std::exception &e) {
			g_debug("Exception resolving package ID %s: %s", package_ids[i], e.what());
		}
	}
	return pkgs;
}


void
dnf5_remove_old_cache_directories (PkBackend *backend, const gchar *release_ver)
{
	PkBackendDnf5Private *priv = (PkBackendDnf5Private *) pk_backend_get_user_data (backend);
	g_assert (priv->conf != NULL);

	/* cache cleanup disabled? */
	if (g_key_file_get_boolean (priv->conf, "Daemon", "KeepCache", NULL)) {
		g_debug ("KeepCache config option set; skipping old cache directory cleanup");
		return;
	}

	/* only do cache cleanup for regular installs */
	g_autofree gchar *destdir = g_key_file_get_string (priv->conf, "Daemon", "DestDir", NULL);
	if (destdir != NULL) {
		g_debug ("DestDir config option set; skipping old cache directory cleanup");
		return;
	}

	std::filesystem::path cache_path("/var/cache/PackageKit");
	if (!std::filesystem::exists(cache_path) || !std::filesystem::is_directory(cache_path))
		return;

	/* look at each subdirectory */
	for (const auto &entry : std::filesystem::directory_iterator(cache_path)) {
		if (!entry.is_directory())
			continue;

		std::string filename = entry.path().filename().string();

		/* is the version older than the current release ver? */
		if (rpmvercmp (filename.c_str(), release_ver) < 0) {
			g_debug ("removing old cache directory %s", entry.path().c_str());
			std::error_code ec;
			std::filesystem::remove_all(entry.path(), ec);
			if (ec)
				g_warning ("failed to remove directory %s: %s", entry.path().c_str(), ec.message().c_str());
		}
	}
}

Dnf5DownloadCallbacks::Dnf5DownloadCallbacks(PkBackendJob *job, uint64_t total_size)
    : job(job), total_size(total_size), finished_size(0), next_id(1) {}

void *
Dnf5DownloadCallbacks::add_new_download(void *user_data, const char *description, double total_to_download)
{
	std::lock_guard<std::mutex> lock(mutex);
	void *id = reinterpret_cast<void*>(next_id++);
	item_progress[id] = 0;
	return id;
}

int
Dnf5DownloadCallbacks::progress(void *user_cb_data, double total_to_download, double downloaded)
{
	std::lock_guard<std::mutex> lock(mutex);
	item_progress[user_cb_data] = downloaded;
	
	if (total_size > 0) {
		double current_total = finished_size;
		for (auto const& [id, prog] : item_progress) {
			current_total += prog;
		}
		pk_backend_job_set_percentage(job, (uint)(current_total * 100 / total_size));
	}
	return 0;
}

int
Dnf5DownloadCallbacks::end(void *user_cb_data, TransferStatus status, const char *msg)
{
	std::lock_guard<std::mutex> lock(mutex);
	finished_size += item_progress[user_cb_data];
	item_progress.erase(user_cb_data);
	return 0;
}

Dnf5TransactionCallbacks::Dnf5TransactionCallbacks(PkBackendJob *job)
    : job(job), total_items(0), current_item_index(0) {}

void
Dnf5TransactionCallbacks::before_begin(uint64_t total)
{
	total_items = total;
}

void
Dnf5TransactionCallbacks::elem_progress(const libdnf5::base::TransactionPackage &item, uint64_t amount, uint64_t total)
{
	current_item_index = amount;
}

void
Dnf5TransactionCallbacks::install_progress(const libdnf5::base::TransactionPackage &item, uint64_t amount, uint64_t total)
{
	if (total_items > 0 && total > 0) {
		double item_frac = (double)amount / total;
		pk_backend_job_set_percentage(job, (uint)((current_item_index + item_frac) * 100 / total_items));
	}
}

void
Dnf5TransactionCallbacks::install_start(const libdnf5::base::TransactionPackage &item, uint64_t total)
{
	auto action = item.get_action();
	PkInfoEnum info = PK_INFO_ENUM_INSTALLING;
	if (action == libdnf5::transaction::TransactionItemAction::UPGRADE ||
	    action == libdnf5::transaction::TransactionItemAction::DOWNGRADE) {
		info = PK_INFO_ENUM_UPDATING;
	}
	dnf5_emit_pkg(job, item.get_package(), info);
}

void
Dnf5TransactionCallbacks::uninstall_progress(const libdnf5::base::TransactionPackage &item, uint64_t amount, uint64_t total)
{
	if (total_items > 0 && total > 0) {
		double item_frac = (double)amount / total;
		pk_backend_job_set_percentage(job, (uint)((current_item_index + item_frac) * 100 / total_items));
	}
}

void
Dnf5TransactionCallbacks::uninstall_start(const libdnf5::base::TransactionPackage &item, uint64_t total)
{
	auto action = item.get_action();
	PkInfoEnum info = PK_INFO_ENUM_REMOVING;
	if (action == libdnf5::transaction::TransactionItemAction::REPLACED) {
		info = PK_INFO_ENUM_CLEANUP;
	}
	dnf5_emit_pkg(job, item.get_package(), info);
}