From 425058be365c608334cd1d48db597cf2ab6a64a7 Mon Sep 17 00:00:00 2001 From: Carson Black Date: Sat, 28 Mar 2020 20:31:27 -0400 Subject: alpm: improve dlcb handling --- backends/alpm/pk-alpm-transaction.c | 69 ++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/backends/alpm/pk-alpm-transaction.c b/backends/alpm/pk-alpm-transaction.c index ff4c1ad8c..c81380665 100644 --- a/backends/alpm/pk-alpm-transaction.c +++ b/backends/alpm/pk-alpm-transaction.c @@ -28,8 +28,8 @@ #include -static off_t dcomplete = 0; -static off_t dtotal = 0; +static off_t transaction_dcomplete = 0; +static off_t transaction_dtotal = 0; static alpm_pkg_t *dpkg = NULL; static GString *dfiles = NULL; @@ -136,11 +136,11 @@ pk_alpm_transaction_totaldlcb (off_t total) g_assert (pkalpm_current_job); job = pkalpm_current_job; - if (dtotal > 0 && dpkg != NULL) + if (transaction_dtotal > 0 && dpkg != NULL) pk_alpm_transaction_download_end (job); - dcomplete = 0; - dtotal = total; + transaction_dcomplete = 0; + transaction_dtotal = total; } static void @@ -152,35 +152,48 @@ pk_alpm_transaction_dlcb (const gchar *basename, off_t complete, off_t total) g_assert (pkalpm_current_job); job = pkalpm_current_job; - /* download start signal */ - if (total == -1) return; + g_return_if_fail(basename != NULL); - g_return_if_fail (basename != NULL); - g_return_if_fail (complete <= total); + // these conditions are documented in libalpm/dload.c + if (complete == 0 && total == -1) { // initialized download + g_debug ("downloading file %s", basename); + pk_backend_job_set_status (job, PK_STATUS_ENUM_DOWNLOAD); + pk_alpm_transaction_download_start (job, basename); - if (total > 0) - sub_percentage = complete * 100 / total; + } else if (complete == 0 && total == 0) { // doing non-download event + return; - if (dtotal > 0) { - percentage = (dcomplete + complete) * 100 / dtotal; - } else if (dtotal < 0) { - /* database files */ - percentage = (dcomplete * 100 + sub_percentage) / -dtotal; + } else if (complete > 0 && complete == total) { // download is complete + pk_backend_job_set_percentage(job, 100); + transaction_dcomplete += complete; + + } else if (complete > 0 && complete < total && total > 0) { // download in progress + sub_percentage = (complete * 100) / (total); + if (transaction_dtotal > 0) { + // positive totals indicate packages + percentage = ((transaction_dcomplete + complete) * 100) / transaction_dtotal; + + pk_backend_job_set_percentage (job, percentage); + } else if (transaction_dtotal < 0) { + // negative totals indicate databases + guint total_databases = -transaction_dtotal; + static off_t previous_total = 0; + static guint current_database = 0; + + if (total != previous_total) { + current_database++; + previous_total = total; + } - if (complete == total) - complete = total = 1; - else - complete = total + 1; - } + percentage = ((current_database-1)*100) / total_databases; + percentage += sub_percentage / total_databases; - if (complete == 0) { - g_debug ("downloading file %s", basename); - pk_backend_job_set_status (job, PK_STATUS_ENUM_DOWNLOAD); - pk_alpm_transaction_download_start (job, basename); - } else if (complete == total) { - dcomplete += complete; + pk_backend_job_set_percentage (job, percentage); + } + + } else { + syslog (LOG_DAEMON | LOG_WARNING, "unhandled download callback case, most likely libalpm change or error"); } - pk_backend_job_set_percentage (job, percentage); } static void -- cgit v1.2.3