From b67e10d6b391d325b1599598b3a15104ccdacab3 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Thu, 8 Feb 2007 20:44:47 +0000 Subject: * Hopefully fixed the download progres bar for real. We should no longer have issues with crazy speeds being displayed. * Minor string updates to remove unnecessary 1-off translations. --- src/pacman/downloadprog.c | 72 +++++++++++++++++++++++------------------------ src/pacman/trans.c | 4 +-- src/pacman/util.c | 71 +++++++++++++++++++++++++--------------------- 3 files changed, 77 insertions(+), 70 deletions(-) (limited to 'src') diff --git a/src/pacman/downloadprog.c b/src/pacman/downloadprog.c index c0115844..a7aa0284 100644 --- a/src/pacman/downloadprog.c +++ b/src/pacman/downloadprog.c @@ -29,6 +29,7 @@ #include #include #include +#include #include /* pacman */ @@ -52,59 +53,58 @@ void log_progress(const char *filename, int xfered, int total) const int infolen = 50; char *fname, *p; - struct timeval current_time; - float rate = 0.0; + float rate = 0.0, timediff = 0.0; unsigned int eta_h = 0, eta_m = 0, eta_s = 0; - float total_timediff, timediff; + int percent; + char rate_size = 'K', xfered_size = 'K'; if(config->noprogressbar) { return; } - int percent = (int)((float)xfered) / ((float)total) * 100; - - if(xfered == 0) { - set_output_padding(1); /* we need padding from pm_fprintf output */ + /* this is basically a switch on xferred: 0, total, and anything else */ + if(xfered == 0) { + /* set default starting values */ gettimeofday(&initial_time, NULL); xfered_last = 0; rate_last = 0.0; timediff = get_update_timediff(1); + rate = 0.0; + eta_s = 0; + set_output_padding(1); /* we need padding from pm_fprintf output */ + } else if(xfered == total) { + /* compute final values */ + struct timeval current_time; + float diff_sec, diff_usec; + + gettimeofday(¤t_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); + rate = (float)total / (timediff * 1024.0); + + /* round elapsed time to the nearest second */ + eta_s = (int)floorf(timediff + 0.5); + + set_output_padding(0); /* shut off padding */ } else { + /* compute current average values */ timediff = get_update_timediff(0); - } - - if(percent > 0 && percent < 100 && !timediff) { - /* only update the progress bar when - * a) we first start - * b) we end the progress - * c) it has been long enough since the last call - */ - return; - } - gettimeofday(¤t_time, NULL); - total_timediff = current_time.tv_sec-initial_time.tv_sec - + (float)(current_time.tv_usec-initial_time.tv_usec) / 1000000.0; - - if(xfered == total) { - /* compute final values */ - rate = (float)total / (total_timediff * 1024.0); - if(total_timediff < 1.0 && total_timediff > 0.5) { - /* round up so we don't display 00:00:00 for quick downloads all the time*/ - eta_s = 1; - } else { - eta_s = (unsigned int)total_timediff; + if(timediff < UPDATE_SPEED_SEC) { + /* return if the calling interval was too short */ + return; } - set_output_padding(0); /* shut off padding */ - } else { rate = (float)(xfered - xfered_last) / (timediff * 1024.0); + /* average rate to reduce jumpiness */ rate = (float)(rate + 2*rate_last) / 3; eta_s = (unsigned int)(total - xfered) / (rate * 1024.0); + rate_last = rate; + xfered_last = xfered; } - rate_last = rate; - xfered_last = xfered; - + percent = (int)((float)xfered) / ((float)total) * 100; + /* fix up time for display */ eta_h = eta_s / 3600; eta_s -= eta_h * 3600; @@ -121,8 +121,7 @@ void log_progress(const char *filename, int xfered, int total) /* Awesome formatting for progress bar. We need a mess of Kb->Mb->Gb stuff * here. We'll use limit of 2048 for each until we get some empirical */ - char rate_size = 'K'; - char xfered_size = 'K'; + /* rate_size = 'K'; was set above */ if(rate > 2048.0) { rate /= 1024.0; rate_size = 'M'; @@ -134,6 +133,7 @@ void log_progress(const char *filename, int xfered, int total) } xfered /= 1024; /* convert to K by default */ + /* xfered_size = 'K'; was set above */ if(xfered > 2048) { xfered /= 1024; xfered_size = 'M'; diff --git a/src/pacman/trans.c b/src/pacman/trans.c index e2dcf645..61c9e005 100644 --- a/src/pacman/trans.c +++ b/src/pacman/trans.c @@ -155,9 +155,9 @@ void cb_trans_evt(pmtransevt_t event, void *data1, void *data2) break; case PM_TRANS_EVT_SCRIPTLET_DONE: if(!(long)data1) { - MSG(CL, _(" done.\n")); + MSG(CL, _("done.\n")); } else { - MSG(CL, _(" failed.\n")); + MSG(CL, _("failed.\n")); } break; case PM_TRANS_EVT_PRINTURI: diff --git a/src/pacman/util.c b/src/pacman/util.c index ac29879f..f85a1d10 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -268,7 +268,9 @@ void display_targets(alpm_list_t *syncpkgs) char *str; alpm_list_t *i, *j; alpm_list_t *targets = NULL, *to_remove = NULL; - unsigned long totalsize = 0, totalisize = 0, totalrsize = 0; + /* TODO these are some messy variable names */ + unsigned long size = 0, isize = 0, rsize = 0; + double mbsize = 0.0, mbisize = 0.0, mbrsize = 0.0; for(i = syncpkgs; i; i = alpm_list_next(i)) { pmsyncpkg_t *sync = alpm_list_getdata(i); @@ -285,48 +287,53 @@ void display_targets(alpm_list_t *syncpkgs) const char *name = alpm_pkg_get_name(rp); if(!alpm_list_find_str(to_remove, name)) { - totalrsize += alpm_pkg_get_isize(rp); + rsize += alpm_pkg_get_isize(rp); to_remove = alpm_list_add(to_remove, strdup(name)); } } } - totalsize += alpm_pkg_get_size(pkg); - totalisize += alpm_pkg_get_isize(pkg); + size += alpm_pkg_get_size(pkg); + isize += alpm_pkg_get_isize(pkg); asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); targets = alpm_list_add(targets, str); } + /* Convert byte sizes to MB */ + mbsize = (double)(size) / (1024.0 * 1024.0); + mbisize = (double)(isize) / (1024.0 * 1024.0); + mbrsize = (double)(rsize) / (1024.0 * 1024.0); + if(to_remove) { MSG(NL, "\n"); /* TODO ugly hack. printing a single NL should be easy */ list_display(_("Remove:"), to_remove); FREELIST(to_remove); - double rmb = (double)(totalrsize) / (1024.0 * 1024.0); - if(rmb > 0) { - if(rmb < 0.1) { - rmb = 0.1; + if(mbrsize > 0) { + /* round up if size is really small */ + if(mbrsize < 0.1) { + mbrsize = 0.1; } - MSG(NL, _("\nTotal Removed Size: %.2f MB\n"), rmb); + MSG(NL, _("\nTotal Removed Size: %.2f MB\n"), mbrsize); } } MSG(NL, "\n"); /* TODO ugly hack. printing a single NL should be easy */ list_display(_("Targets:"), targets); - double mb = (double)(totalsize) / (1024.0 * 1024.0); - if(mb < 0.1) { - mb = 0.1; + /* round up if size is really small */ + if(mbsize < 0.1) { + mbsize = 0.1; } - MSG(NL, _("\nTotal Package Size: %.2f MB\n"), mb); + MSG(NL, _("\nTotal Package Size: %.2f MB\n"), mbsize); - double umb = (double)(totalisize) / (1024.0 * 1024.0); - if(umb > 0) { - if(umb < 0.1) { - umb = 0.1; + if(mbisize > mbsize) { + /*round up if size is really small */ + if(mbisize < 0.1) { + mbisize = 0.1; } - MSG(NL, _("Total Installed Size: %.2f MB\n"), umb); + MSG(NL, _("Total Installed Size: %.2f MB\n"), mbisize); } FREELIST(targets); @@ -342,29 +349,29 @@ float get_update_timediff(int first_call) { float retval = 0.0; static struct timeval last_time = {0}; - struct timeval this_time; + /* on first call, simply set the last time and return */ if(first_call) { - /* always update on the first call */ - retval = 1.0; + gettimeofday(&last_time, NULL); } else { - gettimeofday(&this_time, NULL); + struct timeval this_time; + float diff_sec, diff_usec; - float diff_sec = this_time.tv_sec - last_time.tv_sec; - float diff_usec = this_time.tv_usec - last_time.tv_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.0f); - /*printf("update time: %fs %fus = %f\n", diff_sec, diff_usec, retval);*/ + retval = diff_sec + (diff_usec / 1000000.0); + /* return 0 and do not update last_time if interval was too short */ if(retval < UPDATE_SPEED_SEC) { - /* maintain the last_time value for the next call */ - return(0.0); + retval = 0.0; + } else { + last_time = this_time; + /* printf("\nupdate retval: %f\n", retval); DEBUG*/ } } - - /* set the time for the next call */ - gettimeofday(&last_time, NULL); - + return(retval); } -- cgit v1.2.3-24-g4f1b