summaryrefslogtreecommitdiffstats
path: root/src/pacman/callback.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pacman/callback.c')
-rw-r--r--src/pacman/callback.c117
1 files changed, 64 insertions, 53 deletions
diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index c7c16949..0dcdb69d 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -18,13 +18,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "config.h"
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h> /* off_t */
+#include <time.h>
#include <unistd.h>
#include <wchar.h>
#include <limits.h> /* UINT_MAX */
@@ -44,15 +43,19 @@ static off_t list_total = 0.0;
static int on_progress = 0;
static alpm_list_t *output = NULL;
-/* Silly little helper function, determines if the caller needs a visual update
+/* update speed for the fill_progress based functions */
+#define UPDATE_SPEED_MS 200
+
+/**
+ * Silly little helper function, determines if the caller needs a visual update
* since the last time this function was called.
- * This is made for the two progress bar functions, to prevent flicker
- *
- * first_call indicates if this is the first time it is called, for
- * initialization purposes */
-static double get_update_timediff(int first_call)
+ * This is made for the two progress bar functions, to prevent flicker.
+ * @param first_call 1 on first call for initialization purposes, 0 otherwise
+ * @return number of milliseconds since last call
+ */
+static long get_update_timediff(int first_call)
{
- double retval = 0.0;
+ long retval = 0;
static struct timeval last_time = {0, 0};
/* on first call, simply set the last time and return */
@@ -60,18 +63,17 @@ static double get_update_timediff(int first_call)
gettimeofday(&last_time, NULL);
} else {
struct timeval this_time;
- double diff_sec, diff_usec;
+ time_t diff_sec;
+ suseconds_t diff_usec;
gettimeofday(&this_time, NULL);
diff_sec = this_time.tv_sec - last_time.tv_sec;
diff_usec = this_time.tv_usec - last_time.tv_usec;
- retval = diff_sec + (diff_usec / 1000000.0);
+ retval = (diff_sec * 1000) + (diff_usec / 1000);
- /* return 0 and do not update last_time if interval was too short */
- if(retval < UPDATE_SPEED_SEC) {
- retval = 0.0;
- } else {
+ /* do not update last_time if interval was too short */
+ if(retval >= UPDATE_SPEED_MS) {
last_time = this_time;
}
}
@@ -95,41 +97,41 @@ static void fill_progress(const int bar_percent, const int disp_percent,
}
if(hashlen > 0) {
- printf(" [");
+ fputs(" [", stdout);
for(i = hashlen; i > 0; --i) {
/* if special progress bar enabled */
if(config->chomp) {
if(i > hashlen - hash) {
- printf("-");
+ putchar('-');
} else if(i == hashlen - hash) {
if(lasthash == hash) {
if(mouth) {
- printf("\033[1;33mC\033[m");
+ fputs("\033[1;33mC\033[m", stdout);
} else {
- printf("\033[1;33mc\033[m");
+ fputs("\033[1;33mc\033[m", stdout);
}
} else {
lasthash = hash;
mouth = mouth == 1 ? 0 : 1;
if(mouth) {
- printf("\033[1;33mC\033[m");
+ fputs("\033[1;33mC\033[m", stdout);
} else {
- printf("\033[1;33mc\033[m");
+ fputs("\033[1;33mc\033[m", stdout);
}
}
- } else if(i%3 == 0) {
- printf("\033[0;37mo\033[m");
+ } else if(i % 3 == 0) {
+ fputs("\033[0;37mo\033[m", stdout);
} else {
- printf("\033[0;37m \033[m");
+ fputs("\033[0;37m \033[m", stdout);
}
} /* else regular progress bar */
else if(i > hashlen - hash) {
- printf("#");
+ putchar('#');
} else {
- printf("-");
+ putchar('-');
}
}
- printf("]");
+ putchar(']');
}
/* print display percent after progress bar */
/* 5 = 1 space + 3 digits + 1 % */
@@ -138,9 +140,9 @@ static void fill_progress(const int bar_percent, const int disp_percent,
}
if(bar_percent == 100) {
- printf("\n");
+ putchar('\n');
} else {
- printf("\r");
+ putchar('\r');
}
fflush(stdout);
}
@@ -196,9 +198,9 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
break;
case ALPM_EVENT_UPGRADE_DONE:
alpm_logaction(config->handle, "upgraded %s (%s -> %s)\n",
- (char *)alpm_pkg_get_name(data1),
- (char *)alpm_pkg_get_version(data2),
- (char *)alpm_pkg_get_version(data1));
+ alpm_pkg_get_name(data1),
+ alpm_pkg_get_version(data2),
+ alpm_pkg_get_version(data1));
display_new_optdepends(data2,data1);
break;
case ALPM_EVENT_INTEGRITY_START:
@@ -227,10 +229,10 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
printf(_("failed.\n"));
break;
case ALPM_EVENT_SCRIPTLET_INFO:
- printf("%s", (char *)data1);
+ fputs((const char *)data1, stdout);
break;
case ALPM_EVENT_RETRIEVE_START:
- printf(_(":: Retrieving packages from %s...\n"), (char *)data1);
+ printf(_(":: Retrieving packages ...\n"));
break;
case ALPM_EVENT_DISKSPACE_START:
if(config->noprogressbar) {
@@ -294,10 +296,10 @@ void cb_question(alpm_question_t event, void *data1, void *data2,
break;
case ALPM_QUESTION_REMOVE_PKGS:
{
- alpm_list_t *unresolved = (alpm_list_t *) data1;
+ alpm_list_t *unresolved = data1;
alpm_list_t *namelist = NULL, *i;
size_t count = 0;
- for (i = unresolved; i; i = i->next) {
+ for(i = unresolved; i; i = i->next) {
namelist = alpm_list_add(namelist,
(char *)alpm_pkg_get_name(i->data));
count++;
@@ -317,7 +319,7 @@ void cb_question(alpm_question_t event, void *data1, void *data2,
break;
case ALPM_QUESTION_SELECT_PROVIDER:
{
- alpm_list_t *providers = (alpm_list_t *)data1;
+ alpm_list_t *providers = data1;
size_t count = alpm_list_count(providers);
char *depstring = alpm_dep_compute_string((alpm_depend_t *)data2);
printf(_(":: There are %zd providers available for %s:\n"), count,
@@ -340,15 +342,22 @@ void cb_question(alpm_question_t event, void *data1, void *data2,
*response = yesno(_(":: File %s is corrupted (%s).\n"
"Do you want to delete it?"),
(char *)data1,
- alpm_strerror(*(enum _alpm_errno_t *)data2));
+ alpm_strerror(*(alpm_errno_t *)data2));
break;
case ALPM_QUESTION_IMPORT_KEY:
{
alpm_pgpkey_t *key = data1;
char created[12];
- strftime(created, 12, "%Y-%m-%d", localtime(&(key->created)));
- *response = yesno(_(":: Import PGP key %s, \"%s\", created %s?"),
- key->fingerprint, key->uid, created);
+ const char *revoked = "";
+ time_t time = (time_t)key->created;
+ strftime(created, 12, "%Y-%m-%d", localtime(&time));
+
+ if(key->revoked) {
+ revoked = " (revoked)";
+ }
+
+ *response = yesno(_(":: Import PGP key %d%c/%s, \"%s\", created: %s%s?"),
+ key->length, key->pubkey_algo, key->fingerprint, key->uid, created, revoked);
}
break;
}
@@ -393,7 +402,7 @@ void cb_progress(alpm_progress_t event, const char *pkgname, int percent,
if(current != prevcurrent) {
/* update always */
} else if(!pkgname || percent == prevpercent ||
- get_update_timediff(0) < UPDATE_SPEED_SEC) {
+ get_update_timediff(0) < UPDATE_SPEED_MS) {
/* only update the progress bar when we have a package name, the
* percentage has changed, and it has been long enough. */
return;
@@ -493,7 +502,7 @@ void cb_progress(alpm_progress_t event, const char *pkgname, int percent,
alpm_list_t *i = NULL;
on_progress = 0;
for(i = output; i; i = i->next) {
- printf("%s", (char *)i->data);
+ fputs((const char *)i->data, stdout);
}
fflush(stdout);
FREELIST(output);
@@ -528,7 +537,8 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
int totaldownload = 0;
off_t xfered, total;
- double rate = 0.0, timediff = 0.0;
+ double rate = 0.0;
+ long timediff = 0;
unsigned int eta_h = 0, eta_m = 0, eta_s = 0;
double rate_human, xfered_human;
const char *rate_label, *xfered_label;
@@ -587,16 +597,17 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
} else if(file_xfered == file_total) {
/* compute final values */
struct timeval current_time;
- double diff_sec, diff_usec;
+ time_t diff_sec;
+ suseconds_t diff_usec;
gettimeofday(&current_time, NULL);
diff_sec = current_time.tv_sec - initial_time.tv_sec;
diff_usec = current_time.tv_usec - initial_time.tv_usec;
- timediff = diff_sec + (diff_usec / 1000000.0);
- if(timediff > 0.0) {
- rate = xfered / timediff;
- /* round elapsed time to the nearest second */
- eta_s = (unsigned int)(timediff + 0.5);
+ timediff = (diff_sec * 1000) + (diff_usec / 1000);
+ if(timediff > 0) {
+ rate = (double)xfered / (timediff / 1000.0);
+ /* round elapsed time (in ms) to the nearest second */
+ eta_s = (unsigned int)(timediff + 500) / 1000;
} else {
eta_s = 0;
}
@@ -604,11 +615,11 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
/* compute current average values */
timediff = get_update_timediff(0);
- if(timediff < UPDATE_SPEED_SEC) {
+ if(timediff < UPDATE_SPEED_MS) {
/* return if the calling interval was too short */
return;
}
- rate = (xfered - xfered_last) / timediff;
+ rate = (double)(xfered - xfered_last) / (timediff / 1000.0);
/* average rate to reduce jumpiness */
rate = (rate + 2 * rate_last) / 3;
if(rate > 0.0) {
@@ -713,7 +724,7 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
} else if(eta_h < 100) {
printf("%02u:%02u:%02u", eta_h, eta_m, eta_s);
} else {
- printf("--:--");
+ fputs("--:--", stdout);
}
free(fname);