summaryrefslogtreecommitdiff
path: root/src/pacman/callback.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pacman/callback.c')
-rw-r--r--src/pacman/callback.c142
1 files changed, 63 insertions, 79 deletions
diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index a7686483..ff125c36 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -23,6 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
+#include <sys/types.h> /* off_t */
#include <unistd.h>
#include <dirent.h>
#include <wchar.h>
@@ -34,12 +35,11 @@
#include "util.h"
#include "conf.h"
-/* TODO this should not have to be defined twice- trans.c & log.c */
-#define LOG_STR_LEN 256
-
/* download progress bar */
static float rate_last;
-static int xfered_last;
+static off_t xfered_last;
+static off_t list_xfered = 0.0;
+static off_t list_total = 0.0;
static struct timeval initial_time;
/* transaction progress bar */
@@ -86,17 +86,15 @@ static float get_update_timediff(int first_call)
}
/* refactored from cb_trans_progress */
-static void fill_progress(const int graph_percent, const int display_percent,
+static void fill_progress(const int bar_percent, const int disp_percent,
const int proglen)
{
const unsigned int hashlen = proglen - 8;
- const unsigned int hash = graph_percent * hashlen / 100;
+ const unsigned int hash = bar_percent * hashlen / 100;
static unsigned int lasthash = 0, mouth = 0;
unsigned int i;
- /* printf("\ndebug: proglen: %i\n", proglen); DEBUG*/
-
- if(graph_percent == 0) {
+ if(bar_percent == 0) {
lasthash = 0;
mouth = 0;
}
@@ -141,10 +139,12 @@ static void fill_progress(const int graph_percent, const int display_percent,
}
/* print percent after progress bar */
if(proglen > 5) {
- printf(" %3d%%", display_percent);
+ /* use disp_percent if it is not 0, else show bar_percent */
+ int p = disp_percent ? disp_percent : bar_percent;
+ printf(" %3d%%", p);
}
- if(graph_percent == 100) {
+ if(bar_percent == 100) {
printf("\n");
} else {
printf("\r");
@@ -157,8 +157,6 @@ static void fill_progress(const int graph_percent, const int display_percent,
/* callback to handle messages/notifications from libalpm transactions */
void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
{
- char str[LOG_STR_LEN] = "";
-
switch(event) {
case PM_TRANS_EVT_CHECKDEPS_START:
printf(_("checking dependencies...\n"));
@@ -180,10 +178,9 @@ void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
}
break;
case PM_TRANS_EVT_ADD_DONE:
- snprintf(str, LOG_STR_LEN, "installed %s (%s)\n",
+ alpm_logaction("installed %s (%s)\n",
alpm_pkg_get_name(data1),
alpm_pkg_get_version(data1));
- alpm_logaction(str);
break;
case PM_TRANS_EVT_REMOVE_START:
if(config->noprogressbar) {
@@ -191,10 +188,9 @@ void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
}
break;
case PM_TRANS_EVT_REMOVE_DONE:
- snprintf(str, LOG_STR_LEN, "removed %s (%s)\n",
+ alpm_logaction("removed %s (%s)\n",
alpm_pkg_get_name(data1),
alpm_pkg_get_version(data1));
- alpm_logaction(str);
break;
case PM_TRANS_EVT_UPGRADE_START:
if(config->noprogressbar) {
@@ -202,11 +198,10 @@ void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
}
break;
case PM_TRANS_EVT_UPGRADE_DONE:
- snprintf(str, LOG_STR_LEN, "upgraded %s (%s -> %s)\n",
+ alpm_logaction("upgraded %s (%s -> %s)\n",
(char *)alpm_pkg_get_name(data1),
(char *)alpm_pkg_get_version(data2),
(char *)alpm_pkg_get_version(data1));
- alpm_logaction(str);
break;
case PM_TRANS_EVT_INTEGRITY_START:
printf(_("checking package integrity...\n"));
@@ -237,7 +232,6 @@ void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
break;
/* all the simple done events, with fallthrough for each */
case PM_TRANS_EVT_FILECONFLICTS_DONE:
- case PM_TRANS_EVT_EXTRACT_DONE:
case PM_TRANS_EVT_CHECKDEPS_DONE:
case PM_TRANS_EVT_RESOLVEDEPS_DONE:
case PM_TRANS_EVT_INTERCONFLICTS_DONE:
@@ -255,67 +249,46 @@ void cb_trans_evt(pmtransevt_t event, void *data1, void *data2)
void cb_trans_conv(pmtransconv_t event, void *data1, void *data2,
void *data3, int *response)
{
- char str[LOG_STR_LEN] = "";
-
switch(event) {
case PM_TRANS_CONV_INSTALL_IGNOREPKG:
if(data2) {
/* TODO we take this route based on data2 being not null? WTF */
- snprintf(str, LOG_STR_LEN, _(":: %s requires installing %s from IgnorePkg/IgnoreGroup. Install anyway? [Y/n] "),
+ *response = yesno(1, _(":: %s requires installing %s from IgnorePkg/IgnoreGroup. Install anyway?"),
alpm_pkg_get_name(data1),
alpm_pkg_get_name(data2));
- *response = yesno(str);
} else {
- snprintf(str, LOG_STR_LEN, _(":: %s is in IgnorePkg/IgnoreGroup. Install anyway? [Y/n] "),
+ *response = yesno(1, _(":: %s is in IgnorePkg/IgnoreGroup. Install anyway?"),
alpm_pkg_get_name(data1));
- *response = yesno(str);
}
break;
case PM_TRANS_CONV_REMOVE_HOLDPKG:
- snprintf(str, LOG_STR_LEN, _(":: %s is designated as a HoldPkg. Remove anyway? [Y/n] "),
+ *response = yesno(1, _(":: %s is designated as a HoldPkg. Remove anyway?"),
alpm_pkg_get_name(data1));
- *response = yesno(str);
break;
case PM_TRANS_CONV_REPLACE_PKG:
- if(!config->noconfirm) {
- snprintf(str, LOG_STR_LEN, _(":: Replace %s with %s/%s? [Y/n] "),
- alpm_pkg_get_name(data1),
- (char *)data3,
- alpm_pkg_get_name(data2));
- *response = yesno(str);
- } else {
- printf(_("Replacing %s with %s/%s\n"),
- alpm_pkg_get_name(data1),
- (char *)data3,
- alpm_pkg_get_name(data2));
- *response = 1;
- }
+ *response = yesno(1, _(":: Replace %s with %s/%s?"),
+ alpm_pkg_get_name(data1),
+ (char *)data3,
+ alpm_pkg_get_name(data2));
break;
case PM_TRANS_CONV_CONFLICT_PKG:
- snprintf(str, LOG_STR_LEN, _(":: %s conflicts with %s. Remove %s? [Y/n] "),
+ *response = yesno(1, _(":: %s conflicts with %s. Remove %s?"),
(char *)data1,
(char *)data2,
(char *)data2);
- *response = yesno(str);
break;
case PM_TRANS_CONV_LOCAL_NEWER:
if(!config->op_s_downloadonly) {
- snprintf(str, LOG_STR_LEN, _(":: %s-%s: local version is newer. Upgrade anyway? [Y/n] "),
+ *response = yesno(1, _(":: %s-%s: local version is newer. Upgrade anyway?"),
alpm_pkg_get_name(data1),
alpm_pkg_get_version(data1));
- *response = yesno(str);
} else {
*response = 1;
}
break;
case PM_TRANS_CONV_CORRUPTED_PKG:
- if(!config->noconfirm) {
- snprintf(str, LOG_STR_LEN, _(":: File %s is corrupted. Do you want to delete it? [Y/n] "),
- (char *)data1);
- *response = yesno(str);
- } else {
- *response = 1;
- }
+ *response = yesno(1, _(":: File %s is corrupted. Do you want to delete it?"),
+ (char *)data1);
break;
}
}
@@ -442,9 +415,19 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
}
}
+/* callback to handle receipt of total download value */
+void cb_dl_total(off_t total)
+{
+ list_total = total;
+ /* if we get a 0 value, it means this list has finished downloading,
+ * so clear out our list_xfered as well */
+ if(total == 0) {
+ list_xfered = 0;
+ }
+}
+
/* callback to handle display of download progress */
-void cb_dl_progress(const char *filename, int file_xfered, int file_total,
- int list_xfered, int list_total)
+void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
{
const int infolen = 50;
const int filenamelen = infolen - 27;
@@ -453,44 +436,37 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total,
int len, wclen, wcwid, padwid;
wchar_t *wcfname;
+ off_t xfered, total;
float rate = 0.0, timediff = 0.0, f_xfered = 0.0;
unsigned int eta_h = 0, eta_m = 0, eta_s = 0;
- int graph_percent = 0, display_percent = 0;
+ int file_percent = 0, total_percent = 0;
char rate_size = 'K', xfered_size = 'K';
- int xfered = 0, total = 0;
-
- /* Need this variable when TotalDownload is set to know if we should
- * reset xfered_last and rate_last. */
- static int has_init = 0;
if(config->noprogressbar) {
return;
}
- /* Choose how to display the amount downloaded, rate, ETA, and
- * percentage depending on the TotalDownload option. */
- if (config->totaldownload && list_total > 0) {
- xfered = list_xfered;
+ /* only use TotalDownload if enabled and we have a callback value */
+ if(config->totaldownload && list_total) {
+ xfered = list_xfered + file_xfered;
total = list_total;
} else {
xfered = file_xfered;
total = file_total;
}
- /* this is basically a switch on file_xferred: 0, file_total, and
+ /* this is basically a switch on xfered: 0, total, and
* anything else */
if(file_xfered == 0) {
- /* set default starting values, but only once for TotalDownload */
- if (!(config->totaldownload && list_total > 0) ||
- (config->totaldownload && list_total > 0 && !has_init)) {
+ /* set default starting values, ensure we only call this once
+ * if TotalDownload is enabled */
+ if(!(config->totaldownload)
+ || (config->totaldownload && list_xfered == 0)) {
gettimeofday(&initial_time, NULL);
- timediff = get_update_timediff(1);
- xfered_last = 0;
+ xfered_last = (off_t)0;
rate_last = 0.0;
- has_init = 1;
+ timediff = get_update_timediff(1);
}
- rate = 0.0;
- eta_s = 0;
} else if(file_xfered == file_total) {
/* compute final values */
struct timeval current_time;
@@ -514,12 +490,24 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total,
}
rate = (xfered - xfered_last) / (timediff * 1024.0);
/* average rate to reduce jumpiness */
- rate = (rate + 2*rate_last) / 3;
+ rate = (rate + 2 * rate_last) / 3;
eta_s = (total - xfered) / (rate * 1024.0);
rate_last = rate;
xfered_last = xfered;
}
+ file_percent = (int)((float)file_xfered) / ((float)file_total) * 100;
+
+ if(config->totaldownload && list_total) {
+ total_percent = (int)((float)list_xfered + file_xfered) /
+ ((float)list_total) * 100;
+
+ /* if we are at the end, add the completed file to list_xfered */
+ if(file_xfered == file_total) {
+ list_xfered += file_total;
+ }
+ }
+
/* fix up time for display */
eta_h = eta_s / 3600;
eta_s -= eta_h * 3600;
@@ -591,11 +579,7 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total,
free(fname);
free(wcfname);
- /* The progress bar is based on the file percent regardless of the
- * TotalDownload option. */
- graph_percent = (int)((float)file_xfered) / ((float)file_total) * 100;
- display_percent = (int)((float)xfered) / ((float)total) * 100;
- fill_progress(graph_percent, display_percent, getcols() - infolen);
+ fill_progress(file_percent, total_percent, getcols() - infolen);
return;
}