[pacman-dev] CVS update of pacman-lib/src/pacman (downloadprog.c trans.c util.c)

dan at archlinux.org dan at archlinux.org
Thu Feb 8 15:44:48 EST 2007


    Date: Thursday, February 8, 2007 @ 15:44:48
  Author: dan
    Path: /home/cvs-pacman/pacman-lib/src/pacman

Modified: downloadprog.c (1.12 -> 1.13) trans.c (1.31 -> 1.32)
          util.c (1.27 -> 1.28)

* 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.


----------------+
 downloadprog.c |   72 +++++++++++++++++++++++++++----------------------------
 trans.c        |    4 +--
 util.c         |   71 +++++++++++++++++++++++++++++-------------------------
 3 files changed, 77 insertions(+), 70 deletions(-)


Index: pacman-lib/src/pacman/downloadprog.c
diff -u pacman-lib/src/pacman/downloadprog.c:1.12 pacman-lib/src/pacman/downloadprog.c:1.13
--- pacman-lib/src/pacman/downloadprog.c:1.12	Wed Feb  7 00:25:45 2007
+++ pacman-lib/src/pacman/downloadprog.c	Thu Feb  8 15:44:47 2007
@@ -29,6 +29,7 @@
 #include <time.h>
 #include <sys/time.h>
 #include <libintl.h>
+#include <math.h>
 
 #include <alpm.h>
 /* pacman */
@@ -52,59 +53,58 @@
 	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);
-	} else {
-		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;
-	}
+		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(&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);
+		rate = (float)total / (timediff * 1024.0);
 
-	gettimeofday(&current_time, NULL);
-	total_timediff = current_time.tv_sec-initial_time.tv_sec
-		+ (float)(current_time.tv_usec-initial_time.tv_usec) / 1000000.0;
+		/* round elapsed time to the nearest second */
+		eta_s = (int)floorf(timediff + 0.5);
 
-	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;
-		}
 		set_output_padding(0); /* shut off padding */
 	} else {
+		/* compute current average values */
+		timediff = get_update_timediff(0);
+
+		if(timediff < UPDATE_SPEED_SEC) {
+			/* return if the calling interval was too short */
+			return;
+		}
 		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 @@
 
 	/* 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 @@
 	}
 
 	xfered /= 1024; /* convert to K by default */
+	/* xfered_size = 'K'; was set above */
 	if(xfered > 2048) {
 		xfered /= 1024;
 		xfered_size = 'M';
Index: pacman-lib/src/pacman/trans.c
diff -u pacman-lib/src/pacman/trans.c:1.31 pacman-lib/src/pacman/trans.c:1.32
--- pacman-lib/src/pacman/trans.c:1.31	Sat Feb  3 20:36:45 2007
+++ pacman-lib/src/pacman/trans.c	Thu Feb  8 15:44:47 2007
@@ -155,9 +155,9 @@
 			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:
Index: pacman-lib/src/pacman/util.c
diff -u pacman-lib/src/pacman/util.c:1.27 pacman-lib/src/pacman/util.c:1.28
--- pacman-lib/src/pacman/util.c:1.27	Sun Feb  4 03:26:53 2007
+++ pacman-lib/src/pacman/util.c	Thu Feb  8 15:44:48 2007
@@ -268,7 +268,9 @@
 	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 @@
 				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 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);
 }
 




More information about the pacman-dev mailing list